| 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 |  | 
|---|
| 44 |         this->clientID_ = CLIENTID_UNKNOWN; | 
|---|
| 45 |         this->bHumanPlayer_ = false; | 
|---|
| 46 |         this->bLocalPlayer_ = false; | 
|---|
| 47 |         this->bReadyToSpawn_ = false; | 
|---|
| 48 |         this->bSetUnreadyAfterSpawn_ = true; | 
|---|
| 49 |         this->controller_ = 0; | 
|---|
| 50 |         this->controllableEntity_ = 0; | 
|---|
| 51 |         this->controllableEntityID_ = CLIENTID_UNKNOWN; | 
|---|
| 52 |  | 
|---|
| 53 |         this->registerVariables(); | 
|---|
| 54 |     } | 
|---|
| 55 |  | 
|---|
| 56 |     PlayerInfo::~PlayerInfo() | 
|---|
| 57 |     { | 
|---|
| 58 |         if (this->BaseObject::isInitialized()) | 
|---|
| 59 |         { | 
|---|
| 60 |             this->stopControl(this->controllableEntity_); | 
|---|
| 61 |  | 
|---|
| 62 |             if (this->controller_) | 
|---|
| 63 |             { | 
|---|
| 64 |                 delete this->controller_; | 
|---|
| 65 |                 this->controller_ = 0; | 
|---|
| 66 |             } | 
|---|
| 67 |  | 
|---|
| 68 |             if (this->getGametype()) | 
|---|
| 69 |                 this->getGametype()->playerLeft(this); | 
|---|
| 70 |         } | 
|---|
| 71 |     } | 
|---|
| 72 |  | 
|---|
| 73 |     void PlayerInfo::registerVariables() | 
|---|
| 74 |     { | 
|---|
| 75 |         registerVariable(this->name_,                 variableDirection::toclient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::changedName)); | 
|---|
| 76 |         registerVariable(this->controllableEntityID_, variableDirection::toclient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::networkcallback_changedcontrollableentityID)); | 
|---|
| 77 |         registerVariable(this->bReadyToSpawn_,        variableDirection::toserver); | 
|---|
| 78 |     } | 
|---|
| 79 |  | 
|---|
| 80 |     void PlayerInfo::changedName() | 
|---|
| 81 |     { | 
|---|
| 82 |         SUPER(PlayerInfo, changedName); | 
|---|
| 83 |  | 
|---|
| 84 |         if (this->isInitialized() && this->getGametype()) | 
|---|
| 85 |             this->getGametype()->playerChangedName(this); | 
|---|
| 86 |     } | 
|---|
| 87 |  | 
|---|
| 88 |     void PlayerInfo::changedGametype() | 
|---|
| 89 |     { | 
|---|
| 90 |         if (this->isInitialized()) | 
|---|
| 91 |         { | 
|---|
| 92 |             if (this->getOldGametype()) | 
|---|
| 93 |             { | 
|---|
| 94 |                 if (this->getGametype()) | 
|---|
| 95 |                     this->getOldGametype()->playerSwitched(this, this->getGametype()); | 
|---|
| 96 |                 else | 
|---|
| 97 |                     this->getOldGametype()->playerLeft(this); | 
|---|
| 98 |             } | 
|---|
| 99 |  | 
|---|
| 100 |             if (this->getGametype()) | 
|---|
| 101 |             { | 
|---|
| 102 |                 if (this->getOldGametype()) | 
|---|
| 103 |                     this->getGametype()->playerSwitchedBack(this, this->getOldGametype()); | 
|---|
| 104 |                 else | 
|---|
| 105 |                     this->getGametype()->playerEntered(this); | 
|---|
| 106 |             } | 
|---|
| 107 |         } | 
|---|
| 108 |     } | 
|---|
| 109 |  | 
|---|
| 110 |     void PlayerInfo::createController() | 
|---|
| 111 |     { | 
|---|
| 112 |         this->controller_ = this->defaultController_.fabricate(this); | 
|---|
| 113 |         assert(this->controller_); | 
|---|
| 114 |         this->controller_->setPlayer(this); | 
|---|
| 115 |         if (this->controllableEntity_) | 
|---|
| 116 |             this->controller_->setControllableEntity(this->controllableEntity_); | 
|---|
| 117 |         this->changedController(); | 
|---|
| 118 |     } | 
|---|
| 119 |  | 
|---|
| 120 |     void PlayerInfo::startControl(ControllableEntity* entity, bool callback) | 
|---|
| 121 |     { | 
|---|
| 122 |         if (entity == this->controllableEntity_) | 
|---|
| 123 |             return; | 
|---|
| 124 |  | 
|---|
| 125 |         if (this->controllableEntity_) | 
|---|
| 126 |             this->stopControl(this->controllableEntity_, callback); | 
|---|
| 127 |  | 
|---|
| 128 |         this->controllableEntity_ = entity; | 
|---|
| 129 |  | 
|---|
| 130 |         if (entity) | 
|---|
| 131 |         { | 
|---|
| 132 |             this->controllableEntityID_ = entity->getObjectID(); | 
|---|
| 133 |             entity->setPlayer(this); | 
|---|
| 134 |             this->bReadyToSpawn_ &= (!this->bSetUnreadyAfterSpawn_); | 
|---|
| 135 |         } | 
|---|
| 136 |         else | 
|---|
| 137 |         { | 
|---|
| 138 |             this->controllableEntityID_ = OBJECTID_UNKNOWN; | 
|---|
| 139 |         } | 
|---|
| 140 |  | 
|---|
| 141 |         if (this->controller_) | 
|---|
| 142 |             this->controller_->setControllableEntity(entity); | 
|---|
| 143 |  | 
|---|
| 144 |         this->changedControllableEntity(); | 
|---|
| 145 |     } | 
|---|
| 146 |  | 
|---|
| 147 |     void PlayerInfo::stopControl(ControllableEntity* entity, bool callback) | 
|---|
| 148 |     { | 
|---|
| 149 |         if (entity && this->controllableEntity_ == entity) | 
|---|
| 150 |         { | 
|---|
| 151 |             this->controllableEntity_ = 0; | 
|---|
| 152 |             this->controllableEntityID_ = OBJECTID_UNKNOWN; | 
|---|
| 153 |  | 
|---|
| 154 |             if (this->controller_) | 
|---|
| 155 |                 this->controller_->setControllableEntity(0); | 
|---|
| 156 |  | 
|---|
| 157 |             if (callback) | 
|---|
| 158 |                 entity->removePlayer(); | 
|---|
| 159 |  | 
|---|
| 160 |             this->changedControllableEntity(); | 
|---|
| 161 |         } | 
|---|
| 162 |     } | 
|---|
| 163 |  | 
|---|
| 164 |     void PlayerInfo::networkcallback_changedcontrollableentityID() | 
|---|
| 165 |     { | 
|---|
| 166 |         if (this->controllableEntityID_ != OBJECTID_UNKNOWN) | 
|---|
| 167 |         { | 
|---|
| 168 |             Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_); | 
|---|
| 169 |             ControllableEntity* entity = dynamic_cast<ControllableEntity*>(temp); | 
|---|
| 170 |  | 
|---|
| 171 |             this->startControl(entity); | 
|---|
| 172 |         } | 
|---|
| 173 |         else | 
|---|
| 174 |         { | 
|---|
| 175 |             this->stopControl(this->controllableEntity_); | 
|---|
| 176 |         } | 
|---|
| 177 |     } | 
|---|
| 178 | } | 
|---|