[3573] | 1 | |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | orxonox - the future of 3D-vertical-scrollers |
---|
| 5 | |
---|
| 6 | Copyright (C) 2004 orx |
---|
| 7 | |
---|
| 8 | This program is free software; you can redistribute it and/or modify |
---|
| 9 | it under the terms of the GNU General Public License as published by |
---|
| 10 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 11 | any later version. |
---|
| 12 | |
---|
| 13 | ### File Specific |
---|
| 14 | main-programmer: Patrick Boenzli |
---|
| 15 | co-programmer: |
---|
| 16 | */ |
---|
| 17 | |
---|
[3873] | 18 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON |
---|
[3573] | 19 | |
---|
| 20 | #include "weapon.h" |
---|
| 21 | #include "stdincl.h" |
---|
| 22 | #include "world_entity.h" |
---|
| 23 | #include "vector.h" |
---|
[3677] | 24 | #include "model.h" |
---|
[3575] | 25 | #include "projectile.h" |
---|
[3620] | 26 | #include "list.h" |
---|
| 27 | #include "world.h" |
---|
[3573] | 28 | |
---|
| 29 | using namespace std; |
---|
| 30 | |
---|
| 31 | |
---|
[3862] | 32 | |
---|
[3870] | 33 | /** |
---|
| 34 | \brief this initializes the weaponManager for a given nnumber of weapon slots |
---|
| 35 | \param number of weapon slots of the model/ship <= 8 (limitied) |
---|
| 36 | */ |
---|
[3862] | 37 | WeaponManager::WeaponManager(int nrOfSlots) |
---|
| 38 | { |
---|
[3875] | 39 | for(int i = 0; i < W_MAX_CONFIGS; ++i) |
---|
[3879] | 40 | { |
---|
| 41 | this->configs[i].bUsed = false; |
---|
| 42 | for(int j = 0; j < W_MAX_SLOTS; ++j) |
---|
| 43 | this->configs[i].slots[j] = NULL; |
---|
| 44 | } |
---|
[3881] | 45 | this->nrOfSlots = nrOfSlots; |
---|
[3875] | 46 | this->currConfID = W_CONFIG0; |
---|
[3862] | 47 | } |
---|
| 48 | |
---|
[3870] | 49 | |
---|
[3862] | 50 | WeaponManager::~WeaponManager() |
---|
| 51 | { |
---|
[3881] | 52 | /* |
---|
| 53 | i dont have to delete the weapons itself, because they are |
---|
[3888] | 54 | worldentities and therefore in the entities list of the world. |
---|
| 55 | world will clean them up for me |
---|
[3870] | 56 | */ |
---|
[3881] | 57 | for(int i = 0; i < W_MAX_CONFIGS; ++i) |
---|
| 58 | { |
---|
| 59 | this->configs[i].bUsed = false; |
---|
| 60 | for(int j = 0; j < W_MAX_SLOTS; ++j) |
---|
| 61 | this->configs[i].slots[j] = NULL; |
---|
| 62 | } |
---|
[3862] | 63 | } |
---|
| 64 | |
---|
[3870] | 65 | |
---|
[3878] | 66 | /** |
---|
| 67 | \brief adds a weapon to the selected weaponconfiguration into the selected slot |
---|
| 68 | \param the weapon to add |
---|
| 69 | \param an identifier for the slot: number between 0..7 if not specified: slotID=next free slot |
---|
| 70 | \param an identifier for the weapon configuration, number between 0..3 |
---|
| 71 | |
---|
| 72 | if you add explicitly a weapon at config:n, slot:m, the weapon placed at this location will be |
---|
| 73 | replaced by the weapon specified. if you use the W_FREE_SLOT, the manager will look for a free |
---|
| 74 | slot in this weaponconfiguration. if there is non, the weapon won't be added and there will be |
---|
| 75 | a error message. |
---|
| 76 | */ |
---|
| 77 | void WeaponManager::addWeapon(Weapon* weapon, int configID, int slotID) |
---|
[3862] | 78 | { |
---|
[3870] | 79 | if( slotID == W_FREE_SLOT) |
---|
| 80 | { |
---|
[3878] | 81 | int freeSlot = this->getNextFreeSlot( configID); |
---|
[3870] | 82 | if( freeSlot < 0 || freeSlot >= this->nrOfSlots) |
---|
| 83 | { |
---|
| 84 | PRINTF(0)("There is no free slot in this WeaponConfig to dock this weapon at! Aborting\n"); |
---|
| 85 | return; |
---|
| 86 | } |
---|
[3873] | 87 | PRINTF(3)("Added new Weapon to Config:%i/Slot:%i\n", configID, freeSlot); |
---|
[3878] | 88 | this->configs[configID].bUsed = true; |
---|
[3870] | 89 | this->configs[configID].slots[freeSlot] = weapon; |
---|
| 90 | return; |
---|
| 91 | } |
---|
[3878] | 92 | this->configs[configID].bUsed = true; |
---|
[3870] | 93 | this->configs[configID].slots[slotID] = weapon; |
---|
| 94 | PRINTF(3)("Added a new Weapon to the WeaponManager: config %i/ slot %i\n", configID, slotID); |
---|
[3862] | 95 | } |
---|
| 96 | |
---|
| 97 | |
---|
[3878] | 98 | void WeaponManager::removeWeapon(Weapon* weapon, int configID) |
---|
| 99 | { |
---|
| 100 | /* empty */ |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | /** |
---|
| 105 | \brief changes to the next weapon configuration |
---|
| 106 | |
---|
| 107 | if there are multiple weapon configurations defined by the manager, use this to switch between them |
---|
[3881] | 108 | this function will deactivate the weapons first, change the config and reactivate them later |
---|
[3878] | 109 | */ |
---|
[3862] | 110 | void WeaponManager::nextWeaponConf() |
---|
[3870] | 111 | { |
---|
[3879] | 112 | PRINTF(4)("Changing weapon configuration: from %i\n", this->currConfID); |
---|
[3881] | 113 | |
---|
| 114 | Weapon* w; |
---|
| 115 | for(int i = 0; i < W_MAX_SLOTS; ++i) |
---|
| 116 | { |
---|
| 117 | w = this->configs[this->currConfID].slots[i]; |
---|
| 118 | if( w != NULL) w->deactivate(); |
---|
| 119 | } |
---|
[3873] | 120 | int i; |
---|
[3878] | 121 | for(i = this->currConfID + 1; i < W_MAX_CONFIGS && !this->configs[i].bUsed; ++i); |
---|
[3873] | 122 | if( i == W_MAX_CONFIGS) this->currConfID = W_CONFIG0; |
---|
| 123 | else this->currConfID = i; |
---|
[3879] | 124 | PRINTF(4)("\tto %i\n", this->currConfID); |
---|
[3881] | 125 | |
---|
| 126 | for(int i = 0; i < W_MAX_SLOTS; ++i) |
---|
| 127 | { |
---|
| 128 | w = this->configs[this->currConfID].slots[i]; |
---|
| 129 | if( w != NULL) w->activate(); |
---|
| 130 | } |
---|
[3870] | 131 | } |
---|
[3862] | 132 | |
---|
[3870] | 133 | |
---|
| 134 | |
---|
[3878] | 135 | /** |
---|
| 136 | \brief triggers fire of all weapons in the current weaponconfig |
---|
| 137 | */ |
---|
[3875] | 138 | void WeaponManager::fire() |
---|
| 139 | { |
---|
| 140 | Weapon* firingWeapon; |
---|
| 141 | for(int i = 0; i < W_MAX_SLOTS; ++i) |
---|
| 142 | { |
---|
| 143 | firingWeapon = this->configs[this->currConfID].slots[i]; |
---|
| 144 | if( firingWeapon != NULL) firingWeapon->fire(); |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | |
---|
[3878] | 149 | /** |
---|
| 150 | \brief triggers tick of all weapons in the current weaponconfig |
---|
| 151 | \param second passed since last tick |
---|
| 152 | */ |
---|
[3877] | 153 | void WeaponManager::tick(float sec) |
---|
| 154 | { |
---|
| 155 | Weapon* w; |
---|
| 156 | for(int i = 0; i < W_MAX_SLOTS; ++i) |
---|
| 157 | { |
---|
| 158 | w = this->configs[this->currConfID].slots[i]; |
---|
| 159 | if( w != NULL) w->tick(sec); |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | |
---|
[3878] | 164 | /** |
---|
| 165 | \brief triggers draw of all weapons in the current weaponconfig |
---|
| 166 | */ |
---|
[3877] | 167 | void WeaponManager::draw() |
---|
| 168 | { |
---|
| 169 | Weapon* w; |
---|
| 170 | for(int i = 0; i < W_MAX_SLOTS; ++i) |
---|
| 171 | { |
---|
| 172 | w = this->configs[this->currConfID].slots[i]; |
---|
| 173 | if( w != NULL) w->draw(); |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | |
---|
[3878] | 177 | |
---|
[3575] | 178 | /** |
---|
[3878] | 179 | \brief private gets the next free slot in a certain weaponconfig |
---|
| 180 | \param the selected weaponconfig |
---|
| 181 | */ |
---|
| 182 | int WeaponManager::getNextFreeSlot(int configID) |
---|
| 183 | { |
---|
| 184 | for( int i = 0; i < W_MAX_SLOTS; ++i) |
---|
| 185 | { |
---|
| 186 | if( this->configs[configID].slots[i] == NULL) |
---|
| 187 | return i; |
---|
| 188 | } |
---|
| 189 | return -1; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | |
---|
| 195 | |
---|
| 196 | |
---|
| 197 | /** |
---|
[3575] | 198 | \brief standard constructor |
---|
[3573] | 199 | |
---|
[3575] | 200 | creates a new weapon |
---|
| 201 | */ |
---|
[3881] | 202 | Weapon::Weapon (PNode* parent, const Vector& coordinate, const Quaternion& direction) |
---|
[3631] | 203 | : WorldEntity() |
---|
[3620] | 204 | { |
---|
[3752] | 205 | parent->addChild(this, PNODE_ALL); |
---|
[3881] | 206 | this->setRelCoor(coordinate); |
---|
| 207 | this->setRelDir(direction); |
---|
[3620] | 208 | WorldInterface* wi = WorldInterface::getInstance(); |
---|
| 209 | this->worldEntities = wi->getEntityList(); |
---|
[3888] | 210 | |
---|
| 211 | this->objectComponent1 = NULL; |
---|
| 212 | this->objectComponent2 = NULL; |
---|
| 213 | this->objectComponent3 = NULL; |
---|
| 214 | |
---|
| 215 | this->animation1 = NULL; |
---|
| 216 | this->animation2 = NULL; |
---|
| 217 | this->animation3 = NULL; |
---|
[3620] | 218 | } |
---|
[3573] | 219 | |
---|
| 220 | |
---|
[3575] | 221 | /** |
---|
| 222 | \brief standard deconstructor |
---|
| 223 | */ |
---|
[3573] | 224 | Weapon::~Weapon () |
---|
| 225 | { |
---|
[3579] | 226 | // model will be deleted from WorldEntity-destructor |
---|
[3888] | 227 | //this->worldEntities = NULL; |
---|
| 228 | |
---|
| 229 | /* dont delete objectComponentsX here, they will be killed when the pnodes are cleaned out */ |
---|
| 230 | |
---|
| 231 | /* all animations are deleted via the animation player*/ |
---|
[3573] | 232 | } |
---|
| 233 | |
---|
| 234 | |
---|
[3577] | 235 | /** |
---|
| 236 | \brief enables the weapon |
---|
| 237 | |
---|
| 238 | a weapon can be enabled/disabled because of various reasons. if a weapon is |
---|
| 239 | been enabled, it can interact in a world. elswhere it wont react to any |
---|
| 240 | action. |
---|
| 241 | */ |
---|
| 242 | void Weapon::enable() |
---|
[3583] | 243 | { |
---|
| 244 | this->enabled = true; |
---|
| 245 | } |
---|
[3577] | 246 | |
---|
| 247 | |
---|
| 248 | /** |
---|
| 249 | \brief disables the weapon |
---|
| 250 | |
---|
| 251 | a weapon can be enabled/disabled because of various reasons. if a weapon is |
---|
| 252 | been enabled, it can interact in a world. elswhere it wont react to any |
---|
| 253 | action. |
---|
| 254 | */ |
---|
| 255 | void Weapon::disable() |
---|
[3583] | 256 | { |
---|
| 257 | this->enabled = false; |
---|
| 258 | } |
---|
[3577] | 259 | |
---|
| 260 | |
---|
| 261 | /** |
---|
| 262 | \brief checks if the weapon is enabled |
---|
| 263 | \returns true if enabled |
---|
| 264 | |
---|
| 265 | a weapon can be ebabled/disabled because of various reasons. if a weapon is |
---|
| 266 | been enabled, it can interact in a world. elswhere it wont react to any |
---|
| 267 | action. |
---|
| 268 | */ |
---|
| 269 | bool Weapon::isEnabled() |
---|
[3583] | 270 | { |
---|
| 271 | return this->enabled; |
---|
| 272 | } |
---|
[3577] | 273 | |
---|
| 274 | |
---|
[3575] | 275 | /** |
---|
| 276 | \brief sets a new projectile to the weapon |
---|
| 277 | \param new projectile for this weapon |
---|
[3573] | 278 | |
---|
[3575] | 279 | weapon an projectile are independent, so you can combine them as you want |
---|
| 280 | */ |
---|
| 281 | void Weapon::setProjectile(Projectile* projectile) |
---|
| 282 | { |
---|
| 283 | this->projectile = projectile; |
---|
| 284 | } |
---|
[3573] | 285 | |
---|
| 286 | |
---|
[3575] | 287 | /** |
---|
| 288 | \brief sets a new projectile to the weapon |
---|
| 289 | \returns the current projectile of this weapon |
---|
| 290 | |
---|
| 291 | weapon an projectile are independent, so you can combine them as you want |
---|
| 292 | */ |
---|
| 293 | Projectile* Weapon::getProjectile() |
---|
| 294 | { |
---|
| 295 | return this->projectile; |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | |
---|
| 299 | /** |
---|
| 300 | \brief this activates the weapon |
---|
| 301 | |
---|
| 302 | This is needed, since there can be more than one weapon on a ship. the |
---|
| 303 | activation can be connected with an animation. for example the weapon is |
---|
| 304 | been armed out. |
---|
| 305 | */ |
---|
| 306 | void Weapon::activate() |
---|
| 307 | {} |
---|
| 308 | |
---|
| 309 | |
---|
| 310 | /** |
---|
| 311 | \brief this deactivates the weapon |
---|
| 312 | |
---|
| 313 | This is needed, since there can be more than one weapon on a ship. the |
---|
| 314 | activation can be connected with an animation. for example the weapon is |
---|
| 315 | been armed out. |
---|
| 316 | */ |
---|
| 317 | void Weapon::deactivate() |
---|
| 318 | {} |
---|
| 319 | |
---|
| 320 | /** |
---|
| 321 | \brief asks if the current weapon is active |
---|
| 322 | \returns true if it the weapon is active |
---|
| 323 | */ |
---|
| 324 | bool Weapon::isActive() |
---|
| 325 | {} |
---|
| 326 | |
---|
| 327 | |
---|
| 328 | |
---|
| 329 | |
---|
[3576] | 330 | |
---|
| 331 | |
---|
| 332 | /** |
---|
[3575] | 333 | \brief is called, when the weapon gets hit (=collide with something) |
---|
| 334 | \param from which entity it is been hit |
---|
| 335 | \param where it is been hit |
---|
| 336 | |
---|
| 337 | this may not be used, since it would make the game relay complicated when one |
---|
| 338 | can destroy the weapons of enemies or vice versa. |
---|
| 339 | */ |
---|
[3578] | 340 | void Weapon::hit (WorldEntity* entity, Vector* position) |
---|
[3575] | 341 | {} |
---|
| 342 | |
---|
[3577] | 343 | |
---|
[3575] | 344 | /** |
---|
| 345 | \brief is called, when the weapon is destroyed |
---|
| 346 | |
---|
| 347 | this is in conjunction with the hit function, so when a weapon is able to get |
---|
| 348 | hit, it can also be destoryed. |
---|
| 349 | */ |
---|
| 350 | void Weapon::destroy () |
---|
| 351 | {} |
---|
| 352 | |
---|
| 353 | |
---|
| 354 | /** |
---|
[3577] | 355 | \brief tick signal for time dependent/driven stuff |
---|
| 356 | */ |
---|
| 357 | void Weapon::tick (float time) |
---|
| 358 | {} |
---|
| 359 | |
---|
| 360 | |
---|
| 361 | /** |
---|
[3583] | 362 | \brief is called, when there is no fire button pressed |
---|
| 363 | */ |
---|
| 364 | void Weapon::weaponIdle() |
---|
| 365 | {} |
---|
| 366 | |
---|
| 367 | |
---|
| 368 | /** |
---|
[3575] | 369 | \brief this will draw the weapon |
---|
| 370 | */ |
---|
[3573] | 371 | void Weapon::draw () |
---|
[3583] | 372 | {} |
---|
[3575] | 373 | |
---|