Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/cd/src/world_entities/playable.cc @ 7364

Last change on this file since 7364 was 7356, checked in by bensch, 19 years ago

orxonox/trunk: added alias

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