Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: weapon manager now works in player, cleaning up the rest

File size: 6.5 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
58void WeaponManager::addWeapon(Weapon* weapon, int slotID, int configID) 
59{
60  if( slotID == W_FREE_SLOT)
61    {
62      int freeSlot = this->getNextFreeSlot();
63      if( freeSlot < 0 || freeSlot >= this->nrOfSlots)
64        {
65          PRINTF(0)("There is no free slot in this WeaponConfig to dock this weapon at! Aborting\n");
66          return;
67        }
68      PRINTF(3)("Added new Weapon to Config:%i/Slot:%i\n", configID, freeSlot);
69      this->configs[configID].slots[freeSlot] = weapon;
70      return;
71    }
72  this->configs[configID].slots[slotID] = weapon;
73  PRINTF(3)("Added a new Weapon to the WeaponManager: config %i/ slot %i\n", configID, slotID);
74}
75
76
77void WeaponManager::nextWeaponConf() 
78{
79  int i;
80  for(i = this->currConfID; i < W_MAX_CONFIGS && !this->configs[i].bUsed; ++i);
81  if( i == W_MAX_CONFIGS) this->currConfID = W_CONFIG0;
82  else this->currConfID = i; 
83}
84
85
86int WeaponManager::getNextFreeSlot()
87{
88  for( int i = 0; i < W_MAX_SLOTS; ++i)
89    {
90      if( this->configs[this->currConfID].slots[i] == NULL)
91        return i;
92    }
93  return -1;
94}
95
96
97void WeaponManager::fire()
98{
99  Weapon* firingWeapon;
100  for(int i = 0; i < W_MAX_SLOTS; ++i)
101    {
102      firingWeapon = this->configs[this->currConfID].slots[i];
103      if( firingWeapon != NULL) firingWeapon->fire();
104    }
105}
106
107
108/**
109   \brief standard constructor
110
111   creates a new weapon
112*/
113Weapon::Weapon (PNode* parent, Vector* coordinate, Quaternion* direction) 
114  : WorldEntity()
115{
116  parent->addChild(this, PNODE_ALL);
117  this->setRelCoor(*coordinate);
118  this->setRelDir(*direction);
119  WorldInterface* wi = WorldInterface::getInstance();
120  this->worldEntities = wi->getEntityList();
121}
122
123
124/**
125   \brief standard deconstructor
126*/
127Weapon::~Weapon () 
128{
129  // model will be deleted from WorldEntity-destructor
130}
131
132
133/**
134    \brief enables the weapon
135
136    a weapon can be enabled/disabled because of various reasons. if a weapon is
137    been enabled, it can interact in a world. elswhere it wont react to any
138    action.
139*/
140void Weapon::enable()
141{
142  this->enabled = true;
143}
144
145
146/**
147    \brief disables the weapon
148
149    a weapon can be enabled/disabled because of various reasons. if a weapon is
150    been enabled, it can interact in a world. elswhere it wont react to any
151    action.
152*/
153void Weapon::disable()
154{
155  this->enabled = false;
156}
157
158
159/**
160    \brief checks if the weapon is enabled
161    \returns true if enabled
162
163    a weapon can be ebabled/disabled because of various reasons. if a weapon is
164    been enabled, it can interact in a world. elswhere it wont react to any
165    action.
166*/
167bool Weapon::isEnabled()
168{
169  return this->enabled;
170}
171
172
173/**
174   \brief sets a new projectile to the weapon
175   \param new projectile for this weapon
176
177   weapon an projectile are independent, so you can combine them as you want
178*/
179void Weapon::setProjectile(Projectile* projectile)
180{
181  this->projectile = projectile;
182}
183
184
185/**
186   \brief sets a new projectile to the weapon
187   \returns the current projectile of this weapon
188
189   weapon an projectile are independent, so you can combine them as you want
190*/
191Projectile* Weapon::getProjectile()
192{
193  return this->projectile;
194}
195
196
197/**
198   \brief this activates the weapon
199
200   This is needed, since there can be more than one weapon on a ship. the
201   activation can be connected with an animation. for example the weapon is
202   been armed out.
203*/
204void Weapon::activate()
205{}
206
207
208/**
209   \brief this deactivates the weapon
210
211   This is needed, since there can be more than one weapon on a ship. the
212   activation can be connected with an animation. for example the weapon is
213   been armed out.
214*/
215void Weapon::deactivate()
216{}
217
218/**
219   \brief asks if the current weapon is active
220   \returns true if it the weapon is active
221*/
222bool Weapon::isActive()
223{}
224
225/**
226   \brief sets a weapon idle time
227   \param idle time in ms
228
229   a weapon idle time is the time spend after a shoot until the weapon can
230   shoot again
231*/
232void Weapon::setWeaponIdleTime(float time)
233{
234  this->idleTime = time;
235}
236
237/**
238   \brief gets the weapon idle time
239   \returns idle time in ms
240
241   a weapon idle time is the time spend after a shoot until the weapon can
242   shoot again
243*/
244float Weapon::getWeaponIdleTime(void)
245{
246  return this->idleTime;
247}
248
249/**
250   \brief checks if the idle time is elapsed
251   \return true if time is elapsed
252
253   a weapon idle time is the time spend after a shoot until the weapon can
254   shoot again
255*/
256bool Weapon::hasWeaponIdleTimeElapsed(void)
257{
258  return (this->localTime>this->idleTime)?true:false;
259}
260
261
262/**
263   \brief fires the weapon
264   
265   this is called from the player.cc, when fire-button is been pushed
266*/
267void Weapon::fire()
268{}
269
270
271/**
272   \brief is called, when the weapon gets hit (=collide with something)
273   \param from which entity it is been hit
274   \param where it is been hit
275
276   this may not be used, since it would make the game relay complicated when one
277   can destroy the weapons of enemies or vice versa.
278*/
279void Weapon::hit (WorldEntity* entity, Vector* position) 
280{}
281
282
283/**
284   \brief is called, when the weapon is destroyed
285
286   this is in conjunction with the hit function, so when a weapon is able to get
287   hit, it can also be destoryed.
288*/
289void Weapon::destroy () 
290{}
291
292
293/**
294   \brief tick signal for time dependent/driven stuff
295*/
296void Weapon::tick (float time) 
297{}
298
299
300/**
301   \brief is called, when there is no fire button pressed
302*/
303void Weapon::weaponIdle()
304{}
305
306
307/**
308   \brief this will draw the weapon
309*/
310void Weapon::draw () 
311{}
312
Note: See TracBrowser for help on using the repository browser.