[4826] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific |
---|
| 12 | main-programmer: Patrick Boenzli |
---|
[4832] | 13 | co-programmer: Benjamin Grauer |
---|
[4951] | 14 | |
---|
| 15 | 2005-07-24: Benjamin Grauer: restructurate, so it can handle the new Weapons. |
---|
[10437] | 16 | 2007-01-28: Patrick Boenzli: loadable slots |
---|
[4826] | 17 | */ |
---|
| 18 | |
---|
[9869] | 19 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON |
---|
[4826] | 20 | |
---|
| 21 | #include "weapon_manager.h" |
---|
| 22 | #include "weapon.h" |
---|
[10437] | 23 | #include "weapon_slot.h" |
---|
[4834] | 24 | #include "crosshair.h" |
---|
[4828] | 25 | |
---|
[6561] | 26 | #include "playable.h" |
---|
| 27 | |
---|
[9869] | 28 | #include "util/loading/load_param_xml.h" |
---|
[7193] | 29 | #include "util/loading/factory.h" |
---|
[6055] | 30 | |
---|
[4837] | 31 | #include "t_animation.h" |
---|
[4826] | 32 | |
---|
| 33 | |
---|
[9869] | 34 | ObjectListDefinition(WeaponManager); |
---|
[4826] | 35 | /** |
---|
[6054] | 36 | * @brief this initializes the weaponManager for a given nnumber of weapon slots |
---|
[10698] | 37 | * @param number of weapon slots of the model/ship <= 10 (limitied) |
---|
[4826] | 38 | */ |
---|
[6142] | 39 | WeaponManager::WeaponManager(WorldEntity* parent) |
---|
[4826] | 40 | { |
---|
[4833] | 41 | this->init(); |
---|
[8844] | 42 | this->setParentEntity(parent); |
---|
| 43 | |
---|
| 44 | assert (parent != NULL); |
---|
[4826] | 45 | } |
---|
| 46 | |
---|
[4949] | 47 | WeaponManager::WeaponManager(const TiXmlElement* root) |
---|
| 48 | { |
---|
| 49 | this->init(); |
---|
| 50 | this->loadParams(root); |
---|
| 51 | } |
---|
[4826] | 52 | |
---|
[4833] | 53 | /** |
---|
[6054] | 54 | * @brief Destroys a WeaponManager |
---|
[4833] | 55 | */ |
---|
[4826] | 56 | WeaponManager::~WeaponManager() |
---|
| 57 | { |
---|
[4834] | 58 | // crosshair being a PNode it must not be deleted (this is because PNodes delete themselves.) |
---|
[8147] | 59 | // rennerc: crosshair seems not to delete itselve |
---|
[9869] | 60 | //if (Crosshair::objectList().exists(this->crosshair)) |
---|
| 61 | // delete this->crosshair; |
---|
[4826] | 62 | } |
---|
| 63 | |
---|
[4834] | 64 | /** |
---|
[6054] | 65 | * @brief initializes the WeaponManager |
---|
[4834] | 66 | */ |
---|
[4833] | 67 | void WeaponManager::init() |
---|
| 68 | { |
---|
[9869] | 69 | this->registerObject(this, WeaponManager::_objectList); |
---|
[4826] | 70 | |
---|
[8844] | 71 | this->parentNode = NULL; |
---|
| 72 | this->parentEntity = NULL; |
---|
[4951] | 73 | |
---|
| 74 | for (int i = 0; i < WM_MAX_CONFIGS; i++) |
---|
| 75 | for (int j = 0; j < WM_MAX_SLOTS; j++) |
---|
| 76 | this->configs[i][j] = NULL; |
---|
| 77 | |
---|
| 78 | for (int i = 0; i < WM_MAX_SLOTS; i++) |
---|
[4833] | 79 | { |
---|
[10437] | 80 | this->currentSlotConfig[i] = new WeaponSlot(); |
---|
| 81 | this->currentSlotConfig[i]->setCapability(WTYPE_ALL); |
---|
| 82 | this->currentSlotConfig[i]->setCurrentWeapon(NULL); |
---|
| 83 | this->currentSlotConfig[i]->setNextWeapon(NULL); |
---|
[4992] | 84 | |
---|
| 85 | // NAMING |
---|
| 86 | char* tmpName; |
---|
[9406] | 87 | if (!this->getName().empty()) |
---|
[4992] | 88 | { |
---|
[9406] | 89 | tmpName = new char[this->getName().size() + 10]; |
---|
| 90 | sprintf(tmpName, "%s_slot%d", this->getCName(), i); |
---|
[4992] | 91 | } |
---|
| 92 | else |
---|
| 93 | { |
---|
| 94 | tmpName = new char[30]; |
---|
| 95 | sprintf(tmpName, "WeaponMan_slot%d", i); |
---|
| 96 | } |
---|
[10437] | 97 | this->currentSlotConfig[i]->setName(tmpName); |
---|
| 98 | this->currentSlotConfig[i]->deactivateNode(); |
---|
[5208] | 99 | delete[] tmpName; |
---|
[4833] | 100 | } |
---|
[4895] | 101 | |
---|
[4951] | 102 | for (int i = 0; i < WM_MAX_LOADED_WEAPONS; i++) |
---|
| 103 | this->availiableWeapons[i] = NULL; |
---|
[4895] | 104 | |
---|
| 105 | |
---|
[4951] | 106 | this->currentConfigID = 0; |
---|
[10368] | 107 | this->slotCount = WM_MAX_SLOTS; |
---|
[8315] | 108 | //this->weaponChange; |
---|
[4895] | 109 | |
---|
[4951] | 110 | // CROSSHAIR INITIALISATION |
---|
[4834] | 111 | this->crosshair = new Crosshair(); |
---|
[6807] | 112 | //this->crosshair->setRelCoor(1000,0,0); |
---|
[4837] | 113 | this->crossHairSizeAnim = new tAnimation<Crosshair>(this->crosshair, &Crosshair::setSize); |
---|
| 114 | this->crossHairSizeAnim->setInfinity(ANIM_INF_REWIND); |
---|
| 115 | this->crossHairSizeAnim->addKeyFrame(50, .1, ANIM_LINEAR); |
---|
| 116 | this->crossHairSizeAnim->addKeyFrame(100, .05, ANIM_LINEAR); |
---|
| 117 | this->crossHairSizeAnim->addKeyFrame(50, .01, ANIM_LINEAR); |
---|
[10368] | 118 | |
---|
| 119 | this->hideCrosshair(); |
---|
| 120 | |
---|
| 121 | this->bFire = false; |
---|
[4834] | 122 | } |
---|
[4833] | 123 | |
---|
[10368] | 124 | void WeaponManager::showCrosshair() |
---|
| 125 | { |
---|
| 126 | this->crosshair->setVisibility( true); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | void WeaponManager::hideCrosshair() |
---|
| 130 | { |
---|
| 131 | this->crosshair->setVisibility( false); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | void WeaponManager::setRotationSpeed(float speed) |
---|
| 135 | { |
---|
| 136 | this->crosshair->setRotationSpeed(speed); |
---|
| 137 | } |
---|
| 138 | |
---|
[4834] | 139 | /** |
---|
[7350] | 140 | * @brief loads the settings of the WeaponManager |
---|
[4834] | 141 | * @param root the XML-element to load from |
---|
| 142 | */ |
---|
| 143 | void WeaponManager::loadParams(const TiXmlElement* root) |
---|
| 144 | { |
---|
[6512] | 145 | BaseObject::loadParams(root); |
---|
[4972] | 146 | |
---|
[10698] | 147 | LoadParam(root, "slotCount", this, WeaponManager, setSlotCount) |
---|
[6736] | 148 | .describe("how many slots(cannons) the WeaponManager can handle"); |
---|
[4834] | 149 | |
---|
[10698] | 150 | |
---|
[5644] | 151 | LOAD_PARAM_START_CYCLE(root, element); |
---|
[5654] | 152 | { |
---|
| 153 | // CHECK IF THIS WORKS.... |
---|
| 154 | LoadParamXML_CYCLE(element, "weapons", this, WeaponManager, loadWeapons) |
---|
[6736] | 155 | .describe("loads Weapons"); |
---|
[5654] | 156 | } |
---|
[5644] | 157 | LOAD_PARAM_END_CYCLE(element); |
---|
[10698] | 158 | |
---|
| 159 | |
---|
[4833] | 160 | } |
---|
| 161 | |
---|
[4826] | 162 | /** |
---|
[7350] | 163 | * @brief loads a Weapon onto the WeaponManager |
---|
[4834] | 164 | * @param root the XML-element to load the Weapons from |
---|
| 165 | */ |
---|
| 166 | void WeaponManager::loadWeapons(const TiXmlElement* root) |
---|
| 167 | { |
---|
[5644] | 168 | LOAD_PARAM_START_CYCLE(root, element); |
---|
[4834] | 169 | |
---|
[8315] | 170 | BaseObject* object = Factory::fabricate(element); |
---|
| 171 | if (object != NULL) |
---|
| 172 | { |
---|
| 173 | Weapon* newWeapon = dynamic_cast<Weapon*>(object); |
---|
| 174 | if (newWeapon == NULL) |
---|
| 175 | delete object; |
---|
| 176 | } |
---|
[5644] | 177 | LOAD_PARAM_END_CYCLE(element); |
---|
[4834] | 178 | } |
---|
| 179 | |
---|
[4992] | 180 | /** |
---|
[7350] | 181 | * @brief sets the Parent of the WeaponManager. |
---|
[4992] | 182 | * @param parent the parent of the WeaponManager |
---|
| 183 | * |
---|
| 184 | * this is used, to identify to which ship/man/whatever this WeaponManager is connected. |
---|
| 185 | * also all the Slots will be subconnected to this parent. |
---|
[5435] | 186 | * |
---|
| 187 | * The reason this function exists is that the WeaponManager is neither a WorldEntity nor |
---|
| 188 | * a PNode. |
---|
[4992] | 189 | */ |
---|
[8844] | 190 | void WeaponManager::setParentEntity(WorldEntity* parent) |
---|
[4953] | 191 | { |
---|
[8844] | 192 | this->parentEntity = parent; |
---|
| 193 | if (this->parentNode == NULL) |
---|
| 194 | this->setParentNode(parent); |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | |
---|
| 198 | void WeaponManager::setParentNode(PNode* parent) |
---|
| 199 | { |
---|
| 200 | this->parentNode = parent; |
---|
| 201 | assert(parent != NULL); |
---|
| 202 | |
---|
| 203 | if (this->parentNode != NULL) |
---|
[4953] | 204 | { |
---|
| 205 | for (int i = 0; i < WM_MAX_SLOTS; i++) |
---|
[10437] | 206 | this->parentNode->addChild(this->currentSlotConfig[i]); |
---|
[4953] | 207 | } |
---|
[8844] | 208 | |
---|
[4953] | 209 | } |
---|
| 210 | |
---|
[8844] | 211 | |
---|
[4834] | 212 | /** |
---|
[7350] | 213 | * @brief sets the number of Slots the WeaponManager has |
---|
[4926] | 214 | * @param slotCount the number of slots |
---|
[4834] | 215 | */ |
---|
[4951] | 216 | void WeaponManager::setSlotCount(unsigned int slotCount) |
---|
[4834] | 217 | { |
---|
[4951] | 218 | if (slotCount <= WM_MAX_SLOTS) |
---|
| 219 | this->slotCount = slotCount; |
---|
| 220 | else |
---|
| 221 | this->slotCount = WM_MAX_SLOTS; |
---|
[4834] | 222 | } |
---|
| 223 | |
---|
[4972] | 224 | |
---|
| 225 | /** |
---|
[7350] | 226 | * @brief sets the position of the Slot relative to the parent |
---|
[4972] | 227 | * @param slot the slot to set-up |
---|
| 228 | * @param position the position of the given slot |
---|
| 229 | */ |
---|
[6803] | 230 | void WeaponManager::setSlotPosition(int slot, const Vector& position, PNode* parent) |
---|
[4953] | 231 | { |
---|
| 232 | if (slot < this->slotCount) |
---|
[6803] | 233 | { |
---|
[10437] | 234 | this->currentSlotConfig[slot]->setRelCoor(position); |
---|
[6803] | 235 | |
---|
| 236 | if (parent != NULL) |
---|
[10437] | 237 | this->currentSlotConfig[slot]->setParent(parent); |
---|
[6803] | 238 | } |
---|
[4953] | 239 | } |
---|
| 240 | |
---|
[4972] | 241 | |
---|
| 242 | /** |
---|
[7350] | 243 | * @brief sets the relative rotation of the slot to its parent |
---|
[4972] | 244 | * @param slot the slot to set-up |
---|
| 245 | * @param rotation the relative rotation of the given slot |
---|
| 246 | */ |
---|
[4969] | 247 | void WeaponManager::setSlotDirection(int slot, const Quaternion& rotation) |
---|
| 248 | { |
---|
| 249 | if (slot < this->slotCount) |
---|
[10437] | 250 | this->currentSlotConfig[slot]->setRelDir(rotation); |
---|
[4969] | 251 | } |
---|
| 252 | |
---|
| 253 | |
---|
[4834] | 254 | /** |
---|
[7350] | 255 | * @brief adds a weapon to the selected weaponconfiguration into the selected slot |
---|
[4972] | 256 | * @param weapon the weapon to add |
---|
| 257 | * @param configID an identifier for the slot: number between 0..7 if not specified: slotID=next free slot |
---|
| 258 | * @param slotID an identifier for the weapon configuration, number between 0..3 |
---|
[4832] | 259 | * |
---|
| 260 | * if you add explicitly a weapon at config:n, slot:m, the weapon placed at this location will be |
---|
[4906] | 261 | * replaced by the weapon specified. if you use the WM_FREE_SLOT, the manager will look for a free |
---|
[4832] | 262 | * slot in this weaponconfiguration. if there is non, the weapon won't be added and there will be |
---|
| 263 | * a error message. |
---|
[4826] | 264 | */ |
---|
[6561] | 265 | bool WeaponManager::addWeapon(Weapon* weapon, int configID, int slotID) |
---|
[4826] | 266 | { |
---|
[6753] | 267 | if ( weapon == NULL ) |
---|
[6737] | 268 | return false; |
---|
[6679] | 269 | |
---|
[4951] | 270 | if (unlikely(configID >= WM_MAX_CONFIGS || slotID >= (int)this->slotCount)) |
---|
[4826] | 271 | { |
---|
[6679] | 272 | PRINTF(2)("Slot %d of config %d is not availiabe (max: %d) searching for suitable slot\n", slotID, configID, this->slotCount); |
---|
| 273 | if (configID >= WM_MAX_CONFIGS) |
---|
| 274 | configID = -1; |
---|
| 275 | if (slotID >= (int)this->slotCount) |
---|
| 276 | slotID = -1; |
---|
[4951] | 277 | } |
---|
[6679] | 278 | // if no ConfigID is supplied set to Current Config. |
---|
| 279 | if (configID <= -1) |
---|
| 280 | configID = this->currentConfigID; |
---|
| 281 | // |
---|
| 282 | if (configID > -1 && slotID == -1) |
---|
| 283 | { |
---|
| 284 | slotID = this->getNextFreeSlot(configID, weapon->getCapability()); |
---|
| 285 | if (slotID == -1) |
---|
| 286 | configID = -1; |
---|
| 287 | } |
---|
[4951] | 288 | |
---|
[6676] | 289 | if (configID > 0 && slotID > 0 && this->configs[configID][slotID] != NULL) |
---|
| 290 | { |
---|
[9406] | 291 | PRINTF(3)("Weapon-slot %d/%d of %s already poulated, remove weapon (%s::%s) first\n", configID, slotID, this->getCName(), weapon->getClassCName(), weapon->getCName()); |
---|
[6676] | 292 | return false; |
---|
| 293 | } |
---|
[4951] | 294 | |
---|
[6676] | 295 | if (slotID <= -1) // WM_FREE_SLOT |
---|
[4951] | 296 | { |
---|
[5441] | 297 | slotID = this->getNextFreeSlot(configID, weapon->getCapability()); |
---|
[4951] | 298 | if( slotID < 0 || slotID >= this->slotCount) |
---|
[4826] | 299 | { |
---|
[5441] | 300 | PRINTF(1)("There is no free slot in this WeaponConfig to dock this weapon at! Aborting\n"); |
---|
[6561] | 301 | return false; |
---|
[4826] | 302 | } |
---|
| 303 | } |
---|
[4953] | 304 | |
---|
[10437] | 305 | if (!(this->currentSlotConfig[slotID]->getCapability() & weapon->getCapability() & WTYPE_ALLKINDS) && |
---|
| 306 | this->currentSlotConfig[slotID]->getCapability() & weapon->getCapability() & WTYPE_ALLDIRS) |
---|
[5441] | 307 | { |
---|
| 308 | PRINTF(2)("Unable to add Weapon with wrong capatibility to Slot %d (W:%d M:%d)\n", |
---|
[10437] | 309 | slotID, weapon->getCapability(), this->currentSlotConfig[slotID]->getCapability()); |
---|
[6561] | 310 | return false; |
---|
[5441] | 311 | } |
---|
| 312 | |
---|
[4953] | 313 | //! @todo check if the weapon is already assigned to another config in another slot |
---|
[6714] | 314 | if (this->configs[configID][slotID] != NULL) |
---|
| 315 | return false; |
---|
[6676] | 316 | |
---|
[4951] | 317 | this->configs[configID][slotID] = weapon; |
---|
[6669] | 318 | weapon->setAmmoContainer(this->getAmmoContainer(weapon->getProjectileType())); |
---|
[6736] | 319 | if(configID == this->currentConfigID) |
---|
[10438] | 320 | this->currentSlotConfig[slotID]->setNextWeapon(weapon); |
---|
[8844] | 321 | //if (this->parent != NULL) |
---|
[6142] | 322 | { |
---|
[8844] | 323 | this->parentNode->addChild(weapon); |
---|
[9869] | 324 | if (this->parentEntity->isA(Playable::staticClassID())) |
---|
[8844] | 325 | dynamic_cast<Playable*>(this->parentEntity)->weaponConfigChanged(); |
---|
[10437] | 326 | |
---|
[6920] | 327 | weapon->setDefaultTarget(this->crosshair); |
---|
[6142] | 328 | } |
---|
[9869] | 329 | PRINTF(4)("Added a new Weapon (%s::%s) to the WeaponManager: config %i/ slot %i\n", weapon->getClassCName(), weapon->getCName(), configID, slotID); |
---|
[6561] | 330 | return true; |
---|
[4826] | 331 | } |
---|
| 332 | |
---|
[4834] | 333 | /** |
---|
[6931] | 334 | * @brief increases the Energy of the WeaponContainer of type (projectileType) |
---|
| 335 | * @param projectileType the type of weapon to increase Energy from |
---|
| 336 | * @param ammo the ammo to increase |
---|
| 337 | */ |
---|
[9869] | 338 | float WeaponManager::increaseAmmunition(const ClassID& projectileType, float ammo) |
---|
[6931] | 339 | { |
---|
| 340 | return this->getAmmoContainer(projectileType)->increaseEnergy(ammo); |
---|
| 341 | } |
---|
| 342 | |
---|
[6972] | 343 | /** |
---|
[10368] | 344 | * @brief does the same as the funtion increaseAmmunition, added four your convenience |
---|
[6972] | 345 | * @param weapon, the Weapon to read the ammo-info about. |
---|
| 346 | * @param ammo how much ammo to add. |
---|
| 347 | */ |
---|
[10368] | 348 | float WeaponManager::increaseAmmunition(const Weapon* weapon, float ammo) |
---|
[6972] | 349 | { |
---|
| 350 | assert (weapon != NULL); |
---|
[9869] | 351 | return this->increaseAmmunition(weapon->getClassID(), ammo); |
---|
[6931] | 352 | |
---|
[6972] | 353 | } |
---|
[6931] | 354 | |
---|
[6972] | 355 | |
---|
[6931] | 356 | /** |
---|
[4954] | 357 | * sets the capabilities of a Slot |
---|
| 358 | * @param slot the slot to set the capability |
---|
[10437] | 359 | * @param slotCapability the capability @see WeaponSlotCapability |
---|
[4954] | 360 | */ |
---|
[10698] | 361 | void WeaponManager::setSlotCapability(int slot, unsigned long slotCapability) |
---|
[4954] | 362 | { |
---|
| 363 | if (slot > slotCount) |
---|
| 364 | return; |
---|
[10437] | 365 | this->currentSlotConfig[slot]->setCapability(slotCapability); |
---|
[4954] | 366 | } |
---|
| 367 | |
---|
| 368 | |
---|
| 369 | /** |
---|
[4834] | 370 | * removes a Weapon from the WeaponManager |
---|
[4954] | 371 | * |
---|
| 372 | * !! The weapon must be inactive before you can delete it, !! |
---|
| 373 | * !! because it will still be deactivated (if it is selected) !! |
---|
[4834] | 374 | */ |
---|
[4826] | 375 | void WeaponManager::removeWeapon(Weapon* weapon, int configID) |
---|
| 376 | { |
---|
[4954] | 377 | if (weapon == NULL) |
---|
| 378 | return; |
---|
| 379 | if (configID < 0) |
---|
| 380 | { |
---|
| 381 | for (int j = 0; j < WM_MAX_SLOTS; j++) |
---|
| 382 | { |
---|
| 383 | for (int i = 0; i < WM_MAX_CONFIGS; i++) |
---|
| 384 | { |
---|
| 385 | if (this->configs[i][j] == weapon) |
---|
| 386 | this->configs[i][j] = NULL; |
---|
| 387 | } |
---|
[10437] | 388 | if (this->currentSlotConfig[j]->getCurrentWeapon() == weapon) |
---|
[4954] | 389 | { |
---|
[10437] | 390 | this->currentSlotConfig[j]->setNextWeapon(NULL); |
---|
[4954] | 391 | } |
---|
| 392 | } |
---|
| 393 | } |
---|
[4826] | 394 | } |
---|
| 395 | |
---|
| 396 | |
---|
| 397 | /** |
---|
[4832] | 398 | * changes to the next weapon configuration |
---|
[4826] | 399 | */ |
---|
[4954] | 400 | void WeaponManager::nextWeaponConfig() |
---|
[4826] | 401 | { |
---|
[4951] | 402 | ++this->currentConfigID; |
---|
| 403 | if (this->currentConfigID >= WM_MAX_CONFIGS) |
---|
| 404 | this->currentConfigID = 0; |
---|
[4952] | 405 | this->changeWeaponConfig(this->currentConfigID); |
---|
| 406 | } |
---|
[4826] | 407 | |
---|
[4953] | 408 | /** |
---|
| 409 | * changes to the previous configuration |
---|
| 410 | */ |
---|
[4952] | 411 | void WeaponManager::previousWeaponConfig() |
---|
| 412 | { |
---|
| 413 | --this->currentConfigID; |
---|
| 414 | if (this->currentConfigID < 0) |
---|
| 415 | this->currentConfigID = WM_MAX_CONFIGS -1; |
---|
| 416 | this->changeWeaponConfig(this->currentConfigID); |
---|
| 417 | } |
---|
| 418 | |
---|
[4953] | 419 | /** |
---|
| 420 | * change to a desired configuration |
---|
| 421 | * @param weaponConfig the configuration to jump to. |
---|
| 422 | */ |
---|
[4952] | 423 | void WeaponManager::changeWeaponConfig(int weaponConfig) |
---|
| 424 | { |
---|
| 425 | this->currentConfigID = weaponConfig; |
---|
| 426 | PRINTF(4)("Changing weapon configuration: to %i\n", this->currentConfigID); |
---|
[10368] | 427 | |
---|
[4951] | 428 | for (int i = 0; i < WM_MAX_SLOTS; i++) |
---|
[10438] | 429 | this->currentSlotConfig[i]->setNextWeapon(this->configs[currentConfigID][i]); |
---|
[4826] | 430 | } |
---|
| 431 | |
---|
| 432 | |
---|
| 433 | /** |
---|
[4832] | 434 | * triggers fire of all weapons in the current weaponconfig |
---|
[4826] | 435 | */ |
---|
[4832] | 436 | void WeaponManager::fire() |
---|
[4826] | 437 | { |
---|
| 438 | Weapon* firingWeapon; |
---|
[4951] | 439 | for(int i = 0; i < this->slotCount; i++) |
---|
[4826] | 440 | { |
---|
[10368] | 441 | // printf("%i ", i); |
---|
[10526] | 442 | firingWeapon = this->currentSlotConfig[i]->getCurrentWeapon(); |
---|
| 443 | if( firingWeapon != NULL) |
---|
| 444 | { |
---|
| 445 | if( firingWeapon->getCurrentState() == WS_SHOOTING) continue; |
---|
| 446 | firingWeapon->requestAction(WA_SHOOT); |
---|
| 447 | } |
---|
[10368] | 448 | } |
---|
| 449 | // printf("\n"); |
---|
| 450 | /* |
---|
| 451 | this->crosshair->setRotationSpeed(500); |
---|
| 452 | this->crossHairSizeAnim->replay(); |
---|
| 453 | */ |
---|
| 454 | } |
---|
| 455 | |
---|
| 456 | /** |
---|
[10539] | 457 | * triggers release fire of all weapons in the current weaponconfig |
---|
[10368] | 458 | */ |
---|
| 459 | void WeaponManager::releaseFire() |
---|
| 460 | { |
---|
| 461 | Weapon* firingWeapon; |
---|
| 462 | for(int i = 0; i < this->slotCount; i++) |
---|
| 463 | { |
---|
[10438] | 464 | firingWeapon = this->currentSlotConfig[i]->getCurrentWeapon(); |
---|
[10528] | 465 | if( firingWeapon != NULL) |
---|
| 466 | firingWeapon->requestAction(WA_NONE); |
---|
[4826] | 467 | } |
---|
[10368] | 468 | |
---|
| 469 | /* |
---|
[4837] | 470 | this->crosshair->setRotationSpeed(500); |
---|
| 471 | this->crossHairSizeAnim->replay(); |
---|
[10368] | 472 | */ |
---|
[4826] | 473 | } |
---|
| 474 | |
---|
| 475 | /** |
---|
[4832] | 476 | * triggers tick of all weapons in the current weaponconfig |
---|
| 477 | * @param second passed since last tick |
---|
[4826] | 478 | */ |
---|
[4833] | 479 | void WeaponManager::tick(float dt) |
---|
[4826] | 480 | { |
---|
[4951] | 481 | Weapon* tickWeapon; |
---|
| 482 | |
---|
[10368] | 483 | |
---|
[4951] | 484 | for(int i = 0; i < this->slotCount; i++) |
---|
[4826] | 485 | { |
---|
[10368] | 486 | |
---|
| 487 | //NICE LITTLE DEBUG FUNCTION |
---|
[10437] | 488 | /* if (this->currentSlotConfig[i]->currentWeapon != NULL || this->currentSlotConfig[i]->nextWeapon != NULL) |
---|
| 489 | printf("%p %p\n", this->currentSlotConfig[i]->currentWeapon, this->currentSlotConfig[i]->nextWeapon);*/ |
---|
[4951] | 490 | |
---|
[6736] | 491 | // current Weapon in Slot i |
---|
[10438] | 492 | tickWeapon = this->currentSlotConfig[i]->getCurrentWeapon(); |
---|
[6736] | 493 | // On A change (current != next) |
---|
[10438] | 494 | if (tickWeapon != this->currentSlotConfig[i]->getNextWeapon()) |
---|
[4951] | 495 | { |
---|
[6736] | 496 | // if a Weapon is Active in slot i, deactivate it. |
---|
| 497 | if (tickWeapon != NULL ) |
---|
[4951] | 498 | { |
---|
[6736] | 499 | if (tickWeapon->isActive()) |
---|
[4953] | 500 | { |
---|
[6736] | 501 | tickWeapon->requestAction(WA_DEACTIVATE); |
---|
| 502 | continue; |
---|
[4953] | 503 | } |
---|
[6055] | 504 | else |
---|
[6736] | 505 | { |
---|
| 506 | tickWeapon->toList(OM_NULL); |
---|
[10438] | 507 | this->currentSlotConfig[i]->setCurrentWeapon(NULL); |
---|
[6736] | 508 | } |
---|
[4951] | 509 | } |
---|
[6736] | 510 | // switching to next Weapon |
---|
[10438] | 511 | this->currentSlotConfig[i]->setCurrentWeapon(this->currentSlotConfig[i]->getNextWeapon()); |
---|
| 512 | tickWeapon = this->currentSlotConfig[i]->getCurrentWeapon(); |
---|
[10437] | 513 | |
---|
[6736] | 514 | if (tickWeapon != NULL) |
---|
| 515 | { |
---|
[10368] | 516 | //if (this->parent != NULL) |
---|
| 517 | tickWeapon->toList(this->parentEntity->getOMListNumber()); |
---|
[6736] | 518 | tickWeapon->requestAction(WA_ACTIVATE); |
---|
[10437] | 519 | this->currentSlotConfig[i]->activateNode(); |
---|
| 520 | tickWeapon->setParent(this->currentSlotConfig[i]); |
---|
[6736] | 521 | } |
---|
| 522 | else |
---|
[10437] | 523 | this->currentSlotConfig[i]->deactivateNode(); |
---|
[9869] | 524 | if (this->parentEntity != NULL && this->parentEntity->isA(Playable::staticClassID())) |
---|
[8844] | 525 | dynamic_cast<Playable*>(this->parentEntity)->weaponConfigChanged(); |
---|
[4951] | 526 | } |
---|
[10544] | 527 | else if (unlikely(tickWeapon != NULL && tickWeapon->getCurrentState() == WS_DEACTIVATING)) |
---|
| 528 | this->currentSlotConfig[i]->setCurrentWeapon(NULL); |
---|
[4826] | 529 | } |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | |
---|
| 533 | /** |
---|
[4832] | 534 | * triggers draw of all weapons in the current weaponconfig |
---|
[4826] | 535 | */ |
---|
[4951] | 536 | void WeaponManager::draw() const |
---|
[4826] | 537 | { |
---|
[8315] | 538 | assert(false || "must not be called"); |
---|
[4951] | 539 | Weapon* drawWeapon; |
---|
| 540 | for (int i = 0; i < this->slotCount; i++) |
---|
[4826] | 541 | { |
---|
[10438] | 542 | drawWeapon = this->currentSlotConfig[i]->getCurrentWeapon(); |
---|
[4951] | 543 | if( drawWeapon != NULL && drawWeapon->isVisible()) |
---|
| 544 | drawWeapon->draw(); |
---|
[4826] | 545 | } |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | |
---|
| 549 | /** |
---|
[4832] | 550 | * private gets the next free slot in a certain weaponconfig |
---|
[6669] | 551 | * @param the selected weaponconfig -1 if none found |
---|
[4826] | 552 | */ |
---|
[5440] | 553 | int WeaponManager::getNextFreeSlot(int configID, long capability) |
---|
[4826] | 554 | { |
---|
[6676] | 555 | if (configID == -1) |
---|
[4826] | 556 | { |
---|
[6676] | 557 | for (configID = 0; configID < WM_MAX_CONFIGS; configID++) |
---|
| 558 | for( int i = 0; i < this->slotCount; ++i) |
---|
| 559 | { |
---|
| 560 | if( this->configs[configID][i] == NULL && |
---|
[10437] | 561 | (this->currentSlotConfig[i]->getCapability() & capability & WTYPE_ALLKINDS) && |
---|
| 562 | (this->currentSlotConfig[i]->getCapability() & capability & WTYPE_ALLDIRS)) |
---|
[6676] | 563 | return i; |
---|
[6736] | 564 | } |
---|
[4826] | 565 | } |
---|
[6676] | 566 | else |
---|
| 567 | { |
---|
| 568 | for( int i = 0; i < this->slotCount; ++i) |
---|
| 569 | { |
---|
| 570 | if( this->configs[configID][i] == NULL && |
---|
[10437] | 571 | (this->currentSlotConfig[i]->getCapability() & capability & WTYPE_ALLKINDS) && |
---|
| 572 | (this->currentSlotConfig[i]->getCapability() & capability & WTYPE_ALLDIRS)) |
---|
[6676] | 573 | return i; |
---|
| 574 | } |
---|
| 575 | } |
---|
[4826] | 576 | return -1; |
---|
| 577 | } |
---|
| 578 | |
---|
[9869] | 579 | CountPointer<AmmoContainer>& WeaponManager::getAmmoContainer(const ClassID& projectileType) |
---|
[6669] | 580 | { |
---|
| 581 | for (unsigned int i = 0; i < this->ammo.size(); i++) |
---|
| 582 | { |
---|
| 583 | if (this->ammo[i]->getProjectileType() == projectileType) |
---|
| 584 | return this->ammo[i]; |
---|
| 585 | } |
---|
| 586 | this->ammo.push_back(CountPointer<AmmoContainer>(new AmmoContainer(projectileType))); |
---|
| 587 | return this->ammo.back(); |
---|
| 588 | } |
---|
[4951] | 589 | |
---|
[6972] | 590 | CountPointer<AmmoContainer>& WeaponManager::getAmmoContainer(const Weapon* weapon) |
---|
| 591 | { |
---|
| 592 | assert (weapon != NULL); |
---|
[9869] | 593 | return (this->getAmmoContainer(weapon->getClassID())); |
---|
[6972] | 594 | } |
---|
[4951] | 595 | |
---|
[6972] | 596 | |
---|
[4953] | 597 | /** |
---|
| 598 | * outputs some nice debug information about the WeaponManager |
---|
| 599 | */ |
---|
[4951] | 600 | void WeaponManager::debug() const |
---|
| 601 | { |
---|
| 602 | PRINT(3)("WeaponManager Debug Information\n"); |
---|
| 603 | PRINT(3)("-------------------------------\n"); |
---|
| 604 | PRINT(3)("current Config is %d\n", this->currentConfigID); |
---|
| 605 | for (int i = 0; i < WM_MAX_CONFIGS; i++) |
---|
| 606 | { |
---|
| 607 | PRINT(3)("Listing Weapons in Configuration %d\n", i); |
---|
| 608 | for (int j = 0; j < WM_MAX_SLOTS; j++) |
---|
| 609 | { |
---|
| 610 | if (this->configs[i][j] != NULL) |
---|
[9406] | 611 | PRINT(3)("Slot %d loaded a %s\n", j, this->configs[i][j]->getClassCName()); |
---|
[4951] | 612 | } |
---|
| 613 | } |
---|
| 614 | } |
---|
[10437] | 615 | |
---|
| 616 | |
---|
| 617 | long WeaponManager::getSlotCapability(int slot) const |
---|
| 618 | { |
---|
| 619 | return this->currentSlotConfig[slot]->getCapability(); |
---|
| 620 | } |
---|
| 621 | |
---|
| 622 | const Vector& WeaponManager::getSlotPosition(int slot) const |
---|
| 623 | { |
---|
| 624 | return this->currentSlotConfig[slot]->getRelCoor(); |
---|
| 625 | } |
---|
| 626 | |
---|
| 627 | Weapon* WeaponManager::getWeapon(int slotID) const |
---|
| 628 | { |
---|
| 629 | return (slotID >= 0 && slotID < this->slotCount)? this->currentSlotConfig[slotID]->getNextWeapon(): NULL; |
---|
| 630 | } |
---|
| 631 | |
---|
| 632 | |
---|