Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6588 was 6578, checked in by bensch, 19 years ago

trunk: segfault prevention

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