Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3881 was 3881, checked in by patrick, 19 years ago

orxonox/trunk: weapon change enhanced. weapon handling is now better and safer

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
211
212/**
213   \brief standard deconstructor
214*/
215Weapon::~Weapon () 
216{
217  // model will be deleted from WorldEntity-destructor
218}
219
220
221/**
222    \brief enables the weapon
223
224    a weapon can be enabled/disabled because of various reasons. if a weapon is
225    been enabled, it can interact in a world. elswhere it wont react to any
226    action.
227*/
228void Weapon::enable()
229{
230  this->enabled = true;
231}
232
233
234/**
235    \brief disables the weapon
236
237    a weapon can be enabled/disabled because of various reasons. if a weapon is
238    been enabled, it can interact in a world. elswhere it wont react to any
239    action.
240*/
241void Weapon::disable()
242{
243  this->enabled = false;
244}
245
246
247/**
248    \brief checks if the weapon is enabled
249    \returns true if enabled
250
251    a weapon can be ebabled/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*/
255bool Weapon::isEnabled()
256{
257  return this->enabled;
258}
259
260
261/**
262   \brief sets a new projectile to the weapon
263   \param new projectile for this weapon
264
265   weapon an projectile are independent, so you can combine them as you want
266*/
267void Weapon::setProjectile(Projectile* projectile)
268{
269  this->projectile = projectile;
270}
271
272
273/**
274   \brief sets a new projectile to the weapon
275   \returns the current projectile of this weapon
276
277   weapon an projectile are independent, so you can combine them as you want
278*/
279Projectile* Weapon::getProjectile()
280{
281  return this->projectile;
282}
283
284
285/**
286   \brief this activates the weapon
287
288   This is needed, since there can be more than one weapon on a ship. the
289   activation can be connected with an animation. for example the weapon is
290   been armed out.
291*/
292void Weapon::activate()
293{}
294
295
296/**
297   \brief this deactivates the weapon
298
299   This is needed, since there can be more than one weapon on a ship. the
300   activation can be connected with an animation. for example the weapon is
301   been armed out.
302*/
303void Weapon::deactivate()
304{}
305
306/**
307   \brief asks if the current weapon is active
308   \returns true if it the weapon is active
309*/
310bool Weapon::isActive()
311{}
312
313
314
315
316
317
318/**
319   \brief is called, when the weapon gets hit (=collide with something)
320   \param from which entity it is been hit
321   \param where it is been hit
322
323   this may not be used, since it would make the game relay complicated when one
324   can destroy the weapons of enemies or vice versa.
325*/
326void Weapon::hit (WorldEntity* entity, Vector* position) 
327{}
328
329
330/**
331   \brief is called, when the weapon is destroyed
332
333   this is in conjunction with the hit function, so when a weapon is able to get
334   hit, it can also be destoryed.
335*/
336void Weapon::destroy () 
337{}
338
339
340/**
341   \brief tick signal for time dependent/driven stuff
342*/
343void Weapon::tick (float time) 
344{}
345
346
347/**
348   \brief is called, when there is no fire button pressed
349*/
350void Weapon::weaponIdle()
351{}
352
353
354/**
355   \brief this will draw the weapon
356*/
357void Weapon::draw () 
358{}
359
Note: See TracBrowser for help on using the repository browser.