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