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 "weapon_manager.h" |
---|
38 | |
---|
39 | #define ACTION_LIST_SIZE 4 |
---|
40 | |
---|
41 | |
---|
42 | namespace orxonox { |
---|
43 | using namespace Ogre; |
---|
44 | |
---|
45 | Weapon** WeaponManager::weaponList_s = NULL; |
---|
46 | |
---|
47 | WeaponManager::WeaponManager(SceneManager *sceneMgr, SceneNode *node, |
---|
48 | BulletManager *bulletManager, int slotSize) |
---|
49 | : sceneMgr_(sceneMgr), node_(node), slotSize_(slotSize), slotIndex_(0), |
---|
50 | bulletCounter_(0), primaryFireRequest_(false), currentState_(IDLE), |
---|
51 | secondaryFireRequest_(false), selectedWeapon_(0), |
---|
52 | bulletManager_(bulletManager), |
---|
53 | actionListReadIndex_(0), actionListWriteIndex_(0) |
---|
54 | { |
---|
55 | slots_ = new Weapon*[slotSize]; |
---|
56 | actionList_ = new Action[ACTION_LIST_SIZE]; |
---|
57 | for (int i = 0; i < ACTION_LIST_SIZE; i++) |
---|
58 | actionList_[i] = NOTHING; |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | WeaponManager::~WeaponManager() |
---|
63 | { |
---|
64 | if (slots_) |
---|
65 | delete slots_; |
---|
66 | if (actionList_) |
---|
67 | delete actionList_; |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | bool WeaponManager::addWeapon(const Ogre::String &name) |
---|
72 | { |
---|
73 | if (!weaponList_s) |
---|
74 | return false; |
---|
75 | |
---|
76 | if (name == weaponList_s[0]->name_) |
---|
77 | { |
---|
78 | // this is ugly, but for the time being, it has to fit. |
---|
79 | selectedWeapon_ = slotIndex_; |
---|
80 | slots_[slotIndex_++] = weaponList_s[0]; |
---|
81 | return true; |
---|
82 | } |
---|
83 | else |
---|
84 | return false; |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | bool WeaponManager::addAction(const Action act) |
---|
89 | { |
---|
90 | if (actionList_[actionListWriteIndex_] == NOTHING) |
---|
91 | { |
---|
92 | actionList_[actionListWriteIndex_] = act; |
---|
93 | actionListWriteIndex_ = (actionListWriteIndex_ + 1) % ACTION_LIST_SIZE; |
---|
94 | return true; |
---|
95 | } |
---|
96 | else |
---|
97 | return false; |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | void WeaponManager::primaryFireRequest() |
---|
102 | { |
---|
103 | primaryFireRequest_ = true; |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | void WeaponManager::primaryFire() |
---|
108 | { |
---|
109 | currentState_ = PRIMARY_FIRE; |
---|
110 | |
---|
111 | // TODO: add the name of the weapon manager. but for that, |
---|
112 | // the factory is required. |
---|
113 | SceneNode *temp = sceneMgr_->getRootSceneNode()->createChildSceneNode( |
---|
114 | "BulletNode" + StringConverter::toString(bulletCounter_), |
---|
115 | node_->getWorldPosition(), node_->getWorldOrientation()); |
---|
116 | temp->setScale(Vector3(1, 1, 1) * 10); |
---|
117 | temp->yaw(Degree(-90)); |
---|
118 | Entity* bulletEntity = sceneMgr_->createEntity("BulletEntity" |
---|
119 | + StringConverter::toString(bulletCounter_++), "Barrel.mesh"); |
---|
120 | Vector3 speed = (node_->getOrientation() * Vector3(0, 0, -1)) |
---|
121 | .normalisedCopy() * slots_[selectedWeapon_]->bulletSpeed_; |
---|
122 | bulletManager_->addBullet(new Bullet(temp, bulletEntity, speed)); |
---|
123 | |
---|
124 | currentState_ = IDLE; |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | void WeaponManager::secondaryFireRequest() |
---|
129 | { |
---|
130 | secondaryFireRequest_ = true; |
---|
131 | } |
---|
132 | |
---|
133 | void WeaponManager::secondaryFire() |
---|
134 | { |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | bool WeaponManager::tick(unsigned long time, Real deltaTime) |
---|
139 | { |
---|
140 | // return if no weapon has been added |
---|
141 | if (!slots_[slotIndex_]) |
---|
142 | return true; |
---|
143 | |
---|
144 | switch (currentState_) |
---|
145 | { |
---|
146 | case IDLE: |
---|
147 | // first, process actions |
---|
148 | if (actionList_[actionListReadIndex_] != NOTHING) |
---|
149 | { |
---|
150 | actionListReadIndex_ = (actionListReadIndex_ + 1) % ACTION_LIST_SIZE; |
---|
151 | break; |
---|
152 | } |
---|
153 | |
---|
154 | switch (actionList_[actionListReadIndex_]) |
---|
155 | { |
---|
156 | case RELOAD: |
---|
157 | break; |
---|
158 | |
---|
159 | case ZOOM_IN: |
---|
160 | break; |
---|
161 | |
---|
162 | case ZOOM_OUT: |
---|
163 | break; |
---|
164 | |
---|
165 | default: |
---|
166 | break; |
---|
167 | } |
---|
168 | |
---|
169 | // secondly, execute firing |
---|
170 | if (primaryFireRequest_) |
---|
171 | primaryFire(); |
---|
172 | else if (secondaryFireRequest_) |
---|
173 | secondaryFire(); |
---|
174 | |
---|
175 | break; |
---|
176 | |
---|
177 | case PRIMARY_FIRE: |
---|
178 | break; |
---|
179 | |
---|
180 | case SECONDARY_FIRE: |
---|
181 | break; |
---|
182 | |
---|
183 | case RELOADING: |
---|
184 | break; |
---|
185 | } |
---|
186 | |
---|
187 | primaryFireRequest_ = false; |
---|
188 | secondaryFireRequest_ = false; |
---|
189 | |
---|
190 | return true; |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | // static |
---|
195 | bool WeaponManager::loadWeapons() |
---|
196 | { |
---|
197 | weaponList_s = new Weapon*[5]; |
---|
198 | for (int i = 0; i < 5; i++) |
---|
199 | weaponList_s[i] = NULL; |
---|
200 | weaponList_s[0] = new Weapon("Barrel Gun", 10, 2, 500); |
---|
201 | return true; |
---|
202 | } |
---|
203 | |
---|
204 | |
---|
205 | // static |
---|
206 | void WeaponManager::destroyWeapons() |
---|
207 | { |
---|
208 | if (weaponList_s) |
---|
209 | { |
---|
210 | for (int i = 0; i < 5; i++) |
---|
211 | if (weaponList_s[i]) |
---|
212 | delete weaponList_s[i]; |
---|
213 | delete weaponList_s; |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | } |
---|