Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial/src/orxonox/objects/weapon/BaseWeapon.cc @ 859

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

more or less a copy of the trunk

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 "OrxonoxStableHeaders.h"
29
30#include <OgreStringConverter.h>
31#include <OgreSceneNode.h>
32#include <OgreEntity.h>
33#include <OgreSceneManager.h>
34
35#include "util/Math.h"
36#include "../../Orxonox.h"
37#include "Bullet.h"
38#include "BulletManager.h"
39#include "AmmunitionDump.h"
40
41#include "BaseWeapon.h"
42
43
44namespace orxonox {
45  float BaseWeapon::nextActionValidityPeriod_s = 0.5;
46
47  BaseWeapon::BaseWeapon()
48    : sceneMgr_(Orxonox::getSingleton()->getSceneManager()),
49      bulletCounter_(0),
50      bulletManager_(Orxonox::getSingleton()->getBulletMgr()),
51      ammoDump_(NULL),
52      primaryFireRequest_(false), secondaryFireRequest_(false),
53      totalTime_(0.0f), actionStartTime_(0.0f),
54      currentState_(IDLE), secondaryFired_(false),
55      nextAction_(NOTHING), actionAdded_(false),
56      timeSinceNextActionAdded_(0.0f),
57      leftAmmo_(0)
58  {
59    RegisterObject(BaseWeapon);
60  }
61
62
63  BaseWeapon::~BaseWeapon()
64  {
65  }
66
67
68  bool BaseWeapon::addAction(const Action act)
69  {
70    if (nextAction_ == NOTHING)
71    {
72      nextAction_ = act;
73      actionAdded_ = true;
74      return true;
75    }
76    else
77      return false;
78  }
79
80
81  void BaseWeapon::primaryFireRequest()
82  {
83    primaryFireRequest_ = true;
84  }
85
86
87  void BaseWeapon::secondaryFireRequest()
88  {
89    secondaryFireRequest_ = true;
90  }
91
92
93  void BaseWeapon::tick(float dt)
94  {
95    totalTime_ += dt;
96    // process action adder
97    if (actionAdded_)
98    {
99      timeSinceNextActionAdded_ = totalTime_;
100      actionAdded_ = false;
101    }
102
103    switch (currentState_)
104    {
105    case IDLE:
106      break;
107
108    case PRIMARY_FIRE:
109      primaryFiring(totalTime_ - actionStartTime_);
110      break;
111
112    case SECONDARY_FIRE:
113      secondaryFiring(totalTime_ - actionStartTime_);
114      break;
115
116    case RELOADING:
117      break;
118
119    case CHANGING_AMMO:
120      break;
121    }
122
123    if (currentState_ == IDLE)
124    {
125      // first, process next action
126      if (nextAction_ != NOTHING)
127      {
128        actionStartTime_ = totalTime_;
129        switch (nextAction_)
130        {
131        case RELOAD:
132          leftAmmo_ += ammoDump_->getAmmunition("Barrel", magazineSize_ - leftAmmo_);
133          break;
134
135        case CHANGE_AMMO:
136          break;
137
138        case SPECIAL:
139          break;
140
141        default:
142          break;
143        }
144
145        // pay attention when multithreaded!
146        nextAction_ = NOTHING;
147      }
148      else
149      {
150        // secondly, execute firing
151        if (primaryFireRequest_ && !(secondaryFired_ && secondaryFireRequest_))
152        {
153          actionStartTime_ = totalTime_;
154          currentState_ = PRIMARY_FIRE;
155          secondaryFired_ = false;
156          primaryFire();
157        }
158        else if (secondaryFireRequest_)
159        {
160          actionStartTime_ = totalTime_;
161          currentState_ = SECONDARY_FIRE;
162          secondaryFired_ = true;
163          secondaryFire();
164        }
165      }
166    }
167
168    primaryFireRequest_ = false;
169    secondaryFireRequest_ = false;
170
171    if (totalTime_ - timeSinceNextActionAdded_ > nextActionValidityPeriod_s)
172      nextAction_ = NOTHING;
173  }
174
175
176  int BaseWeapon::getAmmoState()
177  {
178    return leftAmmo_;
179  }
180
181  void BaseWeapon::setAmmoDump(AmmunitionDump* dump)
182  {
183    this->ammoDump_ = dump;
184  }
185}
Note: See TracBrowser for help on using the repository browser.