[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 | |
---|
| 77 | if (this->currentPlayer) |
---|
| 78 | { |
---|
| 79 | PRINTF(2)("There is Still a Player subscribed to this Playable (%s::%s)\n", this->getClassName(), this->getName()); |
---|
| 80 | |
---|
| 81 | } |
---|
[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 | { |
---|
[6959] | 127 | if (entity->isA(CL_PROJECTILE) && !State::isOnline() ) |
---|
[6966] | 128 | { |
---|
[6700] | 129 | this->decreaseHealth(entity->getHealth()); |
---|
[6966] | 130 | // EXTREME HACK |
---|
| 131 | if (this->getHealth() == 0.0f) |
---|
| 132 | { |
---|
| 133 | this->die(); |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | } |
---|
[6436] | 137 | |
---|
[6966] | 138 | |
---|
| 139 | void Playable::die() |
---|
| 140 | { |
---|
[6959] | 141 | //this->deactivateNode(); |
---|
[6966] | 142 | this->toList(OM_DEAD); |
---|
| 143 | this->emitter->setSystem(explosionParticles); |
---|
| 144 | this->setAbsCoor(0, 0, 0); |
---|
[6959] | 145 | //this->setAbsDir(Vector(1,0,0), 0); |
---|
[6966] | 146 | this->emitter->setSystem(NULL); |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | if( this->getOwner()%2 == 0) |
---|
| 150 | this->toList(OM_GROUP_00); |
---|
| 151 | else |
---|
| 152 | this->toList(OM_GROUP_01); |
---|
[6436] | 153 | } |
---|
| 154 | |
---|
[6966] | 155 | |
---|
[6436] | 156 | /** |
---|
[5872] | 157 | * subscribe to all events the controllable needs |
---|
[5898] | 158 | * @param player the player that shall controll this Playable |
---|
[5872] | 159 | */ |
---|
[5895] | 160 | bool Playable::subscribePlayer(Player* player) |
---|
[5872] | 161 | { |
---|
[5895] | 162 | if (this->currentPlayer != NULL) |
---|
[5872] | 163 | { |
---|
[5895] | 164 | PRINTF(1)("Already registered Player:%s to this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName()); |
---|
| 165 | return false; |
---|
[5875] | 166 | } |
---|
[5895] | 167 | else |
---|
| 168 | { |
---|
| 169 | this->currentPlayer = player; |
---|
| 170 | /*EventHandler*/ |
---|
| 171 | EventHandler* evh = EventHandler::getInstance(); |
---|
| 172 | std::list<int>::iterator ev; |
---|
| 173 | for (ev = this->events.begin(); ev != events.end(); ev++) |
---|
| 174 | evh->subscribe(player, ES_GAME, (*ev)); |
---|
[6241] | 175 | this->enter(); |
---|
[5895] | 176 | return true; |
---|
| 177 | } |
---|
[5872] | 178 | } |
---|
[5889] | 179 | |
---|
| 180 | /** |
---|
[5898] | 181 | * unsubscribe from all events the controllable needs |
---|
| 182 | * @param player the Player, that controlled this Ship. |
---|
[5896] | 183 | */ |
---|
| 184 | bool Playable::unsubscribePlayer(Player* player) |
---|
| 185 | { |
---|
| 186 | if (this->currentPlayer != player) |
---|
| 187 | { |
---|
| 188 | PRINTF(1)("not the right Player to unregister from this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName()); |
---|
| 189 | return false; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | else |
---|
| 193 | { |
---|
| 194 | /*EventHandler*/ |
---|
| 195 | EventHandler* evh = EventHandler::getInstance(); |
---|
| 196 | std::list<int>::iterator ev; |
---|
| 197 | for (ev = this->events.begin(); ev != events.end(); ev++) |
---|
| 198 | evh->unsubscribe( ES_GAME, (*ev)); |
---|
| 199 | |
---|
[6241] | 200 | this->leave(); |
---|
[5896] | 201 | this->currentPlayer = NULL; |
---|
| 202 | return true; |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | |
---|
[6547] | 206 | bool Playable::pickup(PowerUp* powerUp) |
---|
| 207 | { |
---|
| 208 | if(powerUp->isA(CL_WEAPON_POWER_UP)) { |
---|
[6973] | 209 | return dynamic_cast<WeaponPowerUp*>(powerUp)->process(this->getWeaponManager()); |
---|
[6547] | 210 | } |
---|
| 211 | else if(powerUp->isA(CL_PARAM_POWER_UP)) { |
---|
| 212 | ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp); |
---|
| 213 | switch(ppu->getType()) { |
---|
| 214 | case POWERUP_PARAM_HEALTH: |
---|
[6700] | 215 | this->increaseHealth(ppu->getValue()); |
---|
[6547] | 216 | return true; |
---|
| 217 | case POWERUP_PARAM_MAX_HEALTH: |
---|
[6700] | 218 | this->increaseHealthMax(ppu->getValue()); |
---|
[6547] | 219 | return true; |
---|
| 220 | } |
---|
| 221 | } |
---|
| 222 | return false; |
---|
| 223 | } |
---|
| 224 | |
---|
[5896] | 225 | /** |
---|
[5898] | 226 | * add an event to the event list of events this Playable can capture |
---|
| 227 | * @param eventType the Type of event to add |
---|
[5889] | 228 | */ |
---|
[5896] | 229 | void Playable::registerEvent(int eventType) |
---|
[5889] | 230 | { |
---|
| 231 | this->events.push_back(eventType); |
---|
| 232 | |
---|
[5896] | 233 | if (this->currentPlayer != NULL) |
---|
| 234 | EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType); |
---|
[5889] | 235 | } |
---|
| 236 | |
---|
[5896] | 237 | /** |
---|
[5898] | 238 | * remove an event to the event list this Playable can capture. |
---|
| 239 | * @param event the event to unregister. |
---|
[5896] | 240 | */ |
---|
| 241 | void Playable::unregisterEvent(int eventType) |
---|
| 242 | { |
---|
[5902] | 243 | this->events.remove(eventType); |
---|
[5889] | 244 | |
---|
[5896] | 245 | if (this->currentPlayer != NULL) |
---|
| 246 | EventHandler::getInstance()->unsubscribe(ES_GAME, eventType); |
---|
| 247 | } |
---|
[5889] | 248 | |
---|
[6804] | 249 | /** |
---|
| 250 | * @brief ticks a Playable |
---|
| 251 | * @param dt: the passed time since the last Tick |
---|
| 252 | */ |
---|
| 253 | void Playable::tick(float dt) |
---|
| 254 | { |
---|
| 255 | this->weaponMan->tick(dt); |
---|
| 256 | if (this->bFire) |
---|
| 257 | weaponMan->fire(); |
---|
| 258 | } |
---|
[5896] | 259 | |
---|
[6804] | 260 | |
---|
| 261 | /** |
---|
| 262 | * @brief processes Playable events. |
---|
| 263 | * @param event the Captured Event. |
---|
| 264 | */ |
---|
| 265 | void Playable::process(const Event &event) |
---|
| 266 | { |
---|
| 267 | if( event.type == KeyMapper::PEV_FIRE1) |
---|
| 268 | this->bFire = event.bPressed; |
---|
| 269 | else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed) |
---|
| 270 | { |
---|
| 271 | this->nextWeaponConfig(); |
---|
| 272 | } |
---|
| 273 | else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed) |
---|
| 274 | this->previousWeaponConfig(); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | |
---|
| 278 | |
---|
[6241] | 279 | void Playable::attachCamera() |
---|
| 280 | { |
---|
[6444] | 281 | State::getCamera()->setParentSoft(this); |
---|
| 282 | State::getCameraTarget()->setParentSoft(this); |
---|
[6241] | 283 | |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | |
---|
[6804] | 287 | |
---|
| 288 | |
---|
[6241] | 289 | void Playable::detachCamera() |
---|
| 290 | { |
---|
| 291 | } |
---|
[6868] | 292 | |
---|
[6959] | 293 | #define DATA_FLAGS 1 |
---|
| 294 | #define DATA_SCORE 2 |
---|
| 295 | |
---|
[6868] | 296 | #define FLAGS_bFire 1 |
---|
| 297 | |
---|
| 298 | int Playable::writeSync( const byte * data, int length, int sender ) |
---|
| 299 | { |
---|
| 300 | SYNCHELP_READ_BEGIN(); |
---|
[6871] | 301 | |
---|
[6868] | 302 | byte flags; |
---|
[6871] | 303 | |
---|
[6868] | 304 | SYNCHELP_READ_BYTE( flags, NWT_PL_FLAGS ); |
---|
[6871] | 305 | |
---|
[6868] | 306 | bFire = (flags & FLAGS_bFire) != 0; |
---|
[6871] | 307 | |
---|
[6868] | 308 | return SYNCHELP_READ_N; |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | int Playable::readSync( byte * data, int maxLength ) |
---|
| 312 | { |
---|
| 313 | SYNCHELP_WRITE_BEGIN(); |
---|
| 314 | byte flags = 0; |
---|
[6871] | 315 | |
---|
[6868] | 316 | if ( bFire ) |
---|
| 317 | flags |= FLAGS_bFire; |
---|
| 318 | |
---|
[6871] | 319 | |
---|
[6868] | 320 | SYNCHELP_WRITE_BYTE( flags, NWT_PL_FLAGS ); |
---|
| 321 | oldFlags = flags; |
---|
| 322 | |
---|
[6871] | 323 | |
---|
[6868] | 324 | return SYNCHELP_WRITE_N; |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | bool Playable::needsReadSync( ) |
---|
| 328 | { |
---|
[6959] | 329 | //if ( score != oldScore ) |
---|
| 330 | // return true; |
---|
| 331 | |
---|
[6868] | 332 | byte flags = 0; |
---|
[6871] | 333 | |
---|
[6868] | 334 | if ( bFire ) |
---|
| 335 | flags |= FLAGS_bFire; |
---|
[6871] | 336 | |
---|
[6868] | 337 | return flags!=oldFlags; |
---|
| 338 | } |
---|