[5838] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
[5841] | 12 | main-programmer: Silvan Nellen |
---|
| 13 | co-programmer: Benjamin Knecht |
---|
[5838] | 14 | */ |
---|
| 15 | |
---|
[5881] | 16 | |
---|
[5838] | 17 | #include "playable.h" |
---|
[5895] | 18 | |
---|
| 19 | #include "weapons/weapon_manager.h" |
---|
[5875] | 20 | #include "event_handler.h" |
---|
| 21 | #include "player.h" |
---|
[6241] | 22 | #include "state.h" |
---|
[7092] | 23 | #include "load_param.h" |
---|
[5838] | 24 | |
---|
[6700] | 25 | #include "world_entities/projectiles/projectile.h" |
---|
| 26 | |
---|
[6547] | 27 | #include "power_ups/weapon_power_up.h" |
---|
| 28 | #include "power_ups/param_power_up.h" |
---|
[5872] | 29 | |
---|
[7044] | 30 | #include "game_rules.h" |
---|
[6547] | 31 | |
---|
[6959] | 32 | #include "dot_emitter.h" |
---|
| 33 | #include "sprite_particles.h" |
---|
| 34 | |
---|
| 35 | |
---|
[5838] | 36 | Playable::Playable() |
---|
| 37 | { |
---|
[6442] | 38 | this->setClassID(CL_PLAYABLE, "Playable"); |
---|
| 39 | PRINTF(4)("PLAYABLE INIT\n"); |
---|
| 40 | |
---|
| 41 | this->toList(OM_GROUP_01); |
---|
| 42 | this->weaponMan = new WeaponManager(this); |
---|
| 43 | |
---|
| 44 | // the reference to the Current Player is NULL, because we dont have one at the beginning. |
---|
| 45 | this->currentPlayer = NULL; |
---|
[6695] | 46 | |
---|
[6804] | 47 | this->bFire = false; |
---|
[6868] | 48 | this->oldFlags = 0; |
---|
[6804] | 49 | |
---|
[6695] | 50 | this->setSynchronized(true); |
---|
[6959] | 51 | |
---|
| 52 | this->score = 0; |
---|
| 53 | this->oldScore = 0; |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | this->emitter = new DotEmitter(100, 5, M_2_PI); |
---|
| 57 | this->emitter->setParent(this); |
---|
| 58 | this->emitter->setSpread(M_PI, M_PI); |
---|
| 59 | this->emitter->setEmissionRate(300.0); |
---|
| 60 | this->emitter->setEmissionVelocity(50.0); |
---|
| 61 | |
---|
| 62 | this->explosionParticles = new SpriteParticles(1000); |
---|
| 63 | this->explosionParticles->setName("LaserExplosionParticles"); |
---|
| 64 | this->explosionParticles->setLifeSpan(.5, .3); |
---|
| 65 | this->explosionParticles->setRadius(0.0, 10.0); |
---|
| 66 | this->explosionParticles->setRadius(.5, 6.0); |
---|
| 67 | this->explosionParticles->setRadius(1.0, 3.0); |
---|
| 68 | this->explosionParticles->setColor(0.0, 1,1,0,.9); |
---|
| 69 | this->explosionParticles->setColor(0.5, .8,.8,0,.5); |
---|
| 70 | this->explosionParticles->setColor(1.0, .8,.8,.7,.0); |
---|
[5838] | 71 | } |
---|
| 72 | |
---|
[6695] | 73 | |
---|
| 74 | |
---|
[5875] | 75 | Playable::~Playable() |
---|
[5838] | 76 | { |
---|
[5881] | 77 | delete this->weaponMan; |
---|
[5895] | 78 | |
---|
[6986] | 79 | // THE DERIVED CLASS MUST UNSUBSCRIBE THE PLAYER THROUGH |
---|
| 80 | // this->setPlayer(NULL); |
---|
| 81 | // IN ITS DESTRUCTOR. |
---|
| 82 | assert(this->currentPlayer == NULL); |
---|
[5875] | 83 | } |
---|
| 84 | |
---|
[5898] | 85 | |
---|
[7092] | 86 | void Playable::loadParams(const TiXmlElement* root) |
---|
| 87 | { |
---|
| 88 | WorldEntity::loadParams(root); |
---|
| 89 | |
---|
| 90 | LoadParam(root, "abs-dir", this, Playable, setAbsDirPlay); |
---|
| 91 | } |
---|
| 92 | |
---|
[6443] | 93 | void Playable::addWeapon(Weapon* weapon, int configID, int slotID) |
---|
| 94 | { |
---|
[6676] | 95 | this->weaponMan->addWeapon(weapon, configID, slotID); |
---|
[6443] | 96 | |
---|
[6578] | 97 | this->weaponConfigChanged(); |
---|
[6443] | 98 | } |
---|
| 99 | |
---|
[6695] | 100 | |
---|
[6443] | 101 | void Playable::removeWeapon(Weapon* weapon) |
---|
| 102 | { |
---|
| 103 | this->weaponMan->removeWeapon(weapon); |
---|
| 104 | |
---|
[6568] | 105 | this->weaponConfigChanged(); |
---|
[6443] | 106 | } |
---|
| 107 | |
---|
[6695] | 108 | |
---|
[6444] | 109 | void Playable::nextWeaponConfig() |
---|
| 110 | { |
---|
| 111 | this->weaponMan->nextWeaponConfig(); |
---|
[6568] | 112 | this->weaponConfigChanged(); |
---|
[6444] | 113 | } |
---|
[6443] | 114 | |
---|
[6695] | 115 | |
---|
[6444] | 116 | void Playable::previousWeaponConfig() |
---|
| 117 | { |
---|
| 118 | this->weaponMan->previousWeaponConfig(); |
---|
[6568] | 119 | this->weaponConfigChanged(); |
---|
[6444] | 120 | } |
---|
| 121 | |
---|
[6695] | 122 | |
---|
[6568] | 123 | void Playable::weaponConfigChanged() |
---|
| 124 | { |
---|
[6578] | 125 | if (this->currentPlayer != NULL) |
---|
| 126 | this->currentPlayer->weaponConfigChanged(); |
---|
[6568] | 127 | } |
---|
[6444] | 128 | |
---|
[6695] | 129 | |
---|
[5872] | 130 | /** |
---|
[6436] | 131 | * @brief helps us colliding Playables |
---|
| 132 | */ |
---|
| 133 | void Playable::collidesWith(WorldEntity* entity, const Vector& location) |
---|
| 134 | { |
---|
[7072] | 135 | if (entity == collider) |
---|
| 136 | return; |
---|
| 137 | collider = entity; |
---|
| 138 | |
---|
| 139 | if (entity->isA(CL_PROJECTILE)) |
---|
[6966] | 140 | { |
---|
[7072] | 141 | this->decreaseHealth(entity->getHealth() *(float)rand()/(float)RAND_MAX); |
---|
[6966] | 142 | // EXTREME HACK |
---|
[7072] | 143 | if (this->getHealth() <= 0.0f) |
---|
[6966] | 144 | { |
---|
| 145 | this->die(); |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | } |
---|
[6436] | 149 | |
---|
[6966] | 150 | |
---|
[7044] | 151 | void Playable::respawn() |
---|
[6966] | 152 | { |
---|
[7044] | 153 | PRINTF(0)("Playable respawn\n"); |
---|
| 154 | // only if this is the spaceship of the player |
---|
| 155 | if( this == State::getPlayer()->getPlayable()) |
---|
| 156 | State::getGameRules()->onPlayerSpawn(); |
---|
[6966] | 157 | |
---|
[7085] | 158 | this->reset(); |
---|
| 159 | |
---|
[7088] | 160 | |
---|
[7082] | 161 | if( this->getOwner() % 2 == 0) |
---|
| 162 | { |
---|
[7091] | 163 | // this->toList(OM_GROUP_00); |
---|
[7082] | 164 | this->setAbsCoor(213.37, 57.71, -47.98); |
---|
[7094] | 165 | this->setAbsDir(0, 0, 1, 0); |
---|
[7082] | 166 | } |
---|
[6966] | 167 | else |
---|
[7082] | 168 | { |
---|
[7091] | 169 | // this->toList(OM_GROUP_01); |
---|
[7082] | 170 | this->setAbsCoor(-314.450, 40.701, 83.554); |
---|
[7088] | 171 | this->setAbsDir(0, 0, 1, 0); |
---|
[7082] | 172 | } |
---|
[6436] | 173 | } |
---|
| 174 | |
---|
[6966] | 175 | |
---|
[7088] | 176 | |
---|
[7044] | 177 | void Playable::die() |
---|
| 178 | { |
---|
| 179 | PRINTF(0)("Playable dies\n"); |
---|
| 180 | // only if this is the spaceship of the player |
---|
[7072] | 181 | if (State::isOnline()) |
---|
| 182 | { |
---|
[7078] | 183 | if( this == State::getPlayer()->getPlayable()) |
---|
| 184 | State::getGameRules()->onPlayerDeath(); |
---|
[7044] | 185 | |
---|
| 186 | |
---|
[7091] | 187 | // this->toList(OM_GROUP_05); |
---|
[7078] | 188 | //HACK: moves the entity to an unknown place far far away: in the future, GameRules will look for that |
---|
| 189 | this->setAbsCoor(-2000.0, -2000.0, -2000.0); |
---|
| 190 | |
---|
[7044] | 191 | //explosion hack |
---|
[7078] | 192 | this->emitter->setSystem(explosionParticles); |
---|
| 193 | this->emitter->setSystem(NULL); |
---|
[7072] | 194 | } |
---|
[7044] | 195 | } |
---|
| 196 | |
---|
| 197 | |
---|
[6436] | 198 | /** |
---|
[5872] | 199 | * subscribe to all events the controllable needs |
---|
[5898] | 200 | * @param player the player that shall controll this Playable |
---|
[5872] | 201 | */ |
---|
[6986] | 202 | bool Playable::setPlayer(Player* player) |
---|
[5872] | 203 | { |
---|
[6986] | 204 | // if we already have a Player inside do nothing |
---|
| 205 | if (this->currentPlayer != NULL && player != NULL) |
---|
[5872] | 206 | { |
---|
[5895] | 207 | return false; |
---|
[5875] | 208 | } |
---|
[6986] | 209 | |
---|
| 210 | // eject the Player if player == NULL |
---|
| 211 | if (this->currentPlayer != NULL && player == NULL) |
---|
[5895] | 212 | { |
---|
[6987] | 213 | PRINTF(4)("Player gets ejected\n"); |
---|
[6986] | 214 | |
---|
| 215 | // unsubscibe all events. |
---|
[5895] | 216 | EventHandler* evh = EventHandler::getInstance(); |
---|
| 217 | std::list<int>::iterator ev; |
---|
| 218 | for (ev = this->events.begin(); ev != events.end(); ev++) |
---|
[6986] | 219 | evh->unsubscribe( ES_GAME, (*ev)); |
---|
| 220 | |
---|
| 221 | // leave the entity |
---|
| 222 | this->leave(); |
---|
| 223 | |
---|
| 224 | // eject the current Player. |
---|
| 225 | Player* ejectPlayer = this->currentPlayer; |
---|
| 226 | this->currentPlayer = NULL; |
---|
| 227 | // eject the Player. |
---|
| 228 | ejectPlayer->setPlayable(NULL); |
---|
| 229 | |
---|
[5895] | 230 | return true; |
---|
| 231 | } |
---|
[5889] | 232 | |
---|
[6986] | 233 | // get the new Player inside |
---|
| 234 | if (this->currentPlayer == NULL && player != NULL) |
---|
[5896] | 235 | { |
---|
[6987] | 236 | PRINTF(4)("New Player gets inside\n"); |
---|
[6986] | 237 | this->currentPlayer = player; |
---|
| 238 | if (this->currentPlayer->getPlayable() != this) |
---|
| 239 | this->currentPlayer->setPlayable(this); |
---|
[5896] | 240 | |
---|
| 241 | /*EventHandler*/ |
---|
| 242 | EventHandler* evh = EventHandler::getInstance(); |
---|
| 243 | std::list<int>::iterator ev; |
---|
| 244 | for (ev = this->events.begin(); ev != events.end(); ev++) |
---|
[6986] | 245 | evh->subscribe(player, ES_GAME, (*ev)); |
---|
[5896] | 246 | |
---|
[6986] | 247 | this->enter(); |
---|
[5896] | 248 | return true; |
---|
| 249 | } |
---|
[6986] | 250 | |
---|
| 251 | return false; |
---|
[5896] | 252 | } |
---|
| 253 | |
---|
[6986] | 254 | |
---|
[6547] | 255 | bool Playable::pickup(PowerUp* powerUp) |
---|
| 256 | { |
---|
| 257 | if(powerUp->isA(CL_WEAPON_POWER_UP)) { |
---|
[6973] | 258 | return dynamic_cast<WeaponPowerUp*>(powerUp)->process(this->getWeaponManager()); |
---|
[6547] | 259 | } |
---|
| 260 | else if(powerUp->isA(CL_PARAM_POWER_UP)) { |
---|
| 261 | ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp); |
---|
| 262 | switch(ppu->getType()) { |
---|
| 263 | case POWERUP_PARAM_HEALTH: |
---|
[6700] | 264 | this->increaseHealth(ppu->getValue()); |
---|
[6547] | 265 | return true; |
---|
| 266 | case POWERUP_PARAM_MAX_HEALTH: |
---|
[6700] | 267 | this->increaseHealthMax(ppu->getValue()); |
---|
[6547] | 268 | return true; |
---|
| 269 | } |
---|
| 270 | } |
---|
| 271 | return false; |
---|
| 272 | } |
---|
| 273 | |
---|
[5896] | 274 | /** |
---|
[5898] | 275 | * add an event to the event list of events this Playable can capture |
---|
| 276 | * @param eventType the Type of event to add |
---|
[5889] | 277 | */ |
---|
[5896] | 278 | void Playable::registerEvent(int eventType) |
---|
[5889] | 279 | { |
---|
| 280 | this->events.push_back(eventType); |
---|
| 281 | |
---|
[5896] | 282 | if (this->currentPlayer != NULL) |
---|
| 283 | EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType); |
---|
[5889] | 284 | } |
---|
| 285 | |
---|
[5896] | 286 | /** |
---|
[5898] | 287 | * remove an event to the event list this Playable can capture. |
---|
| 288 | * @param event the event to unregister. |
---|
[5896] | 289 | */ |
---|
| 290 | void Playable::unregisterEvent(int eventType) |
---|
| 291 | { |
---|
[5902] | 292 | this->events.remove(eventType); |
---|
[5889] | 293 | |
---|
[5896] | 294 | if (this->currentPlayer != NULL) |
---|
| 295 | EventHandler::getInstance()->unsubscribe(ES_GAME, eventType); |
---|
| 296 | } |
---|
[5889] | 297 | |
---|
[6804] | 298 | /** |
---|
| 299 | * @brief ticks a Playable |
---|
| 300 | * @param dt: the passed time since the last Tick |
---|
| 301 | */ |
---|
| 302 | void Playable::tick(float dt) |
---|
| 303 | { |
---|
| 304 | this->weaponMan->tick(dt); |
---|
| 305 | if (this->bFire) |
---|
| 306 | weaponMan->fire(); |
---|
| 307 | } |
---|
[5896] | 308 | |
---|
[6804] | 309 | |
---|
| 310 | /** |
---|
| 311 | * @brief processes Playable events. |
---|
| 312 | * @param event the Captured Event. |
---|
| 313 | */ |
---|
| 314 | void Playable::process(const Event &event) |
---|
| 315 | { |
---|
| 316 | if( event.type == KeyMapper::PEV_FIRE1) |
---|
| 317 | this->bFire = event.bPressed; |
---|
| 318 | else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed) |
---|
| 319 | { |
---|
| 320 | this->nextWeaponConfig(); |
---|
| 321 | } |
---|
| 322 | else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed) |
---|
| 323 | this->previousWeaponConfig(); |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | |
---|
| 327 | |
---|
[6241] | 328 | void Playable::attachCamera() |
---|
| 329 | { |
---|
[7014] | 330 | State::getCameraNode()->setParentSoft(this); |
---|
| 331 | State::getCameraTargetNode()->setParentSoft(this); |
---|
[6241] | 332 | |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | |
---|
[6804] | 336 | |
---|
| 337 | |
---|
[6241] | 338 | void Playable::detachCamera() |
---|
| 339 | { |
---|
| 340 | } |
---|
[6868] | 341 | |
---|
[6959] | 342 | #define DATA_FLAGS 1 |
---|
| 343 | #define DATA_SCORE 2 |
---|
| 344 | |
---|
[6868] | 345 | #define FLAGS_bFire 1 |
---|
| 346 | |
---|
| 347 | int Playable::writeSync( const byte * data, int length, int sender ) |
---|
| 348 | { |
---|
| 349 | SYNCHELP_READ_BEGIN(); |
---|
[7010] | 350 | |
---|
[6994] | 351 | byte b; |
---|
| 352 | SYNCHELP_READ_BYTE( b, NWT_PL_B ); |
---|
[7010] | 353 | |
---|
[6868] | 354 | byte flags; |
---|
[7010] | 355 | |
---|
[6994] | 356 | if ( b == DATA_FLAGS ) |
---|
| 357 | { |
---|
| 358 | SYNCHELP_READ_BYTE( flags, NWT_PL_FLAGS ); |
---|
[6871] | 359 | |
---|
[6994] | 360 | bFire = (flags & FLAGS_bFire) != 0; |
---|
[7010] | 361 | |
---|
[6994] | 362 | return SYNCHELP_READ_N; |
---|
| 363 | } |
---|
[7010] | 364 | |
---|
[6994] | 365 | if ( b == DATA_SCORE ) |
---|
| 366 | { |
---|
| 367 | int newScore; |
---|
| 368 | SYNCHELP_READ_BYTE( newScore, NWT_PL_SCORE ); |
---|
| 369 | setScore( newScore ); |
---|
[7010] | 370 | |
---|
[6994] | 371 | return SYNCHELP_READ_N; |
---|
| 372 | } |
---|
[6871] | 373 | |
---|
[6868] | 374 | return SYNCHELP_READ_N; |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | int Playable::readSync( byte * data, int maxLength ) |
---|
| 378 | { |
---|
| 379 | SYNCHELP_WRITE_BEGIN(); |
---|
[7010] | 380 | |
---|
[6994] | 381 | if ( score != oldScore && isServer() ) |
---|
| 382 | { |
---|
| 383 | SYNCHELP_WRITE_BYTE( DATA_SCORE, NWT_PL_B); |
---|
| 384 | SYNCHELP_WRITE_INT( score, NWT_PL_SCORE ); |
---|
| 385 | oldScore = score; |
---|
[7010] | 386 | |
---|
[6994] | 387 | return SYNCHELP_WRITE_N; |
---|
| 388 | } |
---|
[7010] | 389 | |
---|
[6868] | 390 | byte flags = 0; |
---|
[6871] | 391 | |
---|
[6868] | 392 | if ( bFire ) |
---|
| 393 | flags |= FLAGS_bFire; |
---|
| 394 | |
---|
[6871] | 395 | |
---|
[6994] | 396 | SYNCHELP_WRITE_BYTE( DATA_FLAGS, NWT_PL_B); |
---|
[6868] | 397 | SYNCHELP_WRITE_BYTE( flags, NWT_PL_FLAGS ); |
---|
| 398 | oldFlags = flags; |
---|
| 399 | |
---|
[6871] | 400 | |
---|
[6868] | 401 | return SYNCHELP_WRITE_N; |
---|
| 402 | } |
---|
| 403 | |
---|
| 404 | bool Playable::needsReadSync( ) |
---|
| 405 | { |
---|
[6994] | 406 | if ( score != oldScore && isServer() ) |
---|
| 407 | return true; |
---|
[6959] | 408 | |
---|
[6868] | 409 | byte flags = 0; |
---|
[6871] | 410 | |
---|
[6868] | 411 | if ( bFire ) |
---|
| 412 | flags |= FLAGS_bFire; |
---|
[6871] | 413 | |
---|
[6868] | 414 | return flags!=oldFlags; |
---|
| 415 | } |
---|