[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 "Pawn.h" |
---|
| 30 | |
---|
[3280] | 31 | #include <algorithm> |
---|
| 32 | |
---|
[3196] | 33 | #include "core/CoreIncludes.h" |
---|
[2896] | 34 | #include "core/GameMode.h" |
---|
[2072] | 35 | #include "core/XMLPort.h" |
---|
[3196] | 36 | #include "network/NetworkFunction.h" |
---|
| 37 | |
---|
[2662] | 38 | #include "PawnManager.h" |
---|
[5735] | 39 | #include "infos/PlayerInfo.h" |
---|
| 40 | #include "gametypes/Gametype.h" |
---|
[5737] | 41 | #include "graphics/ParticleSpawner.h" |
---|
[5735] | 42 | #include "worldentities/ExplosionChunk.h" |
---|
| 43 | #include "worldentities/BigExplosion.h" |
---|
| 44 | #include "weaponsystem/WeaponSystem.h" |
---|
| 45 | #include "weaponsystem/WeaponSlot.h" |
---|
| 46 | #include "weaponsystem/WeaponPack.h" |
---|
| 47 | #include "weaponsystem/WeaponSet.h" |
---|
[2072] | 48 | |
---|
[3084] | 49 | |
---|
[2072] | 50 | namespace orxonox |
---|
| 51 | { |
---|
| 52 | CreateFactory(Pawn); |
---|
| 53 | |
---|
[3084] | 54 | registerMemberNetworkFunction( Pawn, doFire ); |
---|
| 55 | |
---|
[2072] | 56 | Pawn::Pawn(BaseObject* creator) : ControllableEntity(creator) |
---|
| 57 | { |
---|
| 58 | RegisterObject(Pawn); |
---|
| 59 | |
---|
[2662] | 60 | PawnManager::touch(); |
---|
| 61 | this->bAlive_ = true; |
---|
[3053] | 62 | this->bReload_ = false; |
---|
[2072] | 63 | |
---|
| 64 | this->health_ = 0; |
---|
| 65 | this->maxHealth_ = 0; |
---|
| 66 | this->initialHealth_ = 0; |
---|
| 67 | |
---|
| 68 | this->lastHitOriginator_ = 0; |
---|
| 69 | |
---|
[2662] | 70 | this->spawnparticleduration_ = 3.0f; |
---|
[2098] | 71 | |
---|
[3073] | 72 | this->getPickups().setOwner(this); |
---|
[2662] | 73 | |
---|
[2896] | 74 | if (GameMode::isMaster()) |
---|
[2662] | 75 | { |
---|
| 76 | this->weaponSystem_ = new WeaponSystem(this); |
---|
[3053] | 77 | this->weaponSystem_->setPawn(this); |
---|
[2662] | 78 | } |
---|
| 79 | else |
---|
| 80 | this->weaponSystem_ = 0; |
---|
| 81 | |
---|
| 82 | this->setRadarObjectColour(ColourValue::Red); |
---|
| 83 | this->setRadarObjectShape(RadarViewable::Dot); |
---|
| 84 | |
---|
[2072] | 85 | this->registerVariables(); |
---|
[3089] | 86 | |
---|
| 87 | this->isHumanShip_ = this->hasLocalController(); |
---|
[2072] | 88 | } |
---|
| 89 | |
---|
| 90 | Pawn::~Pawn() |
---|
| 91 | { |
---|
[2662] | 92 | if (this->isInitialized()) |
---|
| 93 | { |
---|
| 94 | if (this->weaponSystem_) |
---|
[5929] | 95 | this->weaponSystem_->destroy(); |
---|
[2662] | 96 | } |
---|
[2072] | 97 | } |
---|
| 98 | |
---|
| 99 | void Pawn::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 100 | { |
---|
| 101 | SUPER(Pawn, XMLPort, xmlelement, mode); |
---|
| 102 | |
---|
[2662] | 103 | XMLPortParam(Pawn, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100); |
---|
[2072] | 104 | XMLPortParam(Pawn, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200); |
---|
| 105 | XMLPortParam(Pawn, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100); |
---|
[2662] | 106 | XMLPortParam(Pawn, "spawnparticlesource", setSpawnParticleSource, getSpawnParticleSource, xmlelement, mode); |
---|
| 107 | XMLPortParam(Pawn, "spawnparticleduration", setSpawnParticleDuration, getSpawnParticleDuration, xmlelement, mode).defaultValues(3.0f); |
---|
| 108 | XMLPortParam(Pawn, "explosionchunks", setExplosionChunks, getExplosionChunks, xmlelement, mode).defaultValues(7); |
---|
| 109 | |
---|
[3053] | 110 | XMLPortObject(Pawn, WeaponSlot, "weaponslots", addWeaponSlot, getWeaponSlot, xmlelement, mode); |
---|
| 111 | XMLPortObject(Pawn, WeaponSet, "weaponsets", addWeaponSet, getWeaponSet, xmlelement, mode); |
---|
| 112 | XMLPortObject(Pawn, WeaponPack, "weapons", addWeaponPack, getWeaponPack, xmlelement, mode); |
---|
[2072] | 113 | } |
---|
| 114 | |
---|
| 115 | void Pawn::registerVariables() |
---|
| 116 | { |
---|
[3280] | 117 | registerVariable(this->bAlive_, VariableDirection::ToClient); |
---|
| 118 | registerVariable(this->health_, VariableDirection::ToClient); |
---|
| 119 | registerVariable(this->initialHealth_, VariableDirection::ToClient); |
---|
| 120 | registerVariable(this->bReload_, VariableDirection::ToServer); |
---|
[2072] | 121 | } |
---|
| 122 | |
---|
| 123 | void Pawn::tick(float dt) |
---|
| 124 | { |
---|
[2809] | 125 | SUPER(Pawn, tick, dt); |
---|
[2072] | 126 | |
---|
[3053] | 127 | this->bReload_ = false; |
---|
[2662] | 128 | |
---|
[3084] | 129 | if (GameMode::isMaster()) |
---|
[3087] | 130 | if (this->health_ <= 0 && bAlive_) |
---|
| 131 | this->death(); |
---|
[2072] | 132 | } |
---|
| 133 | |
---|
[2826] | 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 | |
---|
[2072] | 150 | void Pawn::setHealth(float health) |
---|
| 151 | { |
---|
[3280] | 152 | this->health_ = std::min(health, this->maxHealth_); |
---|
[2072] | 153 | } |
---|
| 154 | |
---|
| 155 | void Pawn::damage(float damage, Pawn* originator) |
---|
| 156 | { |
---|
[2826] | 157 | if (this->getGametype() && this->getGametype()->allowPawnDamage(this, originator)) |
---|
| 158 | { |
---|
| 159 | this->setHealth(this->health_ - damage); |
---|
| 160 | this->lastHitOriginator_ = originator; |
---|
[2072] | 161 | |
---|
[2826] | 162 | // play damage effect |
---|
| 163 | } |
---|
[2072] | 164 | } |
---|
| 165 | |
---|
| 166 | void Pawn::hit(Pawn* originator, const Vector3& force, float damage) |
---|
| 167 | { |
---|
[2826] | 168 | if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator)) |
---|
| 169 | { |
---|
| 170 | this->damage(damage, originator); |
---|
| 171 | this->setVelocity(this->getVelocity() + force); |
---|
[2072] | 172 | |
---|
[2826] | 173 | // play hit effect |
---|
| 174 | } |
---|
[2072] | 175 | } |
---|
| 176 | |
---|
| 177 | void Pawn::kill() |
---|
| 178 | { |
---|
| 179 | this->damage(this->health_); |
---|
| 180 | this->death(); |
---|
| 181 | } |
---|
| 182 | |
---|
[2662] | 183 | void Pawn::spawneffect() |
---|
[2072] | 184 | { |
---|
| 185 | // play spawn effect |
---|
[2662] | 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 | } |
---|
[2072] | 195 | } |
---|
| 196 | |
---|
| 197 | void Pawn::death() |
---|
| 198 | { |
---|
[3033] | 199 | this->setHealth(1); |
---|
[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 | |
---|
[3073] | 207 | this->dropItems(); |
---|
| 208 | |
---|
[2826] | 209 | if (this->getGametype()) |
---|
| 210 | this->getGametype()->pawnKilled(this, this->lastHitOriginator_); |
---|
[2662] | 211 | |
---|
[3038] | 212 | if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this) |
---|
| 213 | this->getPlayer()->stopControl(); |
---|
[2072] | 214 | |
---|
[2896] | 215 | if (GameMode::isMaster()) |
---|
[3087] | 216 | { |
---|
| 217 | // this->deathEffect(); |
---|
| 218 | this->goWithStyle(); |
---|
| 219 | } |
---|
[2826] | 220 | } |
---|
[2662] | 221 | } |
---|
[3087] | 222 | void Pawn::goWithStyle() |
---|
| 223 | { |
---|
| 224 | this->bAlive_ = false; |
---|
| 225 | this->setDestroyWhenPlayerLeft(false); |
---|
[2072] | 226 | |
---|
[3087] | 227 | BigExplosion* chunk = new BigExplosion(this->getCreator()); |
---|
| 228 | chunk->setPosition(this->getPosition()); |
---|
| 229 | |
---|
| 230 | } |
---|
[2662] | 231 | void Pawn::deatheffect() |
---|
| 232 | { |
---|
[2072] | 233 | // play death effect |
---|
[2662] | 234 | { |
---|
| 235 | ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); |
---|
| 236 | effect->setPosition(this->getPosition()); |
---|
| 237 | effect->setOrientation(this->getOrientation()); |
---|
| 238 | effect->setDestroyAfterLife(true); |
---|
| 239 | effect->setSource("Orxonox/explosion2b"); |
---|
| 240 | effect->setLifetime(4.0f); |
---|
| 241 | } |
---|
| 242 | { |
---|
| 243 | ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); |
---|
| 244 | effect->setPosition(this->getPosition()); |
---|
| 245 | effect->setOrientation(this->getOrientation()); |
---|
| 246 | effect->setDestroyAfterLife(true); |
---|
| 247 | effect->setSource("Orxonox/smoke6"); |
---|
| 248 | effect->setLifetime(4.0f); |
---|
| 249 | } |
---|
| 250 | { |
---|
| 251 | ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); |
---|
| 252 | effect->setPosition(this->getPosition()); |
---|
| 253 | effect->setOrientation(this->getOrientation()); |
---|
| 254 | effect->setDestroyAfterLife(true); |
---|
| 255 | effect->setSource("Orxonox/sparks"); |
---|
| 256 | effect->setLifetime(4.0f); |
---|
| 257 | } |
---|
| 258 | for (unsigned int i = 0; i < this->numexplosionchunks_; ++i) |
---|
| 259 | { |
---|
| 260 | ExplosionChunk* chunk = new ExplosionChunk(this->getCreator()); |
---|
| 261 | chunk->setPosition(this->getPosition()); |
---|
| 262 | } |
---|
[2072] | 263 | } |
---|
| 264 | |
---|
[3053] | 265 | void Pawn::fire(unsigned int firemode) |
---|
[2098] | 266 | { |
---|
[3084] | 267 | this->doFire(firemode); |
---|
[2098] | 268 | } |
---|
| 269 | |
---|
[3084] | 270 | void Pawn::doFire(uint8_t firemode) |
---|
| 271 | { |
---|
| 272 | if(GameMode::isMaster()) |
---|
| 273 | { |
---|
| 274 | if (this->weaponSystem_) |
---|
| 275 | this->weaponSystem_->fire(firemode); |
---|
| 276 | } |
---|
| 277 | else |
---|
| 278 | { |
---|
[3300] | 279 | callMemberNetworkFunction(Pawn, doFire, this->getObjectID(), 0, firemode); |
---|
[3084] | 280 | if (this->weaponSystem_) |
---|
| 281 | this->weaponSystem_->fire(firemode); |
---|
| 282 | } |
---|
| 283 | } |
---|
| 284 | |
---|
[3053] | 285 | void Pawn::reload() |
---|
| 286 | { |
---|
| 287 | this->bReload_ = true; |
---|
| 288 | } |
---|
| 289 | |
---|
[2072] | 290 | void Pawn::postSpawn() |
---|
| 291 | { |
---|
| 292 | this->setHealth(this->initialHealth_); |
---|
[2896] | 293 | if (GameMode::isMaster()) |
---|
[2662] | 294 | this->spawneffect(); |
---|
[2072] | 295 | } |
---|
[2662] | 296 | |
---|
| 297 | void Pawn::dropItems() |
---|
| 298 | { |
---|
[3073] | 299 | this->getPickups().clear(); |
---|
[2662] | 300 | } |
---|
| 301 | |
---|
[2893] | 302 | |
---|
| 303 | /* WeaponSystem: |
---|
| 304 | * functions load Slot, Set, Pack from XML and make sure all parent-pointers are set. |
---|
| 305 | * with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it. |
---|
| 306 | * --> e.g. Pickup-Items |
---|
| 307 | */ |
---|
[3053] | 308 | void Pawn::addWeaponSlot(WeaponSlot * wSlot) |
---|
[2662] | 309 | { |
---|
| 310 | this->attach(wSlot); |
---|
| 311 | if (this->weaponSystem_) |
---|
[3053] | 312 | this->weaponSystem_->addWeaponSlot(wSlot); |
---|
[2662] | 313 | } |
---|
| 314 | |
---|
| 315 | WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const |
---|
| 316 | { |
---|
| 317 | if (this->weaponSystem_) |
---|
[3053] | 318 | return this->weaponSystem_->getWeaponSlot(index); |
---|
[2662] | 319 | else |
---|
| 320 | return 0; |
---|
| 321 | } |
---|
| 322 | |
---|
[3053] | 323 | void Pawn::addWeaponSet(WeaponSet * wSet) |
---|
[2662] | 324 | { |
---|
| 325 | if (this->weaponSystem_) |
---|
[3053] | 326 | this->weaponSystem_->addWeaponSet(wSet); |
---|
[2662] | 327 | } |
---|
| 328 | |
---|
[3053] | 329 | WeaponSet * Pawn::getWeaponSet(unsigned int index) const |
---|
[2662] | 330 | { |
---|
| 331 | if (this->weaponSystem_) |
---|
[3053] | 332 | return this->weaponSystem_->getWeaponSet(index); |
---|
[2662] | 333 | else |
---|
| 334 | return 0; |
---|
| 335 | } |
---|
| 336 | |
---|
[3053] | 337 | void Pawn::addWeaponPack(WeaponPack * wPack) |
---|
[2662] | 338 | { |
---|
| 339 | if (this->weaponSystem_) |
---|
[3053] | 340 | this->weaponSystem_->addWeaponPack(wPack); |
---|
[2662] | 341 | } |
---|
| 342 | |
---|
[3053] | 343 | WeaponPack * Pawn::getWeaponPack(unsigned int index) const |
---|
[2662] | 344 | { |
---|
| 345 | if (this->weaponSystem_) |
---|
[3053] | 346 | return this->weaponSystem_->getWeaponPack(index); |
---|
[2662] | 347 | else |
---|
| 348 | return 0; |
---|
| 349 | } |
---|
| 350 | |
---|
[3089] | 351 | //Tell the Map (RadarViewable), if this is a playership |
---|
| 352 | void Pawn::startLocalHumanControl() |
---|
| 353 | { |
---|
| 354 | // SUPER(ControllableEntity, changedPlayer()); |
---|
| 355 | ControllableEntity::startLocalHumanControl(); |
---|
| 356 | this->isHumanShip_ = true; |
---|
| 357 | } |
---|
[2072] | 358 | } |
---|