Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/orxonox/objects/weapon_system/base_weapon.cc @ 637

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