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