PlayerParser.cpp

Go to the documentation of this file.
00001 /*
00002 SagaEngine library
00003 Copyright (c) 2002-2006 Skalden Studio AS
00004 
00005 This software is provided 'as-is', without any express or implied 
00006 warranty. In no event will the authors be held liable for any 
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to distribute the library under the terms of the 
00010 Q Public License version 1.0. Be sure to read and understand the license
00011 before using the library. It should be included here, or you may read it
00012 at http://www.trolltech.com/products/qt/licenses/licensing/qpl
00013 
00014 The original version of this library can be located at:
00015 http://www.sagaengine.com/
00016 
00017 Rune Myrland
00018 rune@skalden.com
00019 */
00020 
00021 
00022 #include "PlayerParser.hpp"
00023 #include "io/stream/InputStream.hpp"
00024 #include "io/schema/IoSchema.hpp"
00025 #include "client/schema/ClientSchema.hpp"
00026 #include "client/thing/all.hpp"
00027 #include "sim/sim.hpp"
00028 #include "sim/SimEngine.hpp"
00029 #include "sim/schema/SimSchema.hpp"
00030 #include "sim/stat/all.hpp"
00031 #include "sim/area/all.hpp"
00032 #include "sim/thing/all.hpp"
00033 #include "client/thing/all.hpp"
00034 #include "sim/pos/all.hpp"
00035 #include "util/error/Log.hpp"
00036 #include "util/math/CoorT.hpp"
00037 #include "util/type/String.hpp"
00038 #include "sim/spawn/SpawnManager.hpp"
00039 #include "sim/spawn/SpawnAreaComponent.hpp"
00040 #include <cstdio>
00041 #include <cstring>
00042 
00043 using namespace se_core;
00044 
00045 namespace se_client {
00046 
00047     PlayerParser
00048     ::PlayerParser(ParseManager& parser)
00049             : Parser(parser, Parser::ENGINE, Parser::PLAYER, 1) {
00050     }
00051 
00052 
00053     PlayerParser
00054     ::~PlayerParser() {
00055     }
00056 
00057 
00058 
00059     void PlayerParser
00060     ::parse(InputStream& in) {
00061         String tempString;
00062 
00063         int code = ' ';
00064         while((code = in.readInfoCode()) != 'Q' && !in.eof()) {
00065             switch(code) {
00066             case 'P': 
00067                 { // Player
00068                     in.readString(tempString);
00069                     LogDetail("Created player as : " << tempString.get());
00070                     Composite* a = SimSchema::spawnManager().create(tempString.get());
00071                     PosComponent::Ptr pos(*a);
00072                     PhysicsComponent::Ptr physics(*a);
00073                     CameraComponent* camera = new CameraComponent(a);
00074                     ClientSchema::playerX = new PlayerComponent(a);
00075                     Assert(ClientSchema::playerX != 0);
00076                     ClientSchema::player = pos;
00077                     Assert(ClientSchema::player != 0);
00078                     readInfo(in, pos, camera);
00079                     pos->flip();
00080                     physics->nextMove().work_.vp_.face_.yaw_ = pos->nextPos().localFace().yaw_;
00081                 }
00082                 break;
00083 
00084             case 'H': 
00085                 { // Player hitpoints
00086                     int max = in.readShort();
00087                     int now = in.readShort();
00088                     AssertFatal(ClientSchema::player, "Init player before setting hitpoints");
00089                     StatComponent::Ptr(*ClientSchema::player)->health().initHitpoints(max, now);
00090                 }
00091                 break;
00092 
00093             case 'C': // Camera
00094                 {
00095                     // Init camera object
00096                     in.readString(tempString);
00097                     Composite* a = SimSchema::spawnManager().create(tempString.get());
00098                     AssertFatal(a, "Couldn't create camera object");
00099                     PosComponent* pos = static_cast<PosComponent*>(a->component(sct_POS));
00100                     AssertFatal(pos, "Camera didn't have a pos component");
00101                     CameraComponent* camera = new CameraComponent(a);
00102                     ClientSchema::floatingCamera = pos;
00103                     readInfo(in, pos, camera);
00104                     pos->flip();
00105                 }
00106                 break;
00107             }
00108         }
00109     }
00110 
00111 
00112     void PlayerParser
00113     ::readInfo(InputStream& in, PosComponent* pos, CameraComponent* camera) const {
00114 
00115         String tempString;
00116         Area* area;
00117         int code = 'X';
00118         while((code = in.readInfoCode()) != '/') {
00119             switch(code) {
00120             case 'C': // Coor
00121                 in.readString(tempString);
00122                 LogDetail(tempString.get());
00123                 area = SimSchema::areaManager.area(tempString.get());
00124                 Assert(area);
00125                 pos->nextPos().localCoor().x_ = CoorT::fromFloat(in.readFloat());
00126                 pos->nextPos().localCoor().z_ = CoorT::fromFloat(in.readFloat());
00127                 pos->nextPos().localFace().setIdentity();
00128                 pos->nextPos().setArea(*PosComponent::get(*area));
00129                 break;
00130 
00131             case 'E': 
00132                 { // Entrance 
00133                     String name;
00134                     in.readString(tempString);
00135                     if(tempString.get()[0] == '/') {
00136                         name.copy(in.oneDirAndBasename(), -in.basenameLen() - 1);
00137                         //name.copy(in.basename(), -in.extLen());
00138                         name.append(tempString.get());
00139                     }
00140                     else {
00141                         name.set(tempString.get());
00142                     }
00143 
00144                     int value = in.readShort();
00145                     LogDetail(tempString.get());
00146                     area = SimSchema::areaManager.area(name.get());
00147                     Assert(area);
00148                     const ViewPoint* sp = SpawnAreaComponent::Ptr(*area)->spawnPoint(value);
00149                     Assert(sp);
00150                     pos->nextPos().setArea(*PosComponent::get(*area), *sp);
00151                 }
00152                 break;
00153 
00154             case 'A': 
00155                 { // Area
00156                     in.readString(tempString);
00157                     int c = in.readInfoCode();
00158                     Assert(c == 'T');
00159 
00160                     ViewPoint sp;
00161                     sp.coor_.x_ = in.readFloat();
00162                     sp.coor_.y_ = in.readFloat();
00163                     sp.coor_.z_ = in.readFloat();
00164 
00165                     c = in.readInfoCode();
00166                     Assert(c == 'R');
00167                     sp.face_.yaw_ = BrayT::fromDeg(in.readFloat());
00168                     sp.face_.pitch_ = BrayT::fromDeg(in.readFloat());
00169                     sp.face_.roll_ = BrayT::fromDeg(in.readFloat());
00170 
00171                     area = SimSchema::areaManager.area(tempString.get());
00172                     pos->nextPos().setArea(*PosComponent::get(*area), sp);
00173 
00174                     c = in.readInfoCode();
00175                     Assert(c == '/');
00176                 }
00177                 break;
00178 
00179             case 'V': // ViewPort
00180                 camera->grabFocus();
00181                 break;
00182 
00183             case 'T': // Camera tracking player
00184                 AssertFatal(pos != ClientSchema::player, "Player cannot track player");
00185                 pos->nextPos().setParent(*ClientSchema::player);
00186                 break;
00187             }
00188         }
00189     }
00190 
00191 }
00192 

Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:03 2007 by Doxygen version 1.3.9.1.

SourceForge.net Logo