[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 | { |
---|
| 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_); |
---|
[2662] | 117 | this->changedController(); |
---|
[2072] | 118 | } |
---|
| 119 | |
---|
[2662] | 120 | void PlayerInfo::startControl(ControllableEntity* entity, bool callback) |
---|
[2072] | 121 | { |
---|
[2662] | 122 | if (entity == this->controllableEntity_) |
---|
| 123 | return; |
---|
| 124 | |
---|
[2072] | 125 | if (this->controllableEntity_) |
---|
[2662] | 126 | this->stopControl(this->controllableEntity_, callback); |
---|
[2072] | 127 | |
---|
| 128 | this->controllableEntity_ = entity; |
---|
| 129 | |
---|
| 130 | if (entity) |
---|
| 131 | { |
---|
| 132 | this->controllableEntityID_ = entity->getObjectID(); |
---|
| 133 | entity->setPlayer(this); |
---|
[2662] | 134 | this->bReadyToSpawn_ &= (!this->bSetUnreadyAfterSpawn_); |
---|
[2072] | 135 | } |
---|
| 136 | else |
---|
| 137 | { |
---|
[2171] | 138 | this->controllableEntityID_ = OBJECTID_UNKNOWN; |
---|
[2072] | 139 | } |
---|
| 140 | |
---|
| 141 | if (this->controller_) |
---|
| 142 | this->controller_->setControllableEntity(entity); |
---|
[2789] | 143 | |
---|
| 144 | this->changedControllableEntity(); |
---|
[2072] | 145 | } |
---|
| 146 | |
---|
| 147 | void PlayerInfo::stopControl(ControllableEntity* entity, bool callback) |
---|
| 148 | { |
---|
| 149 | if (entity && this->controllableEntity_ == entity) |
---|
| 150 | { |
---|
| 151 | this->controllableEntity_ = 0; |
---|
[2171] | 152 | this->controllableEntityID_ = OBJECTID_UNKNOWN; |
---|
[2072] | 153 | |
---|
| 154 | if (this->controller_) |
---|
| 155 | this->controller_->setControllableEntity(0); |
---|
| 156 | |
---|
| 157 | if (callback) |
---|
| 158 | entity->removePlayer(); |
---|
[2789] | 159 | |
---|
| 160 | this->changedControllableEntity(); |
---|
[2072] | 161 | } |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | void PlayerInfo::networkcallback_changedcontrollableentityID() |
---|
| 165 | { |
---|
[2171] | 166 | if (this->controllableEntityID_ != OBJECTID_UNKNOWN) |
---|
[2072] | 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 | } |
---|