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