Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6966 was 6966, checked in by patrick, 18 years ago

trunk: deactivated the lenseflare temp

File size: 7.3 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    Weapon* weapon = dynamic_cast<WeaponPowerUp*>(powerUp)->getWeapon();
210    WeaponManager* manager = this->getWeaponManager();
211    return manager->addWeapon(weapon);
212  }
213  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
214    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
215    switch(ppu->getType()) {
216      case POWERUP_PARAM_HEALTH:
217        this->increaseHealth(ppu->getValue());
218        return true;
219      case POWERUP_PARAM_MAX_HEALTH:
220        this->increaseHealthMax(ppu->getValue());
221        return true;
222    }
223  }
224  return false;
225}
226
227/**
228 * add an event to the event list of events this Playable can capture
229 * @param eventType the Type of event to add
230 */
231void Playable::registerEvent(int eventType)
232{
233  this->events.push_back(eventType);
234
235  if (this->currentPlayer != NULL)
236    EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType);
237}
238
239/**
240 * remove an event to the event list this Playable can capture.
241 * @param event the event to unregister.
242 */
243void Playable::unregisterEvent(int eventType)
244{
245  this->events.remove(eventType);
246
247  if (this->currentPlayer != NULL)
248    EventHandler::getInstance()->unsubscribe(ES_GAME, eventType);
249}
250
251/**
252 * @brief ticks a Playable
253 * @param dt: the passed time since the last Tick
254 */
255void Playable::tick(float dt)
256{
257  this->weaponMan->tick(dt);
258  if (this->bFire)
259    weaponMan->fire();
260}
261
262
263/**
264 * @brief processes Playable events.
265 * @param event the Captured Event.
266 */
267void Playable::process(const Event &event)
268{
269  if( event.type == KeyMapper::PEV_FIRE1)
270    this->bFire = event.bPressed;
271  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
272  {
273    this->nextWeaponConfig();
274  }
275  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
276    this->previousWeaponConfig();
277}
278
279
280
281void  Playable::attachCamera()
282{
283  State::getCamera()->setParentSoft(this);
284  State::getCameraTarget()->setParentSoft(this);
285
286}
287
288
289
290
291void  Playable::detachCamera()
292{
293}
294
295#define DATA_FLAGS    1
296#define DATA_SCORE    2
297
298#define FLAGS_bFire   1
299
300int Playable::writeSync( const byte * data, int length, int sender )
301{
302  SYNCHELP_READ_BEGIN();
303
304  byte flags;
305
306  SYNCHELP_READ_BYTE( flags, NWT_PL_FLAGS );
307
308  bFire = (flags & FLAGS_bFire) != 0;
309
310  return SYNCHELP_READ_N;
311}
312
313int Playable::readSync( byte * data, int maxLength )
314{
315  SYNCHELP_WRITE_BEGIN();
316  byte flags = 0;
317
318  if ( bFire )
319    flags |= FLAGS_bFire;
320
321
322  SYNCHELP_WRITE_BYTE( flags, NWT_PL_FLAGS );
323  oldFlags = flags;
324
325
326  return SYNCHELP_WRITE_N;
327}
328
329bool Playable::needsReadSync( )
330{
331  //if ( score != oldScore )
332  //  return true;
333
334  byte flags = 0;
335
336  if ( bFire )
337    flags |= FLAGS_bFire;
338
339  return flags!=oldFlags;
340}
Note: See TracBrowser for help on using the repository browser.