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