Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

network: explosion added

File size: 6.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
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  }
135}
136
137/**
138 * subscribe to all events the controllable needs
139 * @param player the player that shall controll this Playable
140 */
141bool Playable::subscribePlayer(Player* player)
142{
143  if (this->currentPlayer != NULL)
144  {
145    PRINTF(1)("Already registered Player:%s to this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName());
146    return false;
147  }
148  else
149  {
150    this->currentPlayer = player;
151    /*EventHandler*/
152    EventHandler* evh = EventHandler::getInstance();
153    std::list<int>::iterator ev;
154    for (ev = this->events.begin(); ev != events.end(); ev++)
155      evh->subscribe(player, ES_GAME, (*ev));
156    this->enter();
157    return true;
158  }
159}
160
161/**
162 * unsubscribe from all events the controllable needs
163 * @param player the Player, that controlled this Ship.
164 */
165bool Playable::unsubscribePlayer(Player* player)
166{
167  if (this->currentPlayer != player)
168  {
169    PRINTF(1)("not the right Player to unregister from this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName());
170    return false;
171  }
172
173  else
174  {
175    /*EventHandler*/
176    EventHandler* evh = EventHandler::getInstance();
177    std::list<int>::iterator ev;
178    for (ev = this->events.begin(); ev != events.end(); ev++)
179      evh->unsubscribe( ES_GAME, (*ev));
180
181    this->leave();
182    this->currentPlayer = NULL;
183    return true;
184  }
185}
186
187bool Playable::pickup(PowerUp* powerUp)
188{
189  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
190    Weapon* weapon = dynamic_cast<WeaponPowerUp*>(powerUp)->getWeapon();
191    WeaponManager* manager = this->getWeaponManager();
192    return manager->addWeapon(weapon);
193  }
194  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
195    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
196    switch(ppu->getType()) {
197      case POWERUP_PARAM_HEALTH:
198        this->increaseHealth(ppu->getValue());
199        return true;
200      case POWERUP_PARAM_MAX_HEALTH:
201        this->increaseHealthMax(ppu->getValue());
202        return true;
203    }
204  }
205  return false;
206}
207
208/**
209 * add an event to the event list of events this Playable can capture
210 * @param eventType the Type of event to add
211 */
212void Playable::registerEvent(int eventType)
213{
214  this->events.push_back(eventType);
215
216  if (this->currentPlayer != NULL)
217    EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType);
218}
219
220/**
221 * remove an event to the event list this Playable can capture.
222 * @param event the event to unregister.
223 */
224void Playable::unregisterEvent(int eventType)
225{
226  this->events.remove(eventType);
227
228  if (this->currentPlayer != NULL)
229    EventHandler::getInstance()->unsubscribe(ES_GAME, eventType);
230}
231
232/**
233 * @brief ticks a Playable
234 * @param dt: the passed time since the last Tick
235 */
236void Playable::tick(float dt)
237{
238  this->weaponMan->tick(dt);
239  if (this->bFire)
240    weaponMan->fire();
241}
242
243
244/**
245 * @brief processes Playable events.
246 * @param event the Captured Event.
247 */
248void Playable::process(const Event &event)
249{
250  if( event.type == KeyMapper::PEV_FIRE1)
251    this->bFire = event.bPressed;
252  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
253  {
254    this->nextWeaponConfig();
255  }
256  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
257    this->previousWeaponConfig();
258}
259
260
261
262void  Playable::attachCamera()
263{
264  State::getCamera()->setParentSoft(this);
265  State::getCameraTarget()->setParentSoft(this);
266
267}
268
269
270
271
272void  Playable::detachCamera()
273{
274}
275
276#define FLAGS_bFire   1
277
278int Playable::writeSync( const byte * data, int length, int sender )
279{
280  SYNCHELP_READ_BEGIN();
281
282  byte flags;
283
284  SYNCHELP_READ_BYTE( flags, NWT_PL_FLAGS );
285
286  bFire = (flags & FLAGS_bFire) != 0;
287
288  return SYNCHELP_READ_N;
289}
290
291int Playable::readSync( byte * data, int maxLength )
292{
293  SYNCHELP_WRITE_BEGIN();
294  byte flags = 0;
295
296  if ( bFire )
297    flags |= FLAGS_bFire;
298
299
300  SYNCHELP_WRITE_BYTE( flags, NWT_PL_FLAGS );
301  oldFlags = flags;
302
303
304  return SYNCHELP_WRITE_N;
305}
306
307bool Playable::needsReadSync( )
308{
309  byte flags = 0;
310
311  if ( bFire )
312    flags |= FLAGS_bFire;
313
314  return flags!=oldFlags;
315}
Note: See TracBrowser for help on using the repository browser.