Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: player group handling

File size: 8.4 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
[7044]150  this->setAbsCoor(0.0, 0.0, 0.0);
[6966]151
152  if( this->getOwner()%2 == 0)
153    this->toList(OM_GROUP_00);
154  else
155    this->toList(OM_GROUP_01);
[6436]156}
157
[6966]158
[7044]159void Playable::die()
160{
161  PRINTF(0)("Playable dies\n");
162  // only if this is the spaceship of the player
[7072]163  if (State::isOnline())
164  {
[7078]165    if( this == State::getPlayer()->getPlayable())
166      State::getGameRules()->onPlayerDeath();
[7044]167
168
[7078]169    this->toList(OM_DEAD);
170  //HACK: moves the entity to an unknown place far far away: in the future, GameRules will look for that
171    this->setAbsCoor(-2000.0, -2000.0, -2000.0);
172
[7044]173  //explosion hack
[7078]174    this->emitter->setSystem(explosionParticles);
175    this->setAbsCoor(0, 0, 0);
176    this->emitter->setSystem(NULL);
[7072]177  }
[7044]178}
179
180
[6436]181/**
[5872]182 * subscribe to all events the controllable needs
[5898]183 * @param player the player that shall controll this Playable
[5872]184 */
[6986]185bool Playable::setPlayer(Player* player)
[5872]186{
[6986]187  // if we already have a Player inside do nothing
188  if (this->currentPlayer != NULL && player != NULL)
[5872]189  {
[5895]190    return false;
[5875]191  }
[6986]192
193  // eject the Player if player == NULL
194  if (this->currentPlayer != NULL && player == NULL)
[5895]195  {
[6987]196    PRINTF(4)("Player gets ejected\n");
[6986]197
198    // unsubscibe all events.
[5895]199    EventHandler* evh = EventHandler::getInstance();
200    std::list<int>::iterator ev;
201    for (ev = this->events.begin(); ev != events.end(); ev++)
[6986]202      evh->unsubscribe( ES_GAME, (*ev));
203
204    // leave the entity
205    this->leave();
206
207    // eject the current Player.
208    Player* ejectPlayer = this->currentPlayer;
209    this->currentPlayer = NULL;
210    // eject the Player.
211    ejectPlayer->setPlayable(NULL);
212
[5895]213    return true;
214  }
[5889]215
[6986]216  // get the new Player inside
217  if (this->currentPlayer == NULL && player != NULL)
[5896]218  {
[6987]219    PRINTF(4)("New Player gets inside\n");
[6986]220    this->currentPlayer = player;
221    if (this->currentPlayer->getPlayable() != this)
222      this->currentPlayer->setPlayable(this);
[5896]223
224    /*EventHandler*/
225    EventHandler* evh = EventHandler::getInstance();
226    std::list<int>::iterator ev;
227    for (ev = this->events.begin(); ev != events.end(); ev++)
[6986]228      evh->subscribe(player, ES_GAME, (*ev));
[5896]229
[6986]230    this->enter();
[5896]231    return true;
232  }
[6986]233
234  return false;
[5896]235}
236
[6986]237
[6547]238bool Playable::pickup(PowerUp* powerUp)
239{
240  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
[6973]241    return dynamic_cast<WeaponPowerUp*>(powerUp)->process(this->getWeaponManager());
[6547]242  }
243  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
244    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
245    switch(ppu->getType()) {
246      case POWERUP_PARAM_HEALTH:
[6700]247        this->increaseHealth(ppu->getValue());
[6547]248        return true;
249      case POWERUP_PARAM_MAX_HEALTH:
[6700]250        this->increaseHealthMax(ppu->getValue());
[6547]251        return true;
252    }
253  }
254  return false;
255}
256
[5896]257/**
[5898]258 * add an event to the event list of events this Playable can capture
259 * @param eventType the Type of event to add
[5889]260 */
[5896]261void Playable::registerEvent(int eventType)
[5889]262{
263  this->events.push_back(eventType);
264
[5896]265  if (this->currentPlayer != NULL)
266    EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType);
[5889]267}
268
[5896]269/**
[5898]270 * remove an event to the event list this Playable can capture.
271 * @param event the event to unregister.
[5896]272 */
273void Playable::unregisterEvent(int eventType)
274{
[5902]275  this->events.remove(eventType);
[5889]276
[5896]277  if (this->currentPlayer != NULL)
278    EventHandler::getInstance()->unsubscribe(ES_GAME, eventType);
279}
[5889]280
[6804]281/**
282 * @brief ticks a Playable
283 * @param dt: the passed time since the last Tick
284 */
285void Playable::tick(float dt)
286{
287  this->weaponMan->tick(dt);
288  if (this->bFire)
289    weaponMan->fire();
290}
[5896]291
[6804]292
293/**
294 * @brief processes Playable events.
295 * @param event the Captured Event.
296 */
297void Playable::process(const Event &event)
298{
299  if( event.type == KeyMapper::PEV_FIRE1)
300    this->bFire = event.bPressed;
301  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
302  {
303    this->nextWeaponConfig();
304  }
305  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
306    this->previousWeaponConfig();
307}
308
309
310
[6241]311void  Playable::attachCamera()
312{
[7014]313  State::getCameraNode()->setParentSoft(this);
314  State::getCameraTargetNode()->setParentSoft(this);
[6241]315
316}
317
318
[6804]319
320
[6241]321void  Playable::detachCamera()
322{
323}
[6868]324
[6959]325#define DATA_FLAGS    1
326#define DATA_SCORE    2
327
[6868]328#define FLAGS_bFire   1
329
330int Playable::writeSync( const byte * data, int length, int sender )
331{
332  SYNCHELP_READ_BEGIN();
[7010]333
[6994]334  byte b;
335  SYNCHELP_READ_BYTE( b, NWT_PL_B );
[7010]336
[6868]337  byte flags;
[7010]338
[6994]339  if ( b == DATA_FLAGS )
340  {
341    SYNCHELP_READ_BYTE( flags, NWT_PL_FLAGS );
[6871]342
[6994]343    bFire = (flags & FLAGS_bFire) != 0;
[7010]344
[6994]345    return SYNCHELP_READ_N;
346  }
[7010]347
[6994]348  if ( b == DATA_SCORE )
349  {
350    int newScore;
351    SYNCHELP_READ_BYTE( newScore, NWT_PL_SCORE );
352    setScore( newScore );
[7010]353
[6994]354    return SYNCHELP_READ_N;
355  }
[6871]356
[6868]357  return SYNCHELP_READ_N;
358}
359
360int Playable::readSync( byte * data, int maxLength )
361{
362  SYNCHELP_WRITE_BEGIN();
[7010]363
[6994]364  if ( score != oldScore && isServer() )
365  {
366    SYNCHELP_WRITE_BYTE( DATA_SCORE, NWT_PL_B);
367    SYNCHELP_WRITE_INT( score, NWT_PL_SCORE );
368    oldScore = score;
[7010]369
[6994]370    return SYNCHELP_WRITE_N;
371  }
[7010]372
[6868]373  byte flags = 0;
[6871]374
[6868]375  if ( bFire )
376    flags |= FLAGS_bFire;
377
[6871]378
[6994]379  SYNCHELP_WRITE_BYTE( DATA_FLAGS, NWT_PL_B);
[6868]380  SYNCHELP_WRITE_BYTE( flags, NWT_PL_FLAGS );
381  oldFlags = flags;
382
[6871]383
[6868]384  return SYNCHELP_WRITE_N;
385}
386
387bool Playable::needsReadSync( )
388{
[6994]389  if ( score != oldScore && isServer() )
390    return true;
[6959]391
[6868]392  byte flags = 0;
[6871]393
[6868]394  if ( bFire )
395    flags |= FLAGS_bFire;
[6871]396
[6868]397  return flags!=oldFlags;
398}
Note: See TracBrowser for help on using the repository browser.