Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/physics/src/world_entities/weapon.cc @ 4098

Last change on this file since 4098 was 3953, checked in by patrick, 20 years ago

orxonox/branches/physics: merged with trunk - with command svn merge -r 3866:HEAD

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