| [2072] | 1 | /* | 
|---|
 | 2 |  *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
 | 3 |  *                    > www.orxonox.net < | 
|---|
 | 4 |  * | 
|---|
 | 5 |  * | 
|---|
 | 6 |  *   License notice: | 
|---|
 | 7 |  * | 
|---|
 | 8 |  *   This program is free software; you can redistribute it and/or | 
|---|
 | 9 |  *   modify it under the terms of the GNU General Public License | 
|---|
 | 10 |  *   as published by the Free Software Foundation; either version 2 | 
|---|
 | 11 |  *   of the License, or (at your option) any later version. | 
|---|
 | 12 |  * | 
|---|
 | 13 |  *   This program is distributed in the hope that it will be useful, | 
|---|
 | 14 |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
 | 15 |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
 | 16 |  *   GNU General Public License for more details. | 
|---|
 | 17 |  * | 
|---|
 | 18 |  *   You should have received a copy of the GNU General Public License | 
|---|
 | 19 |  *   along with this program; if not, write to the Free Software | 
|---|
 | 20 |  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
 | 21 |  * | 
|---|
 | 22 |  *   Author: | 
|---|
 | 23 |  *      Fabian 'x3n' Landau | 
|---|
 | 24 |  *   Co-authors: | 
|---|
 | 25 |  *      ... | 
|---|
 | 26 |  * | 
|---|
 | 27 |  */ | 
|---|
 | 28 |  | 
|---|
 | 29 | #include <cassert> | 
|---|
 | 30 |  | 
|---|
 | 31 | #include "OrxonoxStableHeaders.h" | 
|---|
 | 32 | #include "PlayerInfo.h" | 
|---|
 | 33 |  | 
|---|
 | 34 | #include "core/CoreIncludes.h" | 
|---|
 | 35 | #include "network/ClientInformation.h" | 
|---|
 | 36 | #include "objects/gametypes/Gametype.h" | 
|---|
 | 37 |  | 
|---|
 | 38 | namespace orxonox | 
|---|
 | 39 | { | 
|---|
 | 40 |     PlayerInfo::PlayerInfo(BaseObject* creator) : Info(creator) | 
|---|
 | 41 |     { | 
|---|
 | 42 |         RegisterObject(PlayerInfo); | 
|---|
 | 43 |  | 
|---|
| [2171] | 44 |         this->clientID_ = CLIENTID_UNKNOWN; | 
|---|
| [2072] | 45 |         this->bHumanPlayer_ = false; | 
|---|
 | 46 |         this->bLocalPlayer_ = false; | 
|---|
 | 47 |         this->bReadyToSpawn_ = false; | 
|---|
| [2362] | 48 |         this->bSetUnreadyAfterSpawn_ = true; | 
|---|
| [2072] | 49 |         this->controller_ = 0; | 
|---|
 | 50 |         this->controllableEntity_ = 0; | 
|---|
| [2171] | 51 |         this->controllableEntityID_ = CLIENTID_UNKNOWN; | 
|---|
| [2072] | 52 |  | 
|---|
 | 53 |         this->registerVariables(); | 
|---|
 | 54 |     } | 
|---|
 | 55 |  | 
|---|
 | 56 |     PlayerInfo::~PlayerInfo() | 
|---|
 | 57 |     { | 
|---|
| [2171] | 58 |         if (this->BaseObject::isInitialized()) | 
|---|
| [2072] | 59 |         { | 
|---|
 | 60 |             this->stopControl(this->controllableEntity_); | 
|---|
 | 61 |  | 
|---|
 | 62 |             if (this->controller_) | 
|---|
 | 63 |             { | 
|---|
 | 64 |                 delete this->controller_; | 
|---|
 | 65 |                 this->controller_ = 0; | 
|---|
 | 66 |             } | 
|---|
 | 67 |         } | 
|---|
 | 68 |     } | 
|---|
 | 69 |  | 
|---|
 | 70 |     void PlayerInfo::registerVariables() | 
|---|
 | 71 |     { | 
|---|
| [2171] | 72 |         REGISTERSTRING(this->name_,                 direction::toclient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::changedName)); | 
|---|
 | 73 |         REGISTERDATA  (this->controllableEntityID_, direction::toclient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::networkcallback_changedcontrollableentityID)); | 
|---|
 | 74 |         REGISTERDATA  (this->bReadyToSpawn_,        direction::toserver); | 
|---|
| [2072] | 75 |     } | 
|---|
 | 76 |  | 
|---|
 | 77 |     void PlayerInfo::changedName() | 
|---|
 | 78 |     { | 
|---|
| [2369] | 79 |         SUPER(PlayerInfo, changedName); | 
|---|
 | 80 |  | 
|---|
| [2171] | 81 |         if (this->isInitialized() && this->getGametype()) | 
|---|
| [2072] | 82 |             this->getGametype()->playerChangedName(this); | 
|---|
 | 83 |     } | 
|---|
 | 84 |  | 
|---|
 | 85 |     void PlayerInfo::changedGametype() | 
