Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7276 was 7193, checked in by bensch, 19 years ago

orxonox/trunk: new style for resources (prework/movement)

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