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