Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/cr/src/world_entities/playable.cc @ 7936

Last change on this file since 7936 was 7868, checked in by bensch, 19 years ago

trunk: merge Check in the Event-changes:
r7867 | bensch | 2006-05-26 13:19:46 +0200 (Fri, 26 May 2006) | 1 line

Events better subscribed


r7866 | bensch | 2006-05-26 13:11:10 +0200 (Fri, 26 May 2006) | 1 line

Events are subscribed at the EventListener, and not the EventHandler

File size: 13.8 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->oldScore = 0;
65  this->collider = NULL;
66
67  this->bDead = false;
68}
69
70
71/**
72 * @brief destroys the Playable
73 */
74Playable::~Playable()
75{
76  // THE DERIVED CLASS MUST UNSUBSCRIBE THE PLAYER THROUGH
77  // this->setPlayer(NULL);
78  // IN ITS DESTRUCTOR.
79  assert(this->currentPlayer == NULL);
80}
81
82/**
83 * @brief loads Playable parameters onto the Playable
84 * @param root the XML-root to load from
85 */
86void Playable::loadParams(const TiXmlElement* root)
87{
88  WorldEntity::loadParams(root);
89
90  LoadParam(root, "abs-dir", this, Playable, setPlayDirection);
91}
92
93/**
94 * @brief picks up a powerup
95 * @param powerUp the PowerUp that should be picked.
96 * @returns true on success (pickup was picked, false otherwise)
97 *
98 * This function also checks if the Pickup can be picked up by this Playable
99 */
100bool Playable::pickup(PowerUp* powerUp)
101{
102  if(powerUp->isA(CL_WEAPON_POWER_UP))
103  {
104    return dynamic_cast<WeaponPowerUp*>(powerUp)->process(&this->getWeaponManager());
105  }
106  else if(powerUp->isA(CL_PARAM_POWER_UP))
107  {
108    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
109    switch(ppu->getType())
110    {
111      case POWERUP_PARAM_HEALTH:
112        this->increaseHealth(ppu->getValue());
113        return true;
114      case POWERUP_PARAM_MAX_HEALTH:
115        this->increaseHealthMax(ppu->getValue());
116        return true;
117    }
118  }
119  return false;
120}
121
122/**
123 * @brief adds a Weapon to the Playable.
124 * @param weapon the Weapon to add.
125 * @param configID the Configuration ID to add this weapon to.
126 * @param slotID the slotID to add the Weapon to.
127 */
128bool Playable::addWeapon(Weapon* weapon, int configID, int slotID)
129{
130  if(this->weaponMan.addWeapon(weapon, configID, slotID))
131  {
132    this->weaponConfigChanged();
133    return true;
134  }
135  else
136  {
137    if (weapon != NULL)
138      PRINTF(2)("Unable to add Weapon (%s::%s) to %s::%s\n",
139                weapon->getClassName(), weapon->getName(), this->getClassName(), this->getName());
140    else
141      PRINTF(2)("No weapon defined\n");
142    return false;
143
144  }
145}
146
147/**
148 * @brief removes a Weapon.
149 * @param weapon the Weapon to remove.
150 */
151void Playable::removeWeapon(Weapon* weapon)
152{
153  this->weaponMan.removeWeapon(weapon);
154
155  this->weaponConfigChanged();
156}
157
158/**
159 * @brief jumps to the next WeaponConfiguration
160 */
161void Playable::nextWeaponConfig()
162{
163  this->weaponMan.nextWeaponConfig();
164  this->weaponConfigChanged();
165}
166
167/**
168 * @brief moves to the last WeaponConfiguration
169 */
170void Playable::previousWeaponConfig()
171{
172  this->weaponMan.previousWeaponConfig();
173  this->weaponConfigChanged();
174}
175
176/**
177 * @brief tells the Player, that the Weapon-Configuration has changed.
178 *
179 * TODO remove this
180 * This function is needed, so that the WeponManager of this Playable can easily update the HUD
181 */
182void Playable::weaponConfigChanged()
183{
184  if (this->currentPlayer != NULL)
185    this->currentPlayer->weaponConfigChanged();
186}
187
188/**
189 * @brief a Cheat that gives us some Weapons
190 */
191void Playable::addSomeWeapons_CHEAT()
192{
193  if (State::getPlayer() != NULL)
194  {
195    Playable* playable = State::getPlayer()->getPlayable();
196    if (playable != NULL)
197    {
198      PRINTF(2)("ADDING WEAPONS - you cheater\n");
199      playable->addWeapon(Weapon::createWeapon(CL_HYPERBLASTER));
200      playable->addWeapon(Weapon::createWeapon(CL_TURRET));
201      playable->addWeapon(Weapon::createWeapon(CL_AIMING_TURRET));
202      playable->addWeapon(Weapon::createWeapon(CL_CANNON));
203      playable->addWeapon(Weapon::createWeapon(CL_TARGETING_TURRET));
204      PRINTF(2)("ADDING WEAPONS FINISHED\n");
205    }
206  }
207}
208
209/**
210 * @brief subscribe to all events the controllable needs
211 * @param player the player that shall controll this Playable
212 * @returns false on error true otherwise.
213 */
214bool Playable::setPlayer(Player* player)
215{
216  // if we already have a Player inside do nothing
217  if (this->currentPlayer != NULL && player != NULL)
218  {
219    return false;
220  }
221
222  // eject the Player if player == NULL
223  if (this->currentPlayer != NULL && player == NULL)
224  {
225    PRINTF(4)("Player gets ejected\n");
226
227    // unsubscibe all events.
228    std::vector<int>::iterator ev;
229    for (ev = this->events.begin(); ev != events.end(); ev++)
230      player->unsubscribeEvent(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    std::vector<int>::iterator ev;
254    for (ev = this->events.begin(); ev != events.end(); ev++)
255      player->subscribeEvent(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    this->currentPlayer->subscribeEvent(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    this->currentPlayer->unsubscribeEvent(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.