Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6954 was 6954, checked in by patrick, 18 years ago

network: shorter explosion

File size: 7.0 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
51  this->emitter = new DotEmitter(100, 5, M_2_PI);
52  this->emitter->setParent(this);
53  this->emitter->setSpread(M_PI, M_PI);
54  this->emitter->setEmissionRate(300.0);
55  this->emitter->setEmissionVelocity(50.0);
56
57  this->explosionParticles = new SpriteParticles(1000);
58  this->explosionParticles->setName("LaserExplosionParticles");
59  this->explosionParticles->setLifeSpan(.5, .3);
60  this->explosionParticles->setRadius(0.0, 10.0);
61  this->explosionParticles->setRadius(.5, 6.0);
62  this->explosionParticles->setRadius(1.0, 3.0);
63  this->explosionParticles->setColor(0.0, 1,1,0,.9);
64  this->explosionParticles->setColor(0.5, .8,.8,0,.5);
65  this->explosionParticles->setColor(1.0, .8,.8,.7,.0);
66}
67
68
69
70Playable::~Playable()
71{
72  delete this->weaponMan;
73
74  if (this->currentPlayer)
75  {
76    PRINTF(2)("There is Still a Player subscribed to this Playable (%s::%s)\n", this->getClassName(), this->getName());
77
78  }
79}
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->isA(CL_PROJECTILE) && !State::isOnline() )
125    this->decreaseHealth(entity->getHealth());
126
127  // EXTREME HACK
128  if (this->getHealth() == 0.0f)
129  {
130    //this->deactivateNode();
131    this->emitter->setSystem(explosionParticles);
132    this->setAbsCoor(0, 0, 0);
133    this->setAbsDir(0, 1, 0);
134    this->emitter->setSystem(NULL);
135  }
136}
137
138/**
139 * subscribe to all events the controllable needs
140 * @param player the player that shall controll this Playable
141 */
142bool Playable::subscribePlayer(Player* player)
143{
144  if (this->currentPlayer != NULL)
145  {
146    PRINTF(1)("Already registered Player:%s to this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName());
147    return false;
148  }
149  else
150  {
151    this->currentPlayer = player;
152    /*EventHandler*/
153    EventHandler* evh = EventHandler::getInstance();
154    std::list<int>::iterator ev;
155    for (ev = this->events.begin(); ev != events.end(); ev++)
156      evh->subscribe(player, ES_GAME, (*ev));
157    this->enter();
158    return true;
159  }
160}
161
162/**
163 * unsubscribe from all events the controllable needs
164 * @param player the Player, that controlled this Ship.
165 */
166bool Playable::unsubscribePlayer(Player* player)
167{
168  if (this->currentPlayer != player)
169  {
170    PRINTF(1)("not the right Player to unregister from this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName());
171    return false;
172  }
173
174  else
175  {
176    /*EventHandler*/
177    EventHandler* evh = EventHandler::getInstance();
178    std::list<int>::iterator ev;
179    for (ev = this->events.begin(); ev != events.end(); ev++)
180      evh->unsubscribe( ES_GAME, (*ev));
181
182    this->leave();
183    this->currentPlayer = NULL;
184    return true;
185  }
186}
187
188bool Playable::pickup(PowerUp* powerUp)
189{
190  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
191    Weapon* weapon = dynamic_cast<WeaponPowerUp*>(powerUp)->getWeapon();
192    WeaponManager* manager = this->getWeaponManager();
193    return manager->addWeapon(weapon);
194  }
195  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
196    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
197    switch(ppu->getType()) {
198      case POWERUP_PARAM_HEALTH:
199        this->increaseHealth(ppu->getValue());
200        return true;
201      case POWERUP_PARAM_MAX_HEALTH:
202        this->increaseHealthMax(ppu->getValue());
203        return true;
204    }
205  }
206  return false;
207}
208
209/**
210 * add an event to the event list of events this Playable can capture
211 * @param eventType the Type of event to add
212 */
213void Playable::registerEvent(int eventType)
214{
215  this->events.push_back(eventType);
216
217  if (this->currentPlayer != NULL)
218    EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType);
219}
220
221/**
222 * remove an event to the event list this Playable can capture.
223 * @param event the event to unregister.
224 */
225void Playable::unregisterEvent(int eventType)
226{
227  this->events.remove(eventType);
228
229  if (this->currentPlayer != NULL)
230    EventHandler::getInstance()->unsubscribe(ES_GAME, eventType);
231}
232
233/**
234 * @brief ticks a Playable
235 * @param dt: the passed time since the last Tick
236 */
237void Playable::tick(float dt)
238{
239  this->weaponMan->tick(dt);
240  if (this->bFire)
241    weaponMan->fire();
242}
243
244
245/**
246 * @brief processes Playable events.
247 * @param event the Captured Event.
248 */
249void Playable::process(const Event &event)
250{
251  if( event.type == KeyMapper::PEV_FIRE1)
252    this->bFire = event.bPressed;
253  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
254  {
255    this->nextWeaponConfig();
256  }
257  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
258    this->previousWeaponConfig();
259}
260
261
262
263void  Playable::attachCamera()
264{
265  State::getCamera()->setParentSoft(this);
266  State::getCameraTarget()->setParentSoft(this);
267
268}
269
270
271
272
273void  Playable::detachCamera()
274{
275}
276
277#define FLAGS_bFire   1
278
279int Playable::writeSync( const byte * data, int length, int sender )
280{
281  SYNCHELP_READ_BEGIN();
282
283  byte flags;
284
285  SYNCHELP_READ_BYTE( flags, NWT_PL_FLAGS );
286
287  bFire = (flags & FLAGS_bFire) != 0;
288
289  return SYNCHELP_READ_N;
290}
291
292int Playable::readSync( byte * data, int maxLength )
293{
294  SYNCHELP_WRITE_BEGIN();
295  byte flags = 0;
296
297  if ( bFire )
298    flags |= FLAGS_bFire;
299
300
301  SYNCHELP_WRITE_BYTE( flags, NWT_PL_FLAGS );
302  oldFlags = flags;
303
304
305  return SYNCHELP_WRITE_N;
306}
307
308bool Playable::needsReadSync( )
309{
310  byte flags = 0;
311
312  if ( bFire )
313    flags |= FLAGS_bFire;
314
315  return flags!=oldFlags;
316}
Note: See TracBrowser for help on using the repository browser.