1 | |
---|
2 | /* |
---|
3 | orxonox - the future of 3D-vertical-scrollers |
---|
4 | |
---|
5 | Copyright (C) 2004 orx |
---|
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 2, or (at your option) |
---|
10 | any later version. |
---|
11 | |
---|
12 | ### File Specific |
---|
13 | main-programmer: Patrick Boenzli |
---|
14 | co-programmer: Benjamin Grauer |
---|
15 | */ |
---|
16 | |
---|
17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON |
---|
18 | |
---|
19 | #include "weapon_manager.h" |
---|
20 | #include "weapon.h" |
---|
21 | #include "crosshair.h" |
---|
22 | |
---|
23 | #include "load_param.h" |
---|
24 | #include "factory.h" |
---|
25 | #include "vector.h" |
---|
26 | #include "list.h" |
---|
27 | #include "t_animation.h" |
---|
28 | |
---|
29 | using namespace std; |
---|
30 | |
---|
31 | |
---|
32 | /** |
---|
33 | * this initializes the weaponManager for a given nnumber of weapon slots |
---|
34 | * @param number of weapon slots of the model/ship <= 8 (limitied) |
---|
35 | */ |
---|
36 | WeaponManager::WeaponManager(int slotCount) |
---|
37 | { |
---|
38 | this->init(); |
---|
39 | this->slotCount = slotCount; |
---|
40 | } |
---|
41 | |
---|
42 | WeaponManager::WeaponManager(const TiXmlElement* root) |
---|
43 | { |
---|
44 | this->init(); |
---|
45 | this->loadParams(root); |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * Destroys a WeaponManager |
---|
50 | */ |
---|
51 | WeaponManager::~WeaponManager() |
---|
52 | { |
---|
53 | /* |
---|
54 | i dont have to delete the weapons itself, because they are |
---|
55 | worldentities and therefore in the entities list of the world. |
---|
56 | world will clean them up for me |
---|
57 | */ |
---|
58 | for(int i = 0; i < WM_MAX_CONFIGS; ++i) |
---|
59 | { |
---|
60 | this->configs[i].bUsed = false; |
---|
61 | for(int j = 0; j < WM_MAX_SLOTS; ++j) |
---|
62 | this->configs[i].slots[j] = NULL; |
---|
63 | } |
---|
64 | |
---|
65 | // crosshair being a PNode it must not be deleted (this is because PNodes delete themselves.) |
---|
66 | //delete this->crosshair; |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | * initializes the WeaponManager |
---|
71 | */ |
---|
72 | void WeaponManager::init() |
---|
73 | { |
---|
74 | this->setClassID(CL_WEAPON_MANAGER, "WeaponManager"); |
---|
75 | |
---|
76 | for(int i = 0; i < WM_MAX_CONFIGS; ++i) |
---|
77 | { |
---|
78 | this->configs[i].bUsed = false; |
---|
79 | for(int j = 0; j < WM_MAX_SLOTS; ++j) |
---|
80 | this->configs[i].slots[j] = NULL; |
---|
81 | } |
---|
82 | this->currConfID = WM_CONFIG0; |
---|
83 | |
---|
84 | |
---|
85 | |
---|
86 | |
---|
87 | this->crosshair = new Crosshair(); |
---|
88 | |
---|
89 | this->crossHairSizeAnim = new tAnimation<Crosshair>(this->crosshair, &Crosshair::setSize); |
---|
90 | this->crossHairSizeAnim->setInfinity(ANIM_INF_REWIND); |
---|
91 | this->crossHairSizeAnim->addKeyFrame(50, .1, ANIM_LINEAR); |
---|
92 | this->crossHairSizeAnim->addKeyFrame(100, .05, ANIM_LINEAR); |
---|
93 | this->crossHairSizeAnim->addKeyFrame(50, .01, ANIM_LINEAR); |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * loads the settings of the WeaponManager |
---|
98 | * @param root the XML-element to load from |
---|
99 | */ |
---|
100 | void WeaponManager::loadParams(const TiXmlElement* root) |
---|
101 | { |
---|
102 | static_cast<BaseObject*>(this)->loadParams(root); |
---|
103 | |
---|
104 | LoadParam<WeaponManager>(root, "slot-count", this, &WeaponManager::setSlotCount) |
---|
105 | .describe("how many slots(cannons) the WeaponManager can handle"); |
---|
106 | |
---|
107 | LOAD_PARAM_START_CYCLE; |
---|
108 | |
---|
109 | LoadParam<WeaponManager>(root, "Weapons", this, &WeaponManager::loadWeapons) |
---|
110 | .describe("loads Weapons"); |
---|
111 | // LoadParam<WeaponManager>(root, "Weapon", this, &WeaponManager::addWeapon); |
---|
112 | |
---|
113 | LOAD_PARAM_END_CYCLE; |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * loads a Weapon onto the WeaponManager |
---|
118 | * @param root the XML-element to load the Weapons from |
---|
119 | */ |
---|
120 | void WeaponManager::loadWeapons(const TiXmlElement* root) |
---|
121 | { |
---|
122 | LOAD_PARAM_START_CYCLE; |
---|
123 | |
---|
124 | Weapon* newWeapon = dynamic_cast<Weapon*>(Factory::getFirst()->fabricate(element)); |
---|
125 | |
---|
126 | |
---|
127 | |
---|
128 | LOAD_PARAM_END_CYCLE; |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | /** |
---|
133 | * sets the number of Slots the WeaponManager has |
---|
134 | * @param slotCount the number of slots |
---|
135 | */ |
---|
136 | void WeaponManager::setSlotCount(int slotCount) |
---|
137 | { |
---|
138 | this->slotCount = slotCount; |
---|
139 | } |
---|
140 | |
---|
141 | /** |
---|
142 | * adds a weapon to the selected weaponconfiguration into the selected slot |
---|
143 | * @param the weapon to add |
---|
144 | * @param an identifier for the slot: number between 0..7 if not specified: slotID=next free slot |
---|
145 | * @param an identifier for the weapon configuration, number between 0..3 |
---|
146 | * |
---|
147 | * if you add explicitly a weapon at config:n, slot:m, the weapon placed at this location will be |
---|
148 | * replaced by the weapon specified. if you use the WM_FREE_SLOT, the manager will look for a free |
---|
149 | * slot in this weaponconfiguration. if there is non, the weapon won't be added and there will be |
---|
150 | * a error message. |
---|
151 | */ |
---|
152 | void WeaponManager::addWeapon(Weapon* weapon, int configID, int slotID) |
---|
153 | { |
---|
154 | if( slotID == WM_FREE_SLOT) |
---|
155 | { |
---|
156 | int freeSlot = this->getNextFreeSlot( configID); |
---|
157 | if( freeSlot < 0 || freeSlot >= this->slotCount) |
---|
158 | { |
---|
159 | PRINTF(0)("There is no free slot in this WeaponConfig to dock this weapon at! Aborting\n"); |
---|
160 | return; |
---|
161 | } |
---|
162 | PRINTF(3)("Added new Weapon to Config:%i/Slot:%i\n", configID, freeSlot); |
---|
163 | this->configs[configID].bUsed = true; |
---|
164 | this->configs[configID].slots[freeSlot] = weapon; |
---|
165 | return; |
---|
166 | } |
---|
167 | this->configs[configID].bUsed = true; |
---|
168 | this->configs[configID].slots[slotID] = weapon; |
---|
169 | PRINTF(3)("Added a new Weapon to the WeaponManager: config %i/ slot %i\n", configID, slotID); |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | * removes a Weapon from the WeaponManager |
---|
174 | */ |
---|
175 | void WeaponManager::removeWeapon(Weapon* weapon, int configID) |
---|
176 | { |
---|
177 | /* empty */ |
---|
178 | } |
---|
179 | |
---|
180 | |
---|
181 | /** |
---|
182 | * changes to the next weapon configuration |
---|
183 | * |
---|
184 | * if there are multiple weapon configurations defined by the manager, use this to switch between them |
---|
185 | * this function will deactivate the weapons first, change the config and reactivate them later |
---|
186 | */ |
---|
187 | void WeaponManager::nextWeaponConf() |
---|
188 | { |
---|
189 | PRINTF(4)("Changing weapon configuration: from %i to next\n", this->currConfID); |
---|
190 | |
---|
191 | int i, lastConfID; |
---|
192 | lastConfID = this->currConfID; |
---|
193 | for(i = this->currConfID + 1; i < WM_MAX_CONFIGS && !this->configs[i].bUsed; ++i); |
---|
194 | if( i == WM_MAX_CONFIGS) this->currConfID = WM_CONFIG0; |
---|
195 | else this->currConfID = i; |
---|
196 | |
---|
197 | |
---|
198 | Weapon *w1, *w2; |
---|
199 | for(int j = 0; j < WM_MAX_SLOTS; ++j) |
---|
200 | { |
---|
201 | w1 = this->configs[lastConfID].slots[j]; |
---|
202 | w2 = this->configs[this->currConfID].slots[j]; |
---|
203 | |
---|
204 | if( w1 == w2) |
---|
205 | { |
---|
206 | printf("no need for change\n"); |
---|
207 | } |
---|
208 | else |
---|
209 | { |
---|
210 | if( w1 != NULL ) |
---|
211 | { |
---|
212 | w1->requestAction(WA_DEACTIVATE); |
---|
213 | printf("deactivating %i,%i\n", j,lastConfID); |
---|
214 | } |
---|
215 | if( w2 != NULL) |
---|
216 | { |
---|
217 | w2->requestAction(WA_ACTIVATE); |
---|
218 | printf("activating %i,%i\n", j, this->currConfID); |
---|
219 | } |
---|
220 | } |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | |
---|
225 | /** |
---|
226 | * triggers fire of all weapons in the current weaponconfig |
---|
227 | */ |
---|
228 | void WeaponManager::fire() |
---|
229 | { |
---|
230 | Weapon* firingWeapon; |
---|
231 | for(int i = 0; i < WM_MAX_SLOTS; ++i) |
---|
232 | { |
---|
233 | firingWeapon = this->configs[this->currConfID].slots[i]; |
---|
234 | if( firingWeapon != NULL) firingWeapon->requestAction(WA_SHOOT); |
---|
235 | } |
---|
236 | this->crosshair->setRotationSpeed(500); |
---|
237 | this->crossHairSizeAnim->replay(); |
---|
238 | } |
---|
239 | |
---|
240 | |
---|
241 | /** |
---|
242 | * triggers tick of all weapons in the current weaponconfig |
---|
243 | * @param second passed since last tick |
---|
244 | */ |
---|
245 | void WeaponManager::tick(float dt) |
---|
246 | { |
---|
247 | Weapon* w; |
---|
248 | for(int i = 0; i < WM_MAX_SLOTS; ++i) |
---|
249 | { |
---|
250 | w = this->configs[this->currConfID].slots[i]; |
---|
251 | if( w != NULL && w->isActive()) |
---|
252 | w->tickW(dt); |
---|
253 | } |
---|
254 | |
---|
255 | crosshair->setRotationSpeed(5); |
---|
256 | } |
---|
257 | |
---|
258 | |
---|
259 | /** |
---|
260 | * triggers draw of all weapons in the current weaponconfig |
---|
261 | */ |
---|
262 | void WeaponManager::draw() |
---|
263 | { |
---|
264 | Weapon* w; |
---|
265 | for (int j = 0; j < 4; ++j ) |
---|
266 | for(int i = 0; i < WM_MAX_SLOTS; ++i) |
---|
267 | { |
---|
268 | w = this->configs[j].slots[i]; |
---|
269 | if( w != NULL && w->isVisible()) |
---|
270 | w->draw(); |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | |
---|
275 | /** |
---|
276 | * private gets the next free slot in a certain weaponconfig |
---|
277 | * @param the selected weaponconfig |
---|
278 | */ |
---|
279 | int WeaponManager::getNextFreeSlot(int configID) |
---|
280 | { |
---|
281 | for( int i = 0; i < WM_MAX_SLOTS; ++i) |
---|
282 | { |
---|
283 | if( this->configs[configID].slots[i] == NULL) |
---|
284 | return i; |
---|
285 | } |
---|
286 | return -1; |
---|
287 | } |
---|
288 | |
---|