Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7994 was 7954, checked in by patrick, 18 years ago

trunk: merged the network branche back to trunk.

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