|---|
 | 86 |     { | 
|---|
| [2171] | 87 |         if (this->isInitialized()) | 
|---|
| [2072] | 88 |         { | 
|---|
 | 89 |             if (this->getOldGametype()) | 
|---|
 | 90 |             { | 
|---|
 | 91 |                 if (this->getGametype()) | 
|---|
 | 92 |                     this->getOldGametype()->playerSwitched(this, this->getGametype()); | 
|---|
 | 93 |                 else | 
|---|
 | 94 |                     this->getOldGametype()->playerLeft(this); | 
|---|
 | 95 |             } | 
|---|
 | 96 |  | 
|---|
 | 97 |             if (this->getGametype()) | 
|---|
 | 98 |             { | 
|---|
 | 99 |                 if (this->getOldGametype()) | 
|---|
 | 100 |                     this->getGametype()->playerSwitchedBack(this, this->getOldGametype()); | 
|---|
 | 101 |                 else | 
|---|
 | 102 |                     this->getGametype()->playerEntered(this); | 
|---|
 | 103 |             } | 
|---|
 | 104 |         } | 
|---|
 | 105 |     } | 
|---|
 | 106 |  | 
|---|
 | 107 |     void PlayerInfo::createController() | 
|---|
 | 108 |     { | 
|---|
 | 109 |         this->controller_ = this->defaultController_.fabricate(this); | 
|---|
 | 110 |         assert(this->controller_); | 
|---|
 | 111 |         this->controller_->setPlayer(this); | 
|---|
 | 112 |         if (this->controllableEntity_) | 
|---|
 | 113 |             this->controller_->setControllableEntity(this->controllableEntity_); | 
|---|
| [2428] | 114 |         this->changedController(); | 
|---|
| [2072] | 115 |     } | 
|---|
 | 116 |  | 
|---|
 | 117 |     void PlayerInfo::startControl(ControllableEntity* entity) | 
|---|
 | 118 |     { | 
|---|
| [2428] | 119 |         if (entity == this->controllableEntity_) | 
|---|
 | 120 |             return; | 
|---|
 | 121 |  | 
|---|
| [2072] | 122 |         if (this->controllableEntity_) | 
|---|
 | 123 |             this->stopControl(this->controllableEntity_); | 
|---|
 | 124 |  | 
|---|
 | 125 |         this->controllableEntity_ = entity; | 
|---|
 | 126 |  | 
|---|
 | 127 |         if (entity) | 
|---|
 | 128 |         { | 
|---|
 | 129 |             this->controllableEntityID_ = entity->getObjectID(); | 
|---|
 | 130 |             entity->setPlayer(this); | 
|---|
| [2362] | 131 |             this->bReadyToSpawn_ &= (!this->bSetUnreadyAfterSpawn_); | 
|---|
| [2072] | 132 |         } | 
|---|
 | 133 |         else | 
|---|
 | 134 |         { | 
|---|
| [2171] | 135 |             this->controllableEntityID_ = OBJECTID_UNKNOWN; | 
|---|
| [2072] | 136 |         } | 
|---|
 | 137 |  | 
|---|
 | 138 |         if (this->controller_) | 
|---|
 | 139 |             this->controller_->setControllableEntity(entity); | 
|---|
 | 140 |     } | 
|---|
 | 141 |  | 
|---|
 | 142 |     void PlayerInfo::stopControl(ControllableEntity* entity, bool callback) | 
|---|
 | 143 |     { | 
|---|
 | 144 |         if (entity && this->controllableEntity_ == entity) | 
|---|
 | 145 |         { | 
|---|
 | 146 |             this->controllableEntity_ = 0; | 
|---|
| [2171] | 147 |             this->controllableEntityID_ = OBJECTID_UNKNOWN; | 
|---|
| [2072] | 148 |  | 
|---|
 | 149 |             if (this->controller_) | 
|---|
 | 150 |                 this->controller_->setControllableEntity(0); | 
|---|
 | 151 |  | 
|---|
 | 152 |             if (callback) | 
|---|
 | 153 |                 entity->removePlayer(); | 
|---|
 | 154 |         } | 
|---|
 | 155 |     } | 
|---|
 | 156 |  | 
|---|
 | 157 |     void PlayerInfo::networkcallback_changedcontrollableentityID() | 
|---|
 | 158 |     { | 
|---|
| [2171] | 159 |         if (this->controllableEntityID_ != OBJECTID_UNKNOWN) | 
|---|
| [2072] | 160 |         { | 
|---|
 | 161 |             Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_); | 
|---|
 | 162 |             ControllableEntity* entity = dynamic_cast<ControllableEntity*>(temp); | 
|---|
 | 163 |  | 
|---|
 | 164 |             this->startControl(entity); | 
|---|
 | 165 |         } | 
|---|
 | 166 |         else | 
|---|
 | 167 |         { | 
|---|
 | 168 |             this->stopControl(this->controllableEntity_); | 
|---|
 | 169 |         } | 
|---|
 | 170 |     } | 
|---|
 | 171 | } | 
|---|