Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged the bsp-model-stuff back here

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