Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7042 was 7014, checked in by bensch, 19 years ago

trunk: new algorithm for the Camera-Distance

File size: 7.8 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  // THE DERIVED CLASS MUST UNSUBSCRIBE THE PLAYER THROUGH
78  // this->setPlayer(NULL);
79  // IN ITS DESTRUCTOR.
80  assert(this->currentPlayer == NULL);
81}
82
83
84void Playable::addWeapon(Weapon* weapon, int configID, int slotID)
85{
86  this->weaponMan->addWeapon(weapon, configID, slotID);
87
88  this->weaponConfigChanged();
89}
90
91
92void Playable::removeWeapon(Weapon* weapon)
93{
94  this->weaponMan->removeWeapon(weapon);
95
96    this->weaponConfigChanged();
97}
98
99
100void Playable::nextWeaponConfig()
101{
102  this->weaponMan->nextWeaponConfig();
103    this->weaponConfigChanged();
104}
105
106
107void Playable::previousWeaponConfig()
108{
109  this->weaponMan->previousWeaponConfig();
110    this->weaponConfigChanged();
111}
112
113
114void Playable::weaponConfigChanged()
115{
116  if (this->currentPlayer != NULL)
117    this->currentPlayer->weaponConfigChanged();
118}
119
120
121/**
122 * @brief helps us colliding Playables
123 */
124void Playable::collidesWith(WorldEntity* entity, const Vector& location)
125{
126  if (entity->isA(CL_PROJECTILE) && !State::isOnline() )
127  {
128    this->decreaseHealth(entity->getHealth());
129    // EXTREME HACK
130    if (this->getHealth() == 0.0f)
131    {
132      this->die();
133    }
134  }
135}
136
137
138void Playable::die()
139{
140    //this->deactivateNode();
141  this->toList(OM_DEAD);
142  this->emitter->setSystem(explosionParticles);
143  this->setAbsCoor(0, 0, 0);
144    //this->setAbsDir(Vector(1,0,0), 0);
145  this->emitter->setSystem(NULL);
146
147
148  if( this->getOwner()%2 == 0)
149    this->toList(OM_GROUP_00);
150  else
151    this->toList(OM_GROUP_01);
152}
153
154
155/**
156 * subscribe to all events the controllable needs
157 * @param player the player that shall controll this Playable
158 */
159bool Playable::setPlayer(Player* player)
160{
161  // if we already have a Player inside do nothing
162  if (this->currentPlayer != NULL && player != NULL)
163  {
164    return false;
165  }
166
167  // eject the Player if player == NULL
168  if (this->currentPlayer != NULL && player == NULL)
169  {
170    PRINTF(4)("Player gets ejected\n");
171
172    // unsubscibe all events.
173    EventHandler* evh = EventHandler::getInstance();
174    std::list<int>::iterator ev;
175    for (ev = this->events.begin(); ev != events.end(); ev++)
176      evh->unsubscribe( ES_GAME, (*ev));
177
178    // leave the entity
179    this->leave();
180
181    // eject the current Player.
182    Player* ejectPlayer = this->currentPlayer;
183    this->currentPlayer = NULL;
184    // eject the Player.
185    ejectPlayer->setPlayable(NULL);
186
187    return true;
188  }
189
190  // get the new Player inside
191  if (this->currentPlayer == NULL && player != NULL)
192  {
193    PRINTF(4)("New Player gets inside\n");
194    this->currentPlayer = player;
195    if (this->currentPlayer->getPlayable() != this)
196      this->currentPlayer->setPlayable(this);
197
198    /*EventHandler*/
199    EventHandler* evh = EventHandler::getInstance();
200    std::list<int>::iterator ev;
201    for (ev = this->events.begin(); ev != events.end(); ev++)
202      evh->subscribe(player, ES_GAME, (*ev));
203
204    this->enter();
205    return true;
206  }
207
208  return false;
209}
210
211
212bool Playable::pickup(PowerUp* powerUp)
213{
214  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
215    return dynamic_cast<WeaponPowerUp*>(powerUp)->process(this->getWeaponManager());
216  }
217  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
218    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
219    switch(ppu->getType()) {
220      case POWERUP_PARAM_HEALTH:
221        this->increaseHealth(ppu->getValue());
222        return true;
223      case POWERUP_PARAM_MAX_HEALTH:
224        this->increaseHealthMax(ppu->getValue());
225        return true;
226    }
227  }
228  return false;
229}
230
231/**
232 * add an event to the event list of events this Playable can capture
233 * @param eventType the Type of event to add
234 */
235void Playable::registerEvent(int eventType)
236{
237  this->events.push_back(eventType);
238
239  if (this->currentPlayer != NULL)
240    EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType);
241}
242
243/**
244 * remove an event to the event list this Playable can capture.
245 * @param event the event to unregister.
246 */
247void Playable::unregisterEvent(int eventType)
248{
249  this->events.remove(eventType);
250
251  if (this->currentPlayer != NULL)
252    EventHandler::getInstance()->unsubscribe(ES_GAME, eventType);
253}
254
255/**
256 * @brief ticks a Playable
257 * @param dt: the passed time since the last Tick
258 */
259void Playable::tick(float dt)
260{
261  this->weaponMan->tick(dt);
262  if (this->bFire)
263    weaponMan->fire();
264}
265
266
267/**
268 * @brief processes Playable events.
269 * @param event the Captured Event.
270 */
271void Playable::process(const Event &event)
272{
273  if( event.type == KeyMapper::PEV_FIRE1)
274    this->bFire = event.bPressed;
275  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
276  {
277    this->nextWeaponConfig();
278  }
279  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
280    this->previousWeaponConfig();
281}
282
283
284
285void  Playable::attachCamera()
286{
287  State::getCameraNode()->setParentSoft(this);
288  State::getCameraTargetNode()->setParentSoft(this);
289
290}
291
292
293
294
295void  Playable::detachCamera()
296{
297}
298
299#define DATA_FLAGS    1
300#define DATA_SCORE    2
301
302#define FLAGS_bFire   1
303
304int Playable::writeSync( const byte * data, int length, int sender )
305{
306  SYNCHELP_READ_BEGIN();
307
308  byte b;
309  SYNCHELP_READ_BYTE( b, NWT_PL_B );
310
311  byte flags;
312
313  if ( b == DATA_FLAGS )
314  {
315    SYNCHELP_READ_BYTE( flags, NWT_PL_FLAGS );
316
317    bFire = (flags & FLAGS_bFire) != 0;
318
319    return SYNCHELP_READ_N;
320  }
321
322  if ( b == DATA_SCORE )
323  {
324    int newScore;
325    SYNCHELP_READ_BYTE( newScore, NWT_PL_SCORE );
326    setScore( newScore );
327
328    return SYNCHELP_READ_N;
329  }
330
331  return SYNCHELP_READ_N;
332}
333
334int Playable::readSync( byte * data, int maxLength )
335{
336  SYNCHELP_WRITE_BEGIN();
337
338  if ( score != oldScore && isServer() )
339  {
340    SYNCHELP_WRITE_BYTE( DATA_SCORE, NWT_PL_B);
341    SYNCHELP_WRITE_INT( score, NWT_PL_SCORE );
342    oldScore = score;
343
344    return SYNCHELP_WRITE_N;
345  }
346
347  byte flags = 0;
348
349  if ( bFire )
350    flags |= FLAGS_bFire;
351
352
353  SYNCHELP_WRITE_BYTE( DATA_FLAGS, NWT_PL_B);
354  SYNCHELP_WRITE_BYTE( flags, NWT_PL_FLAGS );
355  oldFlags = flags;
356
357
358  return SYNCHELP_WRITE_N;
359}
360
361bool Playable::needsReadSync( )
362{
363  if ( score != oldScore && isServer() )
364    return true;
365
366  byte flags = 0;
367
368  if ( bFire )
369    flags |= FLAGS_bFire;
370
371  return flags!=oldFlags;
372}
Note: See TracBrowser for help on using the repository browser.