Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/world_entities/weapons/weapon_manager.cc @ 4969

Last change on this file since 4969 was 4969, checked in by bensch, 19 years ago

orxonox/trunk: ability to set a Slots Direction

File size: 10.3 KB
RevLine 
[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
19#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON
20
21#include "weapon_manager.h"
22#include "weapon.h"
[4834]23#include "crosshair.h"
[4828]24
[4834]25#include "load_param.h"
26#include "factory.h"
[4826]27#include "vector.h"
28#include "list.h"
[4837]29#include "t_animation.h"
[4953]30#include "null_parent.h"
[4826]31
32using namespace std;
33
34
35/**
[4832]36 * this initializes the weaponManager for a given nnumber of weapon slots
[4826]37 * @param number of weapon slots of the model/ship <= 8 (limitied)
38 */
[4953]39WeaponManager::WeaponManager(PNode* parent)
[4826]40{
[4833]41  this->init();
[4953]42  this->setParent(parent);
[4826]43}
44
[4949]45WeaponManager::WeaponManager(const TiXmlElement* root)
46{
47  this->init();
48  this->loadParams(root);
49}
[4826]50
[4833]51/**
52 * Destroys a WeaponManager
53 */
[4826]54WeaponManager::~WeaponManager()
55{
[4834]56  // crosshair being a PNode it must not be deleted (this is because PNodes delete themselves.)
57  //delete this->crosshair;
[4826]58}
59
[4834]60/**
61 * initializes the WeaponManager
62 */
[4833]63void WeaponManager::init()
64{
65  this->setClassID(CL_WEAPON_MANAGER, "WeaponManager");
[4826]66
[4951]67  this->parent = NULL;
68
69  for (int i = 0; i < WM_MAX_CONFIGS; i++)
70    for (int j = 0; j < WM_MAX_SLOTS; j++)
71      this->configs[i][j] = NULL;
72
73  for (int i = 0; i < WM_MAX_SLOTS; i++)
[4833]74  {
[4959]75    this->currentSlotConfig[i].capability = WTYPE_ALL;
[4951]76    this->currentSlotConfig[i].currentWeapon = NULL;
77    this->currentSlotConfig[i].nextWeapon = NULL;
[4833]78  }
[4895]79
[4951]80  for (int i = 0; i < WM_MAX_LOADED_WEAPONS; i++)
81    this->availiableWeapons[i] = NULL;
[4895]82
83
[4951]84  this->currentConfigID = 0;
85  this->slotCount = 2;
86  this->weaponChange;
[4895]87
[4951]88  // CROSSHAIR INITIALISATION
[4834]89  this->crosshair = new Crosshair();
[4837]90
91  this->crossHairSizeAnim = new tAnimation<Crosshair>(this->crosshair, &Crosshair::setSize);
92  this->crossHairSizeAnim->setInfinity(ANIM_INF_REWIND);
93  this->crossHairSizeAnim->addKeyFrame(50, .1, ANIM_LINEAR);
94  this->crossHairSizeAnim->addKeyFrame(100, .05, ANIM_LINEAR);
95  this->crossHairSizeAnim->addKeyFrame(50, .01, ANIM_LINEAR);
[4834]96}
[4833]97
[4834]98/**
[4837]99 * loads the settings of the WeaponManager
[4834]100 * @param root the XML-element to load from
101 */
102void WeaponManager::loadParams(const TiXmlElement* root)
103{
104  static_cast<BaseObject*>(this)->loadParams(root);
[4951]105/*
[4834]106  LoadParam<WeaponManager>(root, "slot-count", this, &WeaponManager::setSlotCount)
107      .describe("how many slots(cannons) the WeaponManager can handle");
108
109  LOAD_PARAM_START_CYCLE;
110
111  LoadParam<WeaponManager>(root, "Weapons", this, &WeaponManager::loadWeapons)
112      .describe("loads Weapons");
113      // LoadParam<WeaponManager>(root, "Weapon", this, &WeaponManager::addWeapon);
114
[4951]115  LOAD_PARAM_END_CYCLE;*/
[4833]116}
117
[4826]118/**
[4834]119 * loads a Weapon onto the WeaponManager
120 * @param root the XML-element to load the Weapons from
121 */
122void WeaponManager::loadWeapons(const TiXmlElement* root)
123{
124  LOAD_PARAM_START_CYCLE;
125
126  Weapon* newWeapon = dynamic_cast<Weapon*>(Factory::getFirst()->fabricate(element));
127
128
129
130  LOAD_PARAM_END_CYCLE;
131}
132
[4953]133void WeaponManager::setParent(PNode* parent)
134{
135  if (parent == NULL)
136    parent = NullParent::getInstance();
137  this->parent = parent;
138  if (this->parent != NULL)
139  {
140    for (int i = 0; i < WM_MAX_SLOTS; i++)
141      this->parent->addChild(&this->currentSlotConfig[i].position);
142  }
[4834]143
[4953]144}
145
[4834]146/**
147 * sets the number of Slots the WeaponManager has
[4926]148 * @param slotCount the number of slots
[4834]149 */
[4951]150void WeaponManager::setSlotCount(unsigned int slotCount)
[4834]151{
[4951]152  if (slotCount <= WM_MAX_SLOTS)
153    this->slotCount = slotCount;
154  else
155    this->slotCount = WM_MAX_SLOTS;
[4834]156}
157
[4953]158void WeaponManager::setSlotPosition(int slot, const Vector& position)
159{
160  if (slot < this->slotCount)
161    this->currentSlotConfig[slot].position.setRelCoor(position);
162}
163
[4969]164void WeaponManager::setSlotDirection(int slot, const Quaternion& rotation)
165{
166  if (slot < this->slotCount)
167    this->currentSlotConfig[slot].position.setRelDir(rotation);
168}
169
170
[4834]171/**
[4832]172 * adds a weapon to the selected weaponconfiguration into the selected slot
[4826]173 * @param the weapon to add
174 * @param an identifier for the slot: number between 0..7 if not specified: slotID=next free slot
175 * @param an identifier for the weapon configuration, number between 0..3
[4832]176 *
177 * if you add explicitly a weapon at config:n, slot:m, the weapon placed at this location will be
[4906]178 * replaced by the weapon specified. if you use the WM_FREE_SLOT, the manager will look for a free
[4832]179 * slot in this weaponconfiguration. if there is non, the weapon won't be added and there will be
180 * a error message.
[4826]181 */
[4832]182void WeaponManager::addWeapon(Weapon* weapon, int configID, int slotID)
[4826]183{
[4951]184  if (unlikely(configID >= WM_MAX_CONFIGS || slotID >= (int)this->slotCount))
[4826]185  {
[4951]186    PRINTF(2)("Slot %d of config %d is not availiabe\n", slotID, configID);
187    return;
188  }
189
190  if (this->configs[configID][slotID] != NULL && configID > 0 && slotID > 0)
191    PRINTF(3)("Weapon-slot %d/%d of %s already poulated, overwriting\n", configID, slotID, this->getName());
192
193  if (slotID == -1) // WM_FREE_SLOT
194  {
195    slotID = this->getNextFreeSlot(configID);
196    if( slotID < 0 || slotID >= this->slotCount)
[4826]197    {
198      PRINTF(0)("There is no free slot in this WeaponConfig to dock this weapon at! Aborting\n");
199      return;
200    }
201  }
[4953]202
203  //! @todo check if the weapon is already assigned to another config in another slot
204
[4951]205  this->configs[configID][slotID] = weapon;
[4953]206  if (this->parent != NULL)
207    weapon->setParent(parent);
[4826]208  PRINTF(3)("Added a new Weapon to the WeaponManager: config %i/ slot %i\n", configID, slotID);
209}
210
[4834]211/**
[4954]212 * sets the capabilities of a Slot
213 * @param slot the slot to set the capability
214 * @param slotCapability the capability @see WM_SlotCapability
215 */
216void WeaponManager::setSlotCapability(int slot, long slotCapability)
217{
218  if (slot > slotCount)
219    return;
220  this->currentSlotConfig[slot].capability = slotCapability;
221}
222
223
224/**
[4834]225 * removes a Weapon from the WeaponManager
[4954]226 *
227 * !! The weapon must be inactive before you can delete it,    !!
228 * !! because it will still be deactivated (if it is selected) !!
[4834]229 */
[4826]230void WeaponManager::removeWeapon(Weapon* weapon, int configID)
231{
[4954]232  if (weapon == NULL)
233    return;
234  if (configID < 0)
235  {
236    for (int j = 0; j < WM_MAX_SLOTS; j++)
237    {
238      for (int i = 0; i < WM_MAX_CONFIGS; i++)
239      {
240        if (this->configs[i][j] == weapon)
241          this->configs[i][j] = NULL;
242      }
243      if (this->currentSlotConfig[j].currentWeapon == weapon)
244      {
245        this->currentSlotConfig[j].nextWeapon = NULL;
246      }
247    }
248  }
[4826]249}
250
251
252/**
[4832]253 * changes to the next weapon configuration
[4826]254 */
[4954]255void WeaponManager::nextWeaponConfig()
[4826]256{
[4951]257  ++this->currentConfigID;
258  if (this->currentConfigID >= WM_MAX_CONFIGS)
259    this->currentConfigID = 0;
[4952]260  this->changeWeaponConfig(this->currentConfigID);
261}
[4826]262
[4953]263/**
264 * changes to the previous configuration
265 */
[4952]266void WeaponManager::previousWeaponConfig()
267{
268  --this->currentConfigID;
269  if (this->currentConfigID < 0)
270    this->currentConfigID = WM_MAX_CONFIGS -1;
271  this->changeWeaponConfig(this->currentConfigID);
272}
273
[4953]274/**
275 * change to a desired configuration
276 * @param weaponConfig the configuration to jump to.
277 */
[4952]278void WeaponManager::changeWeaponConfig(int weaponConfig)
279{
280  this->currentConfigID = weaponConfig;
281  PRINTF(4)("Changing weapon configuration: to %i\n", this->currentConfigID);
[4951]282  for (int i = 0; i < WM_MAX_SLOTS; i++)
[4826]283  {
[4951]284    this->currentSlotConfig[i].nextWeapon = this->configs[currentConfigID][i];
[4953]285    if (this->currentSlotConfig[i].currentWeapon != this->currentSlotConfig[i].nextWeapon)
286    {
287      if (this->currentSlotConfig[i].currentWeapon != NULL)
288        (this->currentSlotConfig[i].currentWeapon->requestAction(WA_DEACTIVATE));
289      if (this->currentSlotConfig[i].nextWeapon != NULL && this->currentSlotConfig[i].nextWeapon->isActive())
290        this->currentSlotConfig[i].nextWeapon = NULL;
291    }
[4951]292  }
[4826]293}
294
295
296/**
[4832]297 * triggers fire of all weapons in the current weaponconfig
[4826]298 */
[4832]299void WeaponManager::fire()
[4826]300{
301  Weapon* firingWeapon;
[4951]302  for(int i = 0; i < this->slotCount; i++)
[4826]303  {
[4951]304    firingWeapon = this->currentSlotConfig[i].currentWeapon;
[4885]305    if( firingWeapon != NULL) firingWeapon->requestAction(WA_SHOOT);
[4826]306  }
[4837]307  this->crosshair->setRotationSpeed(500);
308  this->crossHairSizeAnim->replay();
[4826]309}
310
311
312/**
[4832]313 * triggers tick of all weapons in the current weaponconfig
314 * @param second passed since last tick
[4826]315 */
[4833]316void WeaponManager::tick(float dt)
[4826]317{
[4951]318  Weapon* tickWeapon;
319
320  // all weapons
321  for(int i = 0; i < this->slotCount; i++)
[4826]322  {
[4951]323
324    tickWeapon = this->currentSlotConfig[i].currentWeapon;
325    if (tickWeapon != this->currentSlotConfig[i].nextWeapon) // if no change occures
326    {
327      if (tickWeapon != NULL && tickWeapon->isActive())
328      {
329        tickWeapon->requestAction(WA_DEACTIVATE);
330      }
331      else
332      {
333        tickWeapon = this->currentSlotConfig[i].currentWeapon = this->currentSlotConfig[i].nextWeapon;
334        if (tickWeapon != NULL)
[4953]335        {
[4951]336          tickWeapon->requestAction(WA_ACTIVATE);
[4953]337          tickWeapon->setParent(&this->currentSlotConfig[i].position);
338        }
[4951]339      }
340    }
341
342    if( tickWeapon != NULL && tickWeapon->isActive())
343      tickWeapon->tickW(dt);
[4826]344  }
[4834]345
346  crosshair->setRotationSpeed(5);
[4826]347}
348
349
350/**
[4832]351 * triggers draw of all weapons in the current weaponconfig
[4826]352 */
[4951]353void WeaponManager::draw() const
[4826]354{
[4951]355  Weapon* drawWeapon;
356  for (int i = 0; i < this->slotCount; i++)
[4826]357  {
[4951]358    drawWeapon = this->currentSlotConfig[i].currentWeapon;
359    if( drawWeapon != NULL && drawWeapon->isVisible())
360      drawWeapon->draw();
[4826]361  }
362}
363
364
365/**
[4832]366 * private gets the next free slot in a certain weaponconfig
367 * @param the selected weaponconfig
[4826]368 */
[4832]369int WeaponManager::getNextFreeSlot(int configID)
[4826]370{
[4951]371  for( int i = 0; i < this->slotCount; ++i)
[4826]372  {
[4951]373    if( this->configs[configID][i] == NULL)
[4826]374      return i;
375  }
376  return -1;
377}
378
[4951]379
380
[4953]381/**
382 * outputs some nice debug information about the WeaponManager
383 */
[4951]384void WeaponManager::debug() const
385{
386  PRINT(3)("WeaponManager Debug Information\n");
387  PRINT(3)("-------------------------------\n");
388  PRINT(3)("current Config is %d\n", this->currentConfigID);
389  for (int i = 0; i < WM_MAX_CONFIGS; i++)
390  {
391    PRINT(3)("Listing Weapons in Configuration %d\n", i);
392    for (int j = 0; j < WM_MAX_SLOTS; j++)
393    {
394      if (this->configs[i][j] != NULL)
395        PRINT(3)("Slot %d loaded a %s\n", j, this->configs[i][j]->getClassName());
396    }
397  }
398}
Note: See TracBrowser for help on using the repository browser.