| [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 | /** |
|---|
| 57 | * subscribe to all events the controllable needs |
|---|
| [5898] | 58 | * @param player the player that shall controll this Playable |
|---|
| [5872] | 59 | */ |
|---|
| [5895] | 60 | bool Playable::subscribePlayer(Player* player) |
|---|
| [5872] | 61 | { |
|---|
| [5895] | 62 | if (this->currentPlayer != NULL) |
|---|
| [5872] | 63 | { |
|---|
| [5895] | 64 | PRINTF(1)("Already registered Player:%s to this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName()); |
|---|
| 65 | return false; |
|---|
| [5875] | 66 | } |
|---|
| [5895] | 67 | else |
|---|
| 68 | { |
|---|
| 69 | this->currentPlayer = player; |
|---|
| 70 | /*EventHandler*/ |
|---|
| 71 | EventHandler* evh = EventHandler::getInstance(); |
|---|
| 72 | std::list<int>::iterator ev; |
|---|
| 73 | for (ev = this->events.begin(); ev != events.end(); ev++) |
|---|
| 74 | evh->subscribe(player, ES_GAME, (*ev)); |
|---|
| [6241] | 75 | this->enter(); |
|---|
| [5895] | 76 | return true; |
|---|
| 77 | } |
|---|
| [5872] | 78 | } |
|---|
| [5889] | 79 | |
|---|
| 80 | /** |
|---|
| [5898] | 81 | * unsubscribe from all events the controllable needs |
|---|
| 82 | * @param player the Player, that controlled this Ship. |
|---|
| [5896] | 83 | */ |
|---|
| 84 | bool Playable::unsubscribePlayer(Player* player) |
|---|
| 85 | { |
|---|
| 86 | if (this->currentPlayer != player) |
|---|
| 87 | { |
|---|
| 88 | PRINTF(1)("not the right Player to unregister from this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName()); |
|---|
| 89 | return false; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | else |
|---|
| 93 | { |
|---|
| 94 | /*EventHandler*/ |
|---|
| 95 | EventHandler* evh = EventHandler::getInstance(); |
|---|
| 96 | std::list<int>::iterator ev; |
|---|
| 97 | for (ev = this->events.begin(); ev != events.end(); ev++) |
|---|
| 98 | evh->unsubscribe( ES_GAME, (*ev)); |
|---|
| 99 | |
|---|
| [6241] | 100 | this->leave(); |
|---|
| [5896] | 101 | this->currentPlayer = NULL; |
|---|
| 102 | return true; |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | /** |
|---|
| [5898] | 107 | * add an event to the event list of events this Playable can capture |
|---|
| 108 | * @param eventType the Type of event to add |
|---|
| [5889] | 109 | */ |
|---|
| [5896] | 110 | void Playable::registerEvent(int eventType) |
|---|
| [5889] | 111 | { |
|---|
| 112 | this->events.push_back(eventType); |
|---|
| 113 | |
|---|
| [5896] | 114 | if (this->currentPlayer != NULL) |
|---|
| 115 | EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType); |
|---|
| [5889] | 116 | } |
|---|
| 117 | |
|---|
| [5896] | 118 | /** |
|---|
| [5898] | 119 | * remove an event to the event list this Playable can capture. |
|---|
| 120 | * @param event the event to unregister. |
|---|
| [5896] | 121 | */ |
|---|
| 122 | void Playable::unregisterEvent(int eventType) |
|---|
| 123 | { |
|---|
| [5902] | 124 | this->events.remove(eventType); |
|---|
| [5889] | 125 | |
|---|
| [5896] | 126 | if (this->currentPlayer != NULL) |
|---|
| 127 | EventHandler::getInstance()->unsubscribe(ES_GAME, eventType); |
|---|
| 128 | } |
|---|
| [5889] | 129 | |
|---|
| [5896] | 130 | |
|---|
| [6241] | 131 | void Playable::attachCamera() |
|---|
| 132 | { |
|---|
| 133 | State::getCamera()->setParentSoft(this); |
|---|
| 134 | State::getCameraTarget()->setParentSoft(this); |
|---|
| 135 | |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | void Playable::detachCamera() |
|---|
| 140 | { |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | } |
|---|