Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7338 was 7338, checked in by bensch, 18 years ago

orxonox/trunk: some Playable-stuff

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