[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" |
---|
[5838] | 22 | |
---|
[5872] | 23 | |
---|
[5838] | 24 | Playable::Playable() |
---|
| 25 | { |
---|
| 26 | this->init(); |
---|
| 27 | } |
---|
| 28 | |
---|
[5875] | 29 | Playable::~Playable() |
---|
[5838] | 30 | { |
---|
[5881] | 31 | delete this->weaponMan; |
---|
[5895] | 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 | } |
---|
[5875] | 38 | } |
---|
| 39 | |
---|
[5898] | 40 | /** |
---|
| 41 | * initializes this Playable |
---|
| 42 | */ |
---|
[5875] | 43 | void Playable::init() |
---|
| 44 | { |
---|
[5838] | 45 | this->setClassID(CL_PLAYABLE, "Playable"); |
---|
| 46 | PRINTF(4)("PLAYABLE INIT\n"); |
---|
[5898] | 47 | |
---|
[5838] | 48 | this->weaponMan = new WeaponManager(this); |
---|
[5895] | 49 | |
---|
[5898] | 50 | // the reference to the Current Player is NULL, because we dont have one at the beginning. |
---|
[5895] | 51 | this->currentPlayer = NULL; |
---|
[5838] | 52 | } |
---|
| 53 | |
---|
[5872] | 54 | /** |
---|
| 55 | * subscribe to all events the controllable needs |
---|
[5898] | 56 | * @param player the player that shall controll this Playable |
---|
[5872] | 57 | */ |
---|
[5895] | 58 | bool Playable::subscribePlayer(Player* player) |
---|
[5872] | 59 | { |
---|
[5895] | 60 | if (this->currentPlayer != NULL) |
---|
[5872] | 61 | { |
---|
[5895] | 62 | PRINTF(1)("Already registered Player:%s to this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName()); |
---|
| 63 | return false; |
---|
[5875] | 64 | } |
---|
[5895] | 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)); |
---|
[5872] | 73 | |
---|
[5895] | 74 | return true; |
---|
| 75 | } |
---|
[5872] | 76 | } |
---|
[5889] | 77 | |
---|
| 78 | /** |
---|
[5898] | 79 | * unsubscribe from all events the controllable needs |
---|
| 80 | * @param player the Player, that controlled this Ship. |
---|
[5896] | 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 | /** |
---|
[5898] | 104 | * add an event to the event list of events this Playable can capture |
---|
| 105 | * @param eventType the Type of event to add |
---|
[5889] | 106 | */ |
---|
[5896] | 107 | void Playable::registerEvent(int eventType) |
---|
[5889] | 108 | { |
---|
| 109 | this->events.push_back(eventType); |
---|
| 110 | |
---|
[5896] | 111 | if (this->currentPlayer != NULL) |
---|
| 112 | EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType); |
---|
[5889] | 113 | } |
---|
| 114 | |
---|
[5896] | 115 | /** |
---|
[5898] | 116 | * remove an event to the event list this Playable can capture. |
---|
| 117 | * @param event the event to unregister. |
---|
[5896] | 118 | */ |
---|
| 119 | void Playable::unregisterEvent(int eventType) |
---|
| 120 | { |
---|
[5902] | 121 | this->events.remove(eventType); |
---|
[5889] | 122 | |
---|
[5896] | 123 | if (this->currentPlayer != NULL) |
---|
| 124 | EventHandler::getInstance()->unsubscribe(ES_GAME, eventType); |
---|
| 125 | } |
---|
[5889] | 126 | |
---|
[5896] | 127 | |
---|