Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6946 was 6945, checked in by manuel, 19 years ago

powerups work but bugs in weaponmanager

File size: 6.0 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 "world_entities/projectiles/projectile.h"
25
26#include "power_ups/weapon_power_up.h"
27#include "power_ups/param_power_up.h"
28
29
30Playable::Playable()
31{
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;
40
41  this->bFire = false;
42  this->oldFlags = 0;
43
44  this->setSynchronized(true);
45}
46
47
48
49Playable::~Playable()
50{
51  delete this->weaponMan;
52
53  if (this->currentPlayer)
54  {
55    PRINTF(2)("There is Still a Player subscribed to this Playable (%s::%s)\n", this->getClassName(), this->getName());
56
57  }
58}
59
60
61void Playable::addWeapon(Weapon* weapon, int configID, int slotID)
62{
63  this->weaponMan->addWeapon(weapon, configID, slotID);
64
65  this->weaponConfigChanged();
66}
67
68
69void Playable::removeWeapon(Weapon* weapon)
70{
71  this->weaponMan->removeWeapon(weapon);
72
73    this->weaponConfigChanged();
74}
75
76
77void Playable::nextWeaponConfig()
78{
79  this->weaponMan->nextWeaponConfig();
80    this->weaponConfigChanged();
81}
82
83
84void Playable::previousWeaponConfig()
85{
86  this->weaponMan->previousWeaponConfig();
87    this->weaponConfigChanged();
88}
89
90
91void Playable::weaponConfigChanged()
92{
93  if (this->currentPlayer != NULL)
94    this->currentPlayer->weaponConfigChanged();
95}
96
97
98/**
99 * @brief helps us colliding Playables
100 */
101void Playable::collidesWith(WorldEntity* entity, const Vector& location)
102{
103  if (entity->isA(CL_PROJECTILE))
104    this->decreaseHealth(entity->getHealth());
105
106  // EXTREME HACK
107  if (this->getHealth() == 0.0f)
108    this->deactivateNode();
109}
110
111/**
112 * subscribe to all events the controllable needs
113 * @param player the player that shall controll this Playable
114 */
115bool Playable::subscribePlayer(Player* player)
116{
117  if (this->currentPlayer != NULL)
118  {
119    PRINTF(1)("Already registered Player:%s to this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName());
120    return false;
121  }
122  else
123  {
124    this->currentPlayer = player;
125    /*EventHandler*/
126    EventHandler* evh = EventHandler::getInstance();
127    std::list<int>::iterator ev;
128    for (ev = this->events.begin(); ev != events.end(); ev++)
129      evh->subscribe(player, ES_GAME, (*ev));
130    this->enter();
131    return true;
132  }
133}
134
135/**
136 * unsubscribe from all events the controllable needs
137 * @param player the Player, that controlled this Ship.
138 */
139bool Playable::unsubscribePlayer(Player* player)
140{
141  if (this->currentPlayer != player)
142  {
143    PRINTF(1)("not the right Player to unregister from this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName());
144    return false;
145  }
146
147  else
148  {
149    /*EventHandler*/
150    EventHandler* evh = EventHandler::getInstance();
151    std::list<int>::iterator ev;
152    for (ev = this->events.begin(); ev != events.end(); ev++)
153      evh->unsubscribe( ES_GAME, (*ev));
154
155    this->leave();
156    this->currentPlayer = NULL;
157    return true;
158  }
159}
160
161bool Playable::pickup(PowerUp* powerUp)
162{
163  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
164    return dynamic_cast<WeaponPowerUp*>(powerUp)->process(this->getWeaponManager());
165  }
166  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
167    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
168    switch(ppu->getType()) {
169      case POWERUP_PARAM_HEALTH:
170        this->increaseHealth(ppu->getValue());
171        return true;
172      case POWERUP_PARAM_MAX_HEALTH:
173        this->increaseHealthMax(ppu->getValue());
174        return true;
175    }
176  }
177  return false;
178}
179
180/**
181 * add an event to the event list of events this Playable can capture
182 * @param eventType the Type of event to add
183 */
184void Playable::registerEvent(int eventType)
185{
186  this->events.push_back(eventType);
187
188  if (this->currentPlayer != NULL)
189    EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType);
190}
191
192/**
193 * remove an event to the event list this Playable can capture.
194 * @param event the event to unregister.
195 */
196void Playable::unregisterEvent(int eventType)
197{
198  this->events.remove(eventType);
199
200  if (this->currentPlayer != NULL)
201    EventHandler::getInstance()->unsubscribe(ES_GAME, eventType);
202}
203
204/**
205 * @brief ticks a Playable
206 * @param dt: the passed time since the last Tick
207 */
208void Playable::tick(float dt)
209{
210  this->weaponMan->tick(dt);
211  if (this->bFire)
212    weaponMan->fire();
213}
214
215
216/**
217 * @brief processes Playable events.
218 * @param event the Captured Event.
219 */
220void Playable::process(const Event &event)
221{
222  if( event.type == KeyMapper::PEV_FIRE1)
223    this->bFire = event.bPressed;
224  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
225  {
226    this->nextWeaponConfig();
227  }
228  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
229    this->previousWeaponConfig();
230}
231
232
233
234void  Playable::attachCamera()
235{
236  State::getCamera()->setParentSoft(this);
237  State::getCameraTarget()->setParentSoft(this);
238
239}
240
241
242
243
244void  Playable::detachCamera()
245{
246}
247
248#define FLAGS_bFire   1
249
250int Playable::writeSync( const byte * data, int length, int sender )
251{
252  SYNCHELP_READ_BEGIN();
253
254  byte flags;
255
256  SYNCHELP_READ_BYTE( flags, NWT_PL_FLAGS );
257
258  bFire = (flags & FLAGS_bFire) != 0;
259
260  return SYNCHELP_READ_N;
261}
262
263int Playable::readSync( byte * data, int maxLength )
264{
265  SYNCHELP_WRITE_BEGIN();
266  byte flags = 0;
267
268  if ( bFire )
269    flags |= FLAGS_bFire;
270
271
272  SYNCHELP_WRITE_BYTE( flags, NWT_PL_FLAGS );
273  oldFlags = flags;
274
275
276  return SYNCHELP_WRITE_N;
277}
278
279bool Playable::needsReadSync( )
280{
281  byte flags = 0;
282
283  if ( bFire )
284    flags |= FLAGS_bFire;
285
286  return flags!=oldFlags;
287}
Note: See TracBrowser for help on using the repository browser.