Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/multi_player_map/src/world_entities/playable.cc @ 8841

Last change on this file since 8841 was 8838, checked in by patrick, 18 years ago

mulitplayer: debuggin session

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