Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/world_entities/playable.cc @ 7938

Last change on this file since 7938 was 7698, checked in by patrick, 19 years ago

network: added new sync variable

File size: 12.4 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
63  this->bDead = false;
64
65  registerVar( new SynchronizeableInt( &score, &score, "score" ) );
66  registerVar( new SynchronizeableBool( &bFire, &bFire, "bFire", PERMISSION_OWNER));
67}
68
69
70/**
71 * @brief destroys the Playable
72 */
73Playable::~Playable()
74{
75  // THE DERIVED CLASS MUST UNSUBSCRIBE THE PLAYER THROUGH
76  // this->setPlayer(NULL);
77  // IN ITS DESTRUCTOR.
78  assert(this->currentPlayer == NULL);
79}
80
81/**
82 * @brief loads Playable parameters onto the Playable
83 * @param root the XML-root to load from
84 */
85void Playable::loadParams(const TiXmlElement* root)
86{
87  WorldEntity::loadParams(root);
88
89  LoadParam(root, "abs-dir", this, Playable, setPlayDirection);
90}
91
92/**
93 * @brief picks up a powerup
94 * @param powerUp the PowerUp that should be picked.
95 * @returns true on success (pickup was picked, false otherwise)
96 *
97 * This function also checks if the Pickup can be picked up by this Playable
98 */
99bool Playable::pickup(PowerUp* powerUp)
100{
101  if(powerUp->isA(CL_WEAPON_POWER_UP))
102  {
103    return dynamic_cast<WeaponPowerUp*>(powerUp)->process(&this->getWeaponManager());
104  }
105  else if(powerUp->isA(CL_PARAM_POWER_UP))
106  {
107    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
108    switch(ppu->getType())
109    {
110      case POWERUP_PARAM_HEALTH:
111        this->increaseHealth(ppu->getValue());
112        return true;
113      case POWERUP_PARAM_MAX_HEALTH:
114        this->increaseHealthMax(ppu->getValue());
115        return true;
116    }
117  }
118  return false;
119}
120
121/**
122 * @brief adds a Weapon to the Playable.
123 * @param weapon the Weapon to add.
124 * @param configID the Configuration ID to add this weapon to.
125 * @param slotID the slotID to add the Weapon to.
126 */
127bool Playable::addWeapon(Weapon* weapon, int configID, int slotID)
128{
129  if(this->weaponMan.addWeapon(weapon, configID, slotID))
130  {
131    this->weaponConfigChanged();
132    return true;
133  }
134  else
135  {
136    if (weapon != NULL)
137      PRINTF(2)("Unable to add Weapon (%s::%s) to %s::%s\n",
138                weapon->getClassName(), weapon->getName(), this->getClassName(), this->getName());
139    else
140      PRINTF(2)("No weapon defined\n");
141    return false;
142
143  }
144}
145
146/**
147 * @brief removes a Weapon.
148 * @param weapon the Weapon to remove.
149 */
150void Playable::removeWeapon(Weapon* weapon)
151{
152  this->weaponMan.removeWeapon(weapon);
153
154  this->weaponConfigChanged();
155}
156
157/**
158 * @brief jumps to the next WeaponConfiguration
159 */
160void Playable::nextWeaponConfig()
161{
162  this->weaponMan.nextWeaponConfig();
163  this->weaponConfigChanged();
164}
165
166/**
167 * @brief moves to the last WeaponConfiguration
168 */
169void Playable::previousWeaponConfig()
170{
171  this->weaponMan.previousWeaponConfig();
172  this->weaponConfigChanged();
173}
174
175/**
176 * @brief tells the Player, that the Weapon-Configuration has changed.
177 *
178 * TODO remove this
179 * This function is needed, so that the WeponManager of this Playable can easily update the HUD
180 */
181void Playable::weaponConfigChanged()
182{
183  if (this->currentPlayer != NULL)
184    this->currentPlayer->weaponConfigChanged();
185}
186
187/**
188 * @brief a Cheat that gives us some Weapons
189 */
190void Playable::addSomeWeapons_CHEAT()
191{
192  if (State::getPlayer() != NULL)
193  {
194    Playable* playable = State::getPlayer()->getPlayable();
195    if (playable != NULL)
196    {
197      PRINTF(2)("ADDING WEAPONS - you cheater\n");
198      playable->addWeapon(Weapon::createWeapon(CL_HYPERBLASTER));
199      playable->addWeapon(Weapon::createWeapon(CL_TURRET));
200      playable->addWeapon(Weapon::createWeapon(CL_AIMING_TURRET));
201      playable->addWeapon(Weapon::createWeapon(CL_CANNON));
202      playable->addWeapon(Weapon::createWeapon(CL_TARGETING_TURRET));
203      PRINTF(2)("ADDING WEAPONS FINISHED\n");
204    }
205  }
206}
207
208/**
209 * @brief subscribe to all events the controllable needs
210 * @param player the player that shall controll this Playable
211 * @returns false on error true otherwise.
212 */
213bool Playable::setPlayer(Player* player)
214{
215  // if we already have a Player inside do nothing
216  if (this->currentPlayer != NULL && player != NULL)
217  {
218    return false;
219  }
220
221  // eject the Player if player == NULL
222  if (this->currentPlayer != NULL && player == NULL)
223  {
224    PRINTF(4)("Player gets ejected\n");
225
226    // unsubscibe all events.
227    EventHandler* evh = EventHandler::getInstance();
228    std::vector<int>::iterator ev;
229    for (ev = this->events.begin(); ev != events.end(); ev++)
230      evh->unsubscribe( 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    EventHandler* evh = EventHandler::getInstance();
254    std::vector<int>::iterator ev;
255    for (ev = this->events.begin(); ev != events.end(); ev++)
256      evh->subscribe(player, ES_GAME, (*ev));
257
258    this->enter();
259    return true;
260  }
261
262  return false;
263}
264
265/**
266 * @brief attaches the current Camera to this Playable
267 *
268 * this function can be derived, so that any Playable can make the attachment differently.
269 */
270void Playable::attachCamera()
271{
272  State::getCameraNode()->setParentSoft(this);
273  State::getCameraTargetNode()->setParentSoft(this);
274
275}
276
277/**
278 * @brief detaches the Camera
279 * @see void Playable::attachCamera()
280 */
281void  Playable::detachCamera()
282{}
283
284
285/**
286 * @brief sets the CameraMode.
287 * @param cameraMode: the Mode to switch to.
288 */
289void Playable::setCameraMode(unsigned int cameraMode)
290{
291  State::getCamera()->setViewMode((Camera::ViewMode)cameraMode);
292}
293
294
295/**
296 * @brief sets the Playmode
297 * @param playmode the Playmode
298 * @returns true on success, false otherwise
299 */
300bool Playable::setPlaymode(Playable::Playmode playmode)
301{
302  if (!this->playmodeSupported(playmode))
303    return false;
304  else
305  {
306    this->enterPlaymode(playmode);
307    this->playmode = playmode;
308    return true;
309  }
310}
311
312/**
313 * @brief This function look, that the Playable rotates to the given rotation.
314 * @param angle the Angle around
315 * @param dirX directionX
316 * @param dirY directionY
317 * @param dirZ directionZ
318 * @param speed how fast to turn there.
319 */
320void Playable::setPlayDirection(float angle, float dirX, float dirY, float dirZ, float speed)
321{
322  this->setPlayDirection(Quaternion(angle, Vector(dirX, dirY, dirZ)), speed);
323}
324
325/**
326 * @brief all Playable will enter the Playmode Differently, say here how to do it.
327 * @param playmode the Playmode to enter.
328 *
329 * In this function all the actions that are required to enter the Playmode are described.
330 * e.g: camera, rotation, wait cycle and so on...
331 *
332 * on enter of this function the playmode is still the old playmode.
333 */
334void Playable::enterPlaymode(Playable::Playmode playmode)
335{
336  switch(playmode)
337  {
338    default:
339      this->attachCamera();
340      break;
341    case Playable::Horizontal:
342      this->setCameraMode(Camera::ViewTop);
343      break;
344    case Playable::Vertical:
345      this->setCameraMode(Camera::ViewLeft);
346      break;
347    case Playable::FromBehind:
348      this->setCameraMode(Camera::ViewBehind);
349      break;
350  }
351}
352/**
353 * @brief helps us colliding Playables
354 * @param entity the Entity to collide
355 * @param location where the collision occured.
356 */
357void Playable::collidesWith(WorldEntity* entity, const Vector& location)
358{
359  if (entity == collider)
360    return;
361  collider = entity;
362
363  if ( entity->isA(CL_PROJECTILE) && ( !State::isOnline() || SharedNetworkData::getInstance()->isGameServer() ) )
364  {
365    this->decreaseHealth(entity->getHealth() *(float)rand()/(float)RAND_MAX);
366    // EXTREME HACK
367    if (this->getHealth() <= 0.0f)
368    {
369      this->die();
370    }
371  }
372}
373
374
375void Playable::respawn()
376{
377  PRINTF(0)("Playable respawn\n");
378  // only if this is the spaceship of the player
379  if( this == State::getPlayer()->getPlayable())
380    State::getGameRules()->onPlayerSpawn();
381
382
383  if( this->getOwner() % 2 == 0)
384  {
385    //     this->toList(OM_GROUP_00);
386    this->setAbsCoor(213.37, 57.71, -47.98);
387    this->setAbsDir(0, 0, 1, 0);
388  }
389  else
390  { // red team
391    //     this->toList(OM_GROUP_01);
392    this->setAbsCoor(-314.450, 40.701, 83.554);
393    this->setAbsDir(1.0, -0.015, -0.012, 0.011);
394  }
395  this->reset();
396  this->bDead = false;
397}
398
399
400
401void Playable::die()
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    EventHandler::getInstance()->subscribe(this->currentPlayer, 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    EventHandler::getInstance()->unsubscribe(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 == "Vertical")
493    return Playable::Vertical;
494  if (playmode == "Horizontal")
495    return Playable::Horizontal;
496  if (playmode == "FromBehind")
497    return Playable::FromBehind;
498  if (playmode == "Full3D")
499    return Playable::Full3D;
500
501  return Playable::Full3D;
502}
503
504
505/**
506 * @brief converts a playmode into a string.
507 * @param playmode the Playable::Playmode to convert.
508 * @returns the String.
509 */
510const char* Playable::playmodeToString(Playable::Playmode playmode)
511{
512  switch(playmode)
513  {
514    case Playable::Vertical:
515      return "Vertical";
516    case Playable::Horizontal:
517      return "Horizontal";
518    case Playable::FromBehind:
519      return "FromBehind";
520    case Playable::Full3D:
521      return "Full3D";
522
523    default:
524      return "Full3D";
525  }
526
527}
Note: See TracBrowser for help on using the repository browser.