Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6828 was 6804, checked in by bensch, 19 years ago

trunk: remastered Playables a bit

File size: 5.4 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
[6700]24#include "world_entities/projectiles/projectile.h"
25
[6547]26#include "power_ups/weapon_power_up.h"
27#include "power_ups/param_power_up.h"
[5872]28
[6547]29
[5838]30Playable::Playable()
31{
[6442]32  this->setClassID(CL_PLAYABLE, "Playable");
33  PRINTF(4)("PLAYABLE INIT\n");
34
35  this->toList(OM_GROUP_01);
36  this->weaponMan = new WeaponManager(this);
37
38  // the reference to the Current Player is NULL, because we dont have one at the beginning.
39  this->currentPlayer = NULL;
[6695]40
[6804]41  this->bFire = false;
42
[6695]43  this->setSynchronized(true);
[5838]44}
45
[6695]46
47
[5875]48Playable::~Playable()
[5838]49{
[5881]50  delete this->weaponMan;
[5895]51
52  if (this->currentPlayer)
53  {
54    PRINTF(2)("There is Still a Player subscribed to this Playable (%s::%s)\n", this->getClassName(), this->getName());
55
56  }
[5875]57}
58
[5898]59
[6443]60void Playable::addWeapon(Weapon* weapon, int configID, int slotID)
61{
[6676]62  this->weaponMan->addWeapon(weapon, configID, slotID);
[6443]63
[6578]64  this->weaponConfigChanged();
[6443]65}
66
[6695]67
[6443]68void Playable::removeWeapon(Weapon* weapon)
69{
70  this->weaponMan->removeWeapon(weapon);
71
[6568]72    this->weaponConfigChanged();
[6443]73}
74
[6695]75
[6444]76void Playable::nextWeaponConfig()
77{
78  this->weaponMan->nextWeaponConfig();
[6568]79    this->weaponConfigChanged();
[6444]80}
[6443]81
[6695]82
[6444]83void Playable::previousWeaponConfig()
84{
85  this->weaponMan->previousWeaponConfig();
[6568]86    this->weaponConfigChanged();
[6444]87}
88
[6695]89
[6568]90void Playable::weaponConfigChanged()
91{
[6578]92  if (this->currentPlayer != NULL)
93    this->currentPlayer->weaponConfigChanged();
[6568]94}
[6444]95
[6695]96
[5872]97/**
[6436]98 * @brief helps us colliding Playables
99 */
100void Playable::collidesWith(WorldEntity* entity, const Vector& location)
101{
102  if (entity->isA(CL_PROJECTILE))
[6700]103    this->decreaseHealth(entity->getHealth());
[6436]104
105  // EXTREME HACK
[6700]106  if (this->getHealth() == 0.0f)
[6436]107    this->deactivateNode();
108}
109
110/**
[5872]111 * subscribe to all events the controllable needs
[5898]112 * @param player the player that shall controll this Playable
[5872]113 */
[5895]114bool Playable::subscribePlayer(Player* player)
[5872]115{
[5895]116  if (this->currentPlayer != NULL)
[5872]117  {
[5895]118    PRINTF(1)("Already registered Player:%s to this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName());
119    return false;
[5875]120  }
[5895]121  else
122  {
123    this->currentPlayer = player;
124    /*EventHandler*/
125    EventHandler* evh = EventHandler::getInstance();
126    std::list<int>::iterator ev;
127    for (ev = this->events.begin(); ev != events.end(); ev++)
128      evh->subscribe(player, ES_GAME, (*ev));
[6241]129    this->enter();
[5895]130    return true;
131  }
[5872]132}
[5889]133
134/**
[5898]135 * unsubscribe from all events the controllable needs
136 * @param player the Player, that controlled this Ship.
[5896]137 */
138bool Playable::unsubscribePlayer(Player* player)
139{
140  if (this->currentPlayer != player)
141  {
142    PRINTF(1)("not the right Player to unregister from this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName());
143    return false;
144  }
145
146  else
147  {
148    /*EventHandler*/
149    EventHandler* evh = EventHandler::getInstance();
150    std::list<int>::iterator ev;
151    for (ev = this->events.begin(); ev != events.end(); ev++)
152      evh->unsubscribe( ES_GAME, (*ev));
153
[6241]154    this->leave();
[5896]155    this->currentPlayer = NULL;
156    return true;
157  }
158}
159
[6547]160bool Playable::pickup(PowerUp* powerUp)
161{
162  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
163    Weapon* weapon = dynamic_cast<WeaponPowerUp*>(powerUp)->getWeapon();
164    WeaponManager* manager = this->getWeaponManager();
[6710]165    return manager->addWeapon(weapon);
[6547]166  }
167  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
168    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
169    switch(ppu->getType()) {
170      case POWERUP_PARAM_HEALTH:
[6700]171        this->increaseHealth(ppu->getValue());
[6547]172        return true;
173      case POWERUP_PARAM_MAX_HEALTH:
[6700]174        this->increaseHealthMax(ppu->getValue());
[6547]175        return true;
176    }
177  }
178  return false;
179}
180
[5896]181/**
[5898]182 * add an event to the event list of events this Playable can capture
183 * @param eventType the Type of event to add
[5889]184 */
[5896]185void Playable::registerEvent(int eventType)
[5889]186{
187  this->events.push_back(eventType);
188
[5896]189  if (this->currentPlayer != NULL)
190    EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType);
[5889]191}
192
[5896]193/**
[5898]194 * remove an event to the event list this Playable can capture.
195 * @param event the event to unregister.
[5896]196 */
197void Playable::unregisterEvent(int eventType)
198{
[5902]199  this->events.remove(eventType);
[5889]200
[5896]201  if (this->currentPlayer != NULL)
202    EventHandler::getInstance()->unsubscribe(ES_GAME, eventType);
203}
[5889]204
[6804]205/**
206 * @brief ticks a Playable
207 * @param dt: the passed time since the last Tick
208 */
209void Playable::tick(float dt)
210{
211  this->weaponMan->tick(dt);
212  if (this->bFire)
213    weaponMan->fire();
214}
[5896]215
[6804]216
217/**
218 * @brief processes Playable events.
219 * @param event the Captured Event.
220 */
221void Playable::process(const Event &event)
222{
223  if( event.type == KeyMapper::PEV_FIRE1)
224    this->bFire = event.bPressed;
225  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
226  {
227    this->nextWeaponConfig();
228  }
229  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
230    this->previousWeaponConfig();
231}
232
233
234
[6241]235void  Playable::attachCamera()
236{
[6444]237  State::getCamera()->setParentSoft(this);
238  State::getCameraTarget()->setParentSoft(this);
[6241]239
240}
241
242
[6804]243
244
[6241]245void  Playable::detachCamera()
246{
247}
Note: See TracBrowser for help on using the repository browser.