Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/world_entities/playable.cc @ 9074

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

orxonox/trunk: better 2d-playable

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