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