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