1 | |
---|
2 | /* |
---|
3 | orxonox - the future of 3D-vertical-scrollers |
---|
4 | |
---|
5 | Copyright (C) 2004 orx |
---|
6 | |
---|
7 | This program is free software; you can redistribute it and/or modify |
---|
8 | it under the terms of the GNU General Public License as published by |
---|
9 | the Free Software Foundation; either version 2, or (at your option) |
---|
10 | any later version. |
---|
11 | |
---|
12 | ### File Specific |
---|
13 | main-programmer: Patrick Boenzli |
---|
14 | co-programmer: Benjamin Grauer |
---|
15 | |
---|
16 | 2005-07-15: Benjamin Grauer: restructurating the entire Class |
---|
17 | */ |
---|
18 | |
---|
19 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON |
---|
20 | |
---|
21 | #include "weapon.h" |
---|
22 | |
---|
23 | #include "loading/fast_factory.h" |
---|
24 | #include "world_entities/projectiles/projectile.h" |
---|
25 | |
---|
26 | #include "util/loading/factory.h" |
---|
27 | #include "util/loading/load_param.h" |
---|
28 | #include "state.h" |
---|
29 | #include "animation3d.h" |
---|
30 | |
---|
31 | #include "sound_source.h" |
---|
32 | #include "sound_buffer.h" |
---|
33 | #include "resource_sound_buffer.h" |
---|
34 | |
---|
35 | #include "elements/glgui_energywidgetvertical.h" |
---|
36 | |
---|
37 | ObjectListDefinition(Weapon); |
---|
38 | |
---|
39 | //////////////////// |
---|
40 | // INITAILISATION // |
---|
41 | // SETTING VALUES // |
---|
42 | //////////////////// |
---|
43 | /** |
---|
44 | * standard constructor |
---|
45 | * |
---|
46 | * creates a new weapon |
---|
47 | */ |
---|
48 | Weapon::Weapon () |
---|
49 | { |
---|
50 | this->init(); |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * standard deconstructor |
---|
55 | */ |
---|
56 | Weapon::~Weapon () |
---|
57 | { |
---|
58 | for (int i = 0; i < WS_STATE_COUNT; i++) |
---|
59 | if (this->animation[i] && Animation::objectList().exists(animation[i])) //!< @todo this should check animation3D |
---|
60 | delete this->animation[i]; |
---|
61 | |
---|
62 | if (OrxSound::SoundSource::objectList().exists(this->soundSource)) |
---|
63 | delete this->soundSource; |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | * @brief creates a new Weapon of type weaponID and returns it. |
---|
68 | * @param weaponID the WeaponID type to create. |
---|
69 | * @returns the newly created Weapon. |
---|
70 | */ |
---|
71 | Weapon* Weapon::createWeapon(const ClassID& weaponID) |
---|
72 | { |
---|
73 | BaseObject* createdObject = Factory::fabricate(weaponID); |
---|
74 | if (createdObject != NULL) |
---|
75 | { |
---|
76 | if (createdObject->isA(Weapon::staticClassID())) |
---|
77 | return dynamic_cast<Weapon*>(createdObject); |
---|
78 | else |
---|
79 | { |
---|
80 | delete createdObject; |
---|
81 | return NULL; |
---|
82 | } |
---|
83 | } |
---|
84 | return NULL; |
---|
85 | } |
---|
86 | |
---|
87 | Weapon* Weapon::createWeapon(const std::string& weaponName) |
---|
88 | { |
---|
89 | BaseObject* createdObject = Factory::fabricate(weaponName); |
---|
90 | if (createdObject != NULL) |
---|
91 | { |
---|
92 | if (createdObject->isA(Weapon::staticClassID())) |
---|
93 | return dynamic_cast<Weapon*>(createdObject); |
---|
94 | else |
---|
95 | { |
---|
96 | delete createdObject; |
---|
97 | return NULL; |
---|
98 | } |
---|
99 | } |
---|
100 | return NULL; |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | /** |
---|
105 | * initializes the Weapon with ALL default values |
---|
106 | * |
---|
107 | * This Sets the default values of the Weapon |
---|
108 | */ |
---|
109 | void Weapon::init() |
---|
110 | { |
---|
111 | this->registerObject(this, Weapon::_objectList); |
---|
112 | this->currentState = WS_INACTIVE; //< Normaly the Weapon is Inactive |
---|
113 | this->requestedAction = WA_NONE; //< No action is requested by default |
---|
114 | this->stateDuration = 0.0; //< All the States have zero duration |
---|
115 | for (int i = 0; i < WS_STATE_COUNT; i++) //< Every State has: |
---|
116 | { |
---|
117 | this->times[i] = 0.0; //< An infinitesimal duration |
---|
118 | this->animation[i] = NULL; //< No animation |
---|
119 | } |
---|
120 | |
---|
121 | this->soundSource = new OrxSound::SoundSource(this); //< Every Weapon has exacty one SoundSource |
---|
122 | |
---|
123 | this->barrels = 1; |
---|
124 | this->segs = 1; |
---|
125 | |
---|
126 | this->preferedSide = -1; |
---|
127 | this->preferedSlot = -1; |
---|
128 | |
---|
129 | this->shootAnim = new Animation3D**[this->getBarrels()]; |
---|
130 | for (int i = 0; i < this->getBarrels(); i++) |
---|
131 | this->shootAnim[i] = new Animation3D* [this->getSegs()]; |
---|
132 | |
---|
133 | this->emissionPoint = new PNode*[this->barrels]; |
---|
134 | for(int i = 0; i < this->barrels; i++){ |
---|
135 | this->emissionPoint[i] = new PNode; |
---|
136 | this->emissionPoint[i]->setParent(this); //< One EmissionPoint, that is a PNode connected to the weapon. You can set this to the exitting point of the Projectiles |
---|
137 | this->emissionPoint[i]->setName("EmissionPoint"); |
---|
138 | this->emissionPoint[i]->addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
139 | } |
---|
140 | |
---|
141 | this->defaultTarget = NULL; //< Nothing is Targeted by default. |
---|
142 | |
---|
143 | this->projectile = NullClass::staticClassID(); //< No Projectile Class is Connected to this weapon |
---|
144 | this->projectileFactory = NULL; //< No Factory generating Projectiles is selected. |
---|
145 | |
---|
146 | this->hideInactive = true; //< The Weapon will be hidden if it is inactive (by default) |
---|
147 | |
---|
148 | this->minCharge = 1.0; //< The minimum charge the Weapon can hold is 1 unit. |
---|
149 | this->maxCharge = 1.0; //< The maximum charge is also one unit. |
---|
150 | |
---|
151 | this->energy = 10; //< The secondary Buffer (before we have to reload) |
---|
152 | this->energyMax = 10.0; //< How much energy can be carried |
---|
153 | this->capability = WTYPE_ALL; //< The Weapon has all capabilities @see W_Capability. |
---|
154 | |
---|
155 | this->energyWidget = NULL; |
---|
156 | isEnergyWidgetInitialized = false; |
---|
157 | |
---|
158 | // set this object to be synchronized over network |
---|
159 | //this->setSynchronized(true); |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * needed, if there are more than one barrel or segments |
---|
164 | */ |
---|
165 | /* |
---|
166 | void Weapon::init2() |
---|
167 | { |
---|
168 | if (this->barrels == 1 && this->segs == 1) |
---|
169 | return; |
---|
170 | |
---|
171 | delete this->emissionPoint[0]; |
---|
172 | delete this->emissionPoint; |
---|
173 | delete this->shootAnim[0]; |
---|
174 | delete this->shootAnim; |
---|
175 | |
---|
176 | this->shootAnim = new Animation3D**[this->barrels]; |
---|
177 | this->emissionPoint = new PNode*[this->barrels]; |
---|
178 | for(int i = 0; i < this->barrels; i++){ |
---|
179 | this->emissionPoint[i] = new PNode; |
---|
180 | this->emissionPoint[i]->setParent(this); //< One EmissionPoint, that is a PNode connected to the weapon. You can set this to the exitting point of the Projectiles |
---|
181 | this->emissionPoint[i]->setName("EmissionPoint"); |
---|
182 | this->emissionPoint[i]->addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
183 | this->shootAnim[i] = new Animation3D* [this->segs]; |
---|
184 | } |
---|
185 | }*/ |
---|
186 | |
---|
187 | /** |
---|
188 | * deconstructor for init2 |
---|
189 | */ |
---|
190 | /* |
---|
191 | void Weapon::deconstr() |
---|
192 | { |
---|
193 | for(int i = 0; i < this->barrels; i++) { |
---|
194 | delete this->emissionPoint[i]; |
---|
195 | for (int j = 0; j < this->segs; j++) |
---|
196 | delete this->shootAnim[i][j]; |
---|
197 | delete this->shootAnim[i]; |
---|
198 | } |
---|
199 | |
---|
200 | delete this->emissionPoint; |
---|
201 | delete this->shootAnim; |
---|
202 | }*/ |
---|
203 | |
---|
204 | /** |
---|
205 | * loads the Parameters of a Weapon |
---|
206 | * @param root the XML-Element to load the Weapons settings from |
---|
207 | */ |
---|
208 | void Weapon::loadParams(const TiXmlElement* root) |
---|
209 | { |
---|
210 | WorldEntity::loadParams(root); |
---|
211 | |
---|
212 | LoadParam(root, "projectile", this, Weapon, setProjectileTypeC) |
---|
213 | .describe("Sets the name of the Projectile to load onto the Entity"); |
---|
214 | |
---|
215 | LoadParam(root, "energy", this, Weapon, setEnergyMax) |
---|
216 | .describe("number of shoots/ energy whatever"); |
---|
217 | |
---|
218 | LoadParam(root, "slot", this, Weapon, setPreferedSlot) |
---|
219 | .describe("slot this weapon will be added"); |
---|
220 | /* |
---|
221 | LoadParam(root, "emission-point", this, Weapon, setEmissionPoint) |
---|
222 | .describe("Sets the Point of emission of this weapon (1: EmmsionVector; 2: EmissionPoint)");*/ |
---|
223 | |
---|
224 | LoadParam(root, "state-duration", this, Weapon, setStateDuration) |
---|
225 | .describe("Sets the duration of a given state (1: state-Name; 2: duration in seconds)"); |
---|
226 | |
---|
227 | LoadParam(root, "action-sound", this, Weapon, setActionSound) |
---|
228 | .describe("Sets a given sound to an action (1: action-Name; 2: name of the sound (relative to the Data-Path))"); |
---|
229 | } |
---|
230 | |
---|
231 | |
---|
232 | /** |
---|
233 | * sets the Projectile to use for this weapon. |
---|
234 | * @param projectile The ID of the Projectile to use |
---|
235 | * @returns true, if it was sucessfull, false on error |
---|
236 | * |
---|
237 | * be aware, that this function does not create Factories, as this is job of Projecitle/Bullet-classes. |
---|
238 | * What it does, is telling the Weapon what Projectiles it can Emit. |
---|
239 | */ |
---|
240 | void Weapon::setProjectileType(const ClassID& projectile) |
---|
241 | { |
---|
242 | this->projectile = projectile; |
---|
243 | this->projectileFactory = FastFactory::searchFastFactory(projectile); |
---|
244 | if (this->projectileFactory == NULL) |
---|
245 | { |
---|
246 | PRINTF(1)("unable to find FastFactory for the Projectile.\n"); |
---|
247 | return; |
---|
248 | } |
---|
249 | else |
---|
250 | { |
---|
251 | // grabbing Parameters from the Projectile to have them at hand here. |
---|
252 | Projectile* pj = dynamic_cast<Projectile*>(this->projectileFactory->resurrect()); |
---|
253 | this->minCharge = pj->getMinEnergy(); |
---|
254 | this->maxCharge = pj->getHealthMax(); |
---|
255 | this->chargeable = pj->isChageable(); |
---|
256 | this->projectileFactory->kill(pj); |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
260 | |
---|
261 | /** |
---|
262 | * @see bool Weapon::setProjectile(ClassID projectile) |
---|
263 | * @param projectile the Name of the Projectile. |
---|
264 | */ |
---|
265 | void Weapon::setProjectileTypeC(const std::string& projectile) |
---|
266 | { |
---|
267 | FastFactory* tmpFac = FastFactory::searchFastFactory(projectile); |
---|
268 | if (tmpFac != NULL) |
---|
269 | { |
---|
270 | this->setProjectileType(tmpFac->getStoredID()); |
---|
271 | } |
---|
272 | else |
---|
273 | { |
---|
274 | PRINTF(1)("Projectile %s does not exist for weapon %s\n", projectile.c_str(), this->getCName()); |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | |
---|
279 | /** |
---|
280 | * prepares Projectiles of the Weapon |
---|
281 | * @param count how many Projectiles to create (they will be stored in the ProjectileFactory) |
---|
282 | */ |
---|
283 | void Weapon::prepareProjectiles(unsigned int count) |
---|
284 | { |
---|
285 | if (likely(this->projectileFactory != NULL)) |
---|
286 | projectileFactory->prepare(count); |
---|
287 | else |
---|
288 | PRINTF(2)("unable to create %d projectile for Weapon %s::%s\n", count, this->getClassCName(), this->getCName()); |
---|
289 | } |
---|
290 | |
---|
291 | |
---|
292 | /** |
---|
293 | * resurects and returns a Projectile |
---|
294 | * @returns a Projectile on success, NULL on error |
---|
295 | * |
---|
296 | * errors: 1. (ProjectileFastFactory not Found) |
---|
297 | * 2. No more Projectiles availiable. |
---|
298 | */ |
---|
299 | Projectile* Weapon::getProjectile() |
---|
300 | { |
---|
301 | if (likely (this->projectileFactory != NULL)) |
---|
302 | { |
---|
303 | Projectile* pj = dynamic_cast<Projectile*>(this->projectileFactory->resurrect()); |
---|
304 | pj->toList((OM_LIST)(this->getOMListNumber()+1)); |
---|
305 | return pj; |
---|
306 | } |
---|
307 | else |
---|
308 | { |
---|
309 | PRINTF(2)("No projectile defined for Weapon %s(%s) can't return any\n", this->getCName(), this->getClassCName()); |
---|
310 | return NULL; |
---|
311 | } |
---|
312 | } |
---|
313 | |
---|
314 | |
---|
315 | /** |
---|
316 | * sets the emissionPoint's relative position from the Weapon |
---|
317 | * @param point the Point relative to the mass-point of the Weapon |
---|
318 | */ |
---|
319 | void Weapon::setEmissionPoint(const Vector& point, int barrel) |
---|
320 | { |
---|
321 | this->emissionPoint[barrel]->setRelCoor(point); |
---|
322 | } |
---|
323 | |
---|
324 | void Weapon::setEmissionPoint(const Vector& point) |
---|
325 | { |
---|
326 | this->emissionPoint[0]->setRelCoor(point); |
---|
327 | } |
---|
328 | |
---|
329 | /** |
---|
330 | * assigns a Sound-file to an action |
---|
331 | * @param action the action the sound should be assigned too |
---|
332 | * @param soundFile the soundFile's relative position to the data-directory (will be looked for by the ResourceManager) |
---|
333 | */ |
---|
334 | void Weapon::setActionSound(WeaponAction action, const std::string& soundFile) |
---|
335 | { |
---|
336 | if (action >= WA_ACTION_COUNT) |
---|
337 | return; |
---|
338 | |
---|
339 | else if (!soundFile.empty()) |
---|
340 | { |
---|
341 | this->soundBuffers[action] = OrxSound::ResourceSoundBuffer(soundFile); |
---|
342 | if (this->soundBuffers[action].loaded()) |
---|
343 | { |
---|
344 | PRINTF(4)("Loaded sound %s to action %s.\n", soundFile.c_str(), actionToChar(action)); |
---|
345 | } |
---|
346 | else |
---|
347 | { |
---|
348 | PRINTF(2)("Failed to load sound %s to %s.\n.", soundFile.c_str(), actionToChar(action)); |
---|
349 | } |
---|
350 | } |
---|
351 | else |
---|
352 | this->soundBuffers[action] = OrxSound::SoundBuffer(); |
---|
353 | } |
---|
354 | |
---|
355 | |
---|
356 | /** |
---|
357 | * creates/returns an Animation3D for a certain State. |
---|
358 | * @param state what State should the Animation be created/returned for |
---|
359 | * @param node the node this Animation should apply to. (NULL is fine if the animation was already created) |
---|
360 | * @returns The created animation.Animation(), NULL on error (or if the animation does not yet exist). |
---|
361 | * |
---|
362 | * This function does only generate the Animation Object, and if set it will |
---|
363 | * automatically be executed, when a certain State is reached. |
---|
364 | * What this does not do, is set keyframes, you have to operate on the returned animation. |
---|
365 | */ |
---|
366 | Animation3D* Weapon::getAnimation(WeaponState state, PNode* node) |
---|
367 | { |
---|
368 | if (state >= WS_STATE_COUNT) // if the state is not known |
---|
369 | return NULL; |
---|
370 | |
---|
371 | if (unlikely(this->animation[state] == NULL)) // if the animation does not exist yet create it. |
---|
372 | { |
---|
373 | if (likely(node != NULL)) |
---|
374 | return this->animation[state] = new Animation3D(node); |
---|
375 | else |
---|
376 | { |
---|
377 | PRINTF(2)("Node not defined for the Creation of the 3D-animation of state %s\n", stateToChar(state)); |
---|
378 | return NULL; |
---|
379 | } |
---|
380 | } |
---|
381 | else |
---|
382 | return this->animation[state]; |
---|
383 | } |
---|
384 | |
---|
385 | Animation3D* Weapon::getAnimation(int barrel, int seg, PNode* node) |
---|
386 | { |
---|
387 | if (barrel >= this->getBarrels() || seg >= this->getSegs()) // if the state is not known |
---|
388 | return NULL; |
---|
389 | |
---|
390 | if (unlikely(this->shootAnim[barrel][seg] == NULL)) // if the animation does not exist yet create it. |
---|
391 | { |
---|
392 | if (likely(node != NULL)) |
---|
393 | return this->shootAnim[barrel][seg] = new Animation3D(node); |
---|
394 | else |
---|
395 | { |
---|
396 | // PRINTF(2)("Node not defined for the Creation of the 3D-animation of state %s\n", stateToChar(state)); |
---|
397 | return NULL; |
---|
398 | } |
---|
399 | } |
---|
400 | else |
---|
401 | return this->shootAnim[barrel][seg]; |
---|
402 | } |
---|
403 | |
---|
404 | OrxGui::GLGuiWidget* Weapon::getEnergyWidget() |
---|
405 | { |
---|
406 | if ( this->energyWidget == NULL) |
---|
407 | { |
---|
408 | this->energyWidget = new OrxGui::GLGuiEnergyWidgetVertical(); |
---|
409 | //this->energyWidget->setDisplayedName(this->getClassCName()); |
---|
410 | this->energyWidget->setSize2D( 20, 100); |
---|
411 | this->energyWidget->setMaximum(this->getEnergyMax()); |
---|
412 | this->energyWidget->setValue(this->getEnergy()); |
---|
413 | } |
---|
414 | return this->energyWidget; |
---|
415 | } |
---|
416 | |
---|
417 | void Weapon::updateWidgets() |
---|
418 | { |
---|
419 | if (this->energyWidget != NULL) |
---|
420 | { |
---|
421 | this->energyWidget->setMaximum(this->energyMax); |
---|
422 | this->energyWidget->setValue(this->energy); |
---|
423 | } |
---|
424 | } |
---|
425 | |
---|
426 | ///////////////// |
---|
427 | // EXECUTION // |
---|
428 | // GAME ACTION // |
---|
429 | ///////////////// |
---|
430 | /** |
---|
431 | * request an action that should be executed, |
---|
432 | * @param action the next action to take |
---|
433 | * |
---|
434 | * This function must be called instead of the actions (like fire/reload...) |
---|
435 | * to make all the checks needed to have a usefull WeaponSystem. |
---|
436 | */ |
---|
437 | void Weapon::requestAction(WeaponAction action) |
---|
438 | { |
---|
439 | if (likely(this->isActive())) |
---|
440 | { |
---|
441 | /** Disabled for releaseFire() from WM*/ |
---|
442 | if (this->requestedAction != WA_NONE) |
---|
443 | return; |
---|
444 | PRINTF(5)("%s: next action will be %s in %f seconds\n", this->getCName(), actionToChar(action), this->stateDuration); |
---|
445 | this->requestedAction = action; |
---|
446 | } |
---|
447 | //else |
---|
448 | else if (unlikely(action == WA_ACTIVATE)) |
---|
449 | { |
---|
450 | this->currentState = WS_ACTIVATING; |
---|
451 | this->requestedAction = WA_ACTIVATE; |
---|
452 | } |
---|
453 | } |
---|
454 | |
---|
455 | |
---|
456 | /** |
---|
457 | * adds energy to the Weapon |
---|
458 | * @param energyToAdd The amount of energy |
---|
459 | * @returns the amount of energy we did not pick up, because the weapon is already full |
---|
460 | */ |
---|
461 | float Weapon::increaseEnergy(float energyToAdd) |
---|
462 | { |
---|
463 | float maxAddEnergy = this->energyMax - this->energy; |
---|
464 | |
---|
465 | if (maxAddEnergy >= energyToAdd) |
---|
466 | { |
---|
467 | this->energy += energyToAdd; |
---|
468 | this->updateWidgets(); |
---|
469 | return 0.0; |
---|
470 | } |
---|
471 | else |
---|
472 | { |
---|
473 | this->energy += maxAddEnergy; |
---|
474 | this->updateWidgets(); |
---|
475 | return energyToAdd - maxAddEnergy; |
---|
476 | } |
---|
477 | |
---|
478 | } |
---|
479 | |
---|
480 | |
---|
481 | //////////////////////////////////////////////////////////// |
---|
482 | // WEAPON INTERNALS // |
---|
483 | // These are functions, that no other Weapon should over- // |
---|
484 | // write. No class has direct Access to them, as it is // |
---|
485 | // quite a complicated process, handling a Weapon from // |
---|
486 | // the outside // |
---|
487 | //////////////////////////////////////////////////////////// |
---|
488 | /** |
---|
489 | * executes an action, and with it starts a new State. |
---|
490 | * @return true, if it worked, false otherwise |
---|
491 | * |
---|
492 | * This function checks, wheter the possibility of executing an action is valid, |
---|
493 | * and does all the necessary stuff, to execute them. If an action does not succeed, |
---|
494 | * it tries to go around it. (ex. shoot->noAmo->reload()->wait until shoot comes again) |
---|
495 | */ |
---|
496 | bool Weapon::execute() |
---|
497 | { |
---|
498 | #if DEBUG_LEVEL > 4 |
---|
499 | PRINTF(4)("trying to execute action %s\n", actionToChar(this->requestedAction)); |
---|
500 | this->debug(); |
---|
501 | #endif |
---|
502 | |
---|
503 | WeaponAction action = this->requestedAction; |
---|
504 | this->requestedAction = WA_NONE; |
---|
505 | |
---|
506 | switch (action) |
---|
507 | { |
---|
508 | case WA_SHOOT: |
---|
509 | return this->fireW(); |
---|
510 | break; |
---|
511 | case WA_CHARGE: |
---|
512 | return this->chargeW(); |
---|
513 | break; |
---|
514 | case WA_RELOAD: |
---|
515 | return this->reloadW(); |
---|
516 | break; |
---|
517 | case WA_DEACTIVATE: |
---|
518 | return this->deactivateW(); |
---|
519 | break; |
---|
520 | case WA_ACTIVATE: |
---|
521 | return this->activateW(); |
---|
522 | break; |
---|
523 | default: |
---|
524 | PRINTF(5)("Action %s Not Implemented yet \n", Weapon::actionToChar(action)); |
---|
525 | return false; |
---|
526 | } |
---|
527 | } |
---|
528 | |
---|
529 | /** |
---|
530 | * checks and activates the Weapon. |
---|
531 | * @return true on success. |
---|
532 | */ |
---|
533 | bool Weapon::activateW() |
---|
534 | { |
---|
535 | // if (this->currentState == WS_INACTIVE) |
---|
536 | { |
---|
537 | // play Sound |
---|
538 | if (likely(this->soundBuffers[WA_ACTIVATE].loaded())) |
---|
539 | this->soundSource->play(this->soundBuffers[WA_ACTIVATE]); |
---|
540 | this->updateWidgets(); |
---|
541 | // activate |
---|
542 | PRINTF(4)("Activating the Weapon %s\n", this->getCName()); |
---|
543 | this->activate(); |
---|
544 | // setting up for next action |
---|
545 | this->enterState(WS_ACTIVATING); |
---|
546 | } |
---|
547 | return true; |
---|
548 | } |
---|
549 | |
---|
550 | /** |
---|
551 | * checks and deactivates the Weapon |
---|
552 | * @return true on success. |
---|
553 | */ |
---|
554 | bool Weapon::deactivateW() |
---|
555 | { |
---|
556 | // if (this->currentState != WS_INACTIVE) |
---|
557 | { |
---|
558 | PRINTF(4)("Deactivating the Weapon %s\n", this->getCName()); |
---|
559 | // play Sound |
---|
560 | if (this->soundBuffers[WA_DEACTIVATE].loaded()) |
---|
561 | this->soundSource->play(this->soundBuffers[WA_DEACTIVATE]); |
---|
562 | // deactivate |
---|
563 | this->deactivate(); |
---|
564 | this->enterState(WS_DEACTIVATING); |
---|
565 | } |
---|
566 | |
---|
567 | return true; |
---|
568 | } |
---|
569 | |
---|
570 | /** |
---|
571 | * checks and charges the Weapon |
---|
572 | * @return true on success. |
---|
573 | */ |
---|
574 | bool Weapon::chargeW() |
---|
575 | { |
---|
576 | if ( this->currentState != WS_INACTIVE && this->energy >= this->minCharge) |
---|
577 | { |
---|
578 | // playing Sound |
---|
579 | if (this->soundBuffers[WA_CHARGE].loaded()) |
---|
580 | this->soundSource->play(this->soundBuffers[WA_CHARGE]); |
---|
581 | |
---|
582 | // charge |
---|
583 | this->charge(); |
---|
584 | // setting up for the next state |
---|
585 | this->enterState(WS_CHARGING); |
---|
586 | } |
---|
587 | else // deactivate the Weapon if we do not have enough energy |
---|
588 | { |
---|
589 | this->requestAction(WA_RELOAD); |
---|
590 | } |
---|
591 | return true; |
---|
592 | } |
---|
593 | |
---|
594 | /** |
---|
595 | * checks and fires the Weapon |
---|
596 | * @return true on success. |
---|
597 | */ |
---|
598 | bool Weapon::fireW() |
---|
599 | { |
---|
600 | // printf("fireW Weapon\n"); |
---|
601 | //if (likely(this->currentState != WS_INACTIVE)) |
---|
602 | if (this->minCharge <= this->energy) |
---|
603 | { |
---|
604 | // playing Sound |
---|
605 | if (this->soundBuffers[WA_SHOOT].loaded()) |
---|
606 | this->soundSource->play(this->soundBuffers[WA_SHOOT]); |
---|
607 | // fire |
---|
608 | this->energy -= this->minCharge; |
---|
609 | this->fire(); |
---|
610 | // setting up for the next state |
---|
611 | this->enterState(WS_SHOOTING); |
---|
612 | this->updateWidgets(); |
---|
613 | } |
---|
614 | else // reload if we still have the charge |
---|
615 | { |
---|
616 | this->requestAction(WA_RELOAD); |
---|
617 | this->execute(); |
---|
618 | } |
---|
619 | return true; |
---|
620 | } |
---|
621 | |
---|
622 | /** |
---|
623 | * checks and Reloads the Weapon |
---|
624 | * @return true on success. |
---|
625 | */ |
---|
626 | bool Weapon::reloadW() |
---|
627 | { |
---|
628 | PRINTF(4)("Reloading Weapon %s\n", this->getCName()); |
---|
629 | if (!this->ammoContainer.isNull() && |
---|
630 | unlikely(this->energy + this->ammoContainer->getStoredEnergy() < this->minCharge)) |
---|
631 | { |
---|
632 | //this->requestAction(WA_DEACTIVATE); |
---|
633 | this->execute(); |
---|
634 | return false; |
---|
635 | } |
---|
636 | |
---|
637 | |
---|
638 | if (this->soundBuffers[WA_RELOAD].loaded()) |
---|
639 | this->soundSource->play(this->soundBuffers[WA_RELOAD]); |
---|
640 | |
---|
641 | if (!this->ammoContainer.isNull()) |
---|
642 | this->ammoContainer->fillWeapon(this); |
---|
643 | else |
---|
644 | { |
---|
645 | this->energy = this->energyMax; |
---|
646 | } |
---|
647 | this->updateWidgets(); |
---|
648 | this->reload(); |
---|
649 | this->enterState(WS_RELOADING); |
---|
650 | |
---|
651 | return true; |
---|
652 | } |
---|
653 | |
---|
654 | /** |
---|
655 | * enters the requested State, plays back animations updates the timing. |
---|
656 | * @param state the state to enter. |
---|
657 | */ |
---|
658 | inline void Weapon::enterState(WeaponState state) |
---|
659 | { |
---|
660 | PRINTF(4)("ENTERING STATE %s\n", stateToChar(state)); |
---|
661 | // playing animation if availiable |
---|
662 | if (likely(this->animation[state] != NULL)) |
---|
663 | this->animation[state]->replay(); |
---|
664 | |
---|
665 | this->stateDuration += this->times[state]; |
---|
666 | this->currentState = state; |
---|
667 | } |
---|
668 | |
---|
669 | /////////////////// |
---|
670 | // WORLD-ENTITY // |
---|
671 | // FUNCTIONALITY // |
---|
672 | /////////////////// |
---|
673 | /** |
---|
674 | * tick signal for time dependent/driven stuff |
---|
675 | */ |
---|
676 | bool Weapon::tickW(float dt) |
---|
677 | { |
---|
678 | //printf("%s ", stateToChar(this->currentState)); |
---|
679 | |
---|
680 | // setting up the timing properties |
---|
681 | this->stateDuration -= dt; |
---|
682 | |
---|
683 | if (this->stateDuration <= 0.0) |
---|
684 | { |
---|
685 | if (unlikely (this->currentState == WS_DEACTIVATING)) |
---|
686 | { |
---|
687 | this->currentState = WS_INACTIVE; |
---|
688 | return false; |
---|
689 | } |
---|
690 | else |
---|
691 | this->currentState = WS_IDLE; |
---|
692 | |
---|
693 | if (this->requestedAction != WA_NONE) |
---|
694 | { |
---|
695 | this->stateDuration = -dt; |
---|
696 | this->execute(); |
---|
697 | } |
---|
698 | } |
---|
699 | return true; |
---|
700 | } |
---|
701 | |
---|
702 | |
---|
703 | |
---|
704 | |
---|
705 | ////////////////////// |
---|
706 | // HELPER FUNCTIONS // |
---|
707 | ////////////////////// |
---|
708 | /** |
---|
709 | * checks wether all the Weapons functions are valid, and if it is possible to go to action with it. |
---|
710 | * @todo IMPLEMENT the Weapons Check |
---|
711 | */ |
---|
712 | bool Weapon::check() const |
---|
713 | { |
---|
714 | bool retVal = true; |
---|
715 | |
---|
716 | // if (this->projectile == NULL) |
---|
717 | { |
---|
718 | PRINTF(1)("There was no projectile assigned to the Weapon.\n"); |
---|
719 | retVal = false; |
---|
720 | } |
---|
721 | |
---|
722 | |
---|
723 | |
---|
724 | |
---|
725 | return retVal; |
---|
726 | } |
---|
727 | |
---|
728 | /** |
---|
729 | * some nice debugging information about this Weapon |
---|
730 | */ |
---|
731 | void Weapon::debug() const |
---|
732 | { |
---|
733 | PRINT(0)("Weapon-Debug %s, state: %s (duration: %fs), nextAction: %s\n", this->getCName(), Weapon::stateToChar(this->currentState), this->stateDuration, Weapon::actionToChar(requestedAction)); |
---|
734 | PRINT(0)("Energy: max: %f; current: %f; chargeMin: %f, chargeMax %f\n", |
---|
735 | this->energyMax, this->energy, this->minCharge, this->maxCharge); |
---|
736 | |
---|
737 | |
---|
738 | } |
---|
739 | |
---|
740 | //////////////////////////////////////////////////////// |
---|
741 | // static Definitions (transormators for readability) // |
---|
742 | //////////////////////////////////////////////////////// |
---|
743 | /** |
---|
744 | * Converts a String into an Action. |
---|
745 | * @param action the String input holding the Action. |
---|
746 | * @return The Action if known, WA_NONE otherwise. |
---|
747 | */ |
---|
748 | WeaponAction Weapon::charToAction(const std::string& action) |
---|
749 | { |
---|
750 | if (action == "none") |
---|
751 | return WA_NONE; |
---|
752 | else if (action == "shoot") |
---|
753 | return WA_SHOOT; |
---|
754 | else if (action == "charge") |
---|
755 | return WA_CHARGE; |
---|
756 | else if (action == "reload") |
---|
757 | return WA_RELOAD; |
---|
758 | else if (action == "acitvate") |
---|
759 | return WA_ACTIVATE; |
---|
760 | else if (action == "deactivate") |
---|
761 | return WA_DEACTIVATE; |
---|
762 | else if (action == "special1") |
---|
763 | return WA_SPECIAL1; |
---|
764 | else |
---|
765 | { |
---|
766 | PRINTF(2)("action %s could not be identified.\n", action.c_str()); |
---|
767 | return WA_NONE; |
---|
768 | } |
---|
769 | } |
---|
770 | |
---|
771 | /** |
---|
772 | * converts an action into a String |
---|
773 | * @param action the action to convert |
---|
774 | * @return a String matching the name of the action |
---|
775 | */ |
---|
776 | const char* Weapon::actionToChar(WeaponAction action) |
---|
777 | { |
---|
778 | switch (action) |
---|
779 | { |
---|
780 | case WA_SHOOT: |
---|
781 | return "shoot"; |
---|
782 | break; |
---|
783 | case WA_CHARGE: |
---|
784 | return "charge"; |
---|
785 | break; |
---|
786 | case WA_RELOAD: |
---|
787 | return "reload"; |
---|
788 | break; |
---|
789 | case WA_ACTIVATE: |
---|
790 | return "activate"; |
---|
791 | break; |
---|
792 | case WA_DEACTIVATE: |
---|
793 | return "deactivate"; |
---|
794 | break; |
---|
795 | case WA_SPECIAL1: |
---|
796 | return "special1"; |
---|
797 | break; |
---|
798 | default: |
---|
799 | return "none"; |
---|
800 | break; |
---|
801 | } |
---|
802 | } |
---|
803 | |
---|
804 | /** |
---|
805 | * Converts a String into a State. |
---|
806 | * @param state the String input holding the State. |
---|
807 | * @return The State if known, WS_NONE otherwise. |
---|
808 | */ |
---|
809 | WeaponState Weapon::charToState(const std::string& state) |
---|
810 | { |
---|
811 | if (state == "none") |
---|
812 | return WS_NONE; |
---|
813 | else if (state == "shooting") |
---|
814 | return WS_SHOOTING; |
---|
815 | else if (state == "charging") |
---|
816 | return WS_CHARGING; |
---|
817 | else if (state == "reloading") |
---|
818 | return WS_RELOADING; |
---|
819 | else if (state == "activating") |
---|
820 | return WS_ACTIVATING; |
---|
821 | else if (state == "deactivating") |
---|
822 | return WS_DEACTIVATING; |
---|
823 | else if (state == "inactive") |
---|
824 | return WS_INACTIVE; |
---|
825 | else if (state == "idle") |
---|
826 | return WS_IDLE; |
---|
827 | else |
---|
828 | { |
---|
829 | PRINTF(2)("state %s could not be identified.\n", state.c_str()); |
---|
830 | return WS_NONE; |
---|
831 | } |
---|
832 | } |
---|
833 | |
---|
834 | /** |
---|
835 | * converts a State into a String |
---|
836 | * @param state the state to convert |
---|
837 | * @return a String matching the name of the state |
---|
838 | */ |
---|
839 | const char* Weapon::stateToChar(WeaponState state) |
---|
840 | { |
---|
841 | switch (state) |
---|
842 | { |
---|
843 | case WS_SHOOTING: |
---|
844 | return "shooting"; |
---|
845 | break; |
---|
846 | case WS_CHARGING: |
---|
847 | return "charging"; |
---|
848 | break; |
---|
849 | case WS_RELOADING: |
---|
850 | return "reloading"; |
---|
851 | break; |
---|
852 | case WS_ACTIVATING: |
---|
853 | return "activating"; |
---|
854 | break; |
---|
855 | case WS_DEACTIVATING: |
---|
856 | return "deactivating"; |
---|
857 | break; |
---|
858 | case WS_IDLE: |
---|
859 | return "idle"; |
---|
860 | break; |
---|
861 | case WS_INACTIVE: |
---|
862 | return "inactive"; |
---|
863 | break; |
---|
864 | default: |
---|
865 | return "none"; |
---|
866 | break; |
---|
867 | } |
---|
868 | } |
---|