Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: the weaponconfiguration can now be changed by pressing the 'm' key.

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