Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7160 was 7121, checked in by rennerc, 19 years ago

fix: only gameserver checks for collision

File size: 8.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
19#include "weapons/weapon_manager.h"
20#include "event_handler.h"
21#include "player.h"
22#include "state.h"
23#include "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/**
120 * @brief helps us colliding Playables
121 */
122void Playable::collidesWith(WorldEntity* entity, const Vector& location)
123{
124  if (entity == collider)
125    return;
126  collider = entity;
127
128  if ( entity->isA(CL_PROJECTILE) && ( !State::isOnline() || SharedNetworkData::getInstance()->isGameServer() ) )
129  {
130    this->decreaseHealth(entity->getHealth() *(float)rand()/(float)RAND_MAX);
131    // EXTREME HACK
132    if (this->getHealth() <= 0.0f)
133    {
134      this->die();
135    }
136  }
137}
138
139
140void Playable::respawn()
141{
142  PRINTF(0)("Playable respawn\n");
143  // only if this is the spaceship of the player
144  if( this == State::getPlayer()->getPlayable())
145    State::getGameRules()->onPlayerSpawn();
146
147
148  if( this->getOwner() % 2 == 0)
149  {
150//     this->toList(OM_GROUP_00);
151    this->setAbsCoor(213.37, 57.71, -47.98);
152    this->setAbsDir(0, 0, 1, 0);
153  }
154  else
155  { // red team
156//     this->toList(OM_GROUP_01);
157    this->setAbsCoor(-314.450, 40.701, 83.554);
158    this->setAbsDir(1.0, -0.015, -0.012, 0.011);
159  }
160  this->reset();
161  this->bDead = false;
162}
163
164
165
166void Playable::die()
167{
168  Explosion::explode(dynamic_cast<PNode*>(this), Vector(1.0f, 1.0f, 1.0f));
169
170
171  if( !this->bDead)
172  {
173    PRINTF(0)("Playable dies\n");
174  // only if this is the spaceship of the player
175    if (State::isOnline())
176    {
177      if( this == State::getPlayer()->getPlayable())
178        State::getGameRules()->onPlayerDeath();
179
180//     this->toList(OM_GROUP_05);
181  //HACK: moves the entity to an unknown place far far away: in the future, GameRules will look for that
182      this->setAbsCoor(-2000.0, -2000.0, -2000.0);
183
184  //explosion hack
185
186    }
187    this->bDead = true;
188  }
189}
190
191
192/**
193 * subscribe to all events the controllable needs
194 * @param player the player that shall controll this Playable
195 */
196bool Playable::setPlayer(Player* player)
197{
198  // if we already have a Player inside do nothing
199  if (this->currentPlayer != NULL && player != NULL)
200  {
201    return false;
202  }
203
204  // eject the Player if player == NULL
205  if (this->currentPlayer != NULL && player == NULL)
206  {
207    PRINTF(4)("Player gets ejected\n");
208
209    // unsubscibe all events.
210    EventHandler* evh = EventHandler::getInstance();
211    std::list<int>::iterator ev;
212    for (ev = this->events.begin(); ev != events.end(); ev++)
213      evh->unsubscribe( ES_GAME, (*ev));
214
215    // leave the entity
216    this->leave();
217
218    // eject the current Player.
219    Player* ejectPlayer = this->currentPlayer;
220    this->currentPlayer = NULL;
221    // eject the Player.
222    ejectPlayer->setPlayable(NULL);
223
224    return true;
225  }
226
227  // get the new Player inside
228  if (this->currentPlayer == NULL && player != NULL)
229  {
230    PRINTF(4)("New Player gets inside\n");
231    this->currentPlayer = player;
232    if (this->currentPlayer->getPlayable() != this)
233      this->currentPlayer->setPlayable(this);
234
235    /*EventHandler*/
236    EventHandler* evh = EventHandler::getInstance();
237    std::list<int>::iterator ev;
238    for (ev = this->events.begin(); ev != events.end(); ev++)
239      evh->subscribe(player, ES_GAME, (*ev));
240
241    this->enter();
242    return true;
243  }
244
245  return false;
246}
247
248
249bool Playable::pickup(PowerUp* powerUp)
250{
251  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
252    return dynamic_cast<WeaponPowerUp*>(powerUp)->process(this->getWeaponManager());
253  }
254  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
255    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
256    switch(ppu->getType()) {
257      case POWERUP_PARAM_HEALTH:
258        this->increaseHealth(ppu->getValue());
259        return true;
260      case POWERUP_PARAM_MAX_HEALTH:
261        this->increaseHealthMax(ppu->getValue());
262        return true;
263    }
264  }
265  return false;
266}
267
268/**
269 * add an event to the event list of events this Playable can capture
270 * @param eventType the Type of event to add
271 */
272void Playable::registerEvent(int eventType)
273{
274  this->events.push_back(eventType);
275
276  if (this->currentPlayer != NULL)
277    EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType);
278}
279
280/**
281 * remove an event to the event list this Playable can capture.
282 * @param event the event to unregister.
283 */
284void Playable::unregisterEvent(int eventType)
285{
286  this->events.remove(eventType);
287
288  if (this->currentPlayer != NULL)
289    EventHandler::getInstance()->unsubscribe(ES_GAME, eventType);
290}
291
292/**
293 * @brief ticks a Playable
294 * @param dt: the passed time since the last Tick
295 */
296void Playable::tick(float dt)
297{
298  this->weaponMan->tick(dt);
299  if (this->bFire)
300    weaponMan->fire();
301}
302
303
304/**
305 * @brief processes Playable events.
306 * @param event the Captured Event.
307 */
308void Playable::process(const Event &event)
309{
310  if( event.type == KeyMapper::PEV_FIRE1)
311    this->bFire = event.bPressed;
312  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
313  {
314    this->nextWeaponConfig();
315  }
316  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
317    this->previousWeaponConfig();
318}
319
320
321
322void  Playable::attachCamera()
323{
324  State::getCameraNode()->setParentSoft(this);
325  State::getCameraTargetNode()->setParentSoft(this);
326
327}
328
329
330
331
332void  Playable::detachCamera()
333{
334}
335
336#define DATA_FLAGS    1
337#define DATA_SCORE    2
338
339#define FLAGS_bFire   1
340
341int Playable::writeSync( const byte * data, int length, int sender )
342{
343  SYNCHELP_READ_BEGIN();
344
345  byte b;
346  SYNCHELP_READ_BYTE( b, NWT_PL_B );
347
348  byte flags;
349
350  if ( b == DATA_FLAGS )
351  {
352    SYNCHELP_READ_BYTE( flags, NWT_PL_FLAGS );
353
354    bFire = (flags & FLAGS_bFire) != 0;
355
356    return SYNCHELP_READ_N;
357  }
358
359  if ( b == DATA_SCORE )
360  {
361    int newScore;
362    SYNCHELP_READ_BYTE( newScore, NWT_PL_SCORE );
363    setScore( newScore );
364
365    return SYNCHELP_READ_N;
366  }
367
368  return SYNCHELP_READ_N;
369}
370
371int Playable::readSync( byte * data, int maxLength )
372{
373  SYNCHELP_WRITE_BEGIN();
374
375  if ( score != oldScore && isServer() )
376  {
377    SYNCHELP_WRITE_BYTE( DATA_SCORE, NWT_PL_B);
378    SYNCHELP_WRITE_INT( score, NWT_PL_SCORE );
379    oldScore = score;
380
381    return SYNCHELP_WRITE_N;
382  }
383
384  byte flags = 0;
385
386  if ( bFire )
387    flags |= FLAGS_bFire;
388
389
390  SYNCHELP_WRITE_BYTE( DATA_FLAGS, NWT_PL_B);
391  SYNCHELP_WRITE_BYTE( flags, NWT_PL_FLAGS );
392  oldFlags = flags;
393
394
395  return SYNCHELP_WRITE_N;
396}
397
398bool Playable::needsReadSync( )
399{
400  if ( score != oldScore && isServer() )
401    return true;
402
403  byte flags = 0;
404
405  if ( bFire )
406    flags |= FLAGS_bFire;
407
408  return flags!=oldFlags;
409}
Note: See TracBrowser for help on using the repository browser.