Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: many loadParams

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