Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: adapted the Factory algorithm to ObjectManager… still a logn way to go…

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