/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Silvan Nellen co-programmer: Benjamin Knecht */ #include "playable.h" #include "weapons/weapon_manager.h" #include "event_handler.h" #include "player.h" Playable::Playable() { this->init(); } Playable::~Playable() { delete this->weaponMan; if (this->currentPlayer) { PRINTF(2)("There is Still a Player subscribed to this Playable (%s::%s)\n", this->getClassName(), this->getName()); } } /** * initializes this Playable */ void Playable::init() { this->setClassID(CL_PLAYABLE, "Playable"); PRINTF(4)("PLAYABLE INIT\n"); this->weaponMan = new WeaponManager(this); // the reference to the Current Player is NULL, because we dont have one at the beginning. this->currentPlayer = NULL; } /** * subscribe to all events the controllable needs * @param player the player that shall controll this Playable */ bool Playable::subscribePlayer(Player* player) { if (this->currentPlayer != NULL) { PRINTF(1)("Already registered Player:%s to this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName()); return false; } else { this->currentPlayer = player; /*EventHandler*/ EventHandler* evh = EventHandler::getInstance(); std::list::iterator ev; for (ev = this->events.begin(); ev != events.end(); ev++) evh->subscribe(player, ES_GAME, (*ev)); return true; } } /** * unsubscribe from all events the controllable needs * @param player the Player, that controlled this Ship. */ bool Playable::unsubscribePlayer(Player* player) { if (this->currentPlayer != player) { PRINTF(1)("not the right Player to unregister from this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName()); return false; } else { /*EventHandler*/ EventHandler* evh = EventHandler::getInstance(); std::list::iterator ev; for (ev = this->events.begin(); ev != events.end(); ev++) evh->unsubscribe( ES_GAME, (*ev)); this->currentPlayer = NULL; return true; } } /** * add an event to the event list of events this Playable can capture * @param eventType the Type of event to add */ void Playable::registerEvent(int eventType) { this->events.push_back(eventType); if (this->currentPlayer != NULL) EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType); } /** * remove an event to the event list this Playable can capture. * @param event the event to unregister. */ void Playable::unregisterEvent(int eventType) { this->events.remove(eventType); if (this->currentPlayer != NULL) EventHandler::getInstance()->unsubscribe(ES_GAME, eventType); }