Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/world_entities/weapons/weapon.cc @ 4926

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

orxonox/trunk: the activation and deactivation-phase of the Weapon work too.

File size: 13.8 KB
Line 
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 "projectile.h"
24
25#include "class_list.h"
26#include "load_param.h"
27#include "vector.h"
28#include "list.h"
29#include "state.h"
30#include "animation3d.h"
31#include "sound_engine.h"
32
33////////////////////
34// INITAILISATION //
35// SETTING VALUES //
36////////////////////
37/**
38 * standard constructor
39 *
40 * creates a new weapon
41*/
42Weapon::Weapon (PNode* parent, const Vector& coordinate, const Quaternion& direction)
43{
44  this->init();
45  parent->addChild(this, PNODE_ALL);
46  this->setRelCoor(coordinate);
47  this->setRelDir(direction);
48}
49
50/**
51 * standard deconstructor
52*/
53Weapon::~Weapon ()
54{
55  for (int i = 0; i < WS_STATE_COUNT; i++)
56    if (this->animation[i] && ClassList::exists(animation[i], CL_ANIMATION))  //!< @todo this should check animation3D
57      delete this->animation[i];
58  for (int i = 0; i < WA_ACTION_COUNT; i++)
59    if (this->soundBuffers[i])
60      ResourceManager::getInstance()->unload(this->soundBuffers[i]);
61}
62
63/**
64 * initializes the Weapon with ALL default values
65 */
66void Weapon::init()
67{
68  this->currentState     = WS_INACTIVE;
69  this->requestedAction  = WA_NONE;
70  this->stateDuration    = 0.0;
71  for (int i = 0; i < WS_STATE_COUNT; i++)
72    {
73      this->times[i] = 0.0;
74      this->animation[i] = NULL;
75    }
76  for (int i = 0; i < WA_ACTION_COUNT; i++)
77    this->soundBuffers[i] = NULL;
78
79  this->soundSource = new SoundSource(this);
80  this->emissionPoint.setParent(this);
81
82  this->projectile = NULL;
83
84  this->hideInactive = true;
85
86  this->minCharge = 1.0;
87  this->maxCharge = 1.0;
88  this->energyLoaded = .0;
89  this->energyLoadedMax = 10.0;
90  this->energy = .0;
91  this->energyMax = 100.0;
92}
93
94
95/**
96 * sets the emissionPoint's relative position from the Weapon
97 * @param point the Point relative to the mass-point of the Weapon
98 */
99void Weapon::setEmissionPoint(const Vector& point)
100{
101  this->emissionPoint.setRelCoor(point);
102}
103
104/**
105 * assigns a Sound-file to an action
106 * @param action the action the sound should be assigned too
107 * @param soundFile the soundFile's relative position to the data-directory (will be looked for by the ResourceManager)
108 */
109void Weapon::setActionSound(WeaponAction action, const char* soundFile)
110{
111  if (action >= WA_ACTION_COUNT)
112    return;
113  else if (soundFile != NULL)
114  {
115    this->soundBuffers[action] = (SoundBuffer*)ResourceManager::getInstance()->load(soundFile, WAV);
116    if (this->soundBuffers[action] != NULL)
117    {
118      PRINTF(4)("Loaded sound %s to action %s\n", soundFile, actionToChar(action));
119    }
120    else
121    {
122      PRINTF(4)("failed to load sound %s to %s\n", soundFile, actionToChar(action));
123    }
124  }
125  else
126    this->soundBuffers[action] = NULL;
127}
128
129
130/**
131 * creates/returns an Animation3D for a certain State.
132 * @param state what State should the Animation be created/returned for
133 * @param node the node this Animation should apply to. (NULL is fine if the animation was already created)
134 * @returns The created animation.Animation(), NULL on error (or if the animation does not yet exist).
135 *
136 * This function does only generate the Animation Object, and if set it will
137 * automatically be executed, when a certain State is reached.
138 * What this does not do, is set keyframes, you have to operate on the returned animation.
139 */
140Animation3D* Weapon::getAnimation(WeaponState state, PNode* node)
141{
142  if (state >= WS_STATE_COUNT) // if the state is not known
143    return NULL;
144
145  if (unlikely(this->animation[state] == NULL)) // if the animation does not exist yet create it.
146  {
147    if (likely(node != NULL))
148      return this->animation[state] = new Animation3D(node);
149    else
150    {
151      PRINTF(2)("Node not defined for the Creation of the 3D-animation of state %s\n", stateToChar(state));
152      return NULL;
153    }
154  }
155  else
156    return this->animation[state];
157}
158
159
160/////////////////
161//  EXECUTION  //
162// GAME ACTION //
163/////////////////
164/**
165 * request an action that should be executed,
166 * @param action the next action to take
167 *
168 * This function must be called instead of the actions (like fire/reload...)
169 * to make all the checks needed to have a usefull WeaponSystem.
170 */
171void Weapon::requestAction(WeaponAction action)
172{
173  if (likely(this->isActive()))
174  {
175    if (this->requestedAction != WA_NONE)
176      return;
177    printf("next action will be %s in %f seconds\n", actionToChar(action), this->stateDuration);
178    this->requestedAction = action;
179  }
180  //else
181  else if (unlikely(action == WA_ACTIVATE))
182  {
183    this->currentState = WS_ACTIVATING;
184    this->requestedAction = WA_ACTIVATE;
185  }
186}
187
188/**
189 * adds energy to the Weapon
190 * @param energyToAdd The amount of energy
191 * @returns the amount of energy we did not pick up, because the weapon is already full
192 */
193float Weapon::increaseEnergy(float energyToAdd)
194{
195  float maxAddEnergy = this->energyMax - this->energy;
196
197  if (maxAddEnergy >= energyToAdd)
198  {
199    this->energy += energyToAdd;
200    return 0.0;
201  }
202  else
203  {
204    this->energy += maxAddEnergy;
205    return energyToAdd - maxAddEnergy;
206  }
207}
208
209//////////////////////
210// WEAPON INTERNALS //
211//////////////////////
212/**
213 * executes an action, and with it starts a new State.
214 * @return true, if it worked, false otherwise
215 *
216 * This function checks, wheter the possibility of executing an action is valid,
217 * and does all the necessary stuff, to execute them. If an action does not succeed,
218 * it tries to go around it. (ex. shoot->noAmo->reload()->wait until shoot comes again)
219 */
220bool Weapon::execute()
221{
222#if DEBUG > 4
223  PRINTF(4)("trying to execute action %s\n", actionToChar(this->requestedAction));
224  this->debug();
225#endif
226
227  WeaponAction action = this->requestedAction;
228  this->requestedAction = WA_NONE;
229
230  switch (action)
231  {
232    case WA_SHOOT:
233      return this->fireW();
234      break;
235    case WA_CHARGE:
236      return this->chargeW();
237      break;
238    case WA_RELOAD:
239      return this->reloadW();
240      break;
241    case WA_DEACTIVATE:
242      return this->deactivateW();
243      break;
244    case WA_ACTIVATE:
245      return this->activateW();
246      break;
247  }
248}
249
250
251/**
252 * checks and activates the Weapon.
253 * @return true on success.
254 */
255bool Weapon::activateW()
256{
257//  if (this->currentState == WS_INACTIVE)
258  {
259        // play Sound
260    if (likely(this->soundBuffers[WA_ACTIVATE] != NULL))
261      this->soundSource->play(this->soundBuffers[WA_ACTIVATE]);
262        // activate
263    PRINTF(4)("Activating the Weapon %s\n", this->getName());
264    this->activate();
265    // setting up for next action
266    this->enterState(WS_ACTIVATING);
267  }
268}
269
270
271/**
272 * checks and deactivates the Weapon
273 * @return true on success.
274 */
275bool Weapon::deactivateW()
276{
277  if (this->currentState != WS_INACTIVE)
278  {
279    PRINTF(4)("Deactivating the Weapon %s\n", this->getName());
280        // play Sound
281    if (this->soundBuffers[WA_DEACTIVATE] != NULL)
282      this->soundSource->play(this->soundBuffers[WA_DEACTIVATE]);
283    // deactivate
284    this->deactivate();
285    this->enterState(WS_DEACTIVATING);
286  }
287}
288
289/**
290 * checks and charges the Weapon
291 * @return true on success.
292 */
293bool Weapon::chargeW()
294{
295  if ( this->currentState != WS_INACTIVE && this->energyLoaded >= this->minCharge)
296  {
297        // playing Sound
298    if (this->soundBuffers[WA_CHARGE] != NULL)
299      this->soundSource->play(this->soundBuffers[WA_CHARGE]);
300
301        // charge
302    this->charge();
303        // setting up for the next state
304    this->enterState(WS_CHARGING);
305  }
306  else // deactivate the Weapon if we do not have enough energy
307  {
308    this->requestAction(WA_RELOAD);
309  }
310
311}
312
313/**
314 * checks and fires the Weapon
315 * @return true on success.
316 */
317bool Weapon::fireW()
318{
319     //if (likely(this->currentState != WS_INACTIVE))
320  if (this->minCharge <= this->energyLoaded)
321  {
322          // playing Sound
323    if (this->soundBuffers[WA_SHOOT] != NULL)
324      this->soundSource->play(this->soundBuffers[WA_SHOOT]);
325          // fire
326    this->fire();
327    this->energyLoaded -= this->minCharge;
328          // setting up for the next state
329    this->enterState(WS_SHOOTING);
330  }
331  else  // reload if we still have the charge
332  {
333    this->requestAction(WA_RELOAD);
334  }
335}
336
337
338/**
339 * checks and Reloads the Weapon
340 * @return true on success.
341 */
342bool Weapon::reloadW()
343{
344  PRINTF(4)("Reloading Weapon %s\n", this->getName());
345  if (unlikely(this->energy + this->energyLoaded < this->minCharge))
346  {
347    this->requestAction(WA_DEACTIVATE);
348    return false;
349  }
350
351  float chargeSize = this->energyLoadedMax - this->energyLoaded;       //!< The energy to be charged
352
353  if (this->soundBuffers[WA_RELOAD] != NULL)
354    this->soundSource->play(this->soundBuffers[WA_RELOAD]);
355
356  if (chargeSize > this->energy)
357  {
358    this->energyLoaded += this->energy;
359    this->energy = 0.0;
360    PRINT(3)("Energy empty");
361  }
362  else
363  {
364    PRINTF(3)("Loaded %f energy into the Guns Buffer\n", chargeSize);
365    this->energyLoaded += chargeSize;
366    this->energy -= chargeSize;
367  }
368  this->reload();
369  this->enterState(WS_RELOADING);
370}
371
372
373/**
374 * enters the requested State, plays back animations updates the timing.
375 * @param state the state to enter.
376 */
377inline void Weapon::enterState(WeaponState state)
378{
379  // playing animation if availiable
380  if (likely(this->animation[state] != NULL))
381    this->animation[state]->replay();
382
383  this->stateDuration = this->times[state] + this->stateDuration;
384  this->currentState = state;
385}
386
387
388/**
389 * tick signal for time dependent/driven stuff
390*/
391void Weapon::tickW(float dt)
392{
393  printf("%s ", stateToChar(this->currentState));
394
395  // setting up the timing properties
396  this->stateDuration -= dt;
397
398  if (this->isActive())
399  {
400    if (this->stateDuration <= 0.0)
401    {
402      if (unlikely (this->currentState == WS_DEACTIVATING))
403      {
404        this->currentState = WS_INACTIVE;
405        return;
406      }
407      else
408        this->currentState = WS_IDLE;
409
410      if (this->requestedAction != WA_NONE)
411      {
412        this->stateDuration = -dt;
413        this->execute();
414      }
415    }
416  }
417  tick(dt);
418}
419
420/**
421 *  this will draw the weapon
422*/
423void Weapon::draw ()
424{}
425
426
427
428
429
430//////////////////////
431// HELPER FUNCTIONS //
432//////////////////////
433/**
434 * checks wether all the Weapons functions are valid, and if it is possible to go to action with it.
435 *
436 */
437bool Weapon::check() const
438{
439  bool retVal = true;
440
441  if (this->projectile == NULL)
442  {
443    PRINTF(2)("There was no projectile assigned to the Weapon.\n");
444    retVal = false;
445  }
446
447
448
449
450  return retVal;
451}
452
453/**
454 * some nice debugging information about this Weapon
455 */
456void Weapon::debug() const
457{
458  PRINT(3)("Weapon-Debug %s, state: %s, nexAction: %s\n", this->getName(), Weapon::stateToChar(this->currentState), Weapon::actionToChar(requestedAction));
459  PRINT(3)("Energy: max: %f; current: %f;  loadedMax: %f; loadedCurrent: %f; chargeMin: %f, chargeMax %f\n",
460            this->energyMax, this->energy, this->energyLoadedMax, this->energyLoaded, this->minCharge, this->maxCharge);
461}
462
463
464// static
465/**
466 * Converts a String into an Action.
467 * @param action the String input holding the Action.
468 * @return The Action if known, WA_NONE otherwise.
469 */
470WeaponAction Weapon::charToAction(const char* action)
471{
472  if (!strcmp(action, "none"))
473    return WA_NONE;
474  else if (!strcmp(action, "shoot"))
475    return WA_SHOOT;
476  else if (!strcmp(action, "charge"))
477    return WA_CHARGE;
478  else if (!strcmp(action, "reload"))
479    return WA_RELOAD;
480  else if (!strcmp(action, "acitvate"))
481    return WA_ACTIVATE;
482  else if (!strcmp(action, "deactivate"))
483    return WA_DEACTIVATE;
484  else if (!strcmp(action, "special1"))
485    return WA_SPECIAL1;
486  else
487    {
488      PRINTF(2)("action %s could not be identified.\n", action);
489      return WA_NONE;
490    }
491}
492
493/**
494 * converts an action into a String
495 * @param action the action to convert
496 * @return a String matching the name of the action
497 */
498const char* Weapon::actionToChar(WeaponAction action)
499{
500  switch (action)
501  {
502    case WA_SHOOT:
503      return "shoot";
504      break;
505    case WA_CHARGE:
506      return "charge";
507      break;
508    case WA_RELOAD:
509      return "reload";
510      break;
511    case WA_ACTIVATE:
512      return "activate";
513      break;
514    case WA_DEACTIVATE:
515      return "deactivate";
516      break;
517    case WA_SPECIAL1:
518      return "special1";
519      break;
520    default:
521      return "none";
522      break;
523  }
524}
525
526/**
527 * Converts a String into a State.
528 * @param state the String input holding the State.
529 * @return The State if known, WS_NONE otherwise.
530 */
531WeaponState Weapon::charToState(const char* state)
532{
533  if (!strcmp(state, "none"))
534    return WS_NONE;
535  else if (!strcmp(state, "shooting"))
536    return WS_SHOOTING;
537  else if (!strcmp(state, "charging"))
538    return WS_CHARGING;
539  else if (!strcmp(state, "reloading"))
540    return WS_RELOADING;
541  else if (!strcmp(state, "activating"))
542    return WS_ACTIVATING;
543  else if (!strcmp(state, "deactivating"))
544    return WS_DEACTIVATING;
545  else if (!strcmp(state, "inactive"))
546    return WS_INACTIVE;
547  else if (!strcmp(state, "idle"))
548    return WS_IDLE;
549  else
550    {
551      PRINTF(2)("state %s could not be identified.\n", state);
552      return WS_NONE;
553    }
554}
555
556/**
557 * converts a State into a String
558 * @param state the state to convert
559 * @return a String matching the name of the state
560 */
561const char* Weapon::stateToChar(WeaponState state)
562{
563  switch (state)
564  {
565    case WS_SHOOTING:
566      return "shooting";
567      break;
568    case WS_CHARGING:
569      return "charging";
570      break;
571    case WS_RELOADING:
572      return "reloading";
573      break;
574    case WS_ACTIVATING:
575      return "activating";
576      break;
577    case WS_DEACTIVATING:
578      return "deactivating";
579      break;
580    case WS_IDLE:
581      return "idle";
582      break;
583    case WS_INACTIVE:
584      return "inactive";
585      break;
586    default:
587      return "none";
588      break;
589  }
590}
Note: See TracBrowser for help on using the repository browser.