| 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 |  | 
|---|
| 30 | Playable::Playable() | 
|---|
| 31 | { | 
|---|
| 32 |   this->setClassID(CL_PLAYABLE, "Playable"); | 
|---|
| 33 |   PRINTF(4)("PLAYABLE INIT\n"); | 
|---|
| 34 |  | 
|---|
| 35 |   this->toList(OM_GROUP_01); | 
|---|
| 36 |   this->weaponMan = new WeaponManager(this); | 
|---|
| 37 |  | 
|---|
| 38 |   // the reference to the Current Player is NULL, because we dont have one at the beginning. | 
|---|
| 39 |   this->currentPlayer = NULL; | 
|---|
| 40 |  | 
|---|
| 41 |   this->setSynchronized(true); | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 |  | 
|---|
| 45 |  | 
|---|
| 46 | Playable::~Playable() | 
|---|
| 47 | { | 
|---|
| 48 |   delete this->weaponMan; | 
|---|
| 49 |  | 
|---|
| 50 |   if (this->currentPlayer) | 
|---|
| 51 |   { | 
|---|
| 52 |     PRINTF(2)("There is Still a Player subscribed to this Playable (%s::%s)\n", this->getClassName(), this->getName()); | 
|---|
| 53 |  | 
|---|
| 54 |   } | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 |  | 
|---|
| 58 | void Playable::addWeapon(Weapon* weapon, int configID, int slotID) | 
|---|
| 59 | { | 
|---|
| 60 |   this->weaponMan->addWeapon(weapon, configID, slotID); | 
|---|
| 61 |  | 
|---|
| 62 |   this->weaponConfigChanged(); | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 |  | 
|---|
| 66 | void Playable::removeWeapon(Weapon* weapon) | 
|---|
| 67 | { | 
|---|
| 68 |   this->weaponMan->removeWeapon(weapon); | 
|---|
| 69 |  | 
|---|
| 70 |     this->weaponConfigChanged(); | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 |  | 
|---|
| 74 | void Playable::nextWeaponConfig() | 
|---|
| 75 | { | 
|---|
| 76 |   this->weaponMan->nextWeaponConfig(); | 
|---|
| 77 |     this->weaponConfigChanged(); | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 |  | 
|---|
| 81 | void Playable::previousWeaponConfig() | 
|---|
| 82 | { | 
|---|
| 83 |   this->weaponMan->previousWeaponConfig(); | 
|---|
| 84 |     this->weaponConfigChanged(); | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 |  | 
|---|
| 88 | void Playable::weaponConfigChanged() | 
|---|
| 89 | { | 
|---|
| 90 |   if (this->currentPlayer != NULL) | 
|---|
| 91 |     this->currentPlayer->weaponConfigChanged(); | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 |  | 
|---|
| 95 | /** | 
|---|
| 96 |  * @brief helps us colliding Playables | 
|---|
| 97 |  */ | 
|---|
| 98 | void Playable::collidesWith(WorldEntity* entity, const Vector& location) | 
|---|
| 99 | { | 
|---|
| 100 |   if (entity->isA(CL_PROJECTILE)) | 
|---|
| 101 |     this->decreaseHealth(entity->getHealth()); | 
|---|
| 102 |  | 
|---|
| 103 |   // EXTREME HACK | 
|---|
| 104 |   if (this->getHealth() == 0.0f) | 
|---|
| 105 |     this->deactivateNode(); | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | /** | 
|---|
| 109 |  * subscribe to all events the controllable needs | 
|---|
| 110 |  * @param player the player that shall controll this Playable | 
|---|
| 111 |  */ | 
|---|
| 112 | bool Playable::subscribePlayer(Player* player) | 
|---|
| 113 | { | 
|---|
| 114 |   if (this->currentPlayer != NULL) | 
|---|
| 115 |   { | 
|---|
| 116 |     PRINTF(1)("Already registered Player:%s to this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName()); | 
|---|
| 117 |     return false; | 
|---|
| 118 |   } | 
|---|
| 119 |   else | 
|---|
| 120 |   { | 
|---|
| 121 |     this->currentPlayer = player; | 
|---|
| 122 |     /*EventHandler*/ | 
|---|
| 123 |     EventHandler* evh = EventHandler::getInstance(); | 
|---|
| 124 |     std::list<int>::iterator ev; | 
|---|
| 125 |     for (ev = this->events.begin(); ev != events.end(); ev++) | 
|---|
| 126 |       evh->subscribe(player, ES_GAME, (*ev)); | 
|---|
| 127 |     this->enter(); | 
|---|
| 128 |     return true; | 
|---|
| 129 |   } | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | /** | 
|---|
| 133 |  * unsubscribe from all events the controllable needs | 
|---|
| 134 |  * @param player the Player, that controlled this Ship. | 
|---|
| 135 |  */ | 
|---|
| 136 | bool Playable::unsubscribePlayer(Player* player) | 
|---|
| 137 | { | 
|---|
| 138 |   if (this->currentPlayer != player) | 
|---|
| 139 |   { | 
|---|
| 140 |     PRINTF(1)("not the right Player to unregister from this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName()); | 
|---|
| 141 |     return false; | 
|---|
| 142 |   } | 
|---|
| 143 |  | 
|---|
| 144 |   else | 
|---|
| 145 |   { | 
|---|
| 146 |     /*EventHandler*/ | 
|---|
| 147 |     EventHandler* evh = EventHandler::getInstance(); | 
|---|
| 148 |     std::list<int>::iterator ev; | 
|---|
| 149 |     for (ev = this->events.begin(); ev != events.end(); ev++) | 
|---|
| 150 |       evh->unsubscribe( ES_GAME, (*ev)); | 
|---|
| 151 |  | 
|---|
| 152 |     this->leave(); | 
|---|
| 153 |     this->currentPlayer = NULL; | 
|---|
| 154 |     return true; | 
|---|
| 155 |   } | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | bool Playable::pickup(PowerUp* powerUp) | 
|---|
| 159 | { | 
|---|
| 160 |   if(powerUp->isA(CL_WEAPON_POWER_UP)) { | 
|---|
| 161 |     Weapon* weapon = dynamic_cast<WeaponPowerUp*>(powerUp)->getWeapon(); | 
|---|
| 162 |     WeaponManager* manager = this->getWeaponManager(); | 
|---|
| 163 |     return manager->addWeapon(weapon); | 
|---|
| 164 |   } | 
|---|
| 165 |   else if(powerUp->isA(CL_PARAM_POWER_UP)) { | 
|---|
| 166 |     ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp); | 
|---|
| 167 |     switch(ppu->getType()) { | 
|---|
| 168 |       case POWERUP_PARAM_HEALTH: | 
|---|
| 169 |         this->increaseHealth(ppu->getValue()); | 
|---|
| 170 |         return true; | 
|---|
| 171 |       case POWERUP_PARAM_MAX_HEALTH: | 
|---|
| 172 |         this->increaseHealthMax(ppu->getValue()); | 
|---|
| 173 |         return true; | 
|---|
| 174 |     } | 
|---|
| 175 |   } | 
|---|
| 176 |   return false; | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | /** | 
|---|
| 180 |  * add an event to the event list of events this Playable can capture | 
|---|
| 181 |  * @param eventType the Type of event to add | 
|---|
| 182 |  */ | 
|---|
| 183 | void Playable::registerEvent(int eventType) | 
|---|
| 184 | { | 
|---|
| 185 |   this->events.push_back(eventType); | 
|---|
| 186 |  | 
|---|
| 187 |   if (this->currentPlayer != NULL) | 
|---|
| 188 |     EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType); | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 | /** | 
|---|
| 192 |  * remove an event to the event list this Playable can capture. | 
|---|
| 193 |  * @param event the event to unregister. | 
|---|
| 194 |  */ | 
|---|
| 195 | void Playable::unregisterEvent(int eventType) | 
|---|
| 196 | { | 
|---|
| 197 |   this->events.remove(eventType); | 
|---|
| 198 |  | 
|---|
| 199 |   if (this->currentPlayer != NULL) | 
|---|
| 200 |     EventHandler::getInstance()->unsubscribe(ES_GAME, eventType); | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 |  | 
|---|
| 204 | void  Playable::attachCamera() | 
|---|
| 205 | { | 
|---|
| 206 |   State::getCamera()->setParentSoft(this); | 
|---|
| 207 |   State::getCameraTarget()->setParentSoft(this); | 
|---|
| 208 |  | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 |  | 
|---|
| 212 | void  Playable::detachCamera() | 
|---|
| 213 | { | 
|---|
| 214 | } | 
|---|