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