Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6963 was 6963, checked in by rennerc, 18 years ago

playable: sync score

File size: 7.6 KB
RevLine 
[5838]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:
[5841]12   main-programmer: Silvan Nellen
13   co-programmer: Benjamin Knecht
[5838]14*/
15
[5881]16
[5838]17#include "playable.h"
[5895]18
19#include "weapons/weapon_manager.h"
[5875]20#include "event_handler.h"
21#include "player.h"
[6241]22#include "state.h"
[5838]23
[6700]24#include "world_entities/projectiles/projectile.h"
25
[6547]26#include "power_ups/weapon_power_up.h"
27#include "power_ups/param_power_up.h"
[5872]28
[6547]29
[6959]30#include "dot_emitter.h"
31#include "sprite_particles.h"
32
33
[5838]34Playable::Playable()
35{
[6442]36  this->setClassID(CL_PLAYABLE, "Playable");
37  PRINTF(4)("PLAYABLE INIT\n");
38
39  this->toList(OM_GROUP_01);
40  this->weaponMan = new WeaponManager(this);
41
42  // the reference to the Current Player is NULL, because we dont have one at the beginning.
43  this->currentPlayer = NULL;
[6695]44
[6804]45  this->bFire = false;
[6868]46  this->oldFlags = 0;
[6804]47
[6695]48  this->setSynchronized(true);
[6959]49
50  this->score = 0;
51  this->oldScore = 0;
52
53
54  this->emitter = new DotEmitter(100, 5, M_2_PI);
55  this->emitter->setParent(this);
56  this->emitter->setSpread(M_PI, M_PI);
57  this->emitter->setEmissionRate(300.0);
58  this->emitter->setEmissionVelocity(50.0);
59
60  this->explosionParticles = new SpriteParticles(1000);
61  this->explosionParticles->setName("LaserExplosionParticles");
62  this->explosionParticles->setLifeSpan(.5, .3);
63  this->explosionParticles->setRadius(0.0, 10.0);
64  this->explosionParticles->setRadius(.5, 6.0);
65  this->explosionParticles->setRadius(1.0, 3.0);
66  this->explosionParticles->setColor(0.0, 1,1,0,.9);
67  this->explosionParticles->setColor(0.5, .8,.8,0,.5);
68  this->explosionParticles->setColor(1.0, .8,.8,.7,.0);
[5838]69}
70
[6695]71
72
[5875]73Playable::~Playable()
[5838]74{
[5881]75  delete this->weaponMan;
[5895]76
77  if (this->currentPlayer)
78  {
79    PRINTF(2)("There is Still a Player subscribed to this Playable (%s::%s)\n", this->getClassName(), this->getName());
80
81  }
[5875]82}
83
[5898]84
[6443]85void Playable::addWeapon(Weapon* weapon, int configID, int slotID)
86{
[6676]87  this->weaponMan->addWeapon(weapon, configID, slotID);
[6443]88
[6578]89  this->weaponConfigChanged();
[6443]90}
91
[6695]92
[6443]93void Playable::removeWeapon(Weapon* weapon)
94{
95  this->weaponMan->removeWeapon(weapon);
96
[6568]97    this->weaponConfigChanged();
[6443]98}
99
[6695]100
[6444]101void Playable::nextWeaponConfig()
102{
103  this->weaponMan->nextWeaponConfig();
[6568]104    this->weaponConfigChanged();
[6444]105}
[6443]106
[6695]107
[6444]108void Playable::previousWeaponConfig()
109{
110  this->weaponMan->previousWeaponConfig();
[6568]111    this->weaponConfigChanged();
[6444]112}
113
[6695]114
[6568]115void Playable::weaponConfigChanged()
116{
[6578]117  if (this->currentPlayer != NULL)
118    this->currentPlayer->weaponConfigChanged();
[6568]119}
[6444]120
[6695]121
[5872]122/**
[6436]123 * @brief helps us colliding Playables
124 */
125void Playable::collidesWith(WorldEntity* entity, const Vector& location)
126{
[6959]127  if (entity->isA(CL_PROJECTILE) && !State::isOnline() )
[6700]128    this->decreaseHealth(entity->getHealth());
[6436]129
130  // EXTREME HACK
[6700]131  if (this->getHealth() == 0.0f)
[6959]132  {
133    //this->deactivateNode();
134    this->emitter->setSystem(explosionParticles);
135    this->setAbsCoor(0, 0, 0);
136    //this->setAbsDir(Vector(1,0,0), 0);
137    this->emitter->setSystem(NULL);
138  }
[6436]139}
140
141/**
[5872]142 * subscribe to all events the controllable needs
[5898]143 * @param player the player that shall controll this Playable
[5872]144 */
[5895]145bool Playable::subscribePlayer(Player* player)
[5872]146{
[5895]147  if (this->currentPlayer != NULL)
[5872]148  {
[5895]149    PRINTF(1)("Already registered Player:%s to this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName());
150    return false;
[5875]151  }
[5895]152  else
153  {
154    this->currentPlayer = player;
155    /*EventHandler*/
156    EventHandler* evh = EventHandler::getInstance();
157    std::list<int>::iterator ev;
158    for (ev = this->events.begin(); ev != events.end(); ev++)
159      evh->subscribe(player, ES_GAME, (*ev));
[6241]160    this->enter();
[5895]161    return true;
162  }
[5872]163}
[5889]164
165/**
[5898]166 * unsubscribe from all events the controllable needs
167 * @param player the Player, that controlled this Ship.
[5896]168 */
169bool Playable::unsubscribePlayer(Player* player)
170{
171  if (this->currentPlayer != player)
172  {
173    PRINTF(1)("not the right Player to unregister from this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName());
174    return false;
175  }
176
177  else
178  {
179    /*EventHandler*/
180    EventHandler* evh = EventHandler::getInstance();
181    std::list<int>::iterator ev;
182    for (ev = this->events.begin(); ev != events.end(); ev++)
183      evh->unsubscribe( ES_GAME, (*ev));
184
[6241]185    this->leave();
[5896]186    this->currentPlayer = NULL;
187    return true;
188  }
189}
190
[6547]191bool Playable::pickup(PowerUp* powerUp)
192{
193  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
194    Weapon* weapon = dynamic_cast<WeaponPowerUp*>(powerUp)->getWeapon();
195    WeaponManager* manager = this->getWeaponManager();
[6710]196    return manager->addWeapon(weapon);
[6547]197  }
198  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
199    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
200    switch(ppu->getType()) {
201      case POWERUP_PARAM_HEALTH:
[6700]202        this->increaseHealth(ppu->getValue());
[6547]203        return true;
204      case POWERUP_PARAM_MAX_HEALTH:
[6700]205        this->increaseHealthMax(ppu->getValue());
[6547]206        return true;
207    }
208  }
209  return false;
210}
211
[5896]212/**
[5898]213 * add an event to the event list of events this Playable can capture
214 * @param eventType the Type of event to add
[5889]215 */
[5896]216void Playable::registerEvent(int eventType)
[5889]217{
218  this->events.push_back(eventType);
219
[5896]220  if (this->currentPlayer != NULL)
221    EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType);
[5889]222}
223
[5896]224/**
[5898]225 * remove an event to the event list this Playable can capture.
226 * @param event the event to unregister.
[5896]227 */
228void Playable::unregisterEvent(int eventType)
229{
[5902]230  this->events.remove(eventType);
[5889]231
[5896]232  if (this->currentPlayer != NULL)
233    EventHandler::getInstance()->unsubscribe(ES_GAME, eventType);
234}
[5889]235
[6804]236/**
237 * @brief ticks a Playable
238 * @param dt: the passed time since the last Tick
239 */
240void Playable::tick(float dt)
241{
242  this->weaponMan->tick(dt);
243  if (this->bFire)
244    weaponMan->fire();
245}
[5896]246
[6804]247
248/**
249 * @brief processes Playable events.
250 * @param event the Captured Event.
251 */
252void Playable::process(const Event &event)
253{
254  if( event.type == KeyMapper::PEV_FIRE1)
255    this->bFire = event.bPressed;
256  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
257  {
258    this->nextWeaponConfig();
259  }
260  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
261    this->previousWeaponConfig();
262}
263
264
265
[6241]266void  Playable::attachCamera()
267{
[6444]268  State::getCamera()->setParentSoft(this);
269  State::getCameraTarget()->setParentSoft(this);
[6241]270
271}
272
273
[6804]274
275
[6241]276void  Playable::detachCamera()
277{
278}
[6868]279
[6959]280#define DATA_FLAGS    1
281#define DATA_SCORE    2
282
[6868]283#define FLAGS_bFire   1
284
285int Playable::writeSync( const byte * data, int length, int sender )
286{
287  SYNCHELP_READ_BEGIN();
[6963]288 
289  byte b;
290  SYNCHELP_READ_BYTE( b, NWT_PL_B );
291 
[6868]292  byte flags;
[6963]293 
294  if ( b == DATA_FLAGS )
295  {
296    SYNCHELP_READ_BYTE( flags, NWT_PL_FLAGS );
[6871]297
[6963]298    bFire = (flags & FLAGS_bFire) != 0;
299   
300    return SYNCHELP_READ_N;
301  }
302 
303  if ( b == DATA_SCORE )
304  {
305    int newScore;
306    SYNCHELP_READ_BYTE( newScore, NWT_PL_SCORE );
307    setScore( newScore );
308   
309    return SYNCHELP_READ_N;
310  }
[6871]311
[6868]312  return SYNCHELP_READ_N;
313}
314
315int Playable::readSync( byte * data, int maxLength )
316{
317  SYNCHELP_WRITE_BEGIN();
[6963]318 
319  if ( score != oldScore && isServer() )
320  {
321    SYNCHELP_WRITE_BYTE( DATA_SCORE, NWT_PL_B);
322    SYNCHELP_WRITE_INT( score, NWT_PL_SCORE );
323    oldScore = score;
324   
325    return SYNCHELP_WRITE_N;
326  }
327 
[6868]328  byte flags = 0;
[6871]329
[6868]330  if ( bFire )
331    flags |= FLAGS_bFire;
332
[6871]333
[6868]334  SYNCHELP_WRITE_BYTE( flags, NWT_PL_FLAGS );
335  oldFlags = flags;
336
[6871]337
[6868]338  return SYNCHELP_WRITE_N;
339}
340
341bool Playable::needsReadSync( )
342{
[6963]343  if ( score != oldScore && isServer() )
344    return true;
[6959]345
[6868]346  byte flags = 0;
[6871]347
[6868]348  if ( bFire )
349    flags |= FLAGS_bFire;
[6871]350
[6868]351  return flags!=oldFlags;
352}
Note: See TracBrowser for help on using the repository browser.