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 | |
---|
34 | Playable::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 | |
---|
73 | Playable::~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 | |
---|
85 | void Playable::addWeapon(Weapon* weapon, int configID, int slotID) |
---|
86 | { |
---|
87 | this->weaponMan->addWeapon(weapon, configID, slotID); |
---|
88 | |
---|
89 | this->weaponConfigChanged(); |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | void Playable::removeWeapon(Weapon* weapon) |
---|
94 | { |
---|
95 | this->weaponMan->removeWeapon(weapon); |
---|
96 | |
---|
97 | this->weaponConfigChanged(); |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | void Playable::nextWeaponConfig() |
---|
102 | { |
---|
103 | this->weaponMan->nextWeaponConfig(); |
---|
104 | this->weaponConfigChanged(); |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | void Playable::previousWeaponConfig() |
---|
109 | { |
---|
110 | this->weaponMan->previousWeaponConfig(); |
---|
111 | this->weaponConfigChanged(); |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | void Playable::weaponConfigChanged() |
---|
116 | { |
---|
117 | if (this->currentPlayer != NULL) |
---|
118 | this->currentPlayer->weaponConfigChanged(); |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | /** |
---|
123 | * @brief helps us colliding Playables |
---|
124 | */ |
---|
125 | void 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 | */ |
---|
145 | bool 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 | */ |
---|
169 | bool 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 | |
---|
191 | bool 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 | */ |
---|
216 | void 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 | */ |
---|
228 | void 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 | */ |
---|
240 | void 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 | */ |
---|
252 | void 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 | |
---|
266 | void Playable::attachCamera() |
---|
267 | { |
---|
268 | State::getCamera()->setParentSoft(this); |
---|
269 | State::getCameraTarget()->setParentSoft(this); |
---|
270 | |
---|
271 | } |
---|
272 | |
---|
273 | |
---|
274 | |
---|
275 | |
---|
276 | void Playable::detachCamera() |
---|
277 | { |
---|
278 | } |
---|
279 | |
---|
280 | #define DATA_FLAGS 1 |
---|
281 | #define DATA_SCORE 2 |
---|
282 | |
---|
283 | #define FLAGS_bFire 1 |
---|
284 | |
---|
285 | int Playable::writeSync( const byte * data, int length, int sender ) |
---|
286 | { |
---|
287 | SYNCHELP_READ_BEGIN(); |
---|
288 | |
---|
289 | byte flags; |
---|
290 | |
---|
291 | SYNCHELP_READ_BYTE( flags, NWT_PL_FLAGS ); |
---|
292 | |
---|
293 | bFire = (flags & FLAGS_bFire) != 0; |
---|
294 | |
---|
295 | return SYNCHELP_READ_N; |
---|
296 | } |
---|
297 | |
---|
298 | int Playable::readSync( byte * data, int maxLength ) |
---|
299 | { |
---|
300 | SYNCHELP_WRITE_BEGIN(); |
---|
301 | byte flags = 0; |
---|
302 | |
---|
303 | if ( bFire ) |
---|
304 | flags |= FLAGS_bFire; |
---|
305 | |
---|
306 | |
---|
307 | SYNCHELP_WRITE_BYTE( flags, NWT_PL_FLAGS ); |
---|
308 | oldFlags = flags; |
---|
309 | |
---|
310 | |
---|
311 | return SYNCHELP_WRITE_N; |
---|
312 | } |
---|
313 | |
---|
314 | bool Playable::needsReadSync( ) |
---|
315 | { |
---|
316 | //if ( score != oldScore ) |
---|
317 | // return true; |
---|
318 | |
---|
319 | byte flags = 0; |
---|
320 | |
---|
321 | if ( bFire ) |
---|
322 | flags |= FLAGS_bFire; |
---|
323 | |
---|
324 | return flags!=oldFlags; |
---|
325 | } |
---|