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 "OgreSceneManager.h" |
---|
29 | #include "OgreEntity.h" |
---|
30 | #include "OgreSceneNode.h" |
---|
31 | #include "OgreVector3.h" |
---|
32 | #include "OgreStringConverter.h" |
---|
33 | |
---|
34 | #include "weapon.h" |
---|
35 | #include "bullet.h" |
---|
36 | #include "bullet_manager.h" |
---|
37 | #include "inertial_node.h" |
---|
38 | #include "weapon_manager.h" |
---|
39 | |
---|
40 | #define ACTION_LIST_SIZE 4 |
---|
41 | |
---|
42 | |
---|
43 | namespace orxonox { |
---|
44 | namespace weapon { |
---|
45 | using namespace Ogre; |
---|
46 | |
---|
47 | Weapon** WeaponManager::weaponList_s = NULL; |
---|
48 | |
---|
49 | WeaponManager::WeaponManager(SceneManager *sceneMgr, InertialNode *node, |
---|
50 | BulletManager *bulletManager, int slotSize) |
---|
51 | : sceneMgr_(sceneMgr), node_(node), slotSize_(slotSize), slotIndex_(0), |
---|
52 | bulletCounter_(0), primaryFireRequest_(false), currentState_(IDLE), |
---|
53 | secondaryFireRequest_(false), selectedWeapon_(0), |
---|
54 | bulletManager_(bulletManager), secondaryFired_(false), |
---|
55 | timeSinceNextActionAdded_(0), actionAdded_(false), nextAction_(NOTHING) |
---|
56 | { |
---|
57 | slots_ = new Weapon*[slotSize]; |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | WeaponManager::~WeaponManager() |
---|
62 | { |
---|
63 | if (slots_) |
---|
64 | delete slots_; |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | bool WeaponManager::addWeapon(const Ogre::String &name) |
---|
69 | { |
---|
70 | if (!weaponList_s) |
---|
71 | return false; |
---|
72 | |
---|
73 | if (name == weaponList_s[0]->name_) |
---|
74 | { |
---|
75 | // this is ugly, but for the time being, it has to fit. |
---|
76 | selectedWeapon_ = slotIndex_; |
---|
77 | slots_[slotIndex_++] = weaponList_s[0]; |
---|
78 | return true; |
---|
79 | } |
---|
80 | else |
---|
81 | return false; |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | bool WeaponManager::addAction(const Action act) |
---|
86 | { |
---|
87 | if (nextAction_ != NOTHING) |
---|
88 | { |
---|
89 | nextAction_ = act; |
---|
90 | actionAdded_ = true; |
---|
91 | return true; |
---|
92 | } |
---|
93 | else |
---|
94 | return false; |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | void WeaponManager::primaryFireRequest() |
---|
99 | { |
---|
100 | primaryFireRequest_ = true; |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | void WeaponManager::primaryFire() |
---|
105 | { |
---|
106 | SceneNode *temp = sceneMgr_->getRootSceneNode()->createChildSceneNode( |
---|
107 | node_->getSceneNode()->getWorldPosition(), |
---|
108 | node_->getSceneNode()->getWorldOrientation()); |
---|
109 | |
---|
110 | Entity* bulletEntity = sceneMgr_->createEntity("BulletEntity" |
---|
111 | + StringConverter::toString(bulletCounter_++), "Barrel.mesh"); |
---|
112 | |
---|
113 | Vector3 speed = (temp->getOrientation() * Vector3(0, 0, -1)) |
---|
114 | .normalisedCopy() * slots_[selectedWeapon_]->bulletSpeed_; |
---|
115 | speed += node_->getWorldSpeed(); |
---|
116 | |
---|
117 | temp->setScale(Vector3(1, 1, 1) * 4); |
---|
118 | temp->yaw(Degree(-90)); |
---|
119 | |
---|
120 | bulletManager_->addBullet(new Bullet(temp, bulletEntity, speed)); |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | void WeaponManager::primaryFiring(unsigned int time) |
---|
125 | { |
---|
126 | if (time > 100) |
---|
127 | { |
---|
128 | currentState_ = IDLE; |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | void WeaponManager::secondaryFireRequest() |
---|
134 | { |
---|
135 | secondaryFireRequest_ = true; |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | void WeaponManager::secondaryFire() |
---|
140 | { |
---|
141 | SceneNode *temp = sceneMgr_->getRootSceneNode()->createChildSceneNode( |
---|
142 | node_->getSceneNode()->getWorldPosition(), |
---|
143 | node_->getSceneNode()->getWorldOrientation()); |
---|
144 | |
---|
145 | Entity* bulletEntity = sceneMgr_->createEntity("BulletEntity" |
---|
146 | + StringConverter::toString(bulletCounter_++), "Barrel.mesh"); |
---|
147 | |
---|
148 | Vector3 speed = (temp->getOrientation() * Vector3(0, 0, -1)) |
---|
149 | .normalisedCopy() * slots_[selectedWeapon_]->bulletSpeed_*0.5; |
---|
150 | speed += node_->getWorldSpeed(); |
---|
151 | |
---|
152 | temp->setScale(Vector3(1, 1, 1) * 10); |
---|
153 | temp->yaw(Degree(-90)); |
---|
154 | |
---|
155 | bulletManager_->addBullet(new Bullet(temp, bulletEntity, speed)); |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | void WeaponManager::secondaryFiring(unsigned int time) |
---|
160 | { |
---|
161 | if (time > 250) |
---|
162 | currentState_ = IDLE; |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
166 | bool WeaponManager::tick(unsigned long time, Real deltaTime) |
---|
167 | { |
---|
168 | // return if no weapon has been added |
---|
169 | if (!slots_[slotIndex_]) |
---|
170 | return true; |
---|
171 | |
---|
172 | // process action adder |
---|
173 | if (actionAdded_) |
---|
174 | { |
---|
175 | timeSinceNextActionAdded_ = time; |
---|
176 | actionAdded_ = false; |
---|
177 | } |
---|
178 | |
---|
179 | switch (currentState_) |
---|
180 | { |
---|
181 | case IDLE: |
---|
182 | // first, process next action |
---|
183 | if (nextAction_ != NOTHING) |
---|
184 | { |
---|
185 | actionStartTime_ = time; |
---|
186 | switch (nextAction_) |
---|
187 | { |
---|
188 | case RELOAD: |
---|
189 | break; |
---|
190 | |
---|
191 | case CHANGE_AMMO: |
---|
192 | break; |
---|
193 | |
---|
194 | case SPECIAL: |
---|
195 | break; |
---|
196 | |
---|
197 | default: |
---|
198 | break; |
---|
199 | } |
---|
200 | |
---|
201 | // pay attention when multithreaded! |
---|
202 | nextAction_ = NOTHING; |
---|
203 | } |
---|
204 | else |
---|
205 | { |
---|
206 | // secondly, execute firing |
---|
207 | if (primaryFireRequest_ && !(secondaryFired_ && secondaryFireRequest_)) |
---|
208 | { |
---|
209 | actionStartTime_ = time; |
---|
210 | currentState_ = PRIMARY_FIRE; |
---|
211 | secondaryFired_ = false; |
---|
212 | primaryFire(); |
---|
213 | } |
---|
214 | else if (secondaryFireRequest_) |
---|
215 | { |
---|
216 | actionStartTime_ = time; |
---|
217 | currentState_ = SECONDARY_FIRE; |
---|
218 | secondaryFired_ = true; |
---|
219 | secondaryFire(); |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | break; |
---|
224 | |
---|
225 | case PRIMARY_FIRE: |
---|
226 | primaryFiring((unsigned int)(time - actionStartTime_)); |
---|
227 | break; |
---|
228 | |
---|
229 | case SECONDARY_FIRE: |
---|
230 | secondaryFiring((unsigned int)(time - actionStartTime_)); |
---|
231 | break; |
---|
232 | |
---|
233 | case RELOADING: |
---|
234 | break; |
---|
235 | |
---|
236 | case CHANGING_AMMO: |
---|
237 | break; |
---|
238 | } |
---|
239 | |
---|
240 | primaryFireRequest_ = false; |
---|
241 | secondaryFireRequest_ = false; |
---|
242 | |
---|
243 | if (time - timeSinceNextActionAdded_ > nextActionValidityPeriod_) |
---|
244 | nextAction_ = NOTHING; |
---|
245 | |
---|
246 | return true; |
---|
247 | } |
---|
248 | |
---|
249 | |
---|
250 | // static |
---|
251 | bool WeaponManager::loadWeapons() |
---|
252 | { |
---|
253 | weaponList_s = new Weapon*[5]; |
---|
254 | for (int i = 0; i < 5; i++) |
---|
255 | weaponList_s[i] = NULL; |
---|
256 | weaponList_s[0] = new Weapon("Barrel Gun", 10, 2, 1000); |
---|
257 | return true; |
---|
258 | } |
---|
259 | |
---|
260 | |
---|
261 | // static |
---|
262 | void WeaponManager::destroyWeapons() |
---|
263 | { |
---|
264 | if (weaponList_s) |
---|
265 | { |
---|
266 | for (int i = 0; i < 5; i++) |
---|
267 | if (weaponList_s[i]) |
---|
268 | delete weaponList_s[i]; |
---|
269 | delete weaponList_s; |
---|
270 | } |
---|
271 | } |
---|
272 | |
---|
273 | } |
---|
274 | } |
---|