[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; |
---|
[2662] | 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 | } |
---|
[2662] | 67 | |
---|
| 68 | if (this->getGametype()) |
---|
| 69 | this->getGametype()->playerLeft(this); |
---|
[2072] | 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | void PlayerInfo::registerVariables() |
---|
| 74 | { |
---|
[2662] | 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); |
---|
[2072] | 78 | } |
---|
| 79 | |
---|
| 80 | void PlayerInfo::changedName() |
---|
| 81 | { |
---|
[2662] | 82 | SUPER(PlayerInfo, changedName); |
---|
| 83 | |
---|
[2171] | 84 | if (this->isInitialized() && this->getGametype()) |
---|
[2072] | 85 | this->getGametype()->playerChangedName(this); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | void PlayerInfo::changedGametype() |
---|
| 89 | { |
---|
[2171] | 90 | if (this->isInitialized()) |
---|
[2072] | 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 | { |
---|
[2839] | 112 | if (this->controller_) |
---|
| 113 | { |
---|
| 114 | delete this->controller_; |
---|
| 115 | this->controller_ = 0; |
---|
| 116 | } |
---|
[2072] | 117 | this->controller_ = this->defaultController_.fabricate(this); |
---|
| 118 | assert(this->controller_); |
---|
| 119 | this->controller_->setPlayer(this); |
---|
| 120 | if (this->controllableEntity_) |
---|
| 121 | this->controller_->setControllableEntity(this->controllableEntity_); |
---|
[2662] | 122 | this->changedController(); |
---|
[2072] | 123 | } |
---|
| 124 | |
---|
[2662] | 125 | void PlayerInfo::startControl(ControllableEntity* entity, bool callback) |
---|
[2072] | 126 | { |
---|
[2662] | 127 | if (entity == this->controllableEntity_) |
---|
| 128 | return; |
---|
| 129 | |
---|
[2072] | 130 | if (this->controllableEntity_) |
---|
[2662] | 131 | this->stopControl(this->controllableEntity_, callback); |
---|
[2072] | 132 | |
---|
| 133 | this->controllableEntity_ = entity; |
---|
| 134 | |
---|
| 135 | if (entity) |
---|
| 136 | { |
---|
| 137 | this->controllableEntityID_ = entity->getObjectID(); |
---|
| 138 | entity->setPlayer(this); |
---|
[2662] | 139 | this->bReadyToSpawn_ &= (!this->bSetUnreadyAfterSpawn_); |
---|
[2072] | 140 | } |
---|
| 141 | else |
---|
| 142 | { |
---|
[2171] | 143 | this->controllableEntityID_ = OBJECTID_UNKNOWN; |
---|
[2072] | 144 | } |
---|
| 145 | |
---|
| 146 | if (this->controller_) |
---|
| 147 | this->controller_->setControllableEntity(entity); |
---|
[2826] | 148 | |
---|
| 149 | this->changedControllableEntity(); |
---|
[2072] | 150 | } |
---|
| 151 | |
---|
| 152 | void PlayerInfo::stopControl(ControllableEntity* entity, bool callback) |
---|
| 153 | { |
---|
| 154 | if (entity && this->controllableEntity_ == entity) |
---|
| 155 | { |
---|
| 156 | this->controllableEntity_ = 0; |
---|
[2171] | 157 | this->controllableEntityID_ = OBJECTID_UNKNOWN; |
---|
[2072] | 158 | |
---|
| 159 | if (this->controller_) |
---|
| 160 | this->controller_->setControllableEntity(0); |
---|
| 161 | |
---|
| 162 | if (callback) |
---|
| 163 | entity->removePlayer(); |
---|
[2826] | 164 | |
---|
| 165 | this->changedControllableEntity(); |
---|
[2072] | 166 | } |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | void PlayerInfo::networkcallback_changedcontrollableentityID() |
---|
| 170 | { |
---|
[2171] | 171 | if (this->controllableEntityID_ != OBJECTID_UNKNOWN) |
---|
[2072] | 172 | { |
---|
| 173 | Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_); |
---|
| 174 | ControllableEntity* entity = dynamic_cast<ControllableEntity*>(temp); |
---|
| 175 | |
---|
| 176 | this->startControl(entity); |
---|
| 177 | } |
---|
| 178 | else |
---|
| 179 | { |
---|
| 180 | this->stopControl(this->controllableEntity_); |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | } |
---|