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
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
24#include "world_entities/projectiles/projectile.h"
25
26#include "power_ups/weapon_power_up.h"
27#include "power_ups/param_power_up.h"
28
29
30#include "dot_emitter.h"
31#include "sprite_particles.h"
32
33
34Playable::Playable()
35{
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;
44
45  this->bFire = false;
46  this->oldFlags = 0;
47
48  this->setSynchronized(true);
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);
69}
70
71
72
73Playable::~Playable()
74{
75  delete this->weaponMan;
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  }
82}
83
84
85void Playable::addWeapon(Weapon* weapon, int configID, int slotID)
86{
87  this->weaponMan->addWeapon(weapon, configID, slotID);
88
89  this->weaponConfigChanged();
90}
91
92
93void Playable::removeWeapon(Weapon* weapon)
94{
95  this->weaponMan->removeWeapon(weapon);
96
97    this->weaponConfigChanged();
98}
99
100
101void Playable::nextWeaponConfig()
102{
103  this->weaponMan->nextWeaponConfig();
104    this->weaponConfigChanged();
105}
106
107
108void Playable::previousWeaponConfig()
109{
110  this->weaponMan->previousWeaponConfig();
111    this->weaponConfigChanged();
112}
113
114
115void Playable::weaponConfigChanged()
116{
117  if (this->currentPlayer != NULL)
118    this->currentPlayer->weaponConfigChanged();
119}
120
121
122/**
123 * @brief helps us colliding Playables
124 */
125void Playable::collidesWith(WorldEntity* entity, const Vector& location)
126{
127  if (entity->isA(CL_PROJECTILE) && !State::isOnline() )
128    this->decreaseHealth(entity->getHealth());
129
130  // EXTREME HACK
131  if (this->getHealth() == 0.0f)
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  }
139}
140
141/**
142 * subscribe to all events the controllable needs
143 * @param player the player that shall controll this Playable
144 */
145bool Playable::subscribePlayer(Player* player)
146{
147  if (this->currentPlayer != NULL)
148  {
149    PRINTF(1)("Already registered Player:%s to this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName());
150    return false;
151  }
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));
160    this->enter();
161    return true;
162  }
163}
164
165/**
166 * unsubscribe from all events the controllable needs
167 * @param player the Player, that controlled this Ship.
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
185    this->leave();
186    this->currentPlayer = NULL;
187    return true;
188  }
189}
190
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();
196    return manager->addWeapon(weapon);
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:
202        this->increaseHealth(ppu->getValue());
203        return true;
204      case POWERUP_PARAM_MAX_HEALTH:
205        this->increaseHealthMax(ppu->getValue());
206        return true;
207    }
208  }
209  return false;
210}
211
212/**
213 * add an event to the event list of events this Playable can capture
214 * @param eventType the Type of event to add
215 */
216void Playable::registerEvent(int eventType)
217{
218  this->events.push_back(eventType);
219
220  if (this->currentPlayer != NULL)
221    EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType);
222}
223
224/**
225 * remove an event to the event list this Playable can capture.
226 * @param event the event to unregister.
227 */
228void Playable::unregisterEvent(int eventType)
229{
230  this->events.remove(eventType);
231
232  if (this->currentPlayer != NULL)
233    EventHandler::getInstance()->unsubscribe(ES_GAME, eventType);
234}
235
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}
246
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
266void  Playable::attachCamera()
267{
268  State::getCamera()->setParentSoft(this);
269  State::getCameraTarget()->setParentSoft(this);
270
271}
272
273
274
275
276void  Playable::detachCamera()
277{
278}
279
280#define DATA_FLAGS    1
281#define DATA_SCORE    2
282
283#define FLAGS_bFire   1
284
285int Playable::writeSync( const byte * data, int length, int sender )
286{
287  SYNCHELP_READ_BEGIN();
288 
289  byte b;
290  SYNCHELP_READ_BYTE( b, NWT_PL_B );
291 
292  byte flags;
293 
294  if ( b == DATA_FLAGS )
295  {
296    SYNCHELP_READ_BYTE( flags, NWT_PL_FLAGS );
297
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  }
311
312  return SYNCHELP_READ_N;
313}
314
315int Playable::readSync( byte * data, int maxLength )
316{
317  SYNCHELP_WRITE_BEGIN();
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 
328  byte flags = 0;
329
330  if ( bFire )
331    flags |= FLAGS_bFire;
332
333
334  SYNCHELP_WRITE_BYTE( flags, NWT_PL_FLAGS );
335  oldFlags = flags;
336
337
338  return SYNCHELP_WRITE_N;
339}
340
341bool Playable::needsReadSync( )
342{
343  if ( score != oldScore && isServer() )
344    return true;
345
346  byte flags = 0;
347
348  if ( bFire )
349    flags |= FLAGS_bFire;
350
351  return flags!=oldFlags;
352}
Note: See TracBrowser for help on using the repository browser.