Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6569 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
Line 
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#include "state.h"
23
24#include "power_ups/weapon_power_up.h"
25#include "power_ups/param_power_up.h"
26
27
28Playable::Playable()
29{
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;
38}
39
40Playable::~Playable()
41{
42  delete this->weaponMan;
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  }
49}
50
51
52void Playable::addWeapon(Weapon* weapon, int configID, int slotID)
53{
54  this->weaponMan->addWeapon(weapon, configID, slotID);
55
56  if (this->currentPlayer != NULL)
57    this->weaponConfigChanged();
58}
59
60void Playable::removeWeapon(Weapon* weapon)
61{
62  this->weaponMan->removeWeapon(weapon);
63
64  if (this->currentPlayer != NULL)
65    this->weaponConfigChanged();
66}
67
68void Playable::nextWeaponConfig()
69{
70  this->weaponMan->nextWeaponConfig();
71  if (this->currentPlayer != NULL)
72    this->weaponConfigChanged();
73}
74
75void Playable::previousWeaponConfig()
76{
77  this->weaponMan->previousWeaponConfig();
78  if (this->currentPlayer != NULL)
79    this->weaponConfigChanged();
80}
81
82void Playable::weaponConfigChanged()
83{
84  this->currentPlayer->weaponConfigChanged();
85}
86
87/**
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/**
101 * subscribe to all events the controllable needs
102 * @param player the player that shall controll this Playable
103 */
104bool Playable::subscribePlayer(Player* player)
105{
106  if (this->currentPlayer != NULL)
107  {
108    PRINTF(1)("Already registered Player:%s to this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName());
109    return false;
110  }
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));
119    this->enter();
120    return true;
121  }
122}
123
124/**
125 * unsubscribe from all events the controllable needs
126 * @param player the Player, that controlled this Ship.
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
144    this->leave();
145    this->currentPlayer = NULL;
146    return true;
147  }
148}
149
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
175/**
176 * add an event to the event list of events this Playable can capture
177 * @param eventType the Type of event to add
178 */
179void Playable::registerEvent(int eventType)
180{
181  this->events.push_back(eventType);
182
183  if (this->currentPlayer != NULL)
184    EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType);
185}
186
187/**
188 * remove an event to the event list this Playable can capture.
189 * @param event the event to unregister.
190 */
191void Playable::unregisterEvent(int eventType)
192{
193  this->events.remove(eventType);
194
195  if (this->currentPlayer != NULL)
196    EventHandler::getInstance()->unsubscribe(ES_GAME, eventType);
197}
198
199
200void  Playable::attachCamera()
201{
202  State::getCamera()->setParentSoft(this);
203  State::getCameraTarget()->setParentSoft(this);
204
205}
206
207
208void  Playable::detachCamera()
209{
210}
Note: See TracBrowser for help on using the repository browser.