Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7126 was 7121, checked in by rennerc, 18 years ago

fix: only gameserver checks for collision

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