Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7735 was 7713, checked in by patrick, 19 years ago

trunk: removed some valgrind errors, but no serious ones. Thunder sound now works, found a bug in my old class. its absolete anyway with the atmoshperic engine

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