Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model/src/world_entities/playable.cc @ 8686

Last change on this file since 8686 was 8686, checked in by patrick, 18 years ago

bsp: fps player extension

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"
[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.
[8316]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;
[8316]120      default:
121        /// EVERYTHING THAT IS NOT HANDLED
122        /// FIXME
123        return false;
[7346]124    }
125  }
126  return false;
127}
128
129/**
130 * @brief adds a Weapon to the Playable.
131 * @param weapon the Weapon to add.
132 * @param configID the Configuration ID to add this weapon to.
133 * @param slotID the slotID to add the Weapon to.
134 */
[7350]135bool Playable::addWeapon(Weapon* weapon, int configID, int slotID)
[6443]136{
[7350]137  if(this->weaponMan.addWeapon(weapon, configID, slotID))
138  {
139    this->weaponConfigChanged();
140    return true;
141  }
142  else
143  {
144    if (weapon != NULL)
145      PRINTF(2)("Unable to add Weapon (%s::%s) to %s::%s\n",
[7351]146                weapon->getClassName(), weapon->getName(), this->getClassName(), this->getName());
[7350]147    else
148      PRINTF(2)("No weapon defined\n");
149    return false;
[6443]150
[7350]151  }
[6443]152}
153
[7346]154/**
155 * @brief removes a Weapon.
156 * @param weapon the Weapon to remove.
157 */
[6443]158void Playable::removeWeapon(Weapon* weapon)
159{
[7337]160  this->weaponMan.removeWeapon(weapon);
[6443]161
[7338]162  this->weaponConfigChanged();
[6443]163}
164
[7346]165/**
166 * @brief jumps to the next WeaponConfiguration
167 */
[6444]168void Playable::nextWeaponConfig()
169{
[7337]170  this->weaponMan.nextWeaponConfig();
[7338]171  this->weaponConfigChanged();
[6444]172}
[6443]173
[7346]174/**
175 * @brief moves to the last WeaponConfiguration
176 */
[6444]177void Playable::previousWeaponConfig()
178{
[7337]179  this->weaponMan.previousWeaponConfig();
[7338]180  this->weaponConfigChanged();
[6444]181}
182
[7346]183/**
184 * @brief tells the Player, that the Weapon-Configuration has changed.
185 *
186 * TODO remove this
187 * This function is needed, so that the WeponManager of this Playable can easily update the HUD
188 */
[6568]189void Playable::weaponConfigChanged()
190{
[6578]191  if (this->currentPlayer != NULL)
192    this->currentPlayer->weaponConfigChanged();
[6568]193}
[6444]194
[7350]195/**
196 * @brief a Cheat that gives us some Weapons
197 */
198void Playable::addSomeWeapons_CHEAT()
199{
[7351]200  if (State::getPlayer() != NULL)
201  {
202    Playable* playable = State::getPlayer()->getPlayable();
203    if (playable != NULL)
204    {
205      PRINTF(2)("ADDING WEAPONS - you cheater\n");
206      playable->addWeapon(Weapon::createWeapon(CL_HYPERBLASTER));
207      playable->addWeapon(Weapon::createWeapon(CL_TURRET));
208      playable->addWeapon(Weapon::createWeapon(CL_AIMING_TURRET));
209      playable->addWeapon(Weapon::createWeapon(CL_CANNON));
210      playable->addWeapon(Weapon::createWeapon(CL_TARGETING_TURRET));
211      PRINTF(2)("ADDING WEAPONS FINISHED\n");
212    }
213  }
[7350]214}
[7346]215
[7173]216/**
[7346]217 * @brief subscribe to all events the controllable needs
218 * @param player the player that shall controll this Playable
219 * @returns false on error true otherwise.
220 */
221bool Playable::setPlayer(Player* player)
222{
223  // if we already have a Player inside do nothing
224  if (this->currentPlayer != NULL && player != NULL)
225  {
226    return false;
227  }
228
229  // eject the Player if player == NULL
230  if (this->currentPlayer != NULL && player == NULL)
231  {
232    PRINTF(4)("Player gets ejected\n");
233
234    // unsubscibe all events.
235    std::vector<int>::iterator ev;
236    for (ev = this->events.begin(); ev != events.end(); ev++)
[7868]237      player->unsubscribeEvent(ES_GAME, (*ev));
[7346]238
239    // leave the entity
240    this->leave();
241
242    // eject the current Player.
243    Player* ejectPlayer = this->currentPlayer;
244    this->currentPlayer = NULL;
245    // eject the Player.
246    ejectPlayer->setPlayable(NULL);
247
248    return true;
249  }
250
251  // get the new Player inside
252  if (this->currentPlayer == NULL && player != NULL)
253  {
254    PRINTF(4)("New Player gets inside\n");
255    this->currentPlayer = player;
256    if (this->currentPlayer->getPlayable() != this)
257      this->currentPlayer->setPlayable(this);
258
259    /*EventHandler*/
260    std::vector<int>::iterator ev;
261    for (ev = this->events.begin(); ev != events.end(); ev++)
[7868]262      player->subscribeEvent(ES_GAME, (*ev));
[7346]263
264    this->enter();
265    return true;
266  }
267
268  return false;
269}
270
271/**
272 * @brief attaches the current Camera to this Playable
273 *
274 * this function can be derived, so that any Playable can make the attachment differently.
275 */
276void Playable::attachCamera()
277{
278  State::getCameraNode()->setParentSoft(this);
279  State::getCameraTargetNode()->setParentSoft(this);
280
281}
282
283/**
284 * @brief detaches the Camera
285 * @see void Playable::attachCamera()
286 */
287void  Playable::detachCamera()
[8147]288{
289}
[7346]290
291
292/**
[7173]293 * @brief sets the CameraMode.
294 * @param cameraMode: the Mode to switch to.
295 */
296void Playable::setCameraMode(unsigned int cameraMode)
[7347]297{
298  State::getCamera()->setViewMode((Camera::ViewMode)cameraMode);
299}
[7338]300
[7346]301
[7338]302/**
303 * @brief sets the Playmode
304 * @param playmode the Playmode
305 * @returns true on success, false otherwise
306 */
[7339]307bool Playable::setPlaymode(Playable::Playmode playmode)
[7173]308{
[7338]309  if (!this->playmodeSupported(playmode))
310    return false;
311  else
[7345]312  {
[7347]313    this->enterPlaymode(playmode);
[7338]314    this->playmode = playmode;
[7345]315    return true;
316  }
[7173]317}
318
[7346]319/**
320 * @brief This function look, that the Playable rotates to the given rotation.
321 * @param angle the Angle around
322 * @param dirX directionX
323 * @param dirY directionY
324 * @param dirZ directionZ
325 * @param speed how fast to turn there.
326 */
327void Playable::setPlayDirection(float angle, float dirX, float dirY, float dirZ, float speed)
328{
329  this->setPlayDirection(Quaternion(angle, Vector(dirX, dirY, dirZ)), speed);
330}
[7173]331
[5872]332/**
[7346]333 * @brief all Playable will enter the Playmode Differently, say here how to do it.
334 * @param playmode the Playmode to enter.
335 *
336 * In this function all the actions that are required to enter the Playmode are described.
337 * e.g: camera, rotation, wait cycle and so on...
[7347]338 *
339 * on enter of this function the playmode is still the old playmode.
[7346]340 */
341void Playable::enterPlaymode(Playable::Playmode playmode)
342{
[7347]343  switch(playmode)
344  {
345    default:
346      this->attachCamera();
347      break;
348    case Playable::Horizontal:
349      this->setCameraMode(Camera::ViewTop);
350      break;
351    case Playable::Vertical:
352      this->setCameraMode(Camera::ViewLeft);
353      break;
354    case Playable::FromBehind:
355      this->setCameraMode(Camera::ViewBehind);
356      break;
357  }
[7346]358}
359/**
[6436]360 * @brief helps us colliding Playables
[7346]361 * @param entity the Entity to collide
362 * @param location where the collision occured.
[6436]363 */
364void Playable::collidesWith(WorldEntity* entity, const Vector& location)
365{
[7072]366  if (entity == collider)
367    return;
368  collider = entity;
369
[7121]370  if ( entity->isA(CL_PROJECTILE) && ( !State::isOnline() || SharedNetworkData::getInstance()->isGameServer() ) )
[6966]371  {
[7072]372    this->decreaseHealth(entity->getHealth() *(float)rand()/(float)RAND_MAX);
[6966]373    // EXTREME HACK
[7072]374    if (this->getHealth() <= 0.0f)
[6966]375    {
[8511]376//       this->destory();
[7482]377
378      if( State::getGameRules() != NULL)
379        State::getGameRules()->registerKill(Kill(entity, this));
[6966]380    }
381  }
382}
[6436]383
[6966]384
[7044]385void Playable::respawn()
[6966]386{
[7044]387  PRINTF(0)("Playable respawn\n");
388  // only if this is the spaceship of the player
389  if( this == State::getPlayer()->getPlayable())
390    State::getGameRules()->onPlayerSpawn();
[6966]391
[7085]392
[7082]393  if( this->getOwner() % 2 == 0)
394  {
[7338]395    //     this->toList(OM_GROUP_00);
[7082]396    this->setAbsCoor(213.37, 57.71, -47.98);
[7100]397    this->setAbsDir(0, 0, 1, 0);
[7082]398  }
[6966]399  else
[7099]400  { // red team
[7338]401    //     this->toList(OM_GROUP_01);
[7082]402    this->setAbsCoor(-314.450, 40.701, 83.554);
[7100]403    this->setAbsDir(1.0, -0.015, -0.012, 0.011);
[7082]404  }
[7118]405  this->reset();
406  this->bDead = false;
[6436]407}
408
[6966]409
[7088]410
[8511]411
412void Playable::destroy()
[7044]413{
[7119]414  Explosion::explode(dynamic_cast<PNode*>(this), Vector(1.0f, 1.0f, 1.0f));
415
416
[7118]417  if( !this->bDead)
418  {
419    PRINTF(0)("Playable dies\n");
[7338]420    // only if this is the spaceship of the player
[7118]421    if (State::isOnline())
422    {
423      if( this == State::getPlayer()->getPlayable())
424        State::getGameRules()->onPlayerDeath();
[7044]425
[7338]426      //     this->toList(OM_GROUP_05);
427      //HACK: moves the entity to an unknown place far far away: in the future, GameRules will look for that
[7118]428      this->setAbsCoor(-2000.0, -2000.0, -2000.0);
[7078]429
[7338]430      //explosion hack
[7118]431
432    }
433    this->bDead = true;
[7072]434  }
[7044]435}
436
437
[6986]438
439
440
[5896]441/**
[7346]442 * @brief add an event to the event list of events this Playable can capture
[5898]443 * @param eventType the Type of event to add
[5889]444 */
[5896]445void Playable::registerEvent(int eventType)
[5889]446{
447  this->events.push_back(eventType);
448
[5896]449  if (this->currentPlayer != NULL)
[7868]450    this->currentPlayer->subscribeEvent(ES_GAME, eventType);
[5889]451}
452
[5896]453/**
[7339]454 * @brief remove an event to the event list this Playable can capture.
[5898]455 * @param event the event to unregister.
[5896]456 */
457void Playable::unregisterEvent(int eventType)
458{
[7338]459  std::vector<int>::iterator rmEvent = std::find(this->events.begin(), this->events.end(), eventType);
460  this->events.erase(rmEvent);
[5889]461
[5896]462  if (this->currentPlayer != NULL)
[7868]463    this->currentPlayer->unsubscribeEvent(ES_GAME, eventType);
[5896]464}
[5889]465
[6804]466/**
467 * @brief ticks a Playable
468 * @param dt: the passed time since the last Tick
469 */
470void Playable::tick(float dt)
471{
[7337]472  this->weaponMan.tick(dt);
[6804]473  if (this->bFire)
[7337]474    weaponMan.fire();
[6804]475}
[5896]476
[6804]477
478/**
479 * @brief processes Playable events.
480 * @param event the Captured Event.
481 */
482void Playable::process(const Event &event)
483{
484  if( event.type == KeyMapper::PEV_FIRE1)
485    this->bFire = event.bPressed;
486  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
487  {
488    this->nextWeaponConfig();
489  }
490  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
491    this->previousWeaponConfig();
492}
493
494
[6959]495
[7346]496/**
497 * @brief converts a string into a Playable::Playmode.
498 * @param playmode the string naming the Playable::Playmode to convert.
499 * @returns the Playable::Playmode converted from playmode.
500 */
[7339]501Playable::Playmode Playable::stringToPlaymode(const std::string& playmode)
502{
[7412]503  if (playmode == Playable::playmodeNames[0])
[7339]504    return Playable::Vertical;
[7412]505  if (playmode == Playable::playmodeNames[1])
[7339]506    return Playable::Horizontal;
[7412]507  if (playmode == Playable::playmodeNames[2])
[7339]508    return Playable::FromBehind;
[7412]509  if (playmode == Playable::playmodeNames[3])
[7339]510    return Playable::Full3D;
[8055]511  if (playmode == Playable::playmodeNames[4])
512    return Playable::FirstPerson;
[7339]513
514  return Playable::Full3D;
515}
516
[7346]517
518/**
519 * @brief converts a playmode into a string.
520 * @param playmode the Playable::Playmode to convert.
521 * @returns the String.
522 */
[7412]523const std::string& Playable::playmodeToString(Playable::Playmode playmode)
[7339]524{
525  switch(playmode)
526  {
527    case Playable::Vertical:
[7412]528      return Playable::playmodeNames[0];
[7339]529    case Playable::Horizontal:
[7412]530      return Playable::playmodeNames[1];
[7339]531    case Playable::FromBehind:
[7412]532      return Playable::playmodeNames[2];
[7339]533    case Playable::Full3D:
[7412]534      return Playable::playmodeNames[3];
[8055]535    case Playable::FirstPerson:
536      return Playable::playmodeNames[4];
[7339]537
538    default:
[7412]539      return Playable::playmodeNames[3];
[7339]540  }
[7412]541}
[7339]542
[7412]543/**
544 * @brief PlaymodeNames
545 */
546const std::string Playable::playmodeNames[] =
547{
548  "Vertical",
549  "Horizontal",
550  "FromBehind",
[8055]551  "Full3D",
552  "FirstPerson"
[7412]553};
Note: See TracBrowser for help on using the repository browser.