Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8249 was 8147, checked in by bensch, 18 years ago

orxonox/trunk: merged the network branche back here
merged with command:
svn merge -r8070:HEAD https://svn.orxonox.net/orxonox/branches/network .
no conflicts

File size: 12.9 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
[7868]19#include "key_mapper.h"
20
[5875]21#include "player.h"
[6241]22#include "state.h"
[7347]23#include "camera.h"
24
[7193]25#include "util/loading/load_param.h"
[5838]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"
[7482]38#include "kill.cc"
[6959]39
[7350]40#include "shell_command.h"
[7356]41SHELL_COMMAND_STATIC(orxoWeapon, Playable, Playable::addSomeWeapons_CHEAT)
42  ->setAlias("orxoWeapon");
[7118]43
[7350]44
[5838]45Playable::Playable()
[7338]46    : weaponMan(this),
47    supportedPlaymodes(Playable::Full3D),
48    playmode(Playable::Full3D)
[5838]49{
[6442]50  this->setClassID(CL_PLAYABLE, "Playable");
51  PRINTF(4)("PLAYABLE INIT\n");
52
53  this->toList(OM_GROUP_01);
54
55  // the reference to the Current Player is NULL, because we dont have one at the beginning.
56  this->currentPlayer = NULL;
[6695]57
[6804]58  this->bFire = false;
[6868]59  this->oldFlags = 0;
[6804]60
[6695]61  this->setSynchronized(true);
[6959]62
63  this->score = 0;
[7713]64  this->collider = NULL;
[6959]65
[7118]66  this->bDead = false;
[7954]67
68  registerVar( new SynchronizeableInt( &score, &score, "score" ) );
69  registerVar( new SynchronizeableBool( &bFire, &bFire, "bFire", PERMISSION_OWNER));
[5838]70}
71
[6695]72
[7346]73/**
74 * @brief destroys the Playable
75 */
[5875]76Playable::~Playable()
[5838]77{
[6986]78  // THE DERIVED CLASS MUST UNSUBSCRIBE THE PLAYER THROUGH
79  // this->setPlayer(NULL);
80  // IN ITS DESTRUCTOR.
[8147]81 
[6986]82  assert(this->currentPlayer == NULL);
[5875]83}
84
[7346]85/**
86 * @brief loads Playable parameters onto the Playable
87 * @param root the XML-root to load from
88 */
[7092]89void Playable::loadParams(const TiXmlElement* root)
90{
91  WorldEntity::loadParams(root);
92
[7346]93  LoadParam(root, "abs-dir", this, Playable, setPlayDirection);
[7092]94}
95
[7346]96/**
97 * @brief picks up a powerup
98 * @param powerUp the PowerUp that should be picked.
99 * @returns true on success (pickup was picked, false otherwise)
100 *
101 * This function also checks if the Pickup can be picked up by this Playable
102 */
103bool Playable::pickup(PowerUp* powerUp)
104{
105  if(powerUp->isA(CL_WEAPON_POWER_UP))
106  {
107    return dynamic_cast<WeaponPowerUp*>(powerUp)->process(&this->getWeaponManager());
108  }
109  else if(powerUp->isA(CL_PARAM_POWER_UP))
110  {
111    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
112    switch(ppu->getType())
113    {
114      case POWERUP_PARAM_HEALTH:
115        this->increaseHealth(ppu->getValue());
116        return true;
117      case POWERUP_PARAM_MAX_HEALTH:
118        this->increaseHealthMax(ppu->getValue());
119        return true;
120    }
121  }
122  return false;
123}
124
125/**
126 * @brief adds a Weapon to the Playable.
127 * @param weapon the Weapon to add.
128 * @param configID the Configuration ID to add this weapon to.
129 * @param slotID the slotID to add the Weapon to.
130 */
[7350]131bool Playable::addWeapon(Weapon* weapon, int configID, int slotID)
[6443]132{
[7350]133  if(this->weaponMan.addWeapon(weapon, configID, slotID))
134  {
135    this->weaponConfigChanged();
136    return true;
137  }
138  else
139  {
140    if (weapon != NULL)
141      PRINTF(2)("Unable to add Weapon (%s::%s) to %s::%s\n",
[7351]142                weapon->getClassName(), weapon->getName(), this->getClassName(), this->getName());
[7350]143    else
144      PRINTF(2)("No weapon defined\n");
145    return false;
[6443]146
[7350]147  }
[6443]148}
149
[7346]150/**
151 * @brief removes a Weapon.
152 * @param weapon the Weapon to remove.
153 */
[6443]154void Playable::removeWeapon(Weapon* weapon)
155{
[7337]156  this->weaponMan.removeWeapon(weapon);
[6443]157
[7338]158  this->weaponConfigChanged();
[6443]159}
160
[7346]161/**
162 * @brief jumps to the next WeaponConfiguration
163 */
[6444]164void Playable::nextWeaponConfig()
165{
[7337]166  this->weaponMan.nextWeaponConfig();
[7338]167  this->weaponConfigChanged();
[6444]168}
[6443]169
[7346]170/**
171 * @brief moves to the last WeaponConfiguration
172 */
[6444]173void Playable::previousWeaponConfig()
174{
[7337]175  this->weaponMan.previousWeaponConfig();
[7338]176  this->weaponConfigChanged();
[6444]177}
178
[7346]179/**
180 * @brief tells the Player, that the Weapon-Configuration has changed.
181 *
182 * TODO remove this
183 * This function is needed, so that the WeponManager of this Playable can easily update the HUD
184 */
[6568]185void Playable::weaponConfigChanged()
186{
[6578]187  if (this->currentPlayer != NULL)
188    this->currentPlayer->weaponConfigChanged();
[6568]189}
[6444]190
[7350]191/**
192 * @brief a Cheat that gives us some Weapons
193 */
194void Playable::addSomeWeapons_CHEAT()
195{
[7351]196  if (State::getPlayer() != NULL)
197  {
198    Playable* playable = State::getPlayer()->getPlayable();
199    if (playable != NULL)
200    {
201      PRINTF(2)("ADDING WEAPONS - you cheater\n");
202      playable->addWeapon(Weapon::createWeapon(CL_HYPERBLASTER));
203      playable->addWeapon(Weapon::createWeapon(CL_TURRET));
204      playable->addWeapon(Weapon::createWeapon(CL_AIMING_TURRET));
205      playable->addWeapon(Weapon::createWeapon(CL_CANNON));
206      playable->addWeapon(Weapon::createWeapon(CL_TARGETING_TURRET));
207      PRINTF(2)("ADDING WEAPONS FINISHED\n");
208    }
209  }
[7350]210}
[7346]211
[7173]212/**
[7346]213 * @brief subscribe to all events the controllable needs
214 * @param player the player that shall controll this Playable
215 * @returns false on error true otherwise.
216 */
217bool Playable::setPlayer(Player* player)
218{
219  // if we already have a Player inside do nothing
220  if (this->currentPlayer != NULL && player != NULL)
221  {
222    return false;
223  }
224
225  // eject the Player if player == NULL
226  if (this->currentPlayer != NULL && player == NULL)
227  {
228    PRINTF(4)("Player gets ejected\n");
229
230    // unsubscibe all events.
231    std::vector<int>::iterator ev;
232    for (ev = this->events.begin(); ev != events.end(); ev++)
[7868]233      player->unsubscribeEvent(ES_GAME, (*ev));
[7346]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
244    return true;
245  }
246
247  // get the new Player inside
248  if (this->currentPlayer == NULL && player != NULL)
249  {
250    PRINTF(4)("New Player gets inside\n");
251    this->currentPlayer = player;
252    if (this->currentPlayer->getPlayable() != this)
253      this->currentPlayer->setPlayable(this);
254
255    /*EventHandler*/
256    std::vector<int>::iterator ev;
257    for (ev = this->events.begin(); ev != events.end(); ev++)
[7868]258      player->subscribeEvent(ES_GAME, (*ev));
[7346]259
260    this->enter();
261    return true;
262  }
263
264  return false;
265}
266
267/**
268 * @brief attaches the current Camera to this Playable
269 *
270 * this function can be derived, so that any Playable can make the attachment differently.
271 */
272void Playable::attachCamera()
273{
274  State::getCameraNode()->setParentSoft(this);
275  State::getCameraTargetNode()->setParentSoft(this);
276
277}
278
279/**
280 * @brief detaches the Camera
281 * @see void Playable::attachCamera()
282 */
283void  Playable::detachCamera()
[8147]284{
285}
[7346]286
287
288/**
[7173]289 * @brief sets the CameraMode.
290 * @param cameraMode: the Mode to switch to.
291 */
292void Playable::setCameraMode(unsigned int cameraMode)
[7347]293{
294  State::getCamera()->setViewMode((Camera::ViewMode)cameraMode);
295}
[7338]296
[7346]297
[7338]298/**
299 * @brief sets the Playmode
300 * @param playmode the Playmode
301 * @returns true on success, false otherwise
302 */
[7339]303bool Playable::setPlaymode(Playable::Playmode playmode)
[7173]304{
[7338]305  if (!this->playmodeSupported(playmode))
306    return false;
307  else
[7345]308  {
[7347]309    this->enterPlaymode(playmode);
[7338]310    this->playmode = playmode;
[7345]311    return true;
312  }
[7173]313}
314
[7346]315/**
316 * @brief This function look, that the Playable rotates to the given rotation.
317 * @param angle the Angle around
318 * @param dirX directionX
319 * @param dirY directionY
320 * @param dirZ directionZ
321 * @param speed how fast to turn there.
322 */
323void Playable::setPlayDirection(float angle, float dirX, float dirY, float dirZ, float speed)
324{
325  this->setPlayDirection(Quaternion(angle, Vector(dirX, dirY, dirZ)), speed);
326}
[7173]327
[5872]328/**
[7346]329 * @brief all Playable will enter the Playmode Differently, say here how to do it.
330 * @param playmode the Playmode to enter.
331 *
332 * In this function all the actions that are required to enter the Playmode are described.
333 * e.g: camera, rotation, wait cycle and so on...
[7347]334 *
335 * on enter of this function the playmode is still the old playmode.
[7346]336 */
337void Playable::enterPlaymode(Playable::Playmode playmode)
338{
[7347]339  switch(playmode)
340  {
341    default:
342      this->attachCamera();
343      break;
344    case Playable::Horizontal:
345      this->setCameraMode(Camera::ViewTop);
346      break;
347    case Playable::Vertical:
348      this->setCameraMode(Camera::ViewLeft);
349      break;
350    case Playable::FromBehind:
351      this->setCameraMode(Camera::ViewBehind);
352      break;
353  }
[7346]354}
355/**
[6436]356 * @brief helps us colliding Playables
[7346]357 * @param entity the Entity to collide
358 * @param location where the collision occured.
[6436]359 */
360void Playable::collidesWith(WorldEntity* entity, const Vector& location)
361{
[7072]362  if (entity == collider)
363    return;
364  collider = entity;
365
[7121]366  if ( entity->isA(CL_PROJECTILE) && ( !State::isOnline() || SharedNetworkData::getInstance()->isGameServer() ) )
[6966]367  {
[7072]368    this->decreaseHealth(entity->getHealth() *(float)rand()/(float)RAND_MAX);
[6966]369    // EXTREME HACK
[7072]370    if (this->getHealth() <= 0.0f)
[6966]371    {
372      this->die();
[7482]373
374      if( State::getGameRules() != NULL)
375        State::getGameRules()->registerKill(Kill(entity, this));
[6966]376    }
377  }
378}
[6436]379
[6966]380
[7044]381void Playable::respawn()
[6966]382{
[7044]383  PRINTF(0)("Playable respawn\n");
384  // only if this is the spaceship of the player
385  if( this == State::getPlayer()->getPlayable())
386    State::getGameRules()->onPlayerSpawn();
[6966]387
[7085]388
[7082]389  if( this->getOwner() % 2 == 0)
390  {
[7338]391    //     this->toList(OM_GROUP_00);
[7082]392    this->setAbsCoor(213.37, 57.71, -47.98);
[7100]393    this->setAbsDir(0, 0, 1, 0);
[7082]394  }
[6966]395  else
[7099]396  { // red team
[7338]397    //     this->toList(OM_GROUP_01);
[7082]398    this->setAbsCoor(-314.450, 40.701, 83.554);
[7100]399    this->setAbsDir(1.0, -0.015, -0.012, 0.011);
[7082]400  }
[7118]401  this->reset();
402  this->bDead = false;
[6436]403}
404
[6966]405
[7088]406
[7044]407void Playable::die()
408{
[7119]409  Explosion::explode(dynamic_cast<PNode*>(this), Vector(1.0f, 1.0f, 1.0f));
410
411
[7118]412  if( !this->bDead)
413  {
414    PRINTF(0)("Playable dies\n");
[7338]415    // only if this is the spaceship of the player
[7118]416    if (State::isOnline())
417    {
418      if( this == State::getPlayer()->getPlayable())
419        State::getGameRules()->onPlayerDeath();
[7044]420
[7338]421      //     this->toList(OM_GROUP_05);
422      //HACK: moves the entity to an unknown place far far away: in the future, GameRules will look for that
[7118]423      this->setAbsCoor(-2000.0, -2000.0, -2000.0);
[7078]424
[7338]425      //explosion hack
[7118]426
427    }
428    this->bDead = true;
[7072]429  }
[7044]430}
431
432
[6986]433
434
435
[5896]436/**
[7346]437 * @brief add an event to the event list of events this Playable can capture
[5898]438 * @param eventType the Type of event to add
[5889]439 */
[5896]440void Playable::registerEvent(int eventType)
[5889]441{
442  this->events.push_back(eventType);
443
[5896]444  if (this->currentPlayer != NULL)
[7868]445    this->currentPlayer->subscribeEvent(ES_GAME, eventType);
[5889]446}
447
[5896]448/**
[7339]449 * @brief remove an event to the event list this Playable can capture.
[5898]450 * @param event the event to unregister.
[5896]451 */
452void Playable::unregisterEvent(int eventType)
453{
[7338]454  std::vector<int>::iterator rmEvent = std::find(this->events.begin(), this->events.end(), eventType);
455  this->events.erase(rmEvent);
[5889]456
[5896]457  if (this->currentPlayer != NULL)
[7868]458    this->currentPlayer->unsubscribeEvent(ES_GAME, eventType);
[5896]459}
[5889]460
[6804]461/**
462 * @brief ticks a Playable
463 * @param dt: the passed time since the last Tick
464 */
465void Playable::tick(float dt)
466{
[7337]467  this->weaponMan.tick(dt);
[6804]468  if (this->bFire)
[7337]469    weaponMan.fire();
[6804]470}
[5896]471
[6804]472
473/**
474 * @brief processes Playable events.
475 * @param event the Captured Event.
476 */
477void Playable::process(const Event &event)
478{
479  if( event.type == KeyMapper::PEV_FIRE1)
480    this->bFire = event.bPressed;
481  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
482  {
483    this->nextWeaponConfig();
484  }
485  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
486    this->previousWeaponConfig();
487}
488
489
[6959]490
[7346]491/**
492 * @brief converts a string into a Playable::Playmode.
493 * @param playmode the string naming the Playable::Playmode to convert.
494 * @returns the Playable::Playmode converted from playmode.
495 */
[7339]496Playable::Playmode Playable::stringToPlaymode(const std::string& playmode)
497{
[7412]498  if (playmode == Playable::playmodeNames[0])
[7339]499    return Playable::Vertical;
[7412]500  if (playmode == Playable::playmodeNames[1])
[7339]501    return Playable::Horizontal;
[7412]502  if (playmode == Playable::playmodeNames[2])
[7339]503    return Playable::FromBehind;
[7412]504  if (playmode == Playable::playmodeNames[3])
[7339]505    return Playable::Full3D;
[8055]506  if (playmode == Playable::playmodeNames[4])
507    return Playable::FirstPerson;
[7339]508
509  return Playable::Full3D;
510}
511
[7346]512
513/**
514 * @brief converts a playmode into a string.
515 * @param playmode the Playable::Playmode to convert.
516 * @returns the String.
517 */
[7412]518const std::string& Playable::playmodeToString(Playable::Playmode playmode)
[7339]519{
520  switch(playmode)
521  {
522    case Playable::Vertical:
[7412]523      return Playable::playmodeNames[0];
[7339]524    case Playable::Horizontal:
[7412]525      return Playable::playmodeNames[1];
[7339]526    case Playable::FromBehind:
[7412]527      return Playable::playmodeNames[2];
[7339]528    case Playable::Full3D:
[7412]529      return Playable::playmodeNames[3];
[8055]530    case Playable::FirstPerson:
531      return Playable::playmodeNames[4];
[7339]532
533    default:
[7412]534      return Playable::playmodeNames[3];
[7339]535  }
[7412]536}
[7339]537
[7412]538/**
539 * @brief PlaymodeNames
540 */
541const std::string Playable::playmodeNames[] =
542{
543  "Vertical",
544  "Horizontal",
545  "FromBehind",
[8055]546  "Full3D",
547  "FirstPerson"
[7412]548};
Note: See TracBrowser for help on using the repository browser.