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