| 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 |  | 
|---|
| 23 |  | 
|---|
| 24 | Playable::Playable() | 
|---|
| 25 | { | 
|---|
| 26 |   this->init(); | 
|---|
| 27 | } | 
|---|
| 28 |  | 
|---|
| 29 | Playable::~Playable() | 
|---|
| 30 | { | 
|---|
| 31 |   delete this->weaponMan; | 
|---|
| 32 |  | 
|---|
| 33 |   if (this->currentPlayer) | 
|---|
| 34 |   { | 
|---|
| 35 |     PRINTF(2)("There is Still a Player subscribed to this Playable (%s::%s)\n", this->getClassName(), this->getName()); | 
|---|
| 36 |  | 
|---|
| 37 |   } | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 40 | /** | 
|---|
| 41 |  * initializes this Playable | 
|---|
| 42 |  */ | 
|---|
| 43 | void Playable::init() | 
|---|
| 44 | { | 
|---|
| 45 |   this->setClassID(CL_PLAYABLE, "Playable"); | 
|---|
| 46 |   PRINTF(4)("PLAYABLE INIT\n"); | 
|---|
| 47 |  | 
|---|
| 48 |   this->weaponMan = new WeaponManager(this); | 
|---|
| 49 |  | 
|---|
| 50 |   // the reference to the Current Player is NULL, because we dont have one at the beginning. | 
|---|
| 51 |   this->currentPlayer = NULL; | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | /** | 
|---|
| 55 |  * subscribe to all events the controllable needs | 
|---|
| 56 |  * @param player the player that shall controll this Playable | 
|---|
| 57 |  */ | 
|---|
| 58 | bool Playable::subscribePlayer(Player* player) | 
|---|
| 59 | { | 
|---|
| 60 |   if (this->currentPlayer != NULL) | 
|---|
| 61 |   { | 
|---|
| 62 |     PRINTF(1)("Already registered Player:%s to this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName()); | 
|---|
| 63 |     return false; | 
|---|
| 64 |   } | 
|---|
| 65 |   else | 
|---|
| 66 |   { | 
|---|
| 67 |     this->currentPlayer = player; | 
|---|
| 68 |     /*EventHandler*/ | 
|---|
| 69 |     EventHandler* evh = EventHandler::getInstance(); | 
|---|
| 70 |     std::list<int>::iterator ev; | 
|---|
| 71 |     for (ev = this->events.begin(); ev != events.end(); ev++) | 
|---|
| 72 |       evh->subscribe(player, ES_GAME, (*ev)); | 
|---|
| 73 |  | 
|---|
| 74 |     return true; | 
|---|
| 75 |   } | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | /** | 
|---|
| 79 |  * unsubscribe from all events the controllable needs | 
|---|
| 80 |  * @param player the Player, that controlled this Ship. | 
|---|
| 81 |  */ | 
|---|
| 82 | bool Playable::unsubscribePlayer(Player* player) | 
|---|
| 83 | { | 
|---|
| 84 |   if (this->currentPlayer != player) | 
|---|
| 85 |   { | 
|---|
| 86 |     PRINTF(1)("not the right Player to unregister from this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName()); | 
|---|
| 87 |     return false; | 
|---|
| 88 |   } | 
|---|
| 89 |  | 
|---|
| 90 |   else | 
|---|
| 91 |   { | 
|---|
| 92 |     /*EventHandler*/ | 
|---|
| 93 |     EventHandler* evh = EventHandler::getInstance(); | 
|---|
| 94 |     std::list<int>::iterator ev; | 
|---|
| 95 |     for (ev = this->events.begin(); ev != events.end(); ev++) | 
|---|
| 96 |       evh->unsubscribe( ES_GAME, (*ev)); | 
|---|
| 97 |  | 
|---|
| 98 |     this->currentPlayer = NULL; | 
|---|
| 99 |     return true; | 
|---|
| 100 |   } | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | /** | 
|---|
| 104 |  * add an event to the event list of events this Playable can capture | 
|---|
| 105 |  * @param eventType the Type of event to add | 
|---|
| 106 |  */ | 
|---|
| 107 | void Playable::registerEvent(int eventType) | 
|---|
| 108 | { | 
|---|
| 109 |   this->events.push_back(eventType); | 
|---|
| 110 |  | 
|---|
| 111 |   if (this->currentPlayer != NULL) | 
|---|
| 112 |     EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType); | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | /** | 
|---|
| 116 |  * remove an event to the event list this Playable can capture. | 
|---|
| 117 |  * @param event the event to unregister. | 
|---|
| 118 |  */ | 
|---|
| 119 | void Playable::unregisterEvent(int eventType) | 
|---|
| 120 | { | 
|---|
| 121 |   this->events.remove(eventType); | 
|---|
| 122 |  | 
|---|
| 123 |   if (this->currentPlayer != NULL) | 
|---|
| 124 |     EventHandler::getInstance()->unsubscribe(ES_GAME, eventType); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 |  | 
|---|