[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 | |
---|
[5838] | 30 | Playable::Playable() |
---|
| 31 | { |
---|
[6442] | 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; |
---|
[6695] | 40 | |
---|
| 41 | this->setSynchronized(true); |
---|
[5838] | 42 | } |
---|
| 43 | |
---|
[6695] | 44 | |
---|
| 45 | |
---|
[5875] | 46 | Playable::~Playable() |
---|
[5838] | 47 | { |
---|
[5881] | 48 | delete this->weaponMan; |
---|
[5895] | 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 | } |
---|
[5875] | 55 | } |
---|
| 56 | |
---|
[5898] | 57 | |
---|
[6443] | 58 | void Playable::addWeapon(Weapon* weapon, int configID, int slotID) |
---|
| 59 | { |
---|
[6676] | 60 | this->weaponMan->addWeapon(weapon, configID, slotID); |
---|
[6443] | 61 | |
---|
[6578] | 62 | this->weaponConfigChanged(); |
---|
[6443] | 63 | } |
---|
| 64 | |
---|
[6695] | 65 | |
---|
[6443] | 66 | void Playable::removeWeapon(Weapon* weapon) |
---|
| 67 | { |
---|
| 68 | this->weaponMan->removeWeapon(weapon); |
---|
| 69 | |
---|
[6568] | 70 | this->weaponConfigChanged(); |
---|
[6443] | 71 | } |
---|
| 72 | |
---|
[6695] | 73 | |
---|
[6444] | 74 | void Playable::nextWeaponConfig() |
---|
| 75 | { |
---|
| 76 | this->weaponMan->nextWeaponConfig(); |
---|
[6568] | 77 | this->weaponConfigChanged(); |
---|
[6444] | 78 | } |
---|
[6443] | 79 | |
---|
[6695] | 80 | |
---|
[6444] | 81 | void Playable::previousWeaponConfig() |
---|
| 82 | { |
---|
| 83 | this->weaponMan->previousWeaponConfig(); |
---|
[6568] | 84 | this->weaponConfigChanged(); |
---|
[6444] | 85 | } |
---|
| 86 | |
---|
[6695] | 87 | |
---|
[6568] | 88 | void Playable::weaponConfigChanged() |
---|
| 89 | { |
---|
[6578] | 90 | if (this->currentPlayer != NULL) |
---|
| 91 | this->currentPlayer->weaponConfigChanged(); |
---|
[6568] | 92 | } |
---|
[6444] | 93 | |
---|
[6695] | 94 | |
---|
[5872] | 95 | /** |
---|
[6436] | 96 | * @brief helps us colliding Playables |
---|
| 97 | */ |
---|
| 98 | void Playable::collidesWith(WorldEntity* entity, const Vector& location) |
---|
| 99 | { |
---|
| 100 | if (entity->isA(CL_PROJECTILE)) |
---|
[6700] | 101 | this->decreaseHealth(entity->getHealth()); |
---|
[6436] | 102 | |
---|
| 103 | // EXTREME HACK |
---|
[6700] | 104 | if (this->getHealth() == 0.0f) |
---|
[6436] | 105 | this->deactivateNode(); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | /** |
---|
[5872] | 109 | * subscribe to all events the controllable needs |
---|
[5898] | 110 | * @param player the player that shall controll this Playable |
---|
[5872] | 111 | */ |
---|
[5895] | 112 | bool Playable::subscribePlayer(Player* player) |
---|
[5872] | 113 | { |
---|
[5895] | 114 | if (this->currentPlayer != NULL) |
---|
[5872] | 115 | { |
---|
[5895] | 116 | PRINTF(1)("Already registered Player:%s to this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName()); |
---|
| 117 | return false; |
---|
[5875] | 118 | } |
---|
[5895] | 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)); |
---|
[6241] | 127 | this->enter(); |
---|
[5895] | 128 | return true; |
---|
| 129 | } |
---|
[5872] | 130 | } |
---|
[5889] | 131 | |
---|
| 132 | /** |
---|
[5898] | 133 | * unsubscribe from all events the controllable needs |
---|
| 134 | * @param player the Player, that controlled this Ship. |
---|
[5896] | 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 | |
---|
[6241] | 152 | this->leave(); |
---|
[5896] | 153 | this->currentPlayer = NULL; |
---|
| 154 | return true; |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | |
---|
[6547] | 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(); |
---|
[6589] | 163 | manager->addWeapon(weapon, 2, -1); |
---|
| 164 | return true; |
---|
[6547] | 165 | } |
---|
| 166 | else if(powerUp->isA(CL_PARAM_POWER_UP)) { |
---|
| 167 | ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp); |
---|
| 168 | switch(ppu->getType()) { |
---|
| 169 | case POWERUP_PARAM_HEALTH: |
---|
[6700] | 170 | this->increaseHealth(ppu->getValue()); |
---|
[6547] | 171 | return true; |
---|
| 172 | case POWERUP_PARAM_MAX_HEALTH: |
---|
[6700] | 173 | this->increaseHealthMax(ppu->getValue()); |
---|
[6547] | 174 | return true; |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | return false; |
---|
| 178 | } |
---|
| 179 | |
---|
[5896] | 180 | /** |
---|
[5898] | 181 | * add an event to the event list of events this Playable can capture |
---|
| 182 | * @param eventType the Type of event to add |
---|
[5889] | 183 | */ |
---|
[5896] | 184 | void Playable::registerEvent(int eventType) |
---|
[5889] | 185 | { |
---|
| 186 | this->events.push_back(eventType); |
---|
| 187 | |
---|
[5896] | 188 | if (this->currentPlayer != NULL) |
---|
| 189 | EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType); |
---|
[5889] | 190 | } |
---|
| 191 | |
---|
[5896] | 192 | /** |
---|
[5898] | 193 | * remove an event to the event list this Playable can capture. |
---|
| 194 | * @param event the event to unregister. |
---|
[5896] | 195 | */ |
---|
| 196 | void Playable::unregisterEvent(int eventType) |
---|
| 197 | { |
---|
[5902] | 198 | this->events.remove(eventType); |
---|
[5889] | 199 | |
---|
[5896] | 200 | if (this->currentPlayer != NULL) |
---|
| 201 | EventHandler::getInstance()->unsubscribe(ES_GAME, eventType); |
---|
| 202 | } |
---|
[5889] | 203 | |
---|
[5896] | 204 | |
---|
[6241] | 205 | void Playable::attachCamera() |
---|
| 206 | { |
---|
[6444] | 207 | State::getCamera()->setParentSoft(this); |
---|
| 208 | State::getCameraTarget()->setParentSoft(this); |
---|
[6241] | 209 | |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | |
---|
| 213 | void Playable::detachCamera() |
---|
| 214 | { |
---|
| 215 | } |
---|