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 "OrxonoxStableHeaders.h" |
---|
30 | #include "Pawn.h" |
---|
31 | |
---|
32 | #include "core/Core.h" |
---|
33 | #include "core/CoreIncludes.h" |
---|
34 | #include "core/XMLPort.h" |
---|
35 | #include "util/Exception.h" |
---|
36 | #include "util/Math.h" |
---|
37 | #include "PawnManager.h" |
---|
38 | #include "objects/infos/PlayerInfo.h" |
---|
39 | #include "objects/gametypes/Gametype.h" |
---|
40 | #include "objects/worldentities/ParticleSpawner.h" |
---|
41 | #include "objects/worldentities/ExplosionChunk.h" |
---|
42 | |
---|
43 | namespace orxonox |
---|
44 | { |
---|
45 | CreateFactory(Pawn); |
---|
46 | |
---|
47 | Pawn::Pawn(BaseObject* creator) : ControllableEntity(creator) |
---|
48 | { |
---|
49 | RegisterObject(Pawn); |
---|
50 | |
---|
51 | PawnManager::touch(); |
---|
52 | this->bAlive_ = true; |
---|
53 | this->fire_ = 0x0; |
---|
54 | this->firehack_ = 0x0; |
---|
55 | |
---|
56 | this->health_ = 0; |
---|
57 | this->maxHealth_ = 0; |
---|
58 | this->initialHealth_ = 0; |
---|
59 | |
---|
60 | this->lastHitOriginator_ = 0; |
---|
61 | |
---|
62 | this->spawnparticleduration_ = 3.0f; |
---|
63 | |
---|
64 | if (Core::isMaster()) |
---|
65 | { |
---|
66 | this->weaponSystem_ = new WeaponSystem(this); |
---|
67 | this->weaponSystem_->setParentPawn(this); |
---|
68 | } |
---|
69 | else |
---|
70 | this->weaponSystem_ = 0; |
---|
71 | |
---|
72 | this->setRadarObjectColour(ColourValue::Red); |
---|
73 | this->setRadarObjectShape(RadarViewable::Dot); |
---|
74 | |
---|
75 | this->registerVariables(); |
---|
76 | } |
---|
77 | |
---|
78 | Pawn::~Pawn() |
---|
79 | { |
---|
80 | if (this->isInitialized()) |
---|
81 | { |
---|
82 | for (ObjectList<PawnListener>::iterator it = ObjectList<PawnListener>::begin(); it != ObjectList<PawnListener>::end(); ++it) |
---|
83 | it->destroyedPawn(this); |
---|
84 | |
---|
85 | if (this->weaponSystem_) |
---|
86 | delete this->weaponSystem_; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | void Pawn::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
91 | { |
---|
92 | SUPER(Pawn, XMLPort, xmlelement, mode); |
---|
93 | |
---|
94 | XMLPortParam(Pawn, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100); |
---|
95 | XMLPortParam(Pawn, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200); |
---|
96 | XMLPortParam(Pawn, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100); |
---|
97 | XMLPortParam(Pawn, "spawnparticlesource", setSpawnParticleSource, getSpawnParticleSource, xmlelement, mode); |
---|
98 | XMLPortParam(Pawn, "spawnparticleduration", setSpawnParticleDuration, getSpawnParticleDuration, xmlelement, mode).defaultValues(3.0f); |
---|
99 | XMLPortParam(Pawn, "explosionchunks", setExplosionChunks, getExplosionChunks, xmlelement, mode).defaultValues(7); |
---|
100 | |
---|
101 | XMLPortObject(Pawn, WeaponSlot, "weaponslots", setWeaponSlot, getWeaponSlot, xmlelement, mode); |
---|
102 | XMLPortObject(Pawn, WeaponSet, "weaponsets", setWeaponSet, getWeaponSet, xmlelement, mode); |
---|
103 | XMLPortObject(Pawn, WeaponPack, "weapons", setWeaponPack, getWeaponPack, xmlelement, mode); |
---|
104 | } |
---|
105 | |
---|
106 | void Pawn::registerVariables() |
---|
107 | { |
---|
108 | registerVariable(this->bAlive_, variableDirection::toclient); |
---|
109 | registerVariable(this->health_, variableDirection::toclient); |
---|
110 | registerVariable(this->initialHealth_, variableDirection::toclient); |
---|
111 | registerVariable(this->fire_, variableDirection::toserver); |
---|
112 | } |
---|
113 | |
---|
114 | void Pawn::tick(float dt) |
---|
115 | { |
---|
116 | SUPER(Pawn, tick, dt); |
---|
117 | |
---|
118 | if (this->weaponSystem_) |
---|
119 | { |
---|
120 | if (this->fire_ & WeaponMode::fire) |
---|
121 | this->weaponSystem_->fire(WeaponMode::fire); |
---|
122 | if (this->fire_ & WeaponMode::altFire) |
---|
123 | this->weaponSystem_->fire(WeaponMode::altFire); |
---|
124 | if (this->fire_ & WeaponMode::altFire2) |
---|
125 | this->weaponSystem_->fire(WeaponMode::altFire2); |
---|
126 | } |
---|
127 | this->fire_ = this->firehack_; |
---|
128 | this->firehack_ = 0x0; |
---|
129 | |
---|
130 | if (this->health_ <= 0) |
---|
131 | this->death(); |
---|
132 | } |
---|
133 | |
---|
134 | void Pawn::setPlayer(PlayerInfo* player) |
---|
135 | { |
---|
136 | ControllableEntity::setPlayer(player); |
---|
137 | |
---|
138 | if (this->getGametype()) |
---|
139 | this->getGametype()->playerStartsControllingPawn(player, this); |
---|
140 | } |
---|
141 | |
---|
142 | void Pawn::removePlayer() |
---|
143 | { |
---|
144 | if (this->getGametype()) |
---|
145 | this->getGametype()->playerStopsControllingPawn(this->getPlayer(), this); |
---|
146 | |
---|
147 | ControllableEntity::removePlayer(); |
---|
148 | } |
---|
149 | |
---|
150 | void Pawn::setHealth(float health) |
---|
151 | { |
---|
152 | this->health_ = min(health, this->maxHealth_); |
---|
153 | } |
---|
154 | |
---|
155 | void Pawn::damage(float damage, Pawn* originator) |
---|
156 | { |
---|
157 | if (this->getGametype() && this->getGametype()->allowPawnDamage(this, originator)) |
---|
158 | { |
---|
159 | this->setHealth(this->health_ - damage); |
---|
160 | this->lastHitOriginator_ = originator; |
---|
161 | |
---|
162 | // play damage effect |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | void Pawn::hit(Pawn* originator, const Vector3& force, float damage) |
---|
167 | { |
---|
168 | if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator)) |
---|
169 | { |
---|
170 | this->damage(damage, originator); |
---|
171 | this->setVelocity(this->getVelocity() + force); |
---|
172 | |
---|
173 | // play hit effect |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | void Pawn::kill() |
---|
178 | { |
---|
179 | this->damage(this->health_); |
---|
180 | this->death(); |
---|
181 | } |
---|
182 | |
---|
183 | void Pawn::spawneffect() |
---|
184 | { |
---|
185 | // play spawn effect |
---|
186 | if (this->spawnparticlesource_ != "") |
---|
187 | { |
---|
188 | ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); |
---|
189 | effect->setPosition(this->getPosition()); |
---|
190 | effect->setOrientation(this->getOrientation()); |
---|
191 | effect->setDestroyAfterLife(true); |
---|
192 | effect->setSource(this->spawnparticlesource_); |
---|
193 | effect->setLifetime(this->spawnparticleduration_); |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | void Pawn::death() |
---|
198 | { |
---|
199 | if (this->getGametype() && this->getGametype()->allowPawnDeath(this, this->lastHitOriginator_)) |
---|
200 | { |
---|
201 | // Set bAlive_ to false and wait for PawnManager to do the destruction |
---|
202 | this->bAlive_ = false; |
---|
203 | |
---|
204 | this->setDestroyWhenPlayerLeft(false); |
---|
205 | |
---|
206 | if (this->getGametype()) |
---|
207 | this->getGametype()->pawnKilled(this, this->lastHitOriginator_); |
---|
208 | |
---|
209 | if (this->getPlayer()) |
---|
210 | this->getPlayer()->stopControl(this); |
---|
211 | |
---|
212 | if (Core::isMaster()) |
---|
213 | this->deatheffect(); |
---|
214 | } |
---|
215 | else |
---|
216 | this->setHealth(1); |
---|
217 | } |
---|
218 | |
---|
219 | void Pawn::deatheffect() |
---|
220 | { |
---|
221 | // play death effect |
---|
222 | { |
---|
223 | ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); |
---|
224 | effect->setPosition(this->getPosition()); |
---|
225 | effect->setOrientation(this->getOrientation()); |
---|
226 | effect->setDestroyAfterLife(true); |
---|
227 | effect->setSource("Orxonox/explosion2b"); |
---|
228 | effect->setLifetime(4.0f); |
---|
229 | } |
---|
230 | { |
---|
231 | ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); |
---|
232 | effect->setPosition(this->getPosition()); |
---|
233 | effect->setOrientation(this->getOrientation()); |
---|
234 | effect->setDestroyAfterLife(true); |
---|
235 | effect->setSource("Orxonox/smoke6"); |
---|
236 | effect->setLifetime(4.0f); |
---|
237 | } |
---|
238 | { |
---|
239 | ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); |
---|
240 | effect->setPosition(this->getPosition()); |
---|
241 | effect->setOrientation(this->getOrientation()); |
---|
242 | effect->setDestroyAfterLife(true); |
---|
243 | effect->setSource("Orxonox/sparks"); |
---|
244 | effect->setLifetime(4.0f); |
---|
245 | } |
---|
246 | for (unsigned int i = 0; i < this->numexplosionchunks_; ++i) |
---|
247 | { |
---|
248 | ExplosionChunk* chunk = new ExplosionChunk(this->getCreator()); |
---|
249 | chunk->setPosition(this->getPosition()); |
---|
250 | |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | void Pawn::fire(WeaponMode::Enum fireMode) |
---|
255 | { |
---|
256 | this->firehack_ |= fireMode; |
---|
257 | } |
---|
258 | |
---|
259 | void Pawn::postSpawn() |
---|
260 | { |
---|
261 | this->setHealth(this->initialHealth_); |
---|
262 | if (Core::isMaster()) |
---|
263 | this->spawneffect(); |
---|
264 | } |
---|
265 | |
---|
266 | void Pawn::dropItems() |
---|
267 | { |
---|
268 | ThrowException(NotImplemented, "Pickupsystem not implemented yet."); |
---|
269 | } |
---|
270 | |
---|
271 | void Pawn::setWeaponSlot(WeaponSlot * wSlot) |
---|
272 | { |
---|
273 | this->attach(wSlot); |
---|
274 | if (this->weaponSystem_) |
---|
275 | this->weaponSystem_->attachWeaponSlot(wSlot); |
---|
276 | } |
---|
277 | |
---|
278 | WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const |
---|
279 | { |
---|
280 | if (this->weaponSystem_) |
---|
281 | return this->weaponSystem_->getWeaponSlotPointer(index); |
---|
282 | else |
---|
283 | return 0; |
---|
284 | } |
---|
285 | |
---|
286 | void Pawn::setWeaponPack(WeaponPack * wPack) |
---|
287 | { |
---|
288 | if (this->weaponSystem_) |
---|
289 | { |
---|
290 | wPack->setParentWeaponSystem(this->weaponSystem_); |
---|
291 | wPack->setParentWeaponSystemToAllWeapons(this->weaponSystem_); |
---|
292 | this->weaponSystem_->attachWeaponPack( wPack,wPack->getFireMode() ); |
---|
293 | wPack->attachNeededMunitionToAllWeapons(); |
---|
294 | } |
---|
295 | } |
---|
296 | |
---|
297 | WeaponPack * Pawn::getWeaponPack(unsigned int firemode) const |
---|
298 | { |
---|
299 | if (this->weaponSystem_) |
---|
300 | return this->weaponSystem_->getWeaponPackPointer(firemode); |
---|
301 | else |
---|
302 | return 0; |
---|
303 | } |
---|
304 | |
---|
305 | void Pawn::setWeaponSet(WeaponSet * wSet) |
---|
306 | { |
---|
307 | if (this->weaponSystem_) |
---|
308 | this->weaponSystem_->attachWeaponSet(wSet); |
---|
309 | } |
---|
310 | |
---|
311 | WeaponSet * Pawn::getWeaponSet(unsigned int index) const |
---|
312 | { |
---|
313 | if (this->weaponSystem_) |
---|
314 | return this->weaponSystem_->getWeaponSetPointer(index); |
---|
315 | else |
---|
316 | return 0; |
---|
317 | } |
---|
318 | |
---|
319 | |
---|
320 | /////////////////// |
---|
321 | // Pawn Listener // |
---|
322 | /////////////////// |
---|
323 | PawnListener::PawnListener() |
---|
324 | { |
---|
325 | RegisterRootObject(PawnListener); |
---|
326 | } |
---|
327 | } |
---|