Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6490 was 6487, checked in by manuel, 19 years ago

moved pickup from spaceship to playable. playable can now pickup health and max-health powerups

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