Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/playable.cc @ 6575

Last change on this file since 6575 was 6568, checked in by bensch, 19 years ago

trunk: weaponManager is now reporting the change of Weapons to the (if a ) Playable

File size: 5.0 KB
RevLine 
[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
[6547]24#include "power_ups/weapon_power_up.h"
25#include "power_ups/param_power_up.h"
[5872]26
[6547]27
[5838]28Playable::Playable()
29{
[6442]30  this->setClassID(CL_PLAYABLE, "Playable");
31  PRINTF(4)("PLAYABLE INIT\n");
32
33  this->toList(OM_GROUP_01);
34  this->weaponMan = new WeaponManager(this);
35
36  // the reference to the Current Player is NULL, because we dont have one at the beginning.
37  this->currentPlayer = NULL;
[5838]38}
39
[5875]40Playable::~Playable()
[5838]41{
[5881]42  delete this->weaponMan;
[5895]43
44  if (this->currentPlayer)
45  {
46    PRINTF(2)("There is Still a Player subscribed to this Playable (%s::%s)\n", this->getClassName(), this->getName());
47
48  }
[5875]49}
50
[5898]51
[6443]52void Playable::addWeapon(Weapon* weapon, int configID, int slotID)
53{
54  this->weaponMan->addWeapon(weapon, configID, slotID);
55
56  if (this->currentPlayer != NULL)
[6568]57    this->weaponConfigChanged();
[6443]58}
59
60void Playable::removeWeapon(Weapon* weapon)
61{
62  this->weaponMan->removeWeapon(weapon);
63
64  if (this->currentPlayer != NULL)
[6568]65    this->weaponConfigChanged();
[6443]66}
67
[6444]68void Playable::nextWeaponConfig()
69{
70  this->weaponMan->nextWeaponConfig();
71  if (this->currentPlayer != NULL)
[6568]72    this->weaponConfigChanged();
[6444]73}
[6443]74
[6444]75void Playable::previousWeaponConfig()
76{
77  this->weaponMan->previousWeaponConfig();
78  if (this->currentPlayer != NULL)
[6568]79    this->weaponConfigChanged();
[6444]80}
81
[6568]82void Playable::weaponConfigChanged()
83{
84  this->currentPlayer->weaponConfigChanged();
85}
[6444]86
[5872]87/**
[6436]88 * @brief helps us colliding Playables
89 */
90void Playable::collidesWith(WorldEntity* entity, const Vector& location)
91{
92  if (entity->isA(CL_PROJECTILE))
93    this->removeEnergy(entity->getEnergy());
94
95  // EXTREME HACK
96  if (this->getEnergy() == 0.0f)
97    this->deactivateNode();
98}
99
100/**
[5872]101 * subscribe to all events the controllable needs
[5898]102 * @param player the player that shall controll this Playable
[5872]103 */
[5895]104bool Playable::subscribePlayer(Player* player)
[5872]105{
[5895]106  if (this->currentPlayer != NULL)
[5872]107  {
[5895]108    PRINTF(1)("Already registered Player:%s to this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName());
109    return false;
[5875]110  }
[5895]111  else
112  {
113    this->currentPlayer = player;
114    /*EventHandler*/
115    EventHandler* evh = EventHandler::getInstance();
116    std::list<int>::iterator ev;
117    for (ev = this->events.begin(); ev != events.end(); ev++)
118      evh->subscribe(player, ES_GAME, (*ev));
[6241]119    this->enter();
[5895]120    return true;
121  }
[5872]122}
[5889]123
124/**
[5898]125 * unsubscribe from all events the controllable needs
126 * @param player the Player, that controlled this Ship.
[5896]127 */
128bool Playable::unsubscribePlayer(Player* player)
129{
130  if (this->currentPlayer != player)
131  {
132    PRINTF(1)("not the right Player to unregister from this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName());
133    return false;
134  }
135
136  else
137  {
138    /*EventHandler*/
139    EventHandler* evh = EventHandler::getInstance();
140    std::list<int>::iterator ev;
141    for (ev = this->events.begin(); ev != events.end(); ev++)
142      evh->unsubscribe( ES_GAME, (*ev));
143
[6241]144    this->leave();
[5896]145    this->currentPlayer = NULL;
146    return true;
147  }
148}
149
[6547]150bool Playable::pickup(PowerUp* powerUp)
151{
152  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
153    Weapon* weapon = dynamic_cast<WeaponPowerUp*>(powerUp)->getWeapon();
154    WeaponManager* manager = this->getWeaponManager();
155    int slot = manager->getNextFreeSlot(2, weapon->getCapability());
156    if(slot >= 0) {
157      manager->addWeapon(weapon, 2, slot);
158      return true;
159    }
160  }
161  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
162    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
163    switch(ppu->getType()) {
164      case POWERUP_PARAM_HEALTH:
165        this->addEnergy(ppu->getValue());
166        return true;
167      case POWERUP_PARAM_MAX_HEALTH:
168        this->setMaxEnergy(this->getMaxEnergy() + ppu->getValue());
169        return true;
170    }
171  }
172  return false;
173}
174
[5896]175/**
[5898]176 * add an event to the event list of events this Playable can capture
177 * @param eventType the Type of event to add
[5889]178 */
[5896]179void Playable::registerEvent(int eventType)
[5889]180{
181  this->events.push_back(eventType);
182
[5896]183  if (this->currentPlayer != NULL)
184    EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType);
[5889]185}
186
[5896]187/**
[5898]188 * remove an event to the event list this Playable can capture.
189 * @param event the event to unregister.
[5896]190 */
191void Playable::unregisterEvent(int eventType)
192{
[5902]193  this->events.remove(eventType);
[5889]194
[5896]195  if (this->currentPlayer != NULL)
196    EventHandler::getInstance()->unsubscribe(ES_GAME, eventType);
197}
[5889]198
[5896]199
[6241]200void  Playable::attachCamera()
201{
[6444]202  State::getCamera()->setParentSoft(this);
203  State::getCameraTarget()->setParentSoft(this);
[6241]204
205}
206
207
208void  Playable::detachCamera()
209{
210}
Note: See TracBrowser for help on using the repository browser.