Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8349 was 8316, checked in by bensch, 19 years ago

trunk: fixed most -Wall warnings… but there are still many missing :/

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    {
376      this->die();
[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
[7044]411void Playable::die()
412{
[7119]413  Explosion::explode(dynamic_cast<PNode*>(this), Vector(1.0f, 1.0f, 1.0f));
414
415
[7118]416  if( !this->bDead)
417  {
418    PRINTF(0)("Playable dies\n");
[7338]419    // only if this is the spaceship of the player
[7118]420    if (State::isOnline())
421    {
422      if( this == State::getPlayer()->getPlayable())
423        State::getGameRules()->onPlayerDeath();
[7044]424
[7338]425      //     this->toList(OM_GROUP_05);
426      //HACK: moves the entity to an unknown place far far away: in the future, GameRules will look for that
[7118]427      this->setAbsCoor(-2000.0, -2000.0, -2000.0);
[7078]428
[7338]429      //explosion hack
[7118]430
431    }
432    this->bDead = true;
[7072]433  }
[7044]434}
435
436
[6986]437
438
439
[5896]440/**
[7346]441 * @brief add an event to the event list of events this Playable can capture
[5898]442 * @param eventType the Type of event to add
[5889]443 */
[5896]444void Playable::registerEvent(int eventType)
[5889]445{
446  this->events.push_back(eventType);
447
[5896]448  if (this->currentPlayer != NULL)
[7868]449    this->currentPlayer->subscribeEvent(ES_GAME, eventType);
[5889]450}
451
[5896]452/**
[7339]453 * @brief remove an event to the event list this Playable can capture.
[5898]454 * @param event the event to unregister.
[5896]455 */
456void Playable::unregisterEvent(int eventType)
457{
[7338]458  std::vector<int>::iterator rmEvent = std::find(this->events.begin(), this->events.end(), eventType);
459  this->events.erase(rmEvent);
[5889]460
[5896]461  if (this->currentPlayer != NULL)
[7868]462    this->currentPlayer->unsubscribeEvent(ES_GAME, eventType);
[5896]463}
[5889]464
[6804]465/**
466 * @brief ticks a Playable
467 * @param dt: the passed time since the last Tick
468 */
469void Playable::tick(float dt)
470{
[7337]471  this->weaponMan.tick(dt);
[6804]472  if (this->bFire)
[7337]473    weaponMan.fire();
[6804]474}
[5896]475
[6804]476
477/**
478 * @brief processes Playable events.
479 * @param event the Captured Event.
480 */
481void Playable::process(const Event &event)
482{
483  if( event.type == KeyMapper::PEV_FIRE1)
484    this->bFire = event.bPressed;
485  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
486  {
487    this->nextWeaponConfig();
488  }
489  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
490    this->previousWeaponConfig();
491}
492
493
[6959]494
[7346]495/**
496 * @brief converts a string into a Playable::Playmode.
497 * @param playmode the string naming the Playable::Playmode to convert.
498 * @returns the Playable::Playmode converted from playmode.
499 */
[7339]500Playable::Playmode Playable::stringToPlaymode(const std::string& playmode)
501{
[7412]502  if (playmode == Playable::playmodeNames[0])
[7339]503    return Playable::Vertical;
[7412]504  if (playmode == Playable::playmodeNames[1])
[7339]505    return Playable::Horizontal;
[7412]506  if (playmode == Playable::playmodeNames[2])
[7339]507    return Playable::FromBehind;
[7412]508  if (playmode == Playable::playmodeNames[3])
[7339]509    return Playable::Full3D;
[8055]510  if (playmode == Playable::playmodeNames[4])
511    return Playable::FirstPerson;
[7339]512
513  return Playable::Full3D;
514}
515
[7346]516
517/**
518 * @brief converts a playmode into a string.
519 * @param playmode the Playable::Playmode to convert.
520 * @returns the String.
521 */
[7412]522const std::string& Playable::playmodeToString(Playable::Playmode playmode)
[7339]523{
524  switch(playmode)
525  {
526    case Playable::Vertical:
[7412]527      return Playable::playmodeNames[0];
[7339]528    case Playable::Horizontal:
[7412]529      return Playable::playmodeNames[1];
[7339]530    case Playable::FromBehind:
[7412]531      return Playable::playmodeNames[2];
[7339]532    case Playable::Full3D:
[7412]533      return Playable::playmodeNames[3];
[8055]534    case Playable::FirstPerson:
535      return Playable::playmodeNames[4];
[7339]536
537    default:
[7412]538      return Playable::playmodeNames[3];
[7339]539  }
[7412]540}
[7339]541
[7412]542/**
543 * @brief PlaymodeNames
544 */
545const std::string Playable::playmodeNames[] =
546{
547  "Vertical",
548  "Horizontal",
549  "FromBehind",
[8055]550  "Full3D",
551  "FirstPerson"
[7412]552};
Note: See TracBrowser for help on using the repository browser.