- Timestamp:
- Oct 27, 2008, 4:08:51 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/objecthierarchy/src/orxonox/objects/infos/PlayerInfo.cc
r2006 r2019 27 27 */ 28 28 29 #include <cassert> 30 29 31 #include "OrxonoxStableHeaders.h" 30 32 #include "PlayerInfo.h" 31 33 32 #include <OgreSceneManager.h>33 34 34 #include "core/CoreIncludes.h" 35 #include "core/ConfigValueIncludes.h"36 #include "core/XMLPort.h"37 #include "core/Core.h"38 39 #include "network/Host.h"40 35 #include "network/ClientInformation.h" 41 42 #include "GraphicsEngine.h"43 36 #include "objects/gametypes/Gametype.h" 44 #include "objects/worldentities/ControllableEntity.h"45 #include "objects/controllers/HumanController.h"46 37 47 38 namespace orxonox 48 39 { 49 CreateUnloadableFactory(PlayerInfo); 50 51 PlayerInfo::PlayerInfo() 40 PlayerInfo::PlayerInfo(BaseObject* creator) : Info(creator) 52 41 { 53 42 RegisterObject(PlayerInfo); 54 43 55 this->ping_ = -1;56 44 this->clientID_ = network::CLIENTID_UNKNOWN; 45 this->bHumanPlayer_ = false; 57 46 this->bLocalPlayer_ = false; 58 this->bHumanPlayer_ = false; 59 this->bFinishedSetup_ = false; 60 this->gametype_ = 0; 47 this->bReadyToSpawn_ = false; 48 this->controller_ = 0; 49 this->controllableEntity_ = 0; 50 this->controllableEntityID_ = network::CLIENTID_UNKNOWN; 61 51 62 this->pawn_ = 0;63 this->pawnID_ = network::OBJECTID_UNKNOWN;64 this->controller_ = 0;65 this->setDefaultController(Class(HumanController));66 67 this->setConfigValues();68 52 this->registerVariables(); 69 53 } … … 73 57 if (this->isInitialized()) 74 58 { 75 if (this->gametype_) 76 this->gametype_->removePlayer(this); 59 this->stopControl(this->controllableEntity_); 77 60 78 61 if (this->controller_) 62 { 79 63 delete this->controller_; 80 81 if (this->pawn_) 82 this->pawn_->removePlayer(); 64 this->controller_ = 0; 65 } 83 66 } 84 67 } 85 68 86 void PlayerInfo:: setConfigValues()69 void PlayerInfo::registerVariables() 87 70 { 88 SetConfigValue(nick_, "Player").callback(this, &PlayerInfo::checkNick); 89 } 90 91 void PlayerInfo::checkNick() 92 { 93 if (this->bLocalPlayer_) 94 { 95 this->playerName_ = this->nick_; 96 97 if (Core::isMaster()) 98 this->setName(this->playerName_); 99 } 71 REGISTERSTRING(this->name_, network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::changedName)); 72 REGISTERDATA (this->controllableEntityID_, network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::networkcallback_changedcontrollableentityID)); 100 73 } 101 74 102 75 void PlayerInfo::changedName() 103 76 { 104 if (this-> gametype_)105 this->g ametype_->playerChangedName(this);77 if (this->isReady() && this->getGametype()) 78 this->getGametype()->playerChangedName(this); 106 79 } 107 80 108 void PlayerInfo:: registerVariables()81 void PlayerInfo::changedGametype() 109 82 { 110 REGISTERSTRING(name_, network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::changedName)); 111 REGISTERSTRING(playerName_, network::direction::toserver, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::clientChangedName)); 112 REGISTERDATA(clientID_, network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::checkClientID)); 113 REGISTERDATA(ping_, network::direction::toclient); 114 REGISTERDATA(bHumanPlayer_, network::direction::toclient); 115 REGISTERDATA(pawnID_, network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::updatePawn)); 116 REGISTERDATA(bFinishedSetup_, network::direction::bidirectional, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::finishedSetup)); 117 } 83 if (this->isReady()) 84 { 85 if (this->getOldGametype()) 86 { 87 if (this->getGametype()) 88 this->getOldGametype()->playerSwitched(this, this->getGametype()); 89 else 90 this->getOldGametype()->playerLeft(this); 91 } 118 92 119 void PlayerInfo::clientChangedName() 120 { 121 this->setName(this->playerName_); 122 } 123 124 void PlayerInfo::checkClientID() 125 { 126 this->bHumanPlayer_ = true; 127 128 if (this->clientID_ == network::Host::getPlayerID()) 129 { 130 this->takeLocalControl(); 131 132 if (Core::isClient()) 133 this->setObjectMode(network::direction::bidirectional); 134 else 93 if (this->getGametype()) 135 94 { 136 this->clientChangedName(); 137 this->bFinishedSetup_ = true; 138 this->finishedSetup(); 95 if (this->getOldGametype()) 96 this->getGametype()->playerSwitchedBack(this, this->getOldGametype()); 97 else 98 this->getGametype()->playerEntered(this); 139 99 } 140 100 } 141 101 } 142 102 143 void PlayerInfo:: finishedSetup()103 void PlayerInfo::createController() 144 104 { 145 if (Core::isClient()) 146 this->bFinishedSetup_ = true; 147 else if (this->bFinishedSetup_) 105 this->controller_ = this->defaultController_.fabricate(this); 106 assert(this->controller_); 107 this->controller_->setPlayer(this); 108 if (this->controllableEntity_) 109 this->controller_->setControllableEntity(this->controllableEntity_); 110 } 111 112 void PlayerInfo::startControl(ControllableEntity* entity) 113 { 114 if (this->controllableEntity_) 115 this->stopControl(this->controllableEntity_); 116 117 this->controllableEntity_ = entity; 118 119 if (entity) 148 120 { 149 if (this->gametype_) 150 this->gametype_->addPlayer(this); 121 this->controllableEntityID_ = entity->getObjectID(); 122 entity->setPlayer(this); 123 } 124 else 125 { 126 this->controllableEntityID_ = network::OBJECTID_UNKNOWN; 127 } 128 129 if (this->controller_) 130 this->controller_->setControllableEntity(entity); 131 } 132 133 void PlayerInfo::stopControl(ControllableEntity* entity) 134 { 135 if (entity && this->controllableEntity_ == entity) 136 { 137 this->controllableEntity_ = 0; 138 this->controllableEntityID_ = network::OBJECTID_UNKNOWN; 139 140 if (this->controller_) 141 this->controller_->setControllableEntity(0); 142 143 entity->removePlayer(); 151 144 } 152 145 } 153 146 154 void PlayerInfo:: startControl(ControllableEntity* pawn)147 void PlayerInfo::networkcallback_changedcontrollableentityID() 155 148 { 156 pawn->setPlayer(this); 157 this->pawn_ = pawn; 158 this->pawnID_ = pawn->getObjectID(); 149 if (this->controllableEntityID_ != network::OBJECTID_UNKNOWN) 150 { 151 Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_); 152 ControllableEntity* entity = dynamic_cast<ControllableEntity*>(temp); 159 153 160 if (this->controller_) 161 this->controller_->setPawn(this->pawn_); 162 } 163 164 void PlayerInfo::stopControl() 165 { 166 if (this->pawn_) 167 this->pawn_->removePlayer(); 168 this->pawn_ = 0; 169 this->pawnID_ = network::OBJECTID_UNKNOWN; 170 } 171 172 void PlayerInfo::takeLocalControl() 173 { 174 this->bLocalPlayer_ = true; 175 this->playerName_ = this->nick_; 176 this->createController(); 177 } 178 179 void PlayerInfo::createController() 180 { 181 this->controller_ = this->defaultController_.fabricate(); 182 this->controller_->setPawn(this->pawn_); 183 } 184 185 void PlayerInfo::updatePawn() 186 { 187 this->pawn_ = dynamic_cast<ControllableEntity*>(network::Synchronisable::getSynchronisable(this->pawnID_)); 188 if (this->pawn_ && (this->pawn_->getPlayer() != this)) 189 this->pawn_->setPlayer(this); 154 this->startControl(entity); 155 } 156 else 157 { 158 this->stopControl(this->controllableEntity_); 159 } 190 160 } 191 161 }
Note: See TracChangeset
for help on using the changeset viewer.