Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/resources/src/world_entities/playable.cc @ 7793

Last change on this file since 7793 was 7193, checked in by bensch, 19 years ago

orxonox/trunk: new style for resources (prework/movement)

File size: 8.5 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 "weapons/weapon_manager.h"
20#include "event_handler.h"
21#include "player.h"
22#include "state.h"
23#include "util/loading/load_param.h"
24
25#include "world_entities/projectiles/projectile.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
39
40Playable::Playable()
41{
42  this->setClassID(CL_PLAYABLE, "Playable");
43  PRINTF(4)("PLAYABLE INIT\n");
44
45  this->toList(OM_GROUP_01);
46  this->weaponMan = new WeaponManager(this);
47
48  // the reference to the Current Player is NULL, because we dont have one at the beginning.
49  this->currentPlayer = NULL;
50
51  this->bFire = false;
52  this->oldFlags = 0;
53
54  this->setSynchronized(true);
55
56  this->score = 0;
57  this->oldScore = 0;
58
59  this->bDead = false;
60}
61
62
63
64Playable::~Playable()
65{
66  delete this->weaponMan;
67
68  // THE DERIVED CLASS MUST UNSUBSCRIBE THE PLAYER THROUGH
69  // this->setPlayer(NULL);
70  // IN ITS DESTRUCTOR.
71  assert(this->currentPlayer == NULL);
72}
73
74
75void Playable::loadParams(const TiXmlElement* root)
76{
77  WorldEntity::loadParams(root);
78
79  LoadParam(root, "abs-dir", this, Playable, setAbsDirPlay);
80}
81
82void Playable::addWeapon(Weapon* weapon, int configID, int slotID)
83{
84  this->weaponMan->addWeapon(weapon, configID, slotID);
85
86  this->weaponConfigChanged();
87}
88
89
90void Playable::removeWeapon(Weapon* weapon)
91{
92  this->weaponMan->removeWeapon(weapon);
93
94    this->weaponConfigChanged();
95}
96
97
98void Playable::nextWeaponConfig()
99{
100  this->weaponMan->nextWeaponConfig();
101    this->weaponConfigChanged();
102}
103
104
105void Playable::previousWeaponConfig()
106{
107  this->weaponMan->previousWeaponConfig();
108    this->weaponConfigChanged();
109}
110
111
112void Playable::weaponConfigChanged()
113{
114  if (this->currentPlayer != NULL)
115    this->currentPlayer->weaponConfigChanged();
116}
117
118/**
119 * @brief sets the CameraMode.
120 * @param cameraMode: the Mode to switch to.
121 */
122void Playable::setCameraMode(unsigned int cameraMode)
123{
124
125}
126
127
128/**
129 * @brief helps us colliding Playables
130 */
131void Playable::collidesWith(WorldEntity* entity, const Vector& location)
132{
133  if (entity == collider)
134    return;
135  collider = entity;
136
137  if ( entity->isA(CL_PROJECTILE) && ( !State::isOnline() || SharedNetworkData::getInstance()->isGameServer() ) )
138  {
139    this->decreaseHealth(entity->getHealth() *(float)rand()/(float)RAND_MAX);
140    // EXTREME HACK
141    if (this->getHealth() <= 0.0f)
142    {
143      this->die();
144    }
145  }
146}
147
148
149void Playable::respawn()
150{
151  PRINTF(0)("Playable respawn\n");
152  // only if this is the spaceship of the player
153  if( this == State::getPlayer()->getPlayable())
154    State::getGameRules()->onPlayerSpawn();
155
156
157  if( this->getOwner() % 2 == 0)
158  {
159//     this->toList(OM_GROUP_00);
160    this->setAbsCoor(213.37, 57.71, -47.98);
161    this->setAbsDir(0, 0, 1, 0);
162  }
163  else
164  { // red team
165//     this->toList(OM_GROUP_01);
166    this->setAbsCoor(-314.450, 40.701, 83.554);
167    this->setAbsDir(1.0, -0.015, -0.012, 0.011);
168  }
169  this->reset();
170  this->bDead = false;
171}
172
173
174
175void Playable::die()
176{
177  Explosion::explode(dynamic_cast<PNode*>(this), Vector(1.0f, 1.0f, 1.0f));
178
179
180  if( !this->bDead)
181  {
182    PRINTF(0)("Playable dies\n");
183  // only if this is the spaceship of the player
184    if (State::isOnline())
185    {
186      if( this == State::getPlayer()->getPlayable())
187        State::getGameRules()->onPlayerDeath();
188
189//     this->toList(OM_GROUP_05);
190  //HACK: moves the entity to an unknown place far far away: in the future, GameRules will look for that
191      this->setAbsCoor(-2000.0, -2000.0, -2000.0);
192
193  //explosion hack
194
195    }
196    this->bDead = true;
197  }
198}
199
200
201/**
202 * subscribe to all events the controllable needs
203 * @param player the player that shall controll this Playable
204 */
205bool Playable::setPlayer(Player* player)
206{
207  // if we already have a Player inside do nothing
208  if (this->currentPlayer != NULL && player != NULL)
209  {
210    return false;
211  }
212
213  // eject the Player if player == NULL
214  if (this->currentPlayer != NULL && player == NULL)
215  {
216    PRINTF(4)("Player gets ejected\n");
217
218    // unsubscibe all events.
219    EventHandler* evh = EventHandler::getInstance();
220    std::list<int>::iterator ev;
221    for (ev = this->events.begin(); ev != events.end(); ev++)
222      evh->unsubscribe( ES_GAME, (*ev));
223
224    // leave the entity
225    this->leave();
226
227    // eject the current Player.
228    Player* ejectPlayer = this->currentPlayer;
229    this->currentPlayer = NULL;
230    // eject the Player.
231    ejectPlayer->setPlayable(NULL);
232
233    return true;
234  }
235
236  // get the new Player inside
237  if (this->currentPlayer == NULL && player != NULL)
238  {
239    PRINTF(4)("New Player gets inside\n");
240    this->currentPlayer = player;
241    if (this->currentPlayer->getPlayable() != this)
242      this->currentPlayer->setPlayable(this);
243
244    /*EventHandler*/
245    EventHandler* evh = EventHandler::getInstance();
246    std::list<int>::iterator ev;
247    for (ev = this->events.begin(); ev != events.end(); ev++)
248      evh->subscribe(player, ES_GAME, (*ev));
249
250    this->enter();
251    return true;
252  }
253
254  return false;
255}
256
257
258bool Playable::pickup(PowerUp* powerUp)
259{
260  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
261    return dynamic_cast<WeaponPowerUp*>(powerUp)->process(this->getWeaponManager());
262  }
263  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
264    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
265    switch(ppu->getType()) {
266      case POWERUP_PARAM_HEALTH:
267        this->increaseHealth(ppu->getValue());
268        return true;
269      case POWERUP_PARAM_MAX_HEALTH:
270        this->increaseHealthMax(ppu->getValue());
271        return true;
272    }
273  }
274  return false;
275}
276
277/**
278 * add an event to the event list of events this Playable can capture
279 * @param eventType the Type of event to add
280 */
281void Playable::registerEvent(int eventType)
282{
283  this->events.push_back(eventType);
284
285  if (this->currentPlayer != NULL)
286    EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType);
287}
288
289/**
290 * remove an event to the event list this Playable can capture.
291 * @param event the event to unregister.
292 */
293void Playable::unregisterEvent(int eventType)
294{
295  this->events.remove(eventType);
296
297  if (this->currentPlayer != NULL)
298    EventHandler::getInstance()->unsubscribe(ES_GAME, eventType);
299}
300
301/**
302 * @brief ticks a Playable
303 * @param dt: the passed time since the last Tick
304 */
305void Playable::tick(float dt)
306{
307  this->weaponMan->tick(dt);
308  if (this->bFire)
309    weaponMan->fire();
310}
311
312
313/**
314 * @brief processes Playable events.
315 * @param event the Captured Event.
316 */
317void Playable::process(const Event &event)
318{
319  if( event.type == KeyMapper::PEV_FIRE1)
320    this->bFire = event.bPressed;
321  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
322  {
323    this->nextWeaponConfig();
324  }
325  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
326    this->previousWeaponConfig();
327}
328
329
330
331void  Playable::attachCamera()
332{
333  State::getCameraNode()->setParentSoft(this);
334  State::getCameraTargetNode()->setParentSoft(this);
335
336}
337
338
339
340
341void  Playable::detachCamera()
342{
343}
344
345#define DATA_FLAGS    1
346#define DATA_SCORE    2
347
348#define FLAGS_bFire   1
349
350int Playable::writeSync( const byte * data, int length, int sender )
351{
352  SYNCHELP_READ_BEGIN();
353
354  byte b;
355  SYNCHELP_READ_BYTE( b, NWT_PL_B );
356
357  byte flags;
358
359  if ( b == DATA_FLAGS )
360  {
361    SYNCHELP_READ_BYTE( flags, NWT_PL_FLAGS );
362
363    bFire = (flags & FLAGS_bFire) != 0;
364
365    return SYNCHELP_READ_N;
366  }
367
368  if ( b == DATA_SCORE )
369  {
370    int newScore;
371    SYNCHELP_READ_BYTE( newScore, NWT_PL_SCORE );
372    setScore( newScore );
373
374    return SYNCHELP_READ_N;
375  }
376
377  return SYNCHELP_READ_N;
378}
379
380int Playable::readSync( byte * data, int maxLength )
381{
382  SYNCHELP_WRITE_BEGIN();
383
384  if ( score != oldScore && isServer() )
385  {
386    SYNCHELP_WRITE_BYTE( DATA_SCORE, NWT_PL_B);
387    SYNCHELP_WRITE_INT( score, NWT_PL_SCORE );
388    oldScore = score;
389
390    return SYNCHELP_WRITE_N;
391  }
392
393  byte flags = 0;
394
395  if ( bFire )
396    flags |= FLAGS_bFire;
397
398
399  SYNCHELP_WRITE_BYTE( DATA_FLAGS, NWT_PL_B);
400  SYNCHELP_WRITE_BYTE( flags, NWT_PL_FLAGS );
401  oldFlags = flags;
402
403
404  return SYNCHELP_WRITE_N;
405}
406
407bool Playable::needsReadSync( )
408{
409  if ( score != oldScore && isServer() )
410    return true;
411
412  byte flags = 0;
413
414  if ( bFire )
415    flags |= FLAGS_bFire;
416
417  return flags!=oldFlags;
418}
Note: See TracBrowser for help on using the repository browser.