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