Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/world_entities/weapon.cc @ 4617

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

orxonox/trunk: setClassID implemented in all files

File size: 8.7 KB
Line 
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
18#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON
19
20#include "weapon.h"
21#include "stdincl.h"
22#include "world_entity.h"
23#include "vector.h"
24#include "model.h"
25#include "projectile.h"
26#include "list.h"
27#include "world.h"
28
29using namespace std;
30
31
32
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*/
37WeaponManager::WeaponManager(int nrOfSlots)
38{
39  this->setClassID(CL_WEAPON_MANAGER, "WeaponManager");
40
41  for(int i = 0; i < W_MAX_CONFIGS; ++i)
42    {
43      this->configs[i].bUsed = false;
44      for(int j = 0; j < W_MAX_SLOTS; ++j)
45        this->configs[i].slots[j] = NULL;
46    }
47  this->nrOfSlots = nrOfSlots;
48  this->currConfID = W_CONFIG0;
49}
50
51
52WeaponManager::~WeaponManager()
53{
54  /*
55     i dont have to delete the weapons itself, because they are
56     worldentities and therefore in the entities list of the world.
57     world will clean them up for me
58  */
59  for(int i = 0; i < W_MAX_CONFIGS; ++i)
60    {
61      this->configs[i].bUsed = false;
62      for(int j = 0; j < W_MAX_SLOTS; ++j)
63        this->configs[i].slots[j] = NULL;
64    }
65}
66
67
68/**
69   \brief adds a weapon to the selected weaponconfiguration into the selected slot
70   \param the weapon to add
71   \param an identifier for the slot: number between 0..7 if not specified: slotID=next free slot
72   \param an identifier for the weapon configuration, number between 0..3
73
74   if you add explicitly a weapon at config:n, slot:m, the weapon placed at this location will be
75   replaced by the weapon specified. if you use the W_FREE_SLOT, the manager will look for a free
76   slot in this weaponconfiguration. if there is non, the weapon won't be added and there will be
77   a error message.
78*/
79void WeaponManager::addWeapon(Weapon* weapon, int configID, int slotID)
80{
81  if( slotID == W_FREE_SLOT)
82    {
83      int freeSlot = this->getNextFreeSlot( configID);
84      if( freeSlot < 0 || freeSlot >= this->nrOfSlots)
85        {
86          PRINTF(0)("There is no free slot in this WeaponConfig to dock this weapon at! Aborting\n");
87          return;
88        }
89      PRINTF(3)("Added new Weapon to Config:%i/Slot:%i\n", configID, freeSlot);
90      this->configs[configID].bUsed = true;
91      this->configs[configID].slots[freeSlot] = weapon;
92      return;
93    }
94  this->configs[configID].bUsed = true;
95  this->configs[configID].slots[slotID] = weapon;
96  PRINTF(3)("Added a new Weapon to the WeaponManager: config %i/ slot %i\n", configID, slotID);
97}
98
99
100void WeaponManager::removeWeapon(Weapon* weapon, int configID)
101{
102  /* empty */
103}
104
105
106/**
107   \brief changes to the next weapon configuration
108
109   if there are multiple weapon configurations defined by the manager, use this to switch between them
110   this function will deactivate the weapons first, change the config and reactivate them later
111*/
112void WeaponManager::nextWeaponConf()
113{
114  PRINTF(4)("Changing weapon configuration: from %i to next\n", this->currConfID);
115
116  int i, lastConfID;
117  lastConfID = this->currConfID;
118  for(i = this->currConfID + 1; i < W_MAX_CONFIGS && !this->configs[i].bUsed; ++i);
119  if( i == W_MAX_CONFIGS) this->currConfID = W_CONFIG0;
120  else this->currConfID = i;
121
122
123  Weapon *w1, *w2;
124  for(int j = 0; j < W_MAX_SLOTS; ++j)
125    {
126      w1 = this->configs[lastConfID].slots[j];
127      w2 = this->configs[this->currConfID].slots[j];
128
129      if( w1 == w2)
130        {
131          printf("no need for change\n");
132        }
133      else
134        {
135        if( w1 != NULL )
136          {
137            w1->deactivate();
138            printf("deactivating %i,%i\n", j,lastConfID);
139          }
140        if( w2 != NULL)
141          {
142            w2->activate();
143            printf("activating %i,%i\n", j, this->currConfID);
144          }
145        }
146    }
147}
148
149
150
151/**
152   \brief triggers fire of all weapons in the current weaponconfig
153*/
154void WeaponManager::fire()
155{
156  Weapon* firingWeapon;
157  for(int i = 0; i < W_MAX_SLOTS; ++i)
158    {
159      firingWeapon = this->configs[this->currConfID].slots[i];
160      if( firingWeapon != NULL) firingWeapon->fire();
161    }
162}
163
164
165/**
166   \brief triggers tick of all weapons in the current weaponconfig
167   \param second passed since last tick
168*/
169void WeaponManager::tick(float sec)
170{
171  Weapon* w;
172  for(int i = 0; i < W_MAX_SLOTS; ++i)
173    {
174      w = this->configs[this->currConfID].slots[i];
175      if( w != NULL) w->tick(sec);
176    }
177}
178
179
180/**
181   \brief triggers draw of all weapons in the current weaponconfig
182*/
183void WeaponManager::draw()
184{
185  Weapon* w;
186  for(int i = 0; i < W_MAX_SLOTS; ++i)
187    {
188      w = this->configs[this->currConfID].slots[i];
189      if( w != NULL) w->draw();
190    }
191}
192
193
194/**
195   \brief private gets the next free slot in a certain weaponconfig
196   \param the selected weaponconfig
197*/
198int WeaponManager::getNextFreeSlot(int configID)
199{
200  for( int i = 0; i < W_MAX_SLOTS; ++i)
201    {
202      if( this->configs[configID].slots[i] == NULL)
203        return i;
204    }
205  return -1;
206}
207
208
209
210
211
212
213/**
214   \brief standard constructor
215
216   creates a new weapon
217*/
218Weapon::Weapon (PNode* parent, const Vector& coordinate, const Quaternion& direction)
219  : WorldEntity()
220{
221  parent->addChild(this, PNODE_ALL);
222  this->setRelCoor(coordinate);
223  this->setRelDir(direction);
224  WorldInterface* wi = WorldInterface::getInstance();
225  this->worldEntities = wi->getEntityList();
226
227  this->objectComponent1 = NULL;
228  this->objectComponent2 = NULL;
229  this->objectComponent3 = NULL;
230
231  this->animation1 = NULL;
232  this->animation2 = NULL;
233  this->animation3 = NULL;
234}
235
236
237/**
238   \brief standard deconstructor
239*/
240Weapon::~Weapon ()
241{
242  // model will be deleted from WorldEntity-destructor
243  //this->worldEntities = NULL;
244
245  /* dont delete objectComponentsX here, they will be killed when the pnodes are cleaned out */
246
247  /* all animations are deleted via the animation player*/
248}
249
250
251/**
252    \brief enables the weapon
253
254    a weapon can be enabled/disabled because of various reasons. if a weapon is
255    been enabled, it can interact in a world. elswhere it wont react to any
256    action.
257*/
258void Weapon::enable()
259{
260  this->enabled = true;
261}
262
263
264/**
265    \brief disables the weapon
266
267    a weapon can be enabled/disabled because of various reasons. if a weapon is
268    been enabled, it can interact in a world. elswhere it wont react to any
269    action.
270*/
271void Weapon::disable()
272{
273  this->enabled = false;
274}
275
276
277/**
278    \brief checks if the weapon is enabled
279    \returns true if enabled
280
281    a weapon can be ebabled/disabled because of various reasons. if a weapon is
282    been enabled, it can interact in a world. elswhere it wont react to any
283    action.
284*/
285bool Weapon::isEnabled()
286{
287  return this->enabled;
288}
289
290
291/**
292   \brief sets a new projectile to the weapon
293   \param new projectile for this weapon
294
295   weapon an projectile are independent, so you can combine them as you want
296*/
297void Weapon::setProjectile(Projectile* projectile)
298{
299  this->projectile = projectile;
300}
301
302
303/**
304   \brief sets a new projectile to the weapon
305   \returns the current projectile of this weapon
306
307   weapon an projectile are independent, so you can combine them as you want
308*/
309Projectile* Weapon::getProjectile()
310{
311  return this->projectile;
312}
313
314
315/**
316   \brief this activates the weapon
317
318   This is needed, since there can be more than one weapon on a ship. the
319   activation can be connected with an animation. for example the weapon is
320   been armed out.
321*/
322void Weapon::activate()
323{}
324
325
326/**
327   \brief this deactivates the weapon
328
329   This is needed, since there can be more than one weapon on a ship. the
330   activation can be connected with an animation. for example the weapon is
331   been armed out.
332*/
333void Weapon::deactivate()
334{}
335
336/**
337   \brief asks if the current weapon is active
338   \returns true if it the weapon is active
339*/
340bool Weapon::isActive()
341{}
342
343
344
345
346
347
348/**
349   \brief is called, when the weapon gets hit (=collide with something)
350   \param from which entity it is been hit
351   \param where it is been hit
352
353   this may not be used, since it would make the game relay complicated when one
354   can destroy the weapons of enemies or vice versa.
355*/
356void Weapon::hit (WorldEntity* entity, Vector* position)
357{}
358
359
360/**
361   \brief is called, when the weapon is destroyed
362
363   this is in conjunction with the hit function, so when a weapon is able to get
364   hit, it can also be destoryed.
365*/
366void Weapon::destroy ()
367{}
368
369
370/**
371   \brief tick signal for time dependent/driven stuff
372*/
373void Weapon::tick (float time)
374{}
375
376
377/**
378   \brief is called, when there is no fire button pressed
379*/
380void Weapon::weaponIdle()
381{}
382
383
384/**
385   \brief this will draw the weapon
386*/
387void Weapon::draw ()
388{}
389
Note: See TracBrowser for help on using the repository browser.