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