Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/avi_play/src/world_entities/playable.cc @ 6996

Last change on this file since 6996 was 6871, checked in by bensch, 19 years ago

orxonox/trunk: small fixes (cleanup)

File size: 6.1 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
30Playable::Playable()
31{
32  this->setClassID(CL_PLAYABLE, "Playable");
33  PRINTF(4)("PLAYABLE INIT\n");
34
35  this->toList(OM_GROUP_01);
36  this->weaponMan = new WeaponManager(this);
37
38  // the reference to the Current Player is NULL, because we dont have one at the beginning.
39  this->currentPlayer = NULL;
40
41  this->bFire = false;
42  this->oldFlags = 0;
43
44  this->setSynchronized(true);
45}
46
47
48
49Playable::~Playable()
50{
51  delete this->weaponMan;
52
53  if (this->currentPlayer)
54  {
55    PRINTF(2)("There is Still a Player subscribed to this Playable (%s::%s)\n", this->getClassName(), this->getName());
56
57  }
58}
59
60
61void Playable::addWeapon(Weapon* weapon, int configID, int slotID)
62{
63  this->weaponMan->addWeapon(weapon, configID, slotID);
64
65  this->weaponConfigChanged();
66}
67
68
69void Playable::removeWeapon(Weapon* weapon)
70{
71  this->weaponMan->removeWeapon(weapon);
72
73    this->weaponConfigChanged();
74}
75
76
77void Playable::nextWeaponConfig()
78{
79  this->weaponMan->nextWeaponConfig();
80    this->weaponConfigChanged();
81}
82
83
84void Playable::previousWeaponConfig()
85{
86  this->weaponMan->previousWeaponConfig();
87    this->weaponConfigChanged();
88}
89
90
91void Playable::weaponConfigChanged()
92{
93  if (this->currentPlayer != NULL)
94    this->currentPlayer->weaponConfigChanged();
95}
96
97
98/**
99 * @brief helps us colliding Playables
100 */
101void Playable::collidesWith(WorldEntity* entity, const Vector& location)
102{
103  if (entity->isA(CL_PROJECTILE))
104    this->decreaseHealth(entity->getHealth());
105
106  // EXTREME HACK
107  if (this->getHealth() == 0.0f)
108    this->deactivateNode();
109}
110
111/**
112 * subscribe to all events the controllable needs
113 * @param player the player that shall controll this Playable
114 */
115bool Playable::subscribePlayer(Player* player)
116{
117  if (this->currentPlayer != NULL)
118  {
119    PRINTF(1)("Already registered Player:%s to this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName());
120    return false;
121  }
122  else
123  {
124    this->currentPlayer = player;
125    /*EventHandler*/
126    EventHandler* evh = EventHandler::getInstance();
127    std::list<int>::iterator ev;
128    for (ev = this->events.begin(); ev != events.end(); ev++)
129      evh->subscribe(player, ES_GAME, (*ev));
130    this->enter();
131    return true;
132  }
133}
134
135/**
136 * unsubscribe from all events the controllable needs
137 * @param player the Player, that controlled this Ship.
138 */
139bool Playable::unsubscribePlayer(Player* player)
140{
141  if (this->currentPlayer != player)
142  {
143    PRINTF(1)("not the right Player to unregister from this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName());
144    return false;
145  }
146
147  else
148  {
149    /*EventHandler*/
150    EventHandler* evh = EventHandler::getInstance();
151    std::list<int>::iterator ev;
152    for (ev = this->events.begin(); ev != events.end(); ev++)
153      evh->unsubscribe( ES_GAME, (*ev));
154
155    this->leave();
156    this->currentPlayer = NULL;
157    return true;
158  }
159}
160
161bool Playable::pickup(PowerUp* powerUp)
162{
163  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
164    Weapon* weapon = dynamic_cast<WeaponPowerUp*>(powerUp)->getWeapon();
165    WeaponManager* manager = this->getWeaponManager();
166    return manager->addWeapon(weapon);
167  }
168  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
169    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
170    switch(ppu->getType()) {
171      case POWERUP_PARAM_HEALTH:
172        this->increaseHealth(ppu->getValue());
173        return true;
174      case POWERUP_PARAM_MAX_HEALTH:
175        this->increaseHealthMax(ppu->getValue());
176        return true;
177    }
178  }
179  return false;
180}
181
182/**
183 * add an event to the event list of events this Playable can capture
184 * @param eventType the Type of event to add
185 */
186void Playable::registerEvent(int eventType)
187{
188  this->events.push_back(eventType);
189
190  if (this->currentPlayer != NULL)
191    EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType);
192}
193
194/**
195 * remove an event to the event list this Playable can capture.
196 * @param event the event to unregister.
197 */
198void Playable::unregisterEvent(int eventType)
199{
200  this->events.remove(eventType);
201
202  if (this->currentPlayer != NULL)
203    EventHandler::getInstance()->unsubscribe(ES_GAME, eventType);
204}
205
206/**
207 * @brief ticks a Playable
208 * @param dt: the passed time since the last Tick
209 */
210void Playable::tick(float dt)
211{
212  this->weaponMan->tick(dt);
213  if (this->bFire)
214    weaponMan->fire();
215}
216
217
218/**
219 * @brief processes Playable events.
220 * @param event the Captured Event.
221 */
222void Playable::process(const Event &event)
223{
224  if( event.type == KeyMapper::PEV_FIRE1)
225    this->bFire = event.bPressed;
226  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
227  {
228    this->nextWeaponConfig();
229  }
230  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
231    this->previousWeaponConfig();
232}
233
234
235
236void  Playable::attachCamera()
237{
238  State::getCamera()->setParentSoft(this);
239  State::getCameraTarget()->setParentSoft(this);
240
241}
242
243
244
245
246void  Playable::detachCamera()
247{
248}
249
250#define FLAGS_bFire   1
251
252int Playable::writeSync( const byte * data, int length, int sender )
253{
254  SYNCHELP_READ_BEGIN();
255
256  byte flags;
257
258  SYNCHELP_READ_BYTE( flags, NWT_PL_FLAGS );
259
260  bFire = (flags & FLAGS_bFire) != 0;
261
262  return SYNCHELP_READ_N;
263}
264
265int Playable::readSync( byte * data, int maxLength )
266{
267  SYNCHELP_WRITE_BEGIN();
268  byte flags = 0;
269
270  if ( bFire )
271    flags |= FLAGS_bFire;
272
273
274  SYNCHELP_WRITE_BYTE( flags, NWT_PL_FLAGS );
275  oldFlags = flags;
276
277
278  return SYNCHELP_WRITE_N;
279}
280
281bool Playable::needsReadSync( )
282{
283  byte flags = 0;
284
285  if ( bFire )
286    flags |= FLAGS_bFire;
287
288  return flags!=oldFlags;
289}
Note: See TracBrowser for help on using the repository browser.