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