[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 | |
---|
[6547] | 24 | #include "power_ups/weapon_power_up.h" |
---|
| 25 | #include "power_ups/param_power_up.h" |
---|
[5872] | 26 | |
---|
[6547] | 27 | |
---|
[5838] | 28 | Playable::Playable() |
---|
| 29 | { |
---|
[6442] | 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; |
---|
[5838] | 38 | } |
---|
| 39 | |
---|
[5875] | 40 | Playable::~Playable() |
---|
[5838] | 41 | { |
---|
[5881] | 42 | delete this->weaponMan; |
---|
[5895] | 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 | } |
---|
[5875] | 49 | } |
---|
| 50 | |
---|
[5898] | 51 | |
---|
[6443] | 52 | void Playable::addWeapon(Weapon* weapon, int configID, int slotID) |
---|
| 53 | { |
---|
[6676] | 54 | this->weaponMan->addWeapon(weapon, configID, slotID); |
---|
[6443] | 55 | |
---|
[6578] | 56 | this->weaponConfigChanged(); |
---|
[6443] | 57 | } |
---|
| 58 | |
---|
| 59 | void Playable::removeWeapon(Weapon* weapon) |
---|
| 60 | { |
---|
| 61 | this->weaponMan->removeWeapon(weapon); |
---|
| 62 | |
---|
[6568] | 63 | this->weaponConfigChanged(); |
---|
[6443] | 64 | } |
---|
| 65 | |
---|
[6444] | 66 | void Playable::nextWeaponConfig() |
---|
| 67 | { |
---|
| 68 | this->weaponMan->nextWeaponConfig(); |
---|
[6568] | 69 | this->weaponConfigChanged(); |
---|
[6444] | 70 | } |
---|
[6443] | 71 | |
---|
[6444] | 72 | void Playable::previousWeaponConfig() |
---|
| 73 | { |
---|
| 74 | this->weaponMan->previousWeaponConfig(); |
---|
[6568] | 75 | this->weaponConfigChanged(); |
---|
[6444] | 76 | } |
---|
| 77 | |
---|
[6568] | 78 | void Playable::weaponConfigChanged() |
---|
| 79 | { |
---|
[6578] | 80 | if (this->currentPlayer != NULL) |
---|
| 81 | this->currentPlayer->weaponConfigChanged(); |
---|
[6568] | 82 | } |
---|
[6444] | 83 | |
---|
[5872] | 84 | /** |
---|
[6436] | 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 | /** |
---|
[5872] | 98 | * subscribe to all events the controllable needs |
---|
[5898] | 99 | * @param player the player that shall controll this Playable |
---|
[5872] | 100 | */ |
---|
[5895] | 101 | bool Playable::subscribePlayer(Player* player) |
---|
[5872] | 102 | { |
---|
[5895] | 103 | if (this->currentPlayer != NULL) |
---|
[5872] | 104 | { |
---|
[5895] | 105 | PRINTF(1)("Already registered Player:%s to this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName()); |
---|
| 106 | return false; |
---|
[5875] | 107 | } |
---|
[5895] | 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)); |
---|
[6241] | 116 | this->enter(); |
---|
[5895] | 117 | return true; |
---|
| 118 | } |
---|
[5872] | 119 | } |
---|
[5889] | 120 | |
---|
| 121 | /** |
---|
[5898] | 122 | * unsubscribe from all events the controllable needs |
---|
| 123 | * @param player the Player, that controlled this Ship. |
---|
[5896] | 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 | |
---|
[6241] | 141 | this->leave(); |
---|
[5896] | 142 | this->currentPlayer = NULL; |
---|
| 143 | return true; |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | |
---|
[6547] | 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(); |
---|
[6589] | 152 | manager->addWeapon(weapon, 2, -1); |
---|
| 153 | return true; |
---|
[6547] | 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 | |
---|
[5896] | 169 | /** |
---|
[5898] | 170 | * add an event to the event list of events this Playable can capture |
---|
| 171 | * @param eventType the Type of event to add |
---|
[5889] | 172 | */ |
---|
[5896] | 173 | void Playable::registerEvent(int eventType) |
---|
[5889] | 174 | { |
---|
| 175 | this->events.push_back(eventType); |
---|
| 176 | |
---|
[5896] | 177 | if (this->currentPlayer != NULL) |
---|
| 178 | EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType); |
---|
[5889] | 179 | } |
---|
| 180 | |
---|
[5896] | 181 | /** |
---|
[5898] | 182 | * remove an event to the event list this Playable can capture. |
---|
| 183 | * @param event the event to unregister. |
---|
[5896] | 184 | */ |
---|
| 185 | void Playable::unregisterEvent(int eventType) |
---|
| 186 | { |
---|
[5902] | 187 | this->events.remove(eventType); |
---|
[5889] | 188 | |
---|
[5896] | 189 | if (this->currentPlayer != NULL) |
---|
| 190 | EventHandler::getInstance()->unsubscribe(ES_GAME, eventType); |
---|
| 191 | } |
---|
[5889] | 192 | |
---|
[5896] | 193 | |
---|
[6241] | 194 | void Playable::attachCamera() |
---|
| 195 | { |
---|
[6444] | 196 | State::getCamera()->setParentSoft(this); |
---|
| 197 | State::getCameraTarget()->setParentSoft(this); |
---|
[6241] | 198 | |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | |
---|
| 202 | void Playable::detachCamera() |
---|
| 203 | { |
---|
| 204 | } |
---|