Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7707 was 7482, checked in by patrick, 19 years ago

orxonox: working on kill event processing (game rule specific)

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