Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/weaponSystem/src/world_entities/weapons/weapon.cc @ 4882

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

orxonox/trunk: the energy system works

File size: 9.3 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 "load_param.h"
26#include "vector.h"
27#include "list.h"
28#include "state.h"
29#include "animation3d.h"
30#include "sound_engine.h"
31
32/**
33 * standard constructor
34 *
35 * creates a new weapon
36*/
37Weapon::Weapon (PNode* parent, const Vector& coordinate, const Quaternion& direction)
38{
39  this->init();
40  parent->addChild(this, PNODE_ALL);
41  this->setRelCoor(coordinate);
42  this->setRelDir(direction);
43}
44
45/**
46 * standard deconstructor
47*/
48Weapon::~Weapon ()
49{
50  for (int i = 0; i < WS_STATE_COUNT; i++)
51    if (this->animation[i])
52      delete this->animation[i];
53  for (int i = 0; i < WA_ACTION_COUNT; i++)
54    if (this->soundBuffers[i])
55      delete this->soundBuffers[i];
56}
57
58/**
59 * initializes the Weapon with ALL default values
60 */
61void Weapon::init()
62{
63  this->currentState     = WS_INACTIVE;
64  this->requestedAction  = WA_NONE;
65  this->stateDuration    = 0.0;
66  for (int i = 0; i < WS_STATE_COUNT; i++)
67    {
68      this->times[i] = 0.0;
69      this->animation[i] = NULL;
70    }
71  for (int i = 0; i < WA_ACTION_COUNT; i++)
72    this->soundBuffers[i] = NULL;
73
74  this->requestedAction = WA_NONE;
75  this->weaponSource = NULL;
76
77  this->active = true;
78  this->projectile = NULL;
79
80  this->minCharge = 1.0;
81  this->maxCharge = 1.0;
82  this->energyLoaded = .0;
83  this->energyLoadedMax = 10.0;
84  this->energy = .0;
85  this->energyMax = 100.0;
86}
87
88/**
89 * sets a new projectile to the weapon
90 * @param new projectile for this weapon
91 *
92 * weapon an projectile are independent, so you can combine them as you want
93*/
94void Weapon::setProjectile(Projectile* projectile)
95{
96  this->projectile = projectile;
97}
98
99
100/**
101 * sets a new projectile to the weapon
102 * @returns the current projectile of this weapon
103 *
104 * weapon an projectile are independent, so you can combine them as you want
105*/
106Projectile* Weapon::getProjectile()
107{
108  return this->projectile;
109}
110
111
112void Weapon::requestAction(WeaponAction action)
113{
114  if (this->requestedAction != WA_NONE)
115    return;
116  else
117  {
118    printf("next action will be %s in %f seconds\n", actionToChar(action), this->stateDuration);
119    this->requestedAction = action;
120  }
121}
122
123bool Weapon::execute()
124{
125  this->stateDuration = this->times[this->requestedAction] + this->stateDuration;
126
127  PRINTF(4)("trying to execute action %s\n", actionToChar(this->requestedAction));
128  this->debug();
129
130  switch (this->requestedAction)
131  {
132    case WA_SHOOT:
133      //if (likely(this->currentState != WS_INACTIVE))
134      {
135        if (this->minCharge < this->energyLoaded)
136        {
137          this->fire();
138          this->requestedAction = WA_NONE;
139        }
140        else  // reload if we still have the charge
141        {
142          this->requestedAction = WA_NONE;
143          this->requestAction(WA_RELOAD);
144        }
145      }
146      break;
147    case WA_CHARGE:
148      if ( this->currentState != WS_INACTIVE && this->energyLoaded >= this->minCharge)
149      {
150        this->charge();
151        this->requestedAction = WA_NONE;
152      }
153      else // deactivate the Weapon if we do not have enough energy
154      {
155        this->requestedAction = WA_NONE;
156        this->requestAction(WA_RELOAD);
157      }
158      break;
159    case WA_RELOAD:
160      //if (this->currentState != WS_INACTIVE && this->energy + this->energyLoaded >= this->minCharge)
161      {
162        this->reload();
163        this->requestedAction = WA_NONE;
164      }
165      break;
166    case WA_DEACTIVATE:
167      if (this->currentState != WS_INACTIVE)
168      {
169        this->deactivate();
170        this->requestedAction = WA_NONE;
171      }
172      break;
173    case WA_ACTIVATE:
174      if (this->currentState == WS_INACTIVE)
175      {
176        this->activate();
177        this->requestedAction = WA_NONE;
178      }
179      break;
180  }
181}
182
183/**
184 * this activates the weapon
185*/
186void Weapon::activate()
187{
188  PRINTF(4)("Activating the Weapon %s\n", this->getName());
189}
190
191
192/**
193 * this deactivates the weapon
194*/
195void Weapon::deactivate()
196{
197  PRINTF(4)("Deactivating the Weapon %s\n", this->getName());
198}
199
200void Weapon::fire()
201{
202  this->energyLoaded -= this->minCharge;
203}
204
205void Weapon::reload()
206{
207  PRINTF(4)("Reloading Weapon %s\n", this->getName());
208  if (this->energy + this->energyLoaded < this->minCharge)
209  {
210    this->requestAction(WA_DEACTIVATE);
211    return;
212  }
213
214  float chargeSize = this->energyLoadedMax - this->energyLoaded;       //!< The energy to be charged
215
216  if (chargeSize > this->energy)
217  {
218    this->energyLoaded += this->energy;
219    this->energy = 0.0;
220    PRINT(3)("Energy empty");
221  }
222  else
223  {
224    PRINTF(3)("Loaded %f energy into the Guns Buffer\n", chargeSize);
225    this->energyLoaded += chargeSize;
226    this->energy -= chargeSize;
227  }
228}
229
230void Weapon::charge()
231{
232}
233
234
235/**
236 *  is called, when the weapon is destroyed
237 *
238 * this is in conjunction with the hit function, so when a weapon is able to get
239 * hit, it can also be destoryed.
240*/
241void Weapon::destroy ()
242{}
243
244
245/**
246 * tick signal for time dependent/driven stuff
247*/
248void Weapon::tick(float dt)
249{
250  // setting up the timing properties
251  this->stateDuration -= dt;
252
253  if (this->isActive())
254  {
255    if (this->stateDuration <= 0.0 && this->requestedAction != WA_NONE)
256    {
257      this->stateDuration = -dt;
258      this->execute();
259    }
260  }
261  else
262    if (this->requestedAction == WA_ACTIVATE)
263      this->activate();
264
265}
266
267/**
268 *  this will draw the weapon
269*/
270void Weapon::draw ()
271{}
272
273
274
275
276
277//////////////////////
278// HELPER FUNCTIONS //
279//////////////////////
280// inclass
281/**
282 * checks if the next Action given is valid
283 * @returns if the Action that comes next is valid
284 * @todo more checks
285 */
286bool Weapon::nextActionValid() const
287{
288  if (this->currentState == WS_INACTIVE)
289  {
290    return (this->requestedAction == WA_ACTIVATE || this->requestedAction == WA_NONE);
291  }
292  else
293    return true;
294
295}
296
297
298/**
299 * some nice debugging information about this Weapon
300 */
301void Weapon::debug() const
302{
303  PRINT(3)("Weapon-Debug %s, state: %s, nexAction: %s\n", this->getName(), Weapon::stateToChar(this->currentState), Weapon::actionToChar(requestedAction));
304  PRINT(3)("Energy: max: %f; current: %f;  loadedMax: %f; loadedCurrent: %f; chargeMin: %f, chargeMax %f\n",
305            this->energyMax, this->energy, this->energyLoadedMax, this->energyLoaded, this->minCharge, this->maxCharge);
306}
307
308
309// static
310/**
311 * Converts a String into an Action.
312 * @param action the String input holding the Action.
313 * @return The Action if known, WA_NONE otherwise.
314 */
315WeaponAction Weapon::charToAction(const char* action)
316{
317  if (!strcmp(action, "none"))
318    return WA_NONE;
319  else if (!strcmp(action, "shoot"))
320    return WA_SHOOT;
321  else if (!strcmp(action, "charge"))
322    return WA_CHARGE;
323  else if (!strcmp(action, "reload"))
324    return WA_RELOAD;
325  else if (!strcmp(action, "acitvate"))
326    return WA_ACTIVATE;
327  else if (!strcmp(action, "deactivate"))
328    return WA_DEACTIVATE;
329  else if (!strcmp(action, "special1"))
330    return WA_SPECIAL1;
331  else
332    {
333      PRINTF(2)("action %s could not be identified.\n", action);
334      return WA_NONE;
335    }
336}
337
338/**
339 * converts an action into a String
340 * @param action the action to convert
341 * @return a String matching the name of the action
342 */
343const char* Weapon::actionToChar(WeaponAction action)
344{
345  switch (action)
346  {
347    case WA_SHOOT:
348      return "shoot";
349      break;
350    case WA_CHARGE:
351      return "charge";
352      break;
353    case WA_RELOAD:
354      return "reload";
355      break;
356    case WA_ACTIVATE:
357      return "activate";
358      break;
359    case WA_DEACTIVATE:
360      return "deactivate";
361      break;
362    case WA_SPECIAL1:
363      return "special1";
364      break;
365    default:
366      return "none";
367      break;
368  }
369}
370
371/**
372 * Converts a String into a State.
373 * @param state the String input holding the State.
374 * @return The State if known, WS_NONE otherwise.
375 */
376WeaponState Weapon::charToState(const char* state)
377{
378  if (!strcmp(state, "none"))
379    return WS_NONE;
380  else if (!strcmp(state, "shooting"))
381    return WS_SHOOTING;
382  else if (!strcmp(state, "charging"))
383    return WS_CHARGING;
384  else if (!strcmp(state, "reloading"))
385    return WS_RELOADING;
386  else if (!strcmp(state, "activating"))
387    return WS_ACTIVATING;
388  else if (!strcmp(state, "deactivating"))
389    return WS_DEACTIVATING;
390  else if (!strcmp(state, "inactive"))
391    return WS_INACTIVE;
392  else if (!strcmp(state, "idle"))
393    return WS_IDLE;
394  else
395    {
396      PRINTF(2)("state %s could not be identified.\n", state);
397      return WS_NONE;
398    }
399}
400
401/**
402 * converts a State into a String
403 * @param state the state to convert
404 * @return a String matching the name of the state
405 */
406const char* Weapon::stateToChar(WeaponState state)
407{
408  switch (state)
409  {
410    case WS_SHOOTING:
411      return "shooting";
412      break;
413    case WS_CHARGING:
414      return "charging";
415      break;
416    case WS_RELOADING:
417      return "reloading";
418      break;
419    case WS_ACTIVATING:
420      return "activating";
421      break;
422    case WS_DEACTIVATING:
423      return "deactivating";
424      break;
425    case WS_IDLE:
426      return "idle";
427      break;
428    default:
429      return "none";
430      break;
431  }
432}
Note: See TracBrowser for help on using the repository browser.