Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/orxonox/objects/weapon_system/BaseWeapon.cc @ 742

Last change on this file since 742 was 742, checked in by landauf, 16 years ago

moved all files from misc and the tinyxml folder into the new util folder

File size: 4.2 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Reto Grieder
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28#include <OgreStringConverter.h>
29#include <OgreSceneNode.h>
30#include <OgreEntity.h>
31#include <OgreSceneManager.h>
32
33#include "util/Math.h"
34#include "../../Orxonox.h"
35#include "Bullet.h"
36#include "BulletManager.h"
37#include "AmmunitionDump.h"
38
39#include "BaseWeapon.h"
40
41
42namespace orxonox {
43  float BaseWeapon::nextActionValidityPeriod_s = 0.5;
44
45  BaseWeapon::BaseWeapon()
46    : sceneMgr_(Orxonox::getSingleton()->getSceneManager()),
47      bulletCounter_(0),
48      bulletManager_(Orxonox::getSingleton()->getBulletMgr()),
49      ammoDump_(NULL),
50      primaryFireRequest_(false), secondaryFireRequest_(false),
51      totalTime_(0.0f), actionStartTime_(0.0f),
52      currentState_(IDLE), secondaryFired_(false),
53      nextAction_(NOTHING), actionAdded_(false),
54      timeSinceNextActionAdded_(0.0f),
55      leftAmmo_(0)
56  {
57    RegisterObject(BaseWeapon);
58  }
59
60
61  BaseWeapon::~BaseWeapon()
62  {
63  }
64
65
66  bool BaseWeapon::addAction(const Action act)
67  {
68    if (nextAction_ == NOTHING)
69    {
70      nextAction_ = act;
71      actionAdded_ = true;
72      return true;
73    }
74    else
75      return false;
76  }
77
78
79  void BaseWeapon::primaryFireRequest()
80  {
81    primaryFireRequest_ = true;
82  }
83
84
85  void BaseWeapon::secondaryFireRequest()
86  {
87    secondaryFireRequest_ = true;
88  }
89
90
91  void BaseWeapon::tick(float dt)
92  {
93    totalTime_ += dt;
94    // process action adder
95    if (actionAdded_)
96    {
97      timeSinceNextActionAdded_ = totalTime_;
98      actionAdded_ = false;
99    }
100
101    switch (currentState_)
102    {
103    case IDLE:
104      break;
105
106    case PRIMARY_FIRE:
107      primaryFiring(totalTime_ - actionStartTime_);
108      break;
109
110    case SECONDARY_FIRE:
111      secondaryFiring(totalTime_ - actionStartTime_);
112      break;
113
114    case RELOADING:
115      break;
116
117    case CHANGING_AMMO:
118      break;
119    }
120
121    if (currentState_ == IDLE)
122    {
123      // first, process next action
124      if (nextAction_ != NOTHING)
125      {
126        actionStartTime_ = totalTime_;
127        switch (nextAction_)
128        {
129        case RELOAD:
130          leftAmmo_ += ammoDump_->getAmmunition("Barrel", magazineSize_ - leftAmmo_);
131          break;
132
133        case CHANGE_AMMO:
134          break;
135
136        case SPECIAL:
137          break;
138
139        default:
140          break;
141        }
142
143        // pay attention when multithreaded!
144        nextAction_ = NOTHING;
145      }
146      else
147      {
148        // secondly, execute firing
149        if (primaryFireRequest_ && !(secondaryFired_ && secondaryFireRequest_))
150        {
151          actionStartTime_ = totalTime_;
152          currentState_ = PRIMARY_FIRE;
153          secondaryFired_ = false;
154          primaryFire();
155        }
156        else if (secondaryFireRequest_)
157        {
158          actionStartTime_ = totalTime_;
159          currentState_ = SECONDARY_FIRE;
160          secondaryFired_ = true;
161          secondaryFire();
162        }
163      }
164    }
165
166    primaryFireRequest_ = false;
167    secondaryFireRequest_ = false;
168
169    if (totalTime_ - timeSinceNextActionAdded_ > nextActionValidityPeriod_s)
170      nextAction_ = NOTHING;
171  }
172
173
174  int BaseWeapon::getAmmoState()
175  {
176    return leftAmmo_;
177  }
178
179  void BaseWeapon::setAmmoDump(AmmunitionDump* dump)
180  {
181    this->ammoDump_ = dump;
182  }
183}
Note: See TracBrowser for help on using the repository browser.