Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: messed things up a bit, try fixing

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