Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/osX/src/world_entities/playable.cc @ 7205

Last change on this file since 7205 was 6973, checked in by bensch, 19 years ago

orxonox/trunk: added powerups

File size: 7.2 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
30#include "dot_emitter.h"
31#include "sprite_particles.h"
32
33
34Playable::Playable()
35{
36  this->setClassID(CL_PLAYABLE, "Playable");
37  PRINTF(4)("PLAYABLE INIT\n");
38
39  this->toList(OM_GROUP_01);
40  this->weaponMan = new WeaponManager(this);
41
42  // the reference to the Current Player is NULL, because we dont have one at the beginning.
43  this->currentPlayer = NULL;
44
45  this->bFire = false;
46  this->oldFlags = 0;
47
48  this->setSynchronized(true);
49
50  this->score = 0;
51  this->oldScore = 0;
52
53
54  this->emitter = new DotEmitter(100, 5, M_2_PI);
55  this->emitter->setParent(this);
56  this->emitter->setSpread(M_PI, M_PI);
57  this->emitter->setEmissionRate(300.0);
58  this->emitter->setEmissionVelocity(50.0);
59
60  this->explosionParticles = new SpriteParticles(1000);
61  this->explosionParticles->setName("LaserExplosionParticles");
62  this->explosionParticles->setLifeSpan(.5, .3);
63  this->explosionParticles->setRadius(0.0, 10.0);
64  this->explosionParticles->setRadius(.5, 6.0);
65  this->explosionParticles->setRadius(1.0, 3.0);
66  this->explosionParticles->setColor(0.0, 1,1,0,.9);
67  this->explosionParticles->setColor(0.5, .8,.8,0,.5);
68  this->explosionParticles->setColor(1.0, .8,.8,.7,.0);
69}
70
71
72
73Playable::~Playable()
74{
75  delete this->weaponMan;
76
77  if (this->currentPlayer)
78  {
79    PRINTF(2)("There is Still a Player subscribed to this Playable (%s::%s)\n", this->getClassName(), this->getName());
80
81  }
82}
83
84
85void Playable::addWeapon(Weapon* weapon, int configID, int slotID)
86{
87  this->weaponMan->addWeapon(weapon, configID, slotID);
88
89  this->weaponConfigChanged();
90}
91
92
93void Playable::removeWeapon(Weapon* weapon)
94{
95  this->weaponMan->removeWeapon(weapon);
96
97    this->weaponConfigChanged();
98}
99
100
101void Playable::nextWeaponConfig()
102{
103  this->weaponMan->nextWeaponConfig();
104    this->weaponConfigChanged();
105}
106
107
108void Playable::previousWeaponConfig()
109{
110  this->weaponMan->previousWeaponConfig();
111    this->weaponConfigChanged();
112}
113
114
115void Playable::weaponConfigChanged()
116{
117  if (this->currentPlayer != NULL)
118    this->currentPlayer->weaponConfigChanged();
119}
120
121
122/**
123 * @brief helps us colliding Playables
124 */
125void Playable::collidesWith(WorldEntity* entity, const Vector& location)
126{
127  if (entity->isA(CL_PROJECTILE) && !State::isOnline() )
128  {
129    this->decreaseHealth(entity->getHealth());
130    // EXTREME HACK
131    if (this->getHealth() == 0.0f)
132    {
133      this->die();
134    }
135  }
136}
137
138
139void Playable::die()
140{
141    //this->deactivateNode();
142  this->toList(OM_DEAD);
143  this->emitter->setSystem(explosionParticles);
144  this->setAbsCoor(0, 0, 0);
145    //this->setAbsDir(Vector(1,0,0), 0);
146  this->emitter->setSystem(NULL);
147
148
149  if( this->getOwner()%2 == 0)
150    this->toList(OM_GROUP_00);
151  else
152    this->toList(OM_GROUP_01);
153}
154
155
156/**
157 * subscribe to all events the controllable needs
158 * @param player the player that shall controll this Playable
159 */
160bool Playable::subscribePlayer(Player* player)
161{
162  if (this->currentPlayer != NULL)
163  {
164    PRINTF(1)("Already registered Player:%s to this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName());
165    return false;
166  }
167  else
168  {
169    this->currentPlayer = player;
170    /*EventHandler*/
171    EventHandler* evh = EventHandler::getInstance();
172    std::list<int>::iterator ev;
173    for (ev = this->events.begin(); ev != events.end(); ev++)
174      evh->subscribe(player, ES_GAME, (*ev));
175    this->enter();
176    return true;
177  }
178}
179
180/**
181 * unsubscribe from all events the controllable needs
182 * @param player the Player, that controlled this Ship.
183 */
184bool Playable::unsubscribePlayer(Player* player)
185{
186  if (this->currentPlayer != player)
187  {
188    PRINTF(1)("not the right Player to unregister from this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName());
189    return false;
190  }
191
192  else
193  {
194    /*EventHandler*/
195    EventHandler* evh = EventHandler::getInstance();
196    std::list<int>::iterator ev;
197    for (ev = this->events.begin(); ev != events.end(); ev++)
198      evh->unsubscribe( ES_GAME, (*ev));
199
200    this->leave();
201    this->currentPlayer = NULL;
202    return true;
203  }
204}
205
206bool Playable::pickup(PowerUp* powerUp)
207{
208  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
209    return dynamic_cast<WeaponPowerUp*>(powerUp)->process(this->getWeaponManager());
210  }
211  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
212    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
213    switch(ppu->getType()) {
214      case POWERUP_PARAM_HEALTH:
215        this->increaseHealth(ppu->getValue());
216        return true;
217      case POWERUP_PARAM_MAX_HEALTH:
218        this->increaseHealthMax(ppu->getValue());
219        return true;
220    }
221  }
222  return false;
223}
224
225/**
226 * add an event to the event list of events this Playable can capture
227 * @param eventType the Type of event to add
228 */
229void Playable::registerEvent(int eventType)
230{
231  this->events.push_back(eventType);
232
233  if (this->currentPlayer != NULL)
234    EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType);
235}
236
237/**
238 * remove an event to the event list this Playable can capture.
239 * @param event the event to unregister.
240 */
241void Playable::unregisterEvent(int eventType)
242{
243  this->events.remove(eventType);
244
245  if (this->currentPlayer != NULL)
246    EventHandler::getInstance()->unsubscribe(ES_GAME, eventType);
247}
248
249/**
250 * @brief ticks a Playable
251 * @param dt: the passed time since the last Tick
252 */
253void Playable::tick(float dt)
254{
255  this->weaponMan->tick(dt);
256  if (this->bFire)
257    weaponMan->fire();
258}
259
260
261/**
262 * @brief processes Playable events.
263 * @param event the Captured Event.
264 */
265void Playable::process(const Event &event)
266{
267  if( event.type == KeyMapper::PEV_FIRE1)
268    this->bFire = event.bPressed;
269  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
270  {
271    this->nextWeaponConfig();
272  }
273  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
274    this->previousWeaponConfig();
275}
276
277
278
279void  Playable::attachCamera()
280{
281  State::getCamera()->setParentSoft(this);
282  State::getCameraTarget()->setParentSoft(this);
283
284}
285
286
287
288
289void  Playable::detachCamera()
290{
291}
292
293#define DATA_FLAGS    1
294#define DATA_SCORE    2
295
296#define FLAGS_bFire   1
297
298int Playable::writeSync( const byte * data, int length, int sender )
299{
300  SYNCHELP_READ_BEGIN();
301
302  byte flags;
303
304  SYNCHELP_READ_BYTE( flags, NWT_PL_FLAGS );
305
306  bFire = (flags & FLAGS_bFire) != 0;
307
308  return SYNCHELP_READ_N;
309}
310
311int Playable::readSync( byte * data, int maxLength )
312{
313  SYNCHELP_WRITE_BEGIN();
314  byte flags = 0;
315
316  if ( bFire )
317    flags |= FLAGS_bFire;
318
319
320  SYNCHELP_WRITE_BYTE( flags, NWT_PL_FLAGS );
321  oldFlags = flags;
322
323
324  return SYNCHELP_WRITE_N;
325}
326
327bool Playable::needsReadSync( )
328{
329  //if ( score != oldScore )
330  //  return true;
331
332  byte flags = 0;
333
334  if ( bFire )
335    flags |= FLAGS_bFire;
336
337  return flags!=oldFlags;
338}
Note: See TracBrowser for help on using the repository browser.