Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/sound_engine/src/world_entities/weapon.cc @ 3898

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

orxonox/branches/sound_engine: sound works sloppy, test it

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