Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7337 was 7337, checked in by bensch, 20 years ago

orxonox/trunk: simple useless stuff

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