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