[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 | { |
---|
[3993] | 112 | PRINTF(4)("Changing weapon configuration: from %i to next\n", this->currConfID); |
---|
[3881] | 113 | |
---|
[4000] | 114 | int i, lastConfID; |
---|
| 115 | lastConfID = this->currConfID; |
---|
[3878] | 116 | for(i = this->currConfID + 1; i < W_MAX_CONFIGS && !this->configs[i].bUsed; ++i); |
---|
[3873] | 117 | if( i == W_MAX_CONFIGS) this->currConfID = W_CONFIG0; |
---|
| 118 | else this->currConfID = i; |
---|
[3881] | 119 | |
---|
[4000] | 120 | |
---|
| 121 | Weapon *w1, *w2; |
---|
| 122 | for(int j = 0; j < W_MAX_SLOTS; ++j) |
---|
[3881] | 123 | { |
---|
[4000] | 124 | w1 = this->configs[lastConfID].slots[j]; |
---|
| 125 | w2 = this->configs[this->currConfID].slots[j]; |
---|
| 126 | |
---|
| 127 | if( w1 == w2) |
---|
| 128 | { |
---|
| 129 | printf("no need for change\n"); |
---|
| 130 | } |
---|
| 131 | else |
---|
| 132 | { |
---|
| 133 | if( w1 != NULL ) |
---|
| 134 | { |
---|
| 135 | w1->deactivate(); |
---|
| 136 | printf("deactivating %i,%i\n", j,lastConfID); |
---|
| 137 | } |
---|
| 138 | if( w2 != NULL) |
---|
| 139 | { |
---|
| 140 | w2->activate(); |
---|
| 141 | printf("activating %i,%i\n", j, this->currConfID); |
---|
| 142 | } |
---|
| 143 | } |
---|
[3881] | 144 | } |
---|
[3870] | 145 | } |
---|
[3862] | 146 | |
---|
[3870] | 147 | |
---|
| 148 | |
---|
[3878] | 149 | /** |
---|
| 150 | \brief triggers fire of all weapons in the current weaponconfig |
---|
| 151 | */ |
---|
[3875] | 152 | void WeaponManager::fire() |
---|
| 153 | { |
---|
| 154 | Weapon* firingWeapon; |
---|
| 155 | for(int i = 0; i < W_MAX_SLOTS; ++i) |
---|
| 156 | { |
---|
| 157 | firingWeapon = this->configs[this->currConfID].slots[i]; |
---|
| 158 | if( firingWeapon != NULL) firingWeapon->fire(); |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | |
---|
[3878] | 163 | /** |
---|
| 164 | \brief triggers tick of all weapons in the current weaponconfig |
---|
| 165 | \param second passed since last tick |
---|
| 166 | */ |
---|
[3877] | 167 | void WeaponManager::tick(float sec) |
---|
| 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->tick(sec); |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | |
---|
[3878] | 178 | /** |
---|
| 179 | \brief triggers draw of all weapons in the current weaponconfig |
---|
| 180 | */ |
---|
[3877] | 181 | void WeaponManager::draw() |
---|
| 182 | { |
---|
| 183 | Weapon* w; |
---|
| 184 | for(int i = 0; i < W_MAX_SLOTS; ++i) |
---|
| 185 | { |
---|
| 186 | w = this->configs[this->currConfID].slots[i]; |
---|
| 187 | if( w != NULL) w->draw(); |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | |
---|
[3878] | 191 | |
---|
[3575] | 192 | /** |
---|
[3878] | 193 | \brief private gets the next free slot in a certain weaponconfig |
---|
| 194 | \param the selected weaponconfig |
---|
| 195 | */ |
---|
| 196 | int WeaponManager::getNextFreeSlot(int configID) |
---|
| 197 | { |
---|
| 198 | for( int i = 0; i < W_MAX_SLOTS; ++i) |
---|
| 199 | { |
---|
| 200 | if( this->configs[configID].slots[i] == NULL) |
---|
| 201 | return i; |
---|
| 202 | } |
---|
| 203 | return -1; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | |
---|
| 207 | |
---|
| 208 | |
---|
| 209 | |
---|
| 210 | |
---|
| 211 | /** |
---|
[3575] | 212 | \brief standard constructor |
---|
[3573] | 213 | |
---|
[3575] | 214 | creates a new weapon |
---|
| 215 | */ |
---|
[3881] | 216 | Weapon::Weapon (PNode* parent, const Vector& coordinate, const Quaternion& direction) |
---|
[3631] | 217 | : WorldEntity() |
---|
[3620] | 218 | { |
---|
[3752] | 219 | parent->addChild(this, PNODE_ALL); |
---|
[3881] | 220 | this->setRelCoor(coordinate); |
---|
| 221 | this->setRelDir(direction); |
---|
[3620] | 222 | WorldInterface* wi = WorldInterface::getInstance(); |
---|
| 223 | this->worldEntities = wi->getEntityList(); |
---|
[3888] | 224 | |
---|
| 225 | this->objectComponent1 = NULL; |
---|
| 226 | this->objectComponent2 = NULL; |
---|
| 227 | this->objectComponent3 = NULL; |
---|
| 228 | |
---|
| 229 | this->animation1 = NULL; |
---|
| 230 | this->animation2 = NULL; |
---|
| 231 | this->animation3 = NULL; |
---|
[3620] | 232 | } |
---|
[3573] | 233 | |
---|
| 234 | |
---|
[3575] | 235 | /** |
---|
| 236 | \brief standard deconstructor |
---|
| 237 | */ |
---|
[3573] | 238 | Weapon::~Weapon () |
---|
| 239 | { |
---|
[3579] | 240 | // model will be deleted from WorldEntity-destructor |
---|
[3888] | 241 | //this->worldEntities = NULL; |
---|
| 242 | |
---|
| 243 | /* dont delete objectComponentsX here, they will be killed when the pnodes are cleaned out */ |
---|
| 244 | |
---|
| 245 | /* all animations are deleted via the animation player*/ |
---|
[3573] | 246 | } |
---|
| 247 | |
---|
| 248 | |
---|
[3577] | 249 | /** |
---|
| 250 | \brief enables the weapon |
---|
| 251 | |
---|
| 252 | a weapon can be enabled/disabled because of various reasons. if a weapon is |
---|
| 253 | been enabled, it can interact in a world. elswhere it wont react to any |
---|
| 254 | action. |
---|
| 255 | */ |
---|
| 256 | void Weapon::enable() |
---|
[3583] | 257 | { |
---|
| 258 | this->enabled = true; |
---|
| 259 | } |
---|
[3577] | 260 | |
---|
| 261 | |
---|
| 262 | /** |
---|
| 263 | \brief disables the weapon |
---|
| 264 | |
---|
| 265 | a weapon can be enabled/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 | void Weapon::disable() |
---|
[3583] | 270 | { |
---|
| 271 | this->enabled = false; |
---|
| 272 | } |
---|
[3577] | 273 | |
---|
| 274 | |
---|
| 275 | /** |
---|
| 276 | \brief checks if the weapon is enabled |
---|
| 277 | \returns true if enabled |
---|
| 278 | |
---|
| 279 | a weapon can be ebabled/disabled because of various reasons. if a weapon is |
---|
| 280 | been enabled, it can interact in a world. elswhere it wont react to any |
---|
| 281 | action. |
---|
| 282 | */ |
---|
| 283 | bool Weapon::isEnabled() |
---|
[3583] | 284 | { |
---|
| 285 | return this->enabled; |
---|
| 286 | } |
---|
[3577] | 287 | |
---|
| 288 | |
---|
[3575] | 289 | /** |
---|
| 290 | \brief sets a new projectile to the weapon |
---|
| 291 | \param new projectile for this weapon |
---|
[3573] | 292 | |
---|
[3575] | 293 | weapon an projectile are independent, so you can combine them as you want |
---|
| 294 | */ |
---|
| 295 | void Weapon::setProjectile(Projectile* projectile) |
---|
| 296 | { |
---|
| 297 | this->projectile = projectile; |
---|
| 298 | } |
---|
[3573] | 299 | |
---|
| 300 | |
---|
[3575] | 301 | /** |
---|
| 302 | \brief sets a new projectile to the weapon |
---|
| 303 | \returns the current projectile of this weapon |
---|
| 304 | |
---|
| 305 | weapon an projectile are independent, so you can combine them as you want |
---|
| 306 | */ |
---|
| 307 | Projectile* Weapon::getProjectile() |
---|
| 308 | { |
---|
| 309 | return this->projectile; |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | |
---|
| 313 | /** |
---|
| 314 | \brief this activates the weapon |
---|
| 315 | |
---|
| 316 | This is needed, since there can be more than one weapon on a ship. the |
---|
| 317 | activation can be connected with an animation. for example the weapon is |
---|
| 318 | been armed out. |
---|
| 319 | */ |
---|
| 320 | void Weapon::activate() |
---|
| 321 | {} |
---|
| 322 | |
---|
| 323 | |
---|
| 324 | /** |
---|
| 325 | \brief this deactivates the weapon |
---|
| 326 | |
---|
| 327 | This is needed, since there can be more than one weapon on a ship. the |
---|
| 328 | activation can be connected with an animation. for example the weapon is |
---|
| 329 | been armed out. |
---|
| 330 | */ |
---|
| 331 | void Weapon::deactivate() |
---|
| 332 | {} |
---|
| 333 | |
---|
| 334 | /** |
---|
| 335 | \brief asks if the current weapon is active |
---|
| 336 | \returns true if it the weapon is active |
---|
| 337 | */ |
---|
| 338 | bool Weapon::isActive() |
---|
| 339 | {} |
---|
| 340 | |
---|
| 341 | |
---|
| 342 | |
---|
| 343 | |
---|
[3576] | 344 | |
---|
| 345 | |
---|
| 346 | /** |
---|
[3575] | 347 | \brief is called, when the weapon gets hit (=collide with something) |
---|
| 348 | \param from which entity it is been hit |
---|
| 349 | \param where it is been hit |
---|
| 350 | |
---|
| 351 | this may not be used, since it would make the game relay complicated when one |
---|
| 352 | can destroy the weapons of enemies or vice versa. |
---|
| 353 | */ |
---|
[3578] | 354 | void Weapon::hit (WorldEntity* entity, Vector* position) |
---|
[3575] | 355 | {} |
---|
| 356 | |
---|
[3577] | 357 | |
---|
[3575] | 358 | /** |
---|
| 359 | \brief is called, when the weapon is destroyed |
---|
| 360 | |
---|
| 361 | this is in conjunction with the hit function, so when a weapon is able to get |
---|
| 362 | hit, it can also be destoryed. |
---|
| 363 | */ |
---|
| 364 | void Weapon::destroy () |
---|
| 365 | {} |
---|
| 366 | |
---|
| 367 | |
---|
| 368 | /** |
---|
[3577] | 369 | \brief tick signal for time dependent/driven stuff |
---|
| 370 | */ |
---|
| 371 | void Weapon::tick (float time) |
---|
| 372 | {} |
---|
| 373 | |
---|
| 374 | |
---|
| 375 | /** |
---|
[3583] | 376 | \brief is called, when there is no fire button pressed |
---|
| 377 | */ |
---|
| 378 | void Weapon::weaponIdle() |
---|
| 379 | {} |
---|
| 380 | |
---|
| 381 | |
---|
| 382 | /** |
---|
[3575] | 383 | \brief this will draw the weapon |
---|
| 384 | */ |
---|
[3573] | 385 | void Weapon::draw () |
---|
[3583] | 386 | {} |
---|
[3575] | 387 | |
---|