1 | |
---|
2 | |
---|
3 | /* |
---|
4 | orxonox - the future of 3D-vertical-scrollers |
---|
5 | |
---|
6 | Copyright (C) 2004 orx |
---|
7 | |
---|
8 | This program is free software; you can redistribute it and/or modify |
---|
9 | it under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 2, or (at your option) |
---|
11 | any later version. |
---|
12 | |
---|
13 | ### File Specific |
---|
14 | main-programmer: Patrick Boenzli |
---|
15 | co-programmer: |
---|
16 | */ |
---|
17 | |
---|
18 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON |
---|
19 | |
---|
20 | #include "weapon.h" |
---|
21 | #include "stdincl.h" |
---|
22 | #include "world_entity.h" |
---|
23 | #include "vector.h" |
---|
24 | #include "model.h" |
---|
25 | #include "projectile.h" |
---|
26 | #include "list.h" |
---|
27 | #include "world.h" |
---|
28 | |
---|
29 | using namespace std; |
---|
30 | |
---|
31 | |
---|
32 | |
---|
33 | /** |
---|
34 | \brief this initializes the weaponManager for a given nnumber of weapon slots |
---|
35 | \param number of weapon slots of the model/ship <= 8 (limitied) |
---|
36 | */ |
---|
37 | WeaponManager::WeaponManager(int nrOfSlots) |
---|
38 | { |
---|
39 | this->nrOfSlots = nrOfSlots; |
---|
40 | |
---|
41 | for(int i = 0; i < W_MAX_CONFIGS; ++i) |
---|
42 | { |
---|
43 | this->configs[i].bUsed = false; |
---|
44 | for(int j = 0; j < W_MAX_SLOTS; ++j) |
---|
45 | this->configs[i].slots[j] = NULL; |
---|
46 | } |
---|
47 | |
---|
48 | this->currConfID = W_CONFIG0; |
---|
49 | this->configs[this->currConfID].bUsed = true; |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | WeaponManager::~WeaponManager() |
---|
54 | { |
---|
55 | /* i dont have to delete the weapons itself, because they are |
---|
56 | worldentities and therefore in the entities list of the world |
---|
57 | */ |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | /** |
---|
62 | \brief adds a weapon to the selected weaponconfiguration into the selected slot |
---|
63 | \param the weapon to add |
---|
64 | \param an identifier for the slot: number between 0..7 if not specified: slotID=next free slot |
---|
65 | \param an identifier for the weapon configuration, number between 0..3 |
---|
66 | |
---|
67 | if you add explicitly a weapon at config:n, slot:m, the weapon placed at this location will be |
---|
68 | replaced by the weapon specified. if you use the W_FREE_SLOT, the manager will look for a free |
---|
69 | slot in this weaponconfiguration. if there is non, the weapon won't be added and there will be |
---|
70 | a error message. |
---|
71 | */ |
---|
72 | void WeaponManager::addWeapon(Weapon* weapon, int configID, int slotID) |
---|
73 | { |
---|
74 | if( slotID == W_FREE_SLOT) |
---|
75 | { |
---|
76 | int freeSlot = this->getNextFreeSlot( configID); |
---|
77 | if( freeSlot < 0 || freeSlot >= this->nrOfSlots) |
---|
78 | { |
---|
79 | PRINTF(0)("There is no free slot in this WeaponConfig to dock this weapon at! Aborting\n"); |
---|
80 | return; |
---|
81 | } |
---|
82 | PRINTF(3)("Added new Weapon to Config:%i/Slot:%i\n", configID, freeSlot); |
---|
83 | this->configs[configID].bUsed = true; |
---|
84 | this->configs[configID].slots[freeSlot] = weapon; |
---|
85 | return; |
---|
86 | } |
---|
87 | this->configs[configID].bUsed = true; |
---|
88 | this->configs[configID].slots[slotID] = weapon; |
---|
89 | PRINTF(3)("Added a new Weapon to the WeaponManager: config %i/ slot %i\n", configID, slotID); |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | void WeaponManager::removeWeapon(Weapon* weapon, int configID) |
---|
94 | { |
---|
95 | /* empty */ |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | /** |
---|
100 | \brief changes to the next weapon configuration |
---|
101 | |
---|
102 | if there are multiple weapon configurations defined by the manager, use this to switch between them |
---|
103 | */ |
---|
104 | void WeaponManager::nextWeaponConf() |
---|
105 | { |
---|
106 | PRINTF(4)("Changing weapon configuration: from %i\n", this->currConfID); |
---|
107 | int i; |
---|
108 | for(i = this->currConfID + 1; i < W_MAX_CONFIGS && !this->configs[i].bUsed; ++i); |
---|
109 | if( i == W_MAX_CONFIGS) this->currConfID = W_CONFIG0; |
---|
110 | else this->currConfID = i; |
---|
111 | PRINTF(4)("\tto %i\n", this->currConfID); |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | |
---|
116 | /** |
---|
117 | \brief triggers fire of all weapons in the current weaponconfig |
---|
118 | */ |
---|
119 | void WeaponManager::fire() |
---|
120 | { |
---|
121 | Weapon* firingWeapon; |
---|
122 | for(int i = 0; i < W_MAX_SLOTS; ++i) |
---|
123 | { |
---|
124 | firingWeapon = this->configs[this->currConfID].slots[i]; |
---|
125 | if( firingWeapon != NULL) firingWeapon->fire(); |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | /** |
---|
131 | \brief triggers tick of all weapons in the current weaponconfig |
---|
132 | \param second passed since last tick |
---|
133 | */ |
---|
134 | void WeaponManager::tick(float sec) |
---|
135 | { |
---|
136 | Weapon* w; |
---|
137 | for(int i = 0; i < W_MAX_SLOTS; ++i) |
---|
138 | { |
---|
139 | w = this->configs[this->currConfID].slots[i]; |
---|
140 | if( w != NULL) w->tick(sec); |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | /** |
---|
146 | \brief triggers draw of all weapons in the current weaponconfig |
---|
147 | */ |
---|
148 | void WeaponManager::draw() |
---|
149 | { |
---|
150 | Weapon* w; |
---|
151 | for(int i = 0; i < W_MAX_SLOTS; ++i) |
---|
152 | { |
---|
153 | w = this->configs[this->currConfID].slots[i]; |
---|
154 | if( w != NULL) w->draw(); |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | /** |
---|
160 | \brief private gets the next free slot in a certain weaponconfig |
---|
161 | \param the selected weaponconfig |
---|
162 | */ |
---|
163 | int WeaponManager::getNextFreeSlot(int configID) |
---|
164 | { |
---|
165 | for( int i = 0; i < W_MAX_SLOTS; ++i) |
---|
166 | { |
---|
167 | if( this->configs[configID].slots[i] == NULL) |
---|
168 | return i; |
---|
169 | } |
---|
170 | return -1; |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | |
---|
175 | |
---|
176 | |
---|
177 | |
---|
178 | /** |
---|
179 | \brief standard constructor |
---|
180 | |
---|
181 | creates a new weapon |
---|
182 | */ |
---|
183 | Weapon::Weapon (PNode* parent, Vector* coordinate, Quaternion* direction) |
---|
184 | : WorldEntity() |
---|
185 | { |
---|
186 | parent->addChild(this, PNODE_ALL); |
---|
187 | this->setRelCoor(*coordinate); |
---|
188 | this->setRelDir(*direction); |
---|
189 | WorldInterface* wi = WorldInterface::getInstance(); |
---|
190 | this->worldEntities = wi->getEntityList(); |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | /** |
---|
195 | \brief standard deconstructor |
---|
196 | */ |
---|
197 | Weapon::~Weapon () |
---|
198 | { |
---|
199 | // model will be deleted from WorldEntity-destructor |
---|
200 | } |
---|
201 | |
---|
202 | |
---|
203 | /** |
---|
204 | \brief enables the weapon |
---|
205 | |
---|
206 | a weapon can be enabled/disabled because of various reasons. if a weapon is |
---|
207 | been enabled, it can interact in a world. elswhere it wont react to any |
---|
208 | action. |
---|
209 | */ |
---|
210 | void Weapon::enable() |
---|
211 | { |
---|
212 | this->enabled = true; |
---|
213 | } |
---|
214 | |
---|
215 | |
---|
216 | /** |
---|
217 | \brief disables the weapon |
---|
218 | |
---|
219 | a weapon can be enabled/disabled because of various reasons. if a weapon is |
---|
220 | been enabled, it can interact in a world. elswhere it wont react to any |
---|
221 | action. |
---|
222 | */ |
---|
223 | void Weapon::disable() |
---|
224 | { |
---|
225 | this->enabled = false; |
---|
226 | } |
---|
227 | |
---|
228 | |
---|
229 | /** |
---|
230 | \brief checks if the weapon is enabled |
---|
231 | \returns true if enabled |
---|
232 | |
---|
233 | a weapon can be ebabled/disabled because of various reasons. if a weapon is |
---|
234 | been enabled, it can interact in a world. elswhere it wont react to any |
---|
235 | action. |
---|
236 | */ |
---|
237 | bool Weapon::isEnabled() |
---|
238 | { |
---|
239 | return this->enabled; |
---|
240 | } |
---|
241 | |
---|
242 | |
---|
243 | /** |
---|
244 | \brief sets a new projectile to the weapon |
---|
245 | \param new projectile for this weapon |
---|
246 | |
---|
247 | weapon an projectile are independent, so you can combine them as you want |
---|
248 | */ |
---|
249 | void Weapon::setProjectile(Projectile* projectile) |
---|
250 | { |
---|
251 | this->projectile = projectile; |
---|
252 | } |
---|
253 | |
---|
254 | |
---|
255 | /** |
---|
256 | \brief sets a new projectile to the weapon |
---|
257 | \returns the current projectile of this weapon |
---|
258 | |
---|
259 | weapon an projectile are independent, so you can combine them as you want |
---|
260 | */ |
---|
261 | Projectile* Weapon::getProjectile() |
---|
262 | { |
---|
263 | return this->projectile; |
---|
264 | } |
---|
265 | |
---|
266 | |
---|
267 | /** |
---|
268 | \brief this activates the weapon |
---|
269 | |
---|
270 | This is needed, since there can be more than one weapon on a ship. the |
---|
271 | activation can be connected with an animation. for example the weapon is |
---|
272 | been armed out. |
---|
273 | */ |
---|
274 | void Weapon::activate() |
---|
275 | {} |
---|
276 | |
---|
277 | |
---|
278 | /** |
---|
279 | \brief this deactivates the weapon |
---|
280 | |
---|
281 | This is needed, since there can be more than one weapon on a ship. the |
---|
282 | activation can be connected with an animation. for example the weapon is |
---|
283 | been armed out. |
---|
284 | */ |
---|
285 | void Weapon::deactivate() |
---|
286 | {} |
---|
287 | |
---|
288 | /** |
---|
289 | \brief asks if the current weapon is active |
---|
290 | \returns true if it the weapon is active |
---|
291 | */ |
---|
292 | bool Weapon::isActive() |
---|
293 | {} |
---|
294 | |
---|
295 | /** |
---|
296 | \brief sets a weapon idle time |
---|
297 | \param idle time in ms |
---|
298 | |
---|
299 | a weapon idle time is the time spend after a shoot until the weapon can |
---|
300 | shoot again |
---|
301 | */ |
---|
302 | void Weapon::setWeaponIdleTime(float time) |
---|
303 | { |
---|
304 | this->idleTime = time; |
---|
305 | } |
---|
306 | |
---|
307 | /** |
---|
308 | \brief gets the weapon idle time |
---|
309 | \returns idle time in ms |
---|
310 | |
---|
311 | a weapon idle time is the time spend after a shoot until the weapon can |
---|
312 | shoot again |
---|
313 | */ |
---|
314 | float Weapon::getWeaponIdleTime(void) |
---|
315 | { |
---|
316 | return this->idleTime; |
---|
317 | } |
---|
318 | |
---|
319 | /** |
---|
320 | \brief checks if the idle time is elapsed |
---|
321 | \return true if time is elapsed |
---|
322 | |
---|
323 | a weapon idle time is the time spend after a shoot until the weapon can |
---|
324 | shoot again |
---|
325 | */ |
---|
326 | bool Weapon::hasWeaponIdleTimeElapsed(void) |
---|
327 | { |
---|
328 | return (this->localTime>this->idleTime)?true:false; |
---|
329 | } |
---|
330 | |
---|
331 | |
---|
332 | /** |
---|
333 | \brief fires the weapon |
---|
334 | |
---|
335 | this is called from the player.cc, when fire-button is been pushed |
---|
336 | */ |
---|
337 | void Weapon::fire() |
---|
338 | {} |
---|
339 | |
---|
340 | |
---|
341 | /** |
---|
342 | \brief is called, when the weapon gets hit (=collide with something) |
---|
343 | \param from which entity it is been hit |
---|
344 | \param where it is been hit |
---|
345 | |
---|
346 | this may not be used, since it would make the game relay complicated when one |
---|
347 | can destroy the weapons of enemies or vice versa. |
---|
348 | */ |
---|
349 | void Weapon::hit (WorldEntity* entity, Vector* position) |
---|
350 | {} |
---|
351 | |
---|
352 | |
---|
353 | /** |
---|
354 | \brief is called, when the weapon is destroyed |
---|
355 | |
---|
356 | this is in conjunction with the hit function, so when a weapon is able to get |
---|
357 | hit, it can also be destoryed. |
---|
358 | */ |
---|
359 | void Weapon::destroy () |
---|
360 | {} |
---|
361 | |
---|
362 | |
---|
363 | /** |
---|
364 | \brief tick signal for time dependent/driven stuff |
---|
365 | */ |
---|
366 | void Weapon::tick (float time) |
---|
367 | {} |
---|
368 | |
---|
369 | |
---|
370 | /** |
---|
371 | \brief is called, when there is no fire button pressed |
---|
372 | */ |
---|
373 | void Weapon::weaponIdle() |
---|
374 | {} |
---|
375 | |
---|
376 | |
---|
377 | /** |
---|
378 | \brief this will draw the weapon |
---|
379 | */ |
---|
380 | void Weapon::draw () |
---|
381 | {} |
---|
382 | |
---|