Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/powerups/src/world_entities/playable.cc @ 6554

Last change on this file since 6554 was 6547, checked in by bensch, 19 years ago

orxonox/trunk: merged the WeaponManager back to the trunk

File size: 4.9 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->currentPlayer->weaponConfigChanged();
58}
59
60void Playable::removeWeapon(Weapon* weapon)
61{
62  this->weaponMan->removeWeapon(weapon);
63
64  if (this->currentPlayer != NULL)
65    this->currentPlayer->weaponConfigChanged();
66}
67
68void Playable::nextWeaponConfig()
69{
70  this->weaponMan->nextWeaponConfig();
71  if (this->currentPlayer != NULL)
72    this->currentPlayer->weaponConfigChanged();
73}
74
75void Playable::previousWeaponConfig()
76{
77  this->weaponMan->previousWeaponConfig();
78  if (this->currentPlayer != NULL)
79    this->currentPlayer->weaponConfigChanged();
80}
81
82
83/**
84 * @brief helps us colliding Playables
85 */
86void Playable::collidesWith(WorldEntity* entity, const Vector& location)
87{
88  if (entity->isA(CL_PROJECTILE))
89    this->removeEnergy(entity->getEnergy());
90
91  // EXTREME HACK
92  if (this->getEnergy() == 0.0f)
93    this->deactivateNode();
94}
95
96/**
97 * subscribe to all events the controllable needs
98 * @param player the player that shall controll this Playable
99 */
100bool Playable::subscribePlayer(Player* player)
101{
102  if (this->currentPlayer != NULL)
103  {
104    PRINTF(1)("Already registered Player:%s to this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName());
105    return false;
106  }
107  else
108  {
109    this->currentPlayer = player;
110    /*EventHandler*/
111    EventHandler* evh = EventHandler::getInstance();
112    std::list<int>::iterator ev;
113    for (ev = this->events.begin(); ev != events.end(); ev++)
114      evh->subscribe(player, ES_GAME, (*ev));
115    this->enter();
116    return true;
117  }
118}
119
120/**
121 * unsubscribe from all events the controllable needs
122 * @param player the Player, that controlled this Ship.
123 */
124bool Playable::unsubscribePlayer(Player* player)
125{
126  if (this->currentPlayer != player)
127  {
128    PRINTF(1)("not the right Player to unregister from this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName());
129    return false;
130  }
131
132  else
133  {
134    /*EventHandler*/
135    EventHandler* evh = EventHandler::getInstance();
136    std::list<int>::iterator ev;
137    for (ev = this->events.begin(); ev != events.end(); ev++)
138      evh->unsubscribe( ES_GAME, (*ev));
139
140    this->leave();
141    this->currentPlayer = NULL;
142    return true;
143  }
144}
145
146bool Playable::pickup(PowerUp* powerUp)
147{
148  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
149    Weapon* weapon = dynamic_cast<WeaponPowerUp*>(powerUp)->getWeapon();
150    WeaponManager* manager = this->getWeaponManager();
151    int slot = manager->getNextFreeSlot(2, weapon->getCapability());
152    if(slot >= 0) {
153      manager->addWeapon(weapon, 2, slot);
154      return true;
155    }
156  }
157  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
158    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
159    switch(ppu->getType()) {
160      case POWERUP_PARAM_HEALTH:
161        this->addEnergy(ppu->getValue());
162        return true;
163      case POWERUP_PARAM_MAX_HEALTH:
164        this->setMaxEnergy(this->getMaxEnergy() + ppu->getValue());
165        return true;
166    }
167  }
168  return false;
169}
170
171/**
172 * add an event to the event list of events this Playable can capture
173 * @param eventType the Type of event to add
174 */
175void Playable::registerEvent(int eventType)
176{
177  this->events.push_back(eventType);
178
179  if (this->currentPlayer != NULL)
180    EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType);
181}
182
183/**
184 * remove an event to the event list this Playable can capture.
185 * @param event the event to unregister.
186 */
187void Playable::unregisterEvent(int eventType)
188{
189  this->events.remove(eventType);
190
191  if (this->currentPlayer != NULL)
192    EventHandler::getInstance()->unsubscribe(ES_GAME, eventType);
193}
194
195
196void  Playable::attachCamera()
197{
198  State::getCamera()->setParentSoft(this);
199  State::getCameraTarget()->setParentSoft(this);
200
201}
202
203
204void  Playable::detachCamera()
205{
206}
Note: See TracBrowser for help on using the repository browser.