Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: brainfuck: interface to Playable much bea now

File size: 7.3 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  // THE DERIVED CLASS MUST UNSUBSCRIBE THE PLAYER THROUGH
78  // this->setPlayer(NULL);
79  // IN ITS DESTRUCTOR.
80  assert(this->currentPlayer == NULL);
81}
82
83
84void Playable::addWeapon(Weapon* weapon, int configID, int slotID)
85{
86  this->weaponMan->addWeapon(weapon, configID, slotID);
87
88  this->weaponConfigChanged();
89}
90
91
92void Playable::removeWeapon(Weapon* weapon)
93{
94  this->weaponMan->removeWeapon(weapon);
95
96    this->weaponConfigChanged();
97}
98
99
100void Playable::nextWeaponConfig()
101{
102  this->weaponMan->nextWeaponConfig();
103    this->weaponConfigChanged();
104}
105
106
107void Playable::previousWeaponConfig()
108{
109  this->weaponMan->previousWeaponConfig();
110    this->weaponConfigChanged();
111}
112
113
114void Playable::weaponConfigChanged()
115{
116  if (this->currentPlayer != NULL)
117    this->currentPlayer->weaponConfigChanged();
118}
119
120
121/**
122 * @brief helps us colliding Playables
123 */
124void Playable::collidesWith(WorldEntity* entity, const Vector& location)
125{
126  if (entity->isA(CL_PROJECTILE) && !State::isOnline() )
127  {
128    this->decreaseHealth(entity->getHealth());
129    // EXTREME HACK
130    if (this->getHealth() == 0.0f)
131    {
132      this->die();
133    }
134  }
135}
136
137
138void Playable::die()
139{
140    //this->deactivateNode();
141  this->toList(OM_DEAD);
142  this->emitter->setSystem(explosionParticles);
143  this->setAbsCoor(0, 0, 0);
144    //this->setAbsDir(Vector(1,0,0), 0);
145  this->emitter->setSystem(NULL);
146
147
148  if( this->getOwner()%2 == 0)
149    this->toList(OM_GROUP_00);
150  else
151    this->toList(OM_GROUP_01);
152}
153
154
155/**
156 * subscribe to all events the controllable needs
157 * @param player the player that shall controll this Playable
158 */
159bool Playable::setPlayer(Player* player)
160{
161  // if we already have a Player inside do nothing
162  if (this->currentPlayer != NULL && player != NULL)
163  {
164    printf("No Change in Playable\n");
165    return false;
166  }
167
168  // eject the Player if player == NULL
169  if (this->currentPlayer != NULL && player == NULL)
170  {
171    PRINTF(0)("Player gets ejected\n");
172
173    // unsubscibe all events.
174    EventHandler* evh = EventHandler::getInstance();
175    std::list<int>::iterator ev;
176    for (ev = this->events.begin(); ev != events.end(); ev++)
177      evh->unsubscribe( ES_GAME, (*ev));
178
179    // leave the entity
180    this->leave();
181
182    // eject the current Player.
183    Player* ejectPlayer = this->currentPlayer;
184    this->currentPlayer = NULL;
185    // eject the Player.
186    ejectPlayer->setPlayable(NULL);
187
188    return true;
189  }
190
191  // get the new Player inside
192  if (this->currentPlayer == NULL && player != NULL)
193  {
194    PRINTF(0)("New Player gets inside\n");
195    this->currentPlayer = player;
196    if (this->currentPlayer->getPlayable() != this)
197      this->currentPlayer->setPlayable(this);
198
199    /*EventHandler*/
200    EventHandler* evh = EventHandler::getInstance();
201    std::list<int>::iterator ev;
202    for (ev = this->events.begin(); ev != events.end(); ev++)
203      evh->subscribe(player, ES_GAME, (*ev));
204
205    this->enter();
206    return true;
207  }
208
209  printf("none\n");
210  return false;
211}
212
213
214bool Playable::pickup(PowerUp* powerUp)
215{
216  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
217    return dynamic_cast<WeaponPowerUp*>(powerUp)->process(this->getWeaponManager());
218  }
219  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
220    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
221    switch(ppu->getType()) {
222      case POWERUP_PARAM_HEALTH:
223        this->increaseHealth(ppu->getValue());
224        return true;
225      case POWERUP_PARAM_MAX_HEALTH:
226        this->increaseHealthMax(ppu->getValue());
227        return true;
228    }
229  }
230  return false;
231}
232
233/**
234 * add an event to the event list of events this Playable can capture
235 * @param eventType the Type of event to add
236 */
237void Playable::registerEvent(int eventType)
238{
239  this->events.push_back(eventType);
240
241  if (this->currentPlayer != NULL)
242    EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType);
243}
244
245/**
246 * remove an event to the event list this Playable can capture.
247 * @param event the event to unregister.
248 */
249void Playable::unregisterEvent(int eventType)
250{
251  this->events.remove(eventType);
252
253  if (this->currentPlayer != NULL)
254    EventHandler::getInstance()->unsubscribe(ES_GAME, eventType);
255}
256
257/**
258 * @brief ticks a Playable
259 * @param dt: the passed time since the last Tick
260 */
261void Playable::tick(float dt)
262{
263  this->weaponMan->tick(dt);
264  if (this->bFire)
265    weaponMan->fire();
266}
267
268
269/**
270 * @brief processes Playable events.
271 * @param event the Captured Event.
272 */
273void Playable::process(const Event &event)
274{
275  if( event.type == KeyMapper::PEV_FIRE1)
276    this->bFire = event.bPressed;
277  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
278  {
279    this->nextWeaponConfig();
280  }
281  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
282    this->previousWeaponConfig();
283}
284
285
286
287void  Playable::attachCamera()
288{
289  State::getCamera()->setParentSoft(this);
290  State::getCameraTarget()->setParentSoft(this);
291
292}
293
294
295
296
297void  Playable::detachCamera()
298{
299}
300
301#define DATA_FLAGS    1
302#define DATA_SCORE    2
303
304#define FLAGS_bFire   1
305
306int Playable::writeSync( const byte * data, int length, int sender )
307{
308  SYNCHELP_READ_BEGIN();
309
310  byte flags;
311
312  SYNCHELP_READ_BYTE( flags, NWT_PL_FLAGS );
313
314  bFire = (flags & FLAGS_bFire) != 0;
315
316  return SYNCHELP_READ_N;
317}
318
319int Playable::readSync( byte * data, int maxLength )
320{
321  SYNCHELP_WRITE_BEGIN();
322  byte flags = 0;
323
324  if ( bFire )
325    flags |= FLAGS_bFire;
326
327
328  SYNCHELP_WRITE_BYTE( flags, NWT_PL_FLAGS );
329  oldFlags = flags;
330
331
332  return SYNCHELP_WRITE_N;
333}
334
335bool Playable::needsReadSync( )
336{
337  //if ( score != oldScore )
338  //  return true;
339
340  byte flags = 0;
341
342  if ( bFire )
343    flags |= FLAGS_bFire;
344
345  return flags!=oldFlags;
346}
Note: See TracBrowser for help on using the repository browser.