Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: segfault prevention

File size: 4.9 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
[6578]56  this->weaponConfigChanged();
[6443]57}
58
59void Playable::removeWeapon(Weapon* weapon)
60{
61  this->weaponMan->removeWeapon(weapon);
62
[6568]63    this->weaponConfigChanged();
[6443]64}
65
[6444]66void Playable::nextWeaponConfig()
67{
68  this->weaponMan->nextWeaponConfig();
[6568]69    this->weaponConfigChanged();
[6444]70}
[6443]71
[6444]72void Playable::previousWeaponConfig()
73{
74  this->weaponMan->previousWeaponConfig();
[6568]75    this->weaponConfigChanged();
[6444]76}
77
[6568]78void Playable::weaponConfigChanged()
79{
[6578]80  if (this->currentPlayer != NULL)
81    this->currentPlayer->weaponConfigChanged();
[6568]82}
[6444]83
[5872]84/**
[6436]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/**
[5872]98 * subscribe to all events the controllable needs
[5898]99 * @param player the player that shall controll this Playable
[5872]100 */
[5895]101bool Playable::subscribePlayer(Player* player)
[5872]102{
[5895]103  if (this->currentPlayer != NULL)
[5872]104  {
[5895]105    PRINTF(1)("Already registered Player:%s to this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName());
106    return false;
[5875]107  }
[5895]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));
[6241]116    this->enter();
[5895]117    return true;
118  }
[5872]119}
[5889]120
121/**
[5898]122 * unsubscribe from all events the controllable needs
123 * @param player the Player, that controlled this Ship.
[5896]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
[6241]141    this->leave();
[5896]142    this->currentPlayer = NULL;
143    return true;
144  }
145}
146
[6547]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
[5896]172/**
[5898]173 * add an event to the event list of events this Playable can capture
174 * @param eventType the Type of event to add
[5889]175 */
[5896]176void Playable::registerEvent(int eventType)
[5889]177{
178  this->events.push_back(eventType);
179
[5896]180  if (this->currentPlayer != NULL)
181    EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType);
[5889]182}
183
[5896]184/**
[5898]185 * remove an event to the event list this Playable can capture.
186 * @param event the event to unregister.
[5896]187 */
188void Playable::unregisterEvent(int eventType)
189{
[5902]190  this->events.remove(eventType);
[5889]191
[5896]192  if (this->currentPlayer != NULL)
193    EventHandler::getInstance()->unsubscribe(ES_GAME, eventType);
194}
[5889]195
[5896]196
[6241]197void  Playable::attachCamera()
198{
[6444]199  State::getCamera()->setParentSoft(this);
200  State::getCameraTarget()->setParentSoft(this);
[6241]201
202}
203
204
205void  Playable::detachCamera()
206{
207}
Note: See TracBrowser for help on using the repository browser.