Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: the new WeaponManager is online :)

File size: 8.1 KB
Line 
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
14   co-programmer: Benjamin Grauer
15
16   2005-07-24: Benjamin Grauer: restructurate, so it can handle the new Weapons.
17*/
18
19#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON
20
21#include "weapon_manager.h"
22#include "weapon.h"
23#include "crosshair.h"
24
25#include "load_param.h"
26#include "factory.h"
27#include "vector.h"
28#include "list.h"
29#include "t_animation.h"
30
31using namespace std;
32
33
34/**
35 * this initializes the weaponManager for a given nnumber of weapon slots
36 * @param number of weapon slots of the model/ship <= 8 (limitied)
37 */
38WeaponManager::WeaponManager(unsigned int slotCount)
39{
40  this->init();
41  this->setSlotCount(slotCount);
42}
43
44WeaponManager::WeaponManager(const TiXmlElement* root)
45{
46  this->init();
47  this->loadParams(root);
48}
49
50/**
51 * Destroys a WeaponManager
52 */
53WeaponManager::~WeaponManager()
54{
55  // crosshair being a PNode it must not be deleted (this is because PNodes delete themselves.)
56  //delete this->crosshair;
57}
58
59/**
60 * initializes the WeaponManager
61 */
62void WeaponManager::init()
63{
64  this->setClassID(CL_WEAPON_MANAGER, "WeaponManager");
65
66  this->parent = NULL;
67
68  for (int i = 0; i < WM_MAX_CONFIGS; i++)
69    for (int j = 0; j < WM_MAX_SLOTS; j++)
70      this->configs[i][j] = NULL;
71
72  for (int i = 0; i < WM_MAX_SLOTS; i++)
73  {
74    this->currentSlotConfig[i].capability = WM_SLOTC_ALL;
75    this->currentSlotConfig[i].currentWeapon = NULL;
76    this->currentSlotConfig[i].nextWeapon = NULL;
77  }
78
79  for (int i = 0; i < WM_MAX_LOADED_WEAPONS; i++)
80    this->availiableWeapons[i] = NULL;
81
82
83  this->currentConfigID = 0;
84  this->slotCount = 2;
85  this->weaponChange;
86
87  // CROSSHAIR INITIALISATION
88  this->crosshair = new Crosshair();
89
90  this->crossHairSizeAnim = new tAnimation<Crosshair>(this->crosshair, &Crosshair::setSize);
91  this->crossHairSizeAnim->setInfinity(ANIM_INF_REWIND);
92  this->crossHairSizeAnim->addKeyFrame(50, .1, ANIM_LINEAR);
93  this->crossHairSizeAnim->addKeyFrame(100, .05, ANIM_LINEAR);
94  this->crossHairSizeAnim->addKeyFrame(50, .01, ANIM_LINEAR);
95}
96
97/**
98 * loads the settings of the WeaponManager
99 * @param root the XML-element to load from
100 */
101void WeaponManager::loadParams(const TiXmlElement* root)
102{
103  static_cast<BaseObject*>(this)->loadParams(root);
104/*
105  LoadParam<WeaponManager>(root, "slot-count", this, &WeaponManager::setSlotCount)
106      .describe("how many slots(cannons) the WeaponManager can handle");
107
108  LOAD_PARAM_START_CYCLE;
109
110  LoadParam<WeaponManager>(root, "Weapons", this, &WeaponManager::loadWeapons)
111      .describe("loads Weapons");
112      // LoadParam<WeaponManager>(root, "Weapon", this, &WeaponManager::addWeapon);
113
114  LOAD_PARAM_END_CYCLE;*/
115}
116
117/**
118 * loads a Weapon onto the WeaponManager
119 * @param root the XML-element to load the Weapons from
120 */
121void WeaponManager::loadWeapons(const TiXmlElement* root)
122{
123  LOAD_PARAM_START_CYCLE;
124
125  Weapon* newWeapon = dynamic_cast<Weapon*>(Factory::getFirst()->fabricate(element));
126
127
128
129  LOAD_PARAM_END_CYCLE;
130}
131
132
133/**
134 * sets the number of Slots the WeaponManager has
135 * @param slotCount the number of slots
136 */
137void WeaponManager::setSlotCount(unsigned int slotCount)
138{
139  if (slotCount <= WM_MAX_SLOTS)
140    this->slotCount = slotCount;
141  else
142    this->slotCount = WM_MAX_SLOTS;
143}
144
145/**
146 * adds a weapon to the selected weaponconfiguration into the selected slot
147 * @param the weapon to add
148 * @param an identifier for the slot: number between 0..7 if not specified: slotID=next free slot
149 * @param an identifier for the weapon configuration, number between 0..3
150 *
151 * if you add explicitly a weapon at config:n, slot:m, the weapon placed at this location will be
152 * replaced by the weapon specified. if you use the WM_FREE_SLOT, the manager will look for a free
153 * slot in this weaponconfiguration. if there is non, the weapon won't be added and there will be
154 * a error message.
155 */
156void WeaponManager::addWeapon(Weapon* weapon, int configID, int slotID)
157{
158  if (unlikely(configID >= WM_MAX_CONFIGS || slotID >= (int)this->slotCount))
159  {
160    PRINTF(2)("Slot %d of config %d is not availiabe\n", slotID, configID);
161    return;
162  }
163
164  if (this->configs[configID][slotID] != NULL && configID > 0 && slotID > 0)
165    PRINTF(3)("Weapon-slot %d/%d of %s already poulated, overwriting\n", configID, slotID, this->getName());
166
167  if (slotID == -1) // WM_FREE_SLOT
168  {
169    slotID = this->getNextFreeSlot(configID);
170    if( slotID < 0 || slotID >= this->slotCount)
171    {
172      PRINTF(0)("There is no free slot in this WeaponConfig to dock this weapon at! Aborting\n");
173      return;
174    }
175  }
176  this->configs[configID][slotID] = weapon;
177  PRINTF(3)("Added a new Weapon to the WeaponManager: config %i/ slot %i\n", configID, slotID);
178}
179
180/**
181 * removes a Weapon from the WeaponManager
182 */
183void WeaponManager::removeWeapon(Weapon* weapon, int configID)
184{
185  /* empty */
186}
187
188
189/**
190 * changes to the next weapon configuration
191 *
192 * if there are multiple weapon configurations defined by the manager, use this to switch between them
193 * this function will deactivate the weapons first, change the config and reactivate them later
194 */
195void WeaponManager::nextWeaponConf()
196{
197  PRINTF(4)("Changing weapon configuration: from %i to next\n", this->currentConfigID);
198
199  ++this->currentConfigID;
200  if (this->currentConfigID >= WM_MAX_CONFIGS)
201    this->currentConfigID = 0;
202
203  for (int i = 0; i < WM_MAX_SLOTS; i++)
204  {
205    this->currentSlotConfig[i].nextWeapon = this->configs[currentConfigID][i];
206    if (this->currentSlotConfig[i].currentWeapon != this->currentSlotConfig[i].nextWeapon && this->currentSlotConfig[i].currentWeapon != NULL)
207      (this->currentSlotConfig[i].currentWeapon->requestAction(WA_DEACTIVATE));
208  }
209
210  this->debug();
211}
212
213
214/**
215 * triggers fire of all weapons in the current weaponconfig
216 */
217void WeaponManager::fire()
218{
219  Weapon* firingWeapon;
220  for(int i = 0; i < this->slotCount; i++)
221  {
222    firingWeapon = this->currentSlotConfig[i].currentWeapon;
223    if( firingWeapon != NULL) firingWeapon->requestAction(WA_SHOOT);
224  }
225  this->crosshair->setRotationSpeed(500);
226  this->crossHairSizeAnim->replay();
227}
228
229
230/**
231 * triggers tick of all weapons in the current weaponconfig
232 * @param second passed since last tick
233 */
234void WeaponManager::tick(float dt)
235{
236  Weapon* tickWeapon;
237
238  // all weapons
239  for(int i = 0; i < this->slotCount; i++)
240  {
241
242    tickWeapon = this->currentSlotConfig[i].currentWeapon;
243    if (tickWeapon != this->currentSlotConfig[i].nextWeapon) // if no change occures
244    {
245      if (tickWeapon != NULL && tickWeapon->isActive())
246      {
247        tickWeapon->requestAction(WA_DEACTIVATE);
248      }
249      else
250      {
251        tickWeapon = this->currentSlotConfig[i].currentWeapon = this->currentSlotConfig[i].nextWeapon;
252        if (tickWeapon != NULL)
253          tickWeapon->requestAction(WA_ACTIVATE);
254      }
255    }
256
257    if( tickWeapon != NULL && tickWeapon->isActive())
258      tickWeapon->tickW(dt);
259  }
260
261  crosshair->setRotationSpeed(5);
262}
263
264
265/**
266 * triggers draw of all weapons in the current weaponconfig
267 */
268void WeaponManager::draw() const
269{
270  Weapon* drawWeapon;
271  for (int i = 0; i < this->slotCount; i++)
272  {
273    drawWeapon = this->currentSlotConfig[i].currentWeapon;
274    if( drawWeapon != NULL && drawWeapon->isVisible())
275      drawWeapon->draw();
276  }
277}
278
279
280/**
281 * private gets the next free slot in a certain weaponconfig
282 * @param the selected weaponconfig
283 */
284int WeaponManager::getNextFreeSlot(int configID)
285{
286  for( int i = 0; i < this->slotCount; ++i)
287  {
288    if( this->configs[configID][i] == NULL)
289      return i;
290  }
291  return -1;
292}
293
294
295
296void WeaponManager::debug() const
297{
298  PRINT(3)("WeaponManager Debug Information\n");
299  PRINT(3)("-------------------------------\n");
300  PRINT(3)("current Config is %d\n", this->currentConfigID);
301  for (int i = 0; i < WM_MAX_CONFIGS; i++)
302  {
303    PRINT(3)("Listing Weapons in Configuration %d\n", i);
304    for (int j = 0; j < WM_MAX_SLOTS; j++)
305    {
306      if (this->configs[i][j] != NULL)
307        PRINT(3)("Slot %d loaded a %s\n", j, this->configs[i][j]->getClassName());
308    }
309  }
310}
Note: See TracBrowser for help on using the repository browser.