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 modify |
---|
8 | * it under the terms of the GNU General Public License as published by |
---|
9 | * the Free Software Foundation, either version 3 of the License, or |
---|
10 | * (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, see <http://www.gnu.org/licenses/>. |
---|
19 | * |
---|
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/inertial_node.h" |
---|
36 | #include "orxonox/run_manager.h" |
---|
37 | |
---|
38 | #include "bullet.h" |
---|
39 | #include "bullet_manager.h" |
---|
40 | #include "ammunition_dump.h" |
---|
41 | |
---|
42 | #include "base_weapon.h" |
---|
43 | |
---|
44 | |
---|
45 | namespace orxonox { |
---|
46 | namespace weapon { |
---|
47 | using namespace Ogre; |
---|
48 | |
---|
49 | BaseWeapon::BaseWeapon(InertialNode *node, AmmunitionDump *ammoDump) |
---|
50 | : sceneMgr_(RunManager::getSingletonPtr()->getSceneManagerPtr()), node_(node), |
---|
51 | bulletCounter_(0), primaryFireRequest_(false), currentState_(IDLE), |
---|
52 | secondaryFireRequest_(false), |
---|
53 | bulletManager_(RunManager::getSingletonPtr()->getBulletManagerPtr()), |
---|
54 | secondaryFired_(false), |
---|
55 | timeSinceNextActionAdded_(0), actionAdded_(false), nextAction_(NOTHING), |
---|
56 | ammoDump_(ammoDump) |
---|
57 | { |
---|
58 | leftAmmo_ = 0; |
---|
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 | bool BaseWeapon::tick(unsigned long time, Real deltaTime) |
---|
93 | { |
---|
94 | // process action adder |
---|
95 | if (actionAdded_) |
---|
96 | { |
---|
97 | timeSinceNextActionAdded_ = time; |
---|
98 | actionAdded_ = false; |
---|
99 | } |
---|
100 | |
---|
101 | if (currentState_ != IDLE) |
---|
102 | { |
---|
103 | switch (currentState_) |
---|
104 | { |
---|
105 | case PRIMARY_FIRE: |
---|
106 | primaryFiring((unsigned int)(time - actionStartTime_)); |
---|
107 | break; |
---|
108 | |
---|
109 | case SECONDARY_FIRE: |
---|
110 | secondaryFiring((unsigned int)(time - actionStartTime_)); |
---|
111 | break; |
---|
112 | |
---|
113 | case RELOADING: |
---|
114 | break; |
---|
115 | |
---|
116 | case CHANGING_AMMO: |
---|
117 | break; |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | if (currentState_ == IDLE) |
---|
122 | { |
---|
123 | // first, process next action |
---|
124 | if (nextAction_ != NOTHING) |
---|
125 | { |
---|
126 | actionStartTime_ = time; |
---|
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_ = time; |
---|
152 | currentState_ = PRIMARY_FIRE; |
---|
153 | secondaryFired_ = false; |
---|
154 | primaryFire(); |
---|
155 | } |
---|
156 | else if (secondaryFireRequest_) |
---|
157 | { |
---|
158 | actionStartTime_ = time; |
---|
159 | currentState_ = SECONDARY_FIRE; |
---|
160 | secondaryFired_ = true; |
---|
161 | secondaryFire(); |
---|
162 | } |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | primaryFireRequest_ = false; |
---|
167 | secondaryFireRequest_ = false; |
---|
168 | |
---|
169 | if (time - timeSinceNextActionAdded_ > nextActionValidityPeriod_) |
---|
170 | nextAction_ = NOTHING; |
---|
171 | |
---|
172 | return true; |
---|
173 | } |
---|
174 | |
---|
175 | |
---|
176 | int BaseWeapon::getAmmoState() |
---|
177 | { |
---|
178 | return leftAmmo_; |
---|
179 | } |
---|
180 | } |
---|
181 | } |
---|