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