Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7341 was 7339, checked in by bensch, 19 years ago

some nice fixes

File size: 9.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"
[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()
[7338]41    : weaponMan(this),
42    supportedPlaymodes(Playable::Full3D),
43    playmode(Playable::Full3D)
[5838]44{
[6442]45  this->setClassID(CL_PLAYABLE, "Playable");
46  PRINTF(4)("PLAYABLE INIT\n");
47
48  this->toList(OM_GROUP_01);
49
50  // the reference to the Current Player is NULL, because we dont have one at the beginning.
51  this->currentPlayer = NULL;
[6695]52
[6804]53  this->bFire = false;
[6868]54  this->oldFlags = 0;
[6804]55
[6695]56  this->setSynchronized(true);
[6959]57
58  this->score = 0;
59  this->oldScore = 0;
60
[7118]61  this->bDead = false;
[5838]62}
63
[6695]64
65
[5875]66Playable::~Playable()
[5838]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
[7337]79  LoadParam(root, "abs-dir", this, Playable, setStartDirection);
[7092]80}
81
[6443]82void Playable::addWeapon(Weapon* weapon, int configID, int slotID)
83{
[7337]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{
[7337]92  this->weaponMan.removeWeapon(weapon);
[6443]93
[7338]94  this->weaponConfigChanged();
[6443]95}
96
[6695]97
[6444]98void Playable::nextWeaponConfig()
99{
[7337]100  this->weaponMan.nextWeaponConfig();
[7338]101  this->weaponConfigChanged();
[6444]102}
[6443]103
[6695]104
[6444]105void Playable::previousWeaponConfig()
106{
[7337]107  this->weaponMan.previousWeaponConfig();
[7338]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)
[7338]123{}
124
125/**
126 * @brief sets the Playmode
127 * @param playmode the Playmode
128 * @returns true on success, false otherwise
129 */
[7339]130bool Playable::setPlaymode(Playable::Playmode playmode)
[7173]131{
[7338]132  if (!this->playmodeSupported(playmode))
133    return false;
134  else
135    this->playmode = playmode;
[7173]136}
137
138
[5872]139/**
[6436]140 * @brief helps us colliding Playables
141 */
142void Playable::collidesWith(WorldEntity* entity, const Vector& location)
143{
[7072]144  if (entity == collider)
145    return;
146  collider = entity;
147
[7121]148  if ( entity->isA(CL_PROJECTILE) && ( !State::isOnline() || SharedNetworkData::getInstance()->isGameServer() ) )
[6966]149  {
[7072]150    this->decreaseHealth(entity->getHealth() *(float)rand()/(float)RAND_MAX);
[6966]151    // EXTREME HACK
[7072]152    if (this->getHealth() <= 0.0f)
[6966]153    {
154      this->die();
155    }
156  }
157}
[6436]158
[6966]159
[7044]160void Playable::respawn()
[6966]161{
[7044]162  PRINTF(0)("Playable respawn\n");
163  // only if this is the spaceship of the player
164  if( this == State::getPlayer()->getPlayable())
165    State::getGameRules()->onPlayerSpawn();
[6966]166
[7085]167
[7082]168  if( this->getOwner() % 2 == 0)
169  {
[7338]170    //     this->toList(OM_GROUP_00);
[7082]171    this->setAbsCoor(213.37, 57.71, -47.98);
[7100]172    this->setAbsDir(0, 0, 1, 0);
[7082]173  }
[6966]174  else
[7099]175  { // red team
[7338]176    //     this->toList(OM_GROUP_01);
[7082]177    this->setAbsCoor(-314.450, 40.701, 83.554);
[7100]178    this->setAbsDir(1.0, -0.015, -0.012, 0.011);
[7082]179  }
[7118]180  this->reset();
181  this->bDead = false;
[6436]182}
183
[6966]184
[7088]185
[7044]186void Playable::die()
187{
[7119]188  Explosion::explode(dynamic_cast<PNode*>(this), Vector(1.0f, 1.0f, 1.0f));
189
190
[7118]191  if( !this->bDead)
192  {
193    PRINTF(0)("Playable dies\n");
[7338]194    // only if this is the spaceship of the player
[7118]195    if (State::isOnline())
196    {
197      if( this == State::getPlayer()->getPlayable())
198        State::getGameRules()->onPlayerDeath();
[7044]199
[7338]200      //     this->toList(OM_GROUP_05);
201      //HACK: moves the entity to an unknown place far far away: in the future, GameRules will look for that
[7118]202      this->setAbsCoor(-2000.0, -2000.0, -2000.0);
[7078]203
[7338]204      //explosion hack
[7118]205
206    }
207    this->bDead = true;
[7072]208  }
[7044]209}
210
211
[6436]212/**
[5872]213 * subscribe to all events the controllable needs
[5898]214 * @param player the player that shall controll this Playable
[5872]215 */
[6986]216bool Playable::setPlayer(Player* player)
[5872]217{
[6986]218  // if we already have a Player inside do nothing
219  if (this->currentPlayer != NULL && player != NULL)
[5872]220  {
[5895]221    return false;
[5875]222  }
[6986]223
224  // eject the Player if player == NULL
225  if (this->currentPlayer != NULL && player == NULL)
[5895]226  {
[6987]227    PRINTF(4)("Player gets ejected\n");
[6986]228
229    // unsubscibe all events.
[5895]230    EventHandler* evh = EventHandler::getInstance();
[7338]231    std::vector<int>::iterator ev;
[5895]232    for (ev = this->events.begin(); ev != events.end(); ev++)
[6986]233      evh->unsubscribe( ES_GAME, (*ev));
234
235    // leave the entity
236    this->leave();
237
238    // eject the current Player.
239    Player* ejectPlayer = this->currentPlayer;
240    this->currentPlayer = NULL;
241    // eject the Player.
242    ejectPlayer->setPlayable(NULL);
243
[5895]244    return true;
245  }
[5889]246
[6986]247  // get the new Player inside
248  if (this->currentPlayer == NULL && player != NULL)
[5896]249  {
[6987]250    PRINTF(4)("New Player gets inside\n");
[6986]251    this->currentPlayer = player;
252    if (this->currentPlayer->getPlayable() != this)
253      this->currentPlayer->setPlayable(this);
[5896]254
255    /*EventHandler*/
256    EventHandler* evh = EventHandler::getInstance();
[7338]257    std::vector<int>::iterator ev;
[5896]258    for (ev = this->events.begin(); ev != events.end(); ev++)
[6986]259      evh->subscribe(player, ES_GAME, (*ev));
[5896]260
[6986]261    this->enter();
[5896]262    return true;
263  }
[6986]264
265  return false;
[5896]266}
267
[6986]268
[6547]269bool Playable::pickup(PowerUp* powerUp)
270{
[7338]271  if(powerUp->isA(CL_WEAPON_POWER_UP))
272  {
[7337]273    return dynamic_cast<WeaponPowerUp*>(powerUp)->process(&this->getWeaponManager());
[6547]274  }
[7338]275  else if(powerUp->isA(CL_PARAM_POWER_UP))
276  {
[6547]277    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
[7338]278    switch(ppu->getType())
279    {
[6547]280      case POWERUP_PARAM_HEALTH:
[6700]281        this->increaseHealth(ppu->getValue());
[6547]282        return true;
283      case POWERUP_PARAM_MAX_HEALTH:
[6700]284        this->increaseHealthMax(ppu->getValue());
[6547]285        return true;
286    }
287  }
288  return false;
289}
290
[5896]291/**
[5898]292 * add an event to the event list of events this Playable can capture
293 * @param eventType the Type of event to add
[5889]294 */
[5896]295void Playable::registerEvent(int eventType)
[5889]296{
297  this->events.push_back(eventType);
298
[5896]299  if (this->currentPlayer != NULL)
300    EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType);
[5889]301}
302
[5896]303/**
[7339]304 * @brief remove an event to the event list this Playable can capture.
[5898]305 * @param event the event to unregister.
[5896]306 */
307void Playable::unregisterEvent(int eventType)
308{
[7338]309  std::vector<int>::iterator rmEvent = std::find(this->events.begin(), this->events.end(), eventType);
310  this->events.erase(rmEvent);
[5889]311
[5896]312  if (this->currentPlayer != NULL)
313    EventHandler::getInstance()->unsubscribe(ES_GAME, eventType);
314}
[5889]315
[6804]316/**
317 * @brief ticks a Playable
318 * @param dt: the passed time since the last Tick
319 */
320void Playable::tick(float dt)
321{
[7337]322  this->weaponMan.tick(dt);
[6804]323  if (this->bFire)
[7337]324    weaponMan.fire();
[6804]325}
[5896]326
[6804]327
328/**
329 * @brief processes Playable events.
330 * @param event the Captured Event.
331 */
332void Playable::process(const Event &event)
333{
334  if( event.type == KeyMapper::PEV_FIRE1)
335    this->bFire = event.bPressed;
336  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
337  {
338    this->nextWeaponConfig();
339  }
340  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
341    this->previousWeaponConfig();
342}
343
344
345
[6241]346void  Playable::attachCamera()
347{
[7014]348  State::getCameraNode()->setParentSoft(this);
349  State::getCameraTargetNode()->setParentSoft(this);
[6241]350
351}
352
353
[6804]354
355
[6241]356void  Playable::detachCamera()
[7338]357{}
[6868]358
[6959]359#define DATA_FLAGS    1
360#define DATA_SCORE    2
361
[6868]362#define FLAGS_bFire   1
363
364int Playable::writeSync( const byte * data, int length, int sender )
365{
366  SYNCHELP_READ_BEGIN();
[7010]367
[6994]368  byte b;
369  SYNCHELP_READ_BYTE( b, NWT_PL_B );
[7010]370
[6868]371  byte flags;
[7010]372
[6994]373  if ( b == DATA_FLAGS )
374  {
375    SYNCHELP_READ_BYTE( flags, NWT_PL_FLAGS );
[6871]376
[6994]377    bFire = (flags & FLAGS_bFire) != 0;
[7010]378
[6994]379    return SYNCHELP_READ_N;
380  }
[7010]381
[6994]382  if ( b == DATA_SCORE )
383  {
384    int newScore;
385    SYNCHELP_READ_BYTE( newScore, NWT_PL_SCORE );
386    setScore( newScore );
[7010]387
[6994]388    return SYNCHELP_READ_N;
389  }
[6871]390
[6868]391  return SYNCHELP_READ_N;
392}
393
394int Playable::readSync( byte * data, int maxLength )
395{
396  SYNCHELP_WRITE_BEGIN();
[7010]397
[6994]398  if ( score != oldScore && isServer() )
399  {
400    SYNCHELP_WRITE_BYTE( DATA_SCORE, NWT_PL_B);
401    SYNCHELP_WRITE_INT( score, NWT_PL_SCORE );
402    oldScore = score;
[7010]403
[6994]404    return SYNCHELP_WRITE_N;
405  }
[7010]406
[6868]407  byte flags = 0;
[6871]408
[6868]409  if ( bFire )
410    flags |= FLAGS_bFire;
411
[6871]412
[6994]413  SYNCHELP_WRITE_BYTE( DATA_FLAGS, NWT_PL_B);
[6868]414  SYNCHELP_WRITE_BYTE( flags, NWT_PL_FLAGS );
415  oldFlags = flags;
416
[6871]417
[6868]418  return SYNCHELP_WRITE_N;
419}
420
421bool Playable::needsReadSync( )
422{
[6994]423  if ( score != oldScore && isServer() )
424    return true;
[6959]425
[6868]426  byte flags = 0;
[6871]427
[6868]428  if ( bFire )
429    flags |= FLAGS_bFire;
[6871]430
[6868]431  return flags!=oldFlags;
432}
[7339]433
434
435Playable::Playmode Playable::stringToPlaymode(const std::string& playmode)
436{
437  if (playmode == "Vertical")
438    return Playable::Vertical;
439  if (playmode == "Horizontal")
440    return Playable::Horizontal;
441  if (playmode == "FromBehind")
442    return Playable::FromBehind;
443  if (playmode == "Full3D")
444    return Playable::Full3D;
445
446  return Playable::Full3D;
447}
448
449const char* Playable::playmodeToString(Playable::Playmode playmode)
450{
451  switch(playmode)
452  {
453    case Playable::Vertical:
454      return "Vertical";
455    case Playable::Horizontal:
456      return "Horizontal";
457    case Playable::FromBehind:
458      return "FromBehind";
459    case Playable::Full3D:
460      return "Full3D";
461
462    default:
463      return "Full3D";
464  }
465
466}
Note: See TracBrowser for help on using the repository browser.