| [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 | { |
|---|
| [6986] | 164 | printf("No Change in Playable\n"); |
|---|
| [5895] | 165 | return false; |
|---|
| [5875] | 166 | } |
|---|
| [6986] | 167 | |
|---|
| 168 | // eject the Player if player == NULL |
|---|
| 169 | if (this->currentPlayer != NULL && player == NULL) |
|---|
| [5895] | 170 | { |
|---|
| [6986] | 171 | PRINTF(0)("Player gets ejected\n"); |
|---|
| 172 | |
|---|
| 173 | // unsubscibe all events. |
|---|
| [5895] | 174 | EventHandler* evh = EventHandler::getInstance(); |
|---|
| 175 | std::list<int>::iterator ev; |
|---|
| 176 | for (ev = this->events.begin(); ev != events.end(); ev++) |
|---|
| [6986] | 177 | evh->unsubscribe( ES_GAME, (*ev)); |
|---|
| 178 | |
|---|
| 179 | // leave the entity |
|---|
| 180 | this->leave(); |
|---|
| 181 | |
|---|
| 182 | // eject the current Player. |
|---|
| 183 | Player* ejectPlayer = this->currentPlayer; |
|---|
| 184 | this->currentPlayer = NULL; |
|---|
| 185 | // eject the Player. |
|---|
| 186 | ejectPlayer->setPlayable(NULL); |
|---|
| 187 | |
|---|
| [5895] | 188 | return true; |
|---|
| 189 | } |
|---|
| [5889] | 190 | |
|---|
| [6986] | 191 | // get the new Player inside |
|---|
| 192 | if (this->currentPlayer == NULL && player != NULL) |
|---|
| [5896] | 193 | { |
|---|
| [6986] | 194 | PRINTF(0)("New Player gets inside\n"); |
|---|
| 195 | this->currentPlayer = player; |
|---|
| 196 | if (this->currentPlayer->getPlayable() != this) |
|---|
| 197 | this->currentPlayer->setPlayable(this); |
|---|
| [5896] | 198 | |
|---|
| 199 | /*EventHandler*/ |
|---|
| 200 | EventHandler* evh = EventHandler::getInstance(); |
|---|
| 201 | std::list<int>::iterator ev; |
|---|
| 202 | for (ev = this->events.begin(); ev != events.end(); ev++) |
|---|
| [6986] | 203 | evh->subscribe(player, ES_GAME, (*ev)); |
|---|
| [5896] | 204 | |
|---|
| [6986] | 205 | this->enter(); |
|---|
| [5896] | 206 | return true; |
|---|
| 207 | } |
|---|
| [6986] | 208 | |
|---|
| 209 | printf("none\n"); |
|---|
| 210 | return false; |
|---|
| [5896] | 211 | } |
|---|
| 212 | |
|---|
| [6986] | 213 | |
|---|
| [6547] | 214 | bool Playable::pickup(PowerUp* powerUp) |
|---|
| 215 | { |
|---|
| 216 | if(powerUp->isA(CL_WEAPON_POWER_UP)) { |
|---|
| [6973] | 217 | return dynamic_cast<WeaponPowerUp*>(powerUp)->process(this->getWeaponManager()); |
|---|
| [6547] | 218 | } |
|---|
| 219 | else if(powerUp->isA(CL_PARAM_POWER_UP)) { |
|---|
| 220 | ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp); |
|---|
| 221 | switch(ppu->getType()) { |
|---|
| 222 | case POWERUP_PARAM_HEALTH: |
|---|
| [6700] | 223 | this->increaseHealth(ppu->getValue()); |
|---|
| [6547] | 224 | return true; |
|---|
| 225 | case POWERUP_PARAM_MAX_HEALTH: |
|---|
| [6700] | 226 | this->increaseHealthMax(ppu->getValue()); |
|---|
| [6547] | 227 | return true; |
|---|
| 228 | } |
|---|
| 229 | } |
|---|
| 230 | return false; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| [5896] | 233 | /** |
|---|
| [5898] | 234 | * add an event to the event list of events this Playable can capture |
|---|
| 235 | * @param eventType the Type of event to add |
|---|
| [5889] | 236 | */ |
|---|
| [5896] | 237 | void Playable::registerEvent(int eventType) |
|---|
| [5889] | 238 | { |
|---|
| 239 | this->events.push_back(eventType); |
|---|
| 240 | |
|---|
| [5896] | 241 | if (this->currentPlayer != NULL) |
|---|
| 242 | EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType); |
|---|
| [5889] | 243 | } |
|---|
| 244 | |
|---|
| [5896] | 245 | /** |
|---|
| [5898] | 246 | * remove an event to the event list this Playable can capture. |
|---|
| 247 | * @param event the event to unregister. |
|---|
| [5896] | 248 | */ |
|---|
| 249 | void Playable::unregisterEvent(int eventType) |
|---|
| 250 | { |
|---|
| [5902] | 251 | this->events.remove(eventType); |
|---|
| [5889] | 252 | |
|---|
| [5896] | 253 | if (this->currentPlayer != NULL) |
|---|
| 254 | EventHandler::getInstance()->unsubscribe(ES_GAME, eventType); |
|---|
| 255 | } |
|---|
| [5889] | 256 | |
|---|
| [6804] | 257 | /** |
|---|
| 258 | * @brief ticks a Playable |
|---|
| 259 | * @param dt: the passed time since the last Tick |
|---|
| 260 | */ |
|---|
| 261 | void Playable::tick(float dt) |
|---|
| 262 | { |
|---|
| 263 | this->weaponMan->tick(dt); |
|---|
| 264 | if (this->bFire) |
|---|
| 265 | weaponMan->fire(); |
|---|
| 266 | } |
|---|
| [5896] | 267 | |
|---|
| [6804] | 268 | |
|---|
| 269 | /** |
|---|
| 270 | * @brief processes Playable events. |
|---|
| 271 | * @param event the Captured Event. |
|---|
| 272 | */ |
|---|
| 273 | void Playable::process(const Event &event) |
|---|
| 274 | { |
|---|
| 275 | if( event.type == KeyMapper::PEV_FIRE1) |
|---|
| 276 | this->bFire = event.bPressed; |
|---|
| 277 | else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed) |
|---|
| 278 | { |
|---|
| 279 | this->nextWeaponConfig(); |
|---|
| 280 | } |
|---|
| 281 | else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed) |
|---|
| 282 | this->previousWeaponConfig(); |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| [6241] | 287 | void Playable::attachCamera() |
|---|
| 288 | { |
|---|
| [6444] | 289 | State::getCamera()->setParentSoft(this); |
|---|
| 290 | State::getCameraTarget()->setParentSoft(this); |
|---|
| [6241] | 291 | |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | |
|---|
| [6804] | 295 | |
|---|
| 296 | |
|---|
| [6241] | 297 | void Playable::detachCamera() |
|---|
| 298 | { |
|---|
| 299 | } |
|---|
| [6868] | 300 | |
|---|
| [6959] | 301 | #define DATA_FLAGS 1 |
|---|
| 302 | #define DATA_SCORE 2 |
|---|
| 303 | |
|---|
| [6868] | 304 | #define FLAGS_bFire 1 |
|---|
| 305 | |
|---|
| 306 | int Playable::writeSync( const byte * data, int length, int sender ) |
|---|
| 307 | { |
|---|
| 308 | SYNCHELP_READ_BEGIN(); |
|---|
| [6871] | 309 | |
|---|
| [6868] | 310 | byte flags; |
|---|
| [6871] | 311 | |
|---|
| [6868] | 312 | SYNCHELP_READ_BYTE( flags, NWT_PL_FLAGS ); |
|---|
| [6871] | 313 | |
|---|
| [6868] | 314 | bFire = (flags & FLAGS_bFire) != 0; |
|---|
| [6871] | 315 | |
|---|
| [6868] | 316 | return SYNCHELP_READ_N; |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | int Playable::readSync( byte * data, int maxLength ) |
|---|
| 320 | { |
|---|
| 321 | SYNCHELP_WRITE_BEGIN(); |
|---|
| 322 | byte flags = 0; |
|---|
| [6871] | 323 | |
|---|
| [6868] | 324 | if ( bFire ) |
|---|
| 325 | flags |= FLAGS_bFire; |
|---|
| 326 | |
|---|
| [6871] | 327 | |
|---|
| [6868] | 328 | SYNCHELP_WRITE_BYTE( flags, NWT_PL_FLAGS ); |
|---|
| 329 | oldFlags = flags; |
|---|
| 330 | |
|---|
| [6871] | 331 | |
|---|
| [6868] | 332 | return SYNCHELP_WRITE_N; |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | bool Playable::needsReadSync( ) |
|---|
| 336 | { |
|---|
| [6959] | 337 | //if ( score != oldScore ) |
|---|
| 338 | // return true; |
|---|
| 339 | |
|---|
| [6868] | 340 | byte flags = 0; |
|---|
| [6871] | 341 | |
|---|
| [6868] | 342 | if ( bFire ) |
|---|
| 343 | flags |= FLAGS_bFire; |
|---|
| [6871] | 344 | |
|---|
| [6868] | 345 | return flags!=oldFlags; |
|---|
| 346 | } |
|---|