Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7086 was 7085, checked in by patrick, 19 years ago

trunk: entity is reset now after death

File size: 8.6 KB
RevLine 
[5838]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:
[5841]12   main-programmer: Silvan Nellen
13   co-programmer: Benjamin Knecht
[5838]14*/
15
[5881]16
[5838]17#include "playable.h"
[5895]18
19#include "weapons/weapon_manager.h"
[5875]20#include "event_handler.h"
21#include "player.h"
[6241]22#include "state.h"
[5838]23
[6700]24#include "world_entities/projectiles/projectile.h"
25
[6547]26#include "power_ups/weapon_power_up.h"
27#include "power_ups/param_power_up.h"
[5872]28
[7044]29#include "game_rules.h"
[6547]30
[6959]31#include "dot_emitter.h"
32#include "sprite_particles.h"
33
34
[5838]35Playable::Playable()
36{
[6442]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;
[6695]45
[6804]46  this->bFire = false;
[6868]47  this->oldFlags = 0;
[6804]48
[6695]49  this->setSynchronized(true);
[6959]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);
[5838]70}
71
[6695]72
73
[5875]74Playable::~Playable()
[5838]75{
[5881]76  delete this->weaponMan;
[5895]77
[6986]78  // THE DERIVED CLASS MUST UNSUBSCRIBE THE PLAYER THROUGH
79  // this->setPlayer(NULL);
80  // IN ITS DESTRUCTOR.
81  assert(this->currentPlayer == NULL);
[5875]82}
83
[5898]84
[6443]85void Playable::addWeapon(Weapon* weapon, int configID, int slotID)
86{
[6676]87  this->weaponMan->addWeapon(weapon, configID, slotID);
[6443]88
[6578]89  this->weaponConfigChanged();
[6443]90}
91
[6695]92
[6443]93void Playable::removeWeapon(Weapon* weapon)
94{
95  this->weaponMan->removeWeapon(weapon);
96
[6568]97    this->weaponConfigChanged();
[6443]98}
99
[6695]100
[6444]101void Playable::nextWeaponConfig()
102{
103  this->weaponMan->nextWeaponConfig();
[6568]104    this->weaponConfigChanged();
[6444]105}
[6443]106
[6695]107
[6444]108void Playable::previousWeaponConfig()
109{
110  this->weaponMan->previousWeaponConfig();
[6568]111    this->weaponConfigChanged();
[6444]112}
113
[6695]114
[6568]115void Playable::weaponConfigChanged()
116{
[6578]117  if (this->currentPlayer != NULL)
118    this->currentPlayer->weaponConfigChanged();
[6568]119}
[6444]120
[6695]121
[5872]122/**
[6436]123 * @brief helps us colliding Playables
124 */
125void Playable::collidesWith(WorldEntity* entity, const Vector& location)
126{
[7072]127  if (entity == collider)
128    return;
129  collider = entity;
130
131  if (entity->isA(CL_PROJECTILE))
[6966]132  {
[7072]133    this->decreaseHealth(entity->getHealth() *(float)rand()/(float)RAND_MAX);
[6966]134    // EXTREME HACK
[7072]135    if (this->getHealth() <= 0.0f)
[6966]136    {
137      this->die();
138    }
139  }
140}
[6436]141
[6966]142
[7044]143void Playable::respawn()
[6966]144{
[7044]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();
[6966]149
[7085]150  this->reset();
151
[7082]152  if( this->getOwner() % 2 == 0)
153  {
[6966]154    this->toList(OM_GROUP_00);
[7082]155    this->setAbsCoor(213.37, 57.71, -47.98);
156    this->setAbsDir(Quaternion(0.16, 0.98, -0.10));
157  }
[6966]158  else
[7082]159  {
[6966]160    this->toList(OM_GROUP_01);
[7082]161    this->setAbsCoor(-314.450, 40.701, 83.554);
[7085]162    this->setAbsDir(Quaternion(0.0, 0.0, 0.0));
[7082]163  }
[6436]164}
165
[6966]166
[7044]167void Playable::die()
168{
169  PRINTF(0)("Playable dies\n");
170  // only if this is the spaceship of the player
[7072]171  if (State::isOnline())
172  {
[7078]173    if( this == State::getPlayer()->getPlayable())
174      State::getGameRules()->onPlayerDeath();
[7044]175
176
[7079]177    this->toList(OM_DEAD_TICK);
[7078]178  //HACK: moves the entity to an unknown place far far away: in the future, GameRules will look for that
179    this->setAbsCoor(-2000.0, -2000.0, -2000.0);
180
[7044]181  //explosion hack
[7078]182    this->emitter->setSystem(explosionParticles);
183    this->emitter->setSystem(NULL);
[7072]184  }
[7044]185}
186
187
[6436]188/**
[5872]189 * subscribe to all events the controllable needs
[5898]190 * @param player the player that shall controll this Playable
[5872]191 */
[6986]192bool Playable::setPlayer(Player* player)
[5872]193{
[6986]194  // if we already have a Player inside do nothing
195  if (this->currentPlayer != NULL && player != NULL)
[5872]196  {
[5895]197    return false;
[5875]198  }
[6986]199
200  // eject the Player if player == NULL
201  if (this->currentPlayer != NULL && player == NULL)
[5895]202  {
[6987]203    PRINTF(4)("Player gets ejected\n");
[6986]204
205    // unsubscibe all events.
[5895]206    EventHandler* evh = EventHandler::getInstance();
207    std::list<int>::iterator ev;
208    for (ev = this->events.begin(); ev != events.end(); ev++)
[6986]209      evh->unsubscribe( ES_GAME, (*ev));
210
211    // leave the entity
212    this->leave();
213
214    // eject the current Player.
215    Player* ejectPlayer = this->currentPlayer;
216    this->currentPlayer = NULL;
217    // eject the Player.
218    ejectPlayer->setPlayable(NULL);
219
[5895]220    return true;
221  }
[5889]222
[6986]223  // get the new Player inside
224  if (this->currentPlayer == NULL && player != NULL)
[5896]225  {
[6987]226    PRINTF(4)("New Player gets inside\n");
[6986]227    this->currentPlayer = player;
228    if (this->currentPlayer->getPlayable() != this)
229      this->currentPlayer->setPlayable(this);
[5896]230
231    /*EventHandler*/
232    EventHandler* evh = EventHandler::getInstance();
233    std::list<int>::iterator ev;
234    for (ev = this->events.begin(); ev != events.end(); ev++)
[6986]235      evh->subscribe(player, ES_GAME, (*ev));
[5896]236
[6986]237    this->enter();
[5896]238    return true;
239  }
[6986]240
241  return false;
[5896]242}
243
[6986]244
[6547]245bool Playable::pickup(PowerUp* powerUp)
246{
247  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
[6973]248    return dynamic_cast<WeaponPowerUp*>(powerUp)->process(this->getWeaponManager());
[6547]249  }
250  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
251    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
252    switch(ppu->getType()) {
253      case POWERUP_PARAM_HEALTH:
[6700]254        this->increaseHealth(ppu->getValue());
[6547]255        return true;
256      case POWERUP_PARAM_MAX_HEALTH:
[6700]257        this->increaseHealthMax(ppu->getValue());
[6547]258        return true;
259    }
260  }
261  return false;
262}
263
[5896]264/**
[5898]265 * add an event to the event list of events this Playable can capture
266 * @param eventType the Type of event to add
[5889]267 */
[5896]268void Playable::registerEvent(int eventType)
[5889]269{
270  this->events.push_back(eventType);
271
[5896]272  if (this->currentPlayer != NULL)
273    EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType);
[5889]274}
275
[5896]276/**
[5898]277 * remove an event to the event list this Playable can capture.
278 * @param event the event to unregister.
[5896]279 */
280void Playable::unregisterEvent(int eventType)
281{
[5902]282  this->events.remove(eventType);
[5889]283
[5896]284  if (this->currentPlayer != NULL)
285    EventHandler::getInstance()->unsubscribe(ES_GAME, eventType);
286}
[5889]287
[6804]288/**
289 * @brief ticks a Playable
290 * @param dt: the passed time since the last Tick
291 */
292void Playable::tick(float dt)
293{
294  this->weaponMan->tick(dt);
295  if (this->bFire)
296    weaponMan->fire();
297}
[5896]298
[6804]299
300/**
301 * @brief processes Playable events.
302 * @param event the Captured Event.
303 */
304void Playable::process(const Event &event)
305{
306  if( event.type == KeyMapper::PEV_FIRE1)
307    this->bFire = event.bPressed;
308  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
309  {
310    this->nextWeaponConfig();
311  }
312  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
313    this->previousWeaponConfig();
314}
315
316
317
[6241]318void  Playable::attachCamera()
319{
[7014]320  State::getCameraNode()->setParentSoft(this);
321  State::getCameraTargetNode()->setParentSoft(this);
[6241]322
323}
324
325
[6804]326
327
[6241]328void  Playable::detachCamera()
329{
330}
[6868]331
[6959]332#define DATA_FLAGS    1
333#define DATA_SCORE    2
334
[6868]335#define FLAGS_bFire   1
336
337int Playable::writeSync( const byte * data, int length, int sender )
338{
339  SYNCHELP_READ_BEGIN();
[7010]340
[6994]341  byte b;
342  SYNCHELP_READ_BYTE( b, NWT_PL_B );
[7010]343
[6868]344  byte flags;
[7010]345
[6994]346  if ( b == DATA_FLAGS )
347  {
348    SYNCHELP_READ_BYTE( flags, NWT_PL_FLAGS );
[6871]349
[6994]350    bFire = (flags & FLAGS_bFire) != 0;
[7010]351
[6994]352    return SYNCHELP_READ_N;
353  }
[7010]354
[6994]355  if ( b == DATA_SCORE )
356  {
357    int newScore;
358    SYNCHELP_READ_BYTE( newScore, NWT_PL_SCORE );
359    setScore( newScore );
[7010]360
[6994]361    return SYNCHELP_READ_N;
362  }
[6871]363
[6868]364  return SYNCHELP_READ_N;
365}
366
367int Playable::readSync( byte * data, int maxLength )
368{
369  SYNCHELP_WRITE_BEGIN();
[7010]370
[6994]371  if ( score != oldScore && isServer() )
372  {
373    SYNCHELP_WRITE_BYTE( DATA_SCORE, NWT_PL_B);
374    SYNCHELP_WRITE_INT( score, NWT_PL_SCORE );
375    oldScore = score;
[7010]376
[6994]377    return SYNCHELP_WRITE_N;
378  }
[7010]379
[6868]380  byte flags = 0;
[6871]381
[6868]382  if ( bFire )
383    flags |= FLAGS_bFire;
384
[6871]385
[6994]386  SYNCHELP_WRITE_BYTE( DATA_FLAGS, NWT_PL_B);
[6868]387  SYNCHELP_WRITE_BYTE( flags, NWT_PL_FLAGS );
388  oldFlags = flags;
389
[6871]390
[6868]391  return SYNCHELP_WRITE_N;
392}
393
394bool Playable::needsReadSync( )
395{
[6994]396  if ( score != oldScore && isServer() )
397    return true;
[6959]398
[6868]399  byte flags = 0;
[6871]400
[6868]401  if ( bFire )
402    flags |= FLAGS_bFire;
[6871]403
[6868]404  return flags!=oldFlags;
405}
Note: See TracBrowser for help on using the repository browser.