Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: model display patch

File size: 8.6 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#include "game_rules.h"
30
31#include "dot_emitter.h"
32#include "sprite_particles.h"
33
34
35Playable::Playable()
36{
37  this->setClassID(CL_PLAYABLE, "Playable");
38  PRINTF(4)("PLAYABLE INIT\n");
39
40  this->toList(OM_GROUP_01);
41  this->weaponMan = new WeaponManager(this);
42
43  // the reference to the Current Player is NULL, because we dont have one at the beginning.
44  this->currentPlayer = NULL;
45
46  this->bFire = false;
47  this->oldFlags = 0;
48
49  this->setSynchronized(true);
50
51  this->score = 0;
52  this->oldScore = 0;
53
54
55  this->emitter = new DotEmitter(100, 5, M_2_PI);
56  this->emitter->setParent(this);
57  this->emitter->setSpread(M_PI, M_PI);
58  this->emitter->setEmissionRate(300.0);
59  this->emitter->setEmissionVelocity(50.0);
60
61  this->explosionParticles = new SpriteParticles(1000);
62  this->explosionParticles->setName("LaserExplosionParticles");
63  this->explosionParticles->setLifeSpan(.5, .3);
64  this->explosionParticles->setRadius(0.0, 10.0);
65  this->explosionParticles->setRadius(.5, 6.0);
66  this->explosionParticles->setRadius(1.0, 3.0);
67  this->explosionParticles->setColor(0.0, 1,1,0,.9);
68  this->explosionParticles->setColor(0.5, .8,.8,0,.5);
69  this->explosionParticles->setColor(1.0, .8,.8,.7,.0);
70}
71
72
73
74Playable::~Playable()
75{
76  delete this->weaponMan;
77
78  // THE DERIVED CLASS MUST UNSUBSCRIBE THE PLAYER THROUGH
79  // this->setPlayer(NULL);
80  // IN ITS DESTRUCTOR.
81  assert(this->currentPlayer == NULL);
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 == collider)
128    return;
129  collider = entity;
130
131  if (entity->isA(CL_PROJECTILE))
132  {
133    this->decreaseHealth(entity->getHealth() *(float)rand()/(float)RAND_MAX);
134    // EXTREME HACK
135    if (this->getHealth() <= 0.0f)
136    {
137      this->die();
138    }
139  }
140}
141
142
143void Playable::respawn()
144{
145  PRINTF(0)("Playable respawn\n");
146  // only if this is the spaceship of the player
147  if( this == State::getPlayer()->getPlayable())
148    State::getGameRules()->onPlayerSpawn();
149
150  this->reset();
151
152
153  if( this->getOwner() % 2 == 0)
154  {
155//     this->toList(OM_GROUP_00);
156    this->setAbsCoor(213.37, 57.71, -47.98);
157    this->setAbsDir(Quaternion(0.16, 0.98, -0.10));
158  }
159  else
160  {
161//     this->toList(OM_GROUP_01);
162    this->setAbsCoor(-314.450, 40.701, 83.554);
163    this->setAbsDir(0, 0, 1, 0);
164  }
165}
166
167
168
169void Playable::die()
170{
171  PRINTF(0)("Playable dies\n");
172  // only if this is the spaceship of the player
173  if (State::isOnline())
174  {
175    if( this == State::getPlayer()->getPlayable())
176      State::getGameRules()->onPlayerDeath();
177
178
179//     this->toList(OM_GROUP_05);
180  //HACK: moves the entity to an unknown place far far away: in the future, GameRules will look for that
181    this->setAbsCoor(-2000.0, -2000.0, -2000.0);
182
183  //explosion hack
184    this->emitter->setSystem(explosionParticles);
185    this->emitter->setSystem(NULL);
186  }
187}
188
189
190/**
191 * subscribe to all events the controllable needs
192 * @param player the player that shall controll this Playable
193 */
194bool Playable::setPlayer(Player* player)
195{
196  // if we already have a Player inside do nothing
197  if (this->currentPlayer != NULL && player != NULL)
198  {
199    return false;
200  }
201
202  // eject the Player if player == NULL
203  if (this->currentPlayer != NULL && player == NULL)
204  {
205    PRINTF(4)("Player gets ejected\n");
206
207    // unsubscibe all events.
208    EventHandler* evh = EventHandler::getInstance();
209    std::list<int>::iterator ev;
210    for (ev = this->events.begin(); ev != events.end(); ev++)
211      evh->unsubscribe( ES_GAME, (*ev));
212
213    // leave the entity
214    this->leave();
215
216    // eject the current Player.
217    Player* ejectPlayer = this->currentPlayer;
218    this->currentPlayer = NULL;
219    // eject the Player.
220    ejectPlayer->setPlayable(NULL);
221
222    return true;
223  }
224
225  // get the new Player inside
226  if (this->currentPlayer == NULL && player != NULL)
227  {
228    PRINTF(4)("New Player gets inside\n");
229    this->currentPlayer = player;
230    if (this->currentPlayer->getPlayable() != this)
231      this->currentPlayer->setPlayable(this);
232
233    /*EventHandler*/
234    EventHandler* evh = EventHandler::getInstance();
235    std::list<int>::iterator ev;
236    for (ev = this->events.begin(); ev != events.end(); ev++)
237      evh->subscribe(player, ES_GAME, (*ev));
238
239    this->enter();
240    return true;
241  }
242
243  return false;
244}
245
246
247bool Playable::pickup(PowerUp* powerUp)
248{
249  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
250    return dynamic_cast<WeaponPowerUp*>(powerUp)->process(this->getWeaponManager());
251  }
252  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
253    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
254    switch(ppu->getType()) {
255      case POWERUP_PARAM_HEALTH:
256        this->increaseHealth(ppu->getValue());
257        return true;
258      case POWERUP_PARAM_MAX_HEALTH:
259        this->increaseHealthMax(ppu->getValue());
260        return true;
261    }
262  }
263  return false;
264}
265
266/**
267 * add an event to the event list of events this Playable can capture
268 * @param eventType the Type of event to add
269 */
270void Playable::registerEvent(int eventType)
271{
272  this->events.push_back(eventType);
273
274  if (this->currentPlayer != NULL)
275    EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType);
276}
277
278/**
279 * remove an event to the event list this Playable can capture.
280 * @param event the event to unregister.
281 */
282void Playable::unregisterEvent(int eventType)
283{
284  this->events.remove(eventType);
285
286  if (this->currentPlayer != NULL)
287    EventHandler::getInstance()->unsubscribe(ES_GAME, eventType);
288}
289
290/**
291 * @brief ticks a Playable
292 * @param dt: the passed time since the last Tick
293 */
294void Playable::tick(float dt)
295{
296  this->weaponMan->tick(dt);
297  if (this->bFire)
298    weaponMan->fire();
299}
300
301
302/**
303 * @brief processes Playable events.
304 * @param event the Captured Event.
305 */
306void Playable::process(const Event &event)
307{
308  if( event.type == KeyMapper::PEV_FIRE1)
309    this->bFire = event.bPressed;
310  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
311  {
312    this->nextWeaponConfig();
313  }
314  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
315    this->previousWeaponConfig();
316}
317
318
319
320void  Playable::attachCamera()
321{
322  State::getCameraNode()->setParentSoft(this);
323  State::getCameraTargetNode()->setParentSoft(this);
324
325}
326
327
328
329
330void  Playable::detachCamera()
331{
332}
333
334#define DATA_FLAGS    1
335#define DATA_SCORE    2
336
337#define FLAGS_bFire   1
338
339int Playable::writeSync( const byte * data, int length, int sender )
340{
341  SYNCHELP_READ_BEGIN();
342
343  byte b;
344  SYNCHELP_READ_BYTE( b, NWT_PL_B );
345
346  byte flags;
347
348  if ( b == DATA_FLAGS )
349  {
350    SYNCHELP_READ_BYTE( flags, NWT_PL_FLAGS );
351
352    bFire = (flags & FLAGS_bFire) != 0;
353
354    return SYNCHELP_READ_N;
355  }
356
357  if ( b == DATA_SCORE )
358  {
359    int newScore;
360    SYNCHELP_READ_BYTE( newScore, NWT_PL_SCORE );
361    setScore( newScore );
362
363    return SYNCHELP_READ_N;
364  }
365
366  return SYNCHELP_READ_N;
367}
368
369int Playable::readSync( byte * data, int maxLength )
370{
371  SYNCHELP_WRITE_BEGIN();
372
373  if ( score != oldScore && isServer() )
374  {
375    SYNCHELP_WRITE_BYTE( DATA_SCORE, NWT_PL_B);
376    SYNCHELP_WRITE_INT( score, NWT_PL_SCORE );
377    oldScore = score;
378
379    return SYNCHELP_WRITE_N;
380  }
381
382  byte flags = 0;
383
384  if ( bFire )
385    flags |= FLAGS_bFire;
386
387
388  SYNCHELP_WRITE_BYTE( DATA_FLAGS, NWT_PL_B);
389  SYNCHELP_WRITE_BYTE( flags, NWT_PL_FLAGS );
390  oldFlags = flags;
391
392
393  return SYNCHELP_WRITE_N;
394}
395
396bool Playable::needsReadSync( )
397{
398  if ( score != oldScore && isServer() )
399    return true;
400
401  byte flags = 0;
402
403  if ( bFire )
404    flags |= FLAGS_bFire;
405
406  return flags!=oldFlags;
407}
Note: See TracBrowser for help on using the repository browser.