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