Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10620 was 10618, checked in by bknecht, 18 years ago

merged cleanup into trunk (only improvements)

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