Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/single_player_map/src/world_entities/playable.cc @ 9047

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

orxonox/trunk: Each Playable has an EnterRange

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