[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 "PlayerInfo.h" |
---|
| 32 | |
---|
| 33 | #include "core/CoreIncludes.h" |
---|
| 34 | #include "network/ClientInformation.h" |
---|
[5735] | 35 | #include "gametypes/Gametype.h" |
---|
| 36 | #include "worldentities/ControllableEntity.h" |
---|
[2072] | 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; |
---|
[2662] | 48 | this->bSetUnreadyAfterSpawn_ = true; |
---|
[2072] | 49 | this->controller_ = 0; |
---|
| 50 | this->controllableEntity_ = 0; |
---|
[2171] | 51 | this->controllableEntityID_ = CLIENTID_UNKNOWN; |
---|
[2072] | 52 | |
---|
[2973] | 53 | this->gtinfo_ = 0; |
---|
| 54 | this->gtinfoID_ = OBJECTID_UNKNOWN; |
---|
| 55 | this->updateGametypeInfo(); |
---|
| 56 | |
---|
[2072] | 57 | this->registerVariables(); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | PlayerInfo::~PlayerInfo() |
---|
| 61 | { |
---|
[2171] | 62 | if (this->BaseObject::isInitialized()) |
---|
[2072] | 63 | { |
---|
[3038] | 64 | this->stopControl(); |
---|
[2072] | 65 | |
---|
| 66 | if (this->controller_) |
---|
| 67 | { |
---|
| 68 | delete this->controller_; |
---|
| 69 | this->controller_ = 0; |
---|
| 70 | } |
---|
[2662] | 71 | |
---|
| 72 | if (this->getGametype()) |
---|
| 73 | this->getGametype()->playerLeft(this); |
---|
[2072] | 74 | } |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | void PlayerInfo::registerVariables() |
---|
| 78 | { |
---|
[3280] | 79 | registerVariable(this->name_, VariableDirection::ToClient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::changedName)); |
---|
| 80 | registerVariable(this->controllableEntityID_, VariableDirection::ToClient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::networkcallback_changedcontrollableentityID)); |
---|
| 81 | registerVariable(this->bReadyToSpawn_, VariableDirection::ToServer); |
---|
| 82 | registerVariable(this->gtinfoID_, VariableDirection::ToClient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::networkcallback_changedgtinfoID)); |
---|
[2072] | 83 | } |
---|
| 84 | |
---|
| 85 | void PlayerInfo::changedName() |
---|
| 86 | { |
---|
[2662] | 87 | SUPER(PlayerInfo, changedName); |
---|
| 88 | |
---|
[2171] | 89 | if (this->isInitialized() && this->getGametype()) |
---|
[2072] | 90 | this->getGametype()->playerChangedName(this); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | void PlayerInfo::changedGametype() |
---|
| 94 | { |
---|
[2973] | 95 | this->updateGametypeInfo(); |
---|
| 96 | |
---|
[2171] | 97 | if (this->isInitialized()) |
---|
[2072] | 98 | { |
---|
| 99 | if (this->getOldGametype()) |
---|
| 100 | { |
---|
| 101 | if (this->getGametype()) |
---|
| 102 | this->getOldGametype()->playerSwitched(this, this->getGametype()); |
---|
| 103 | else |
---|
| 104 | this->getOldGametype()->playerLeft(this); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | if (this->getGametype()) |
---|
| 108 | { |
---|
| 109 | if (this->getOldGametype()) |
---|
| 110 | this->getGametype()->playerSwitchedBack(this, this->getOldGametype()); |
---|
| 111 | else |
---|
| 112 | this->getGametype()->playerEntered(this); |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
[2973] | 117 | void PlayerInfo::updateGametypeInfo() |
---|
| 118 | { |
---|
| 119 | this->gtinfo_ = 0; |
---|
| 120 | this->gtinfoID_ = OBJECTID_UNKNOWN; |
---|
| 121 | |
---|
| 122 | if (this->getGametype() && this->getGametype()->getGametypeInfo()) |
---|
| 123 | { |
---|
| 124 | this->gtinfo_ = this->getGametype()->getGametypeInfo(); |
---|
| 125 | this->gtinfoID_ = this->gtinfo_->getObjectID(); |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | |
---|
[2072] | 129 | void PlayerInfo::createController() |
---|
| 130 | { |
---|
[2839] | 131 | if (this->controller_) |
---|
| 132 | { |
---|
| 133 | delete this->controller_; |
---|
| 134 | this->controller_ = 0; |
---|
| 135 | } |
---|
[2072] | 136 | this->controller_ = this->defaultController_.fabricate(this); |
---|
| 137 | assert(this->controller_); |
---|
| 138 | this->controller_->setPlayer(this); |
---|
| 139 | if (this->controllableEntity_) |
---|
| 140 | this->controller_->setControllableEntity(this->controllableEntity_); |
---|
[2662] | 141 | this->changedController(); |
---|
[2072] | 142 | } |
---|
| 143 | |
---|
[3038] | 144 | void PlayerInfo::startControl(ControllableEntity* entity) |
---|
[2072] | 145 | { |
---|
[3038] | 146 | if (!entity || entity == this->controllableEntity_) |
---|
[2662] | 147 | return; |
---|
| 148 | |
---|
[2072] | 149 | if (this->controllableEntity_) |
---|
[3038] | 150 | this->stopControl(); |
---|
[2072] | 151 | |
---|
| 152 | this->controllableEntity_ = entity; |
---|
[3038] | 153 | this->controllableEntityID_ = entity->getObjectID(); |
---|
[2072] | 154 | |
---|
[3038] | 155 | entity->setPlayer(this); |
---|
[2072] | 156 | |
---|
[3038] | 157 | this->bReadyToSpawn_ &= (!this->bSetUnreadyAfterSpawn_); |
---|
| 158 | |
---|
[2072] | 159 | if (this->controller_) |
---|
| 160 | this->controller_->setControllableEntity(entity); |
---|
[2826] | 161 | |
---|
| 162 | this->changedControllableEntity(); |
---|
[2072] | 163 | } |
---|
| 164 | |
---|
[3038] | 165 | void PlayerInfo::stopControl() |
---|
[2072] | 166 | { |
---|
[3038] | 167 | ControllableEntity* entity = this->controllableEntity_; |
---|
[2072] | 168 | |
---|
[3038] | 169 | if (!entity) |
---|
| 170 | return; |
---|
[2072] | 171 | |
---|
[3038] | 172 | this->controllableEntity_ = 0; |
---|
| 173 | this->controllableEntityID_ = OBJECTID_UNKNOWN; |
---|
[2826] | 174 | |
---|
[3038] | 175 | if (this->controller_) |
---|
| 176 | this->controller_->setControllableEntity(0); |
---|
| 177 | |
---|
| 178 | entity->removePlayer(); |
---|
| 179 | |
---|
| 180 | this->changedControllableEntity(); |
---|
[2072] | 181 | } |
---|
| 182 | |
---|
| 183 | void PlayerInfo::networkcallback_changedcontrollableentityID() |
---|
| 184 | { |
---|
[2171] | 185 | if (this->controllableEntityID_ != OBJECTID_UNKNOWN) |
---|
[2072] | 186 | { |
---|
| 187 | Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_); |
---|
[3325] | 188 | ControllableEntity* entity = orxonox_cast<ControllableEntity*>(temp); |
---|
[2072] | 189 | this->startControl(entity); |
---|
| 190 | } |
---|
| 191 | else |
---|
| 192 | { |
---|
[3038] | 193 | this->stopControl(); |
---|
[2072] | 194 | } |
---|
| 195 | } |
---|
[2973] | 196 | |
---|
| 197 | void PlayerInfo::networkcallback_changedgtinfoID() |
---|
| 198 | { |
---|
| 199 | if (this->gtinfoID_ != OBJECTID_UNKNOWN) |
---|
| 200 | { |
---|
[3325] | 201 | this->gtinfo_ = orxonox_cast<GametypeInfo*>(Synchronisable::getSynchronisable(this->gtinfoID_)); |
---|
[2973] | 202 | |
---|
| 203 | if (!this->gtinfo_) |
---|
| 204 | this->gtinfoID_ = OBJECTID_UNKNOWN; |
---|
| 205 | } |
---|
| 206 | } |
---|
[2072] | 207 | } |
---|