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