[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 "OrxonoxStableHeaders.h" |
---|
| 30 | #include "Pawn.h" |
---|
| 31 | |
---|
[2896] | 32 | #include "core/GameMode.h" |
---|
[2072] | 33 | #include "core/CoreIncludes.h" |
---|
| 34 | #include "core/XMLPort.h" |
---|
| 35 | #include "util/Math.h" |
---|
[2662] | 36 | #include "PawnManager.h" |
---|
[2072] | 37 | #include "objects/infos/PlayerInfo.h" |
---|
| 38 | #include "objects/gametypes/Gametype.h" |
---|
[2662] | 39 | #include "objects/worldentities/ParticleSpawner.h" |
---|
| 40 | #include "objects/worldentities/ExplosionChunk.h" |
---|
[2072] | 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
| 44 | CreateFactory(Pawn); |
---|
| 45 | |
---|
| 46 | Pawn::Pawn(BaseObject* creator) : ControllableEntity(creator) |
---|
| 47 | { |
---|
| 48 | RegisterObject(Pawn); |
---|
| 49 | |
---|
[2662] | 50 | PawnManager::touch(); |
---|
| 51 | this->bAlive_ = true; |
---|
| 52 | this->fire_ = 0x0; |
---|
| 53 | this->firehack_ = 0x0; |
---|
[2072] | 54 | |
---|
| 55 | this->health_ = 0; |
---|
| 56 | this->maxHealth_ = 0; |
---|
| 57 | this->initialHealth_ = 0; |
---|
| 58 | |
---|
| 59 | this->lastHitOriginator_ = 0; |
---|
| 60 | |
---|
[2662] | 61 | this->spawnparticleduration_ = 3.0f; |
---|
[2098] | 62 | |
---|
[2917] | 63 | this->getPickups().setOwner(this); |
---|
[2662] | 64 | |
---|
[2896] | 65 | if (GameMode::isMaster()) |
---|
[2662] | 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 | |
---|
[2072] | 76 | this->registerVariables(); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | Pawn::~Pawn() |
---|
| 80 | { |
---|
[2662] | 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 | } |
---|
[2072] | 89 | } |
---|
| 90 | |
---|
| 91 | void Pawn::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 92 | { |
---|
| 93 | SUPER(Pawn, XMLPort, xmlelement, mode); |
---|
| 94 | |
---|
[2662] | 95 | XMLPortParam(Pawn, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100); |
---|
[2072] | 96 | XMLPortParam(Pawn, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200); |
---|
| 97 | XMLPortParam(Pawn, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100); |
---|
[2662] | 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); |
---|
[2072] | 105 | } |
---|
| 106 | |
---|
| 107 | void Pawn::registerVariables() |
---|
| 108 | { |
---|
[2662] | 109 | registerVariable(this->bAlive_, variableDirection::toclient); |
---|
| 110 | registerVariable(this->health_, variableDirection::toclient); |
---|
| 111 | registerVariable(this->initialHealth_, variableDirection::toclient); |
---|
| 112 | registerVariable(this->fire_, variableDirection::toserver); |
---|
[2072] | 113 | } |
---|
| 114 | |
---|
| 115 | void Pawn::tick(float dt) |
---|
| 116 | { |
---|
[2809] | 117 | SUPER(Pawn, tick, dt); |
---|
[2072] | 118 | |
---|
[2662] | 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 | |
---|
[2072] | 131 | if (this->health_ <= 0) |
---|
| 132 | this->death(); |
---|
| 133 | } |
---|
| 134 | |
---|
[2826] | 135 | void Pawn::setPlayer(PlayerInfo* player) |
---|
| 136 | { |
---|
| 137 | ControllableEntity::setPlayer(player); |
---|
| 138 | |
---|
| 139 | if (this->getGametype()) |
---|
| 140 | this->getGametype()->playerStartsControllingPawn(player, this); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | void Pawn::removePlayer() |
---|
| 144 | { |
---|
| 145 | if (this->getGametype()) |
---|
| 146 | this->getGametype()->playerStopsControllingPawn(this->getPlayer(), this); |
---|
| 147 | |
---|
| 148 | ControllableEntity::removePlayer(); |
---|
| 149 | } |
---|
| 150 | |
---|
[2072] | 151 | void Pawn::setHealth(float health) |
---|
| 152 | { |
---|
| 153 | this->health_ = min(health, this->maxHealth_); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | void Pawn::damage(float damage, Pawn* originator) |
---|
| 157 | { |
---|
[2826] | 158 | if (this->getGametype() && this->getGametype()->allowPawnDamage(this, originator)) |
---|
| 159 | { |
---|
| 160 | this->setHealth(this->health_ - damage); |
---|
| 161 | this->lastHitOriginator_ = originator; |
---|
[2072] | 162 | |
---|
[2826] | 163 | // play damage effect |
---|
| 164 | } |
---|
[2072] | 165 | } |
---|
| 166 | |
---|
| 167 | void Pawn::hit(Pawn* originator, const Vector3& force, float damage) |
---|
| 168 | { |
---|
[2826] | 169 | if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator)) |
---|
| 170 | { |
---|
| 171 | this->damage(damage, originator); |
---|
| 172 | this->setVelocity(this->getVelocity() + force); |
---|
[2072] | 173 | |
---|
[2826] | 174 | // play hit effect |
---|
| 175 | } |
---|
[2072] | 176 | } |
---|
| 177 | |
---|
| 178 | void Pawn::kill() |
---|
| 179 | { |
---|
| 180 | this->damage(this->health_); |
---|
| 181 | this->death(); |
---|
| 182 | } |
---|
| 183 | |
---|
[2662] | 184 | void Pawn::spawneffect() |
---|
[2072] | 185 | { |
---|
| 186 | // play spawn effect |
---|
[2662] | 187 | if (this->spawnparticlesource_ != "") |
---|
| 188 | { |
---|
| 189 | ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); |
---|
| 190 | effect->setPosition(this->getPosition()); |
---|
| 191 | effect->setOrientation(this->getOrientation()); |
---|
| 192 | effect->setDestroyAfterLife(true); |
---|
| 193 | effect->setSource(this->spawnparticlesource_); |
---|
| 194 | effect->setLifetime(this->spawnparticleduration_); |
---|
| 195 | } |
---|
[2072] | 196 | } |
---|
| 197 | |
---|
| 198 | void Pawn::death() |
---|
| 199 | { |
---|
[2826] | 200 | if (this->getGametype() && this->getGametype()->allowPawnDeath(this, this->lastHitOriginator_)) |
---|
| 201 | { |
---|
| 202 | // Set bAlive_ to false and wait for PawnManager to do the destruction |
---|
| 203 | this->bAlive_ = false; |
---|
[2662] | 204 | |
---|
[2826] | 205 | this->setDestroyWhenPlayerLeft(false); |
---|
[2662] | 206 | |
---|
[2917] | 207 | this->dropItems(); |
---|
| 208 | |
---|
[2826] | 209 | if (this->getGametype()) |
---|
| 210 | this->getGametype()->pawnKilled(this, this->lastHitOriginator_); |
---|
[2662] | 211 | |
---|
[2826] | 212 | if (this->getPlayer()) |
---|
| 213 | this->getPlayer()->stopControl(this); |
---|
[2072] | 214 | |
---|
[2896] | 215 | if (GameMode::isMaster()) |
---|
[2826] | 216 | this->deatheffect(); |
---|
| 217 | } |
---|
[2904] | 218 | else |
---|
| 219 | this->setHealth(1); |
---|
[2662] | 220 | } |
---|
[2072] | 221 | |
---|
[2662] | 222 | void Pawn::deatheffect() |
---|
| 223 | { |
---|
[2072] | 224 | // play death effect |
---|
[2662] | 225 | { |
---|
| 226 | ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); |
---|
| 227 | effect->setPosition(this->getPosition()); |
---|
| 228 | effect->setOrientation(this->getOrientation()); |
---|
| 229 | effect->setDestroyAfterLife(true); |
---|
| 230 | effect->setSource("Orxonox/explosion2b"); |
---|
| 231 | effect->setLifetime(4.0f); |
---|
| 232 | } |
---|
| 233 | { |
---|
| 234 | ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); |
---|
| 235 | effect->setPosition(this->getPosition()); |
---|
| 236 | effect->setOrientation(this->getOrientation()); |
---|
| 237 | effect->setDestroyAfterLife(true); |
---|
| 238 | effect->setSource("Orxonox/smoke6"); |
---|
| 239 | effect->setLifetime(4.0f); |
---|
| 240 | } |
---|
| 241 | { |
---|
| 242 | ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); |
---|
| 243 | effect->setPosition(this->getPosition()); |
---|
| 244 | effect->setOrientation(this->getOrientation()); |
---|
| 245 | effect->setDestroyAfterLife(true); |
---|
| 246 | effect->setSource("Orxonox/sparks"); |
---|
| 247 | effect->setLifetime(4.0f); |
---|
| 248 | } |
---|
| 249 | for (unsigned int i = 0; i < this->numexplosionchunks_; ++i) |
---|
| 250 | { |
---|
| 251 | ExplosionChunk* chunk = new ExplosionChunk(this->getCreator()); |
---|
| 252 | chunk->setPosition(this->getPosition()); |
---|
| 253 | } |
---|
[2072] | 254 | } |
---|
| 255 | |
---|
[2662] | 256 | void Pawn::fire(WeaponMode::Enum fireMode) |
---|
[2098] | 257 | { |
---|
[2662] | 258 | this->firehack_ |= fireMode; |
---|
[2098] | 259 | } |
---|
| 260 | |
---|
[2072] | 261 | void Pawn::postSpawn() |
---|
| 262 | { |
---|
| 263 | this->setHealth(this->initialHealth_); |
---|
[2896] | 264 | if (GameMode::isMaster()) |
---|
[2662] | 265 | this->spawneffect(); |
---|
[2072] | 266 | } |
---|
[2662] | 267 | |
---|
| 268 | void Pawn::dropItems() |
---|
| 269 | { |
---|
[2917] | 270 | this->getPickups().clear(); |
---|
[2662] | 271 | } |
---|
| 272 | |
---|
[2893] | 273 | |
---|
| 274 | /* WeaponSystem: |
---|
| 275 | * functions load Slot, Set, Pack from XML and make sure all parent-pointers are set. |
---|
| 276 | * with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it. |
---|
| 277 | * --> e.g. Pickup-Items |
---|
| 278 | */ |
---|
[2662] | 279 | void Pawn::setWeaponSlot(WeaponSlot * wSlot) |
---|
| 280 | { |
---|
| 281 | this->attach(wSlot); |
---|
| 282 | if (this->weaponSystem_) |
---|
| 283 | this->weaponSystem_->attachWeaponSlot(wSlot); |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const |
---|
| 287 | { |
---|
| 288 | if (this->weaponSystem_) |
---|
| 289 | return this->weaponSystem_->getWeaponSlotPointer(index); |
---|
| 290 | else |
---|
| 291 | return 0; |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | void Pawn::setWeaponPack(WeaponPack * wPack) |
---|
| 295 | { |
---|
| 296 | if (this->weaponSystem_) |
---|
| 297 | { |
---|
| 298 | wPack->setParentWeaponSystem(this->weaponSystem_); |
---|
| 299 | wPack->setParentWeaponSystemToAllWeapons(this->weaponSystem_); |
---|
| 300 | this->weaponSystem_->attachWeaponPack( wPack,wPack->getFireMode() ); |
---|
| 301 | wPack->attachNeededMunitionToAllWeapons(); |
---|
| 302 | } |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | WeaponPack * Pawn::getWeaponPack(unsigned int firemode) const |
---|
| 306 | { |
---|
| 307 | if (this->weaponSystem_) |
---|
| 308 | return this->weaponSystem_->getWeaponPackPointer(firemode); |
---|
| 309 | else |
---|
| 310 | return 0; |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | void Pawn::setWeaponSet(WeaponSet * wSet) |
---|
| 314 | { |
---|
| 315 | if (this->weaponSystem_) |
---|
| 316 | this->weaponSystem_->attachWeaponSet(wSet); |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | WeaponSet * Pawn::getWeaponSet(unsigned int index) const |
---|
| 320 | { |
---|
| 321 | if (this->weaponSystem_) |
---|
| 322 | return this->weaponSystem_->getWeaponSetPointer(index); |
---|
| 323 | else |
---|
| 324 | return 0; |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | |
---|
| 328 | /////////////////// |
---|
| 329 | // Pawn Listener // |
---|
| 330 | /////////////////// |
---|
| 331 | PawnListener::PawnListener() |
---|
| 332 | { |
---|
| 333 | RegisterRootObject(PawnListener); |
---|
| 334 | } |
---|
[2072] | 335 | } |
---|