Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4324 was 4000, checked in by patrick, 20 years ago

orxonox/trunk: weapon animation not realy nice but working now.. :)

File size: 8.6 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 to next\n", this->currConfID);
113
114  int i, lastConfID;
115  lastConfID = this->currConfID;
116  for(i = this->currConfID + 1; i < W_MAX_CONFIGS && !this->configs[i].bUsed; ++i);
117  if( i == W_MAX_CONFIGS) this->currConfID = W_CONFIG0;
118  else this->currConfID = i; 
119
120 
121  Weapon *w1, *w2;
122  for(int j = 0; j < W_MAX_SLOTS; ++j)
123    {
124      w1 = this->configs[lastConfID].slots[j];
125      w2 = this->configs[this->currConfID].slots[j];
126     
127      if( w1 == w2)
128        {
129          printf("no need for change\n");
130        }
131      else
132        {
133        if( w1 != NULL )
134          {
135            w1->deactivate();
136            printf("deactivating %i,%i\n", j,lastConfID);
137          }
138        if( w2 != NULL)
139          {
140            w2->activate();
141            printf("activating %i,%i\n", j, this->currConfID);
142          }
143        }
144    }
145}
146
147
148
149/**
150   \brief triggers fire of all weapons in the current weaponconfig
151*/
152void WeaponManager::fire()
153{
154  Weapon* firingWeapon;
155  for(int i = 0; i < W_MAX_SLOTS; ++i)
156    {
157      firingWeapon = this->configs[this->currConfID].slots[i];
158      if( firingWeapon != NULL) firingWeapon->fire();
159    }
160}
161
162
163/**
164   \brief triggers tick of all weapons in the current weaponconfig
165   \param second passed since last tick
166*/
167void WeaponManager::tick(float sec)
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->tick(sec);
174    }
175}
176
177
178/**
179   \brief triggers draw of all weapons in the current weaponconfig
180*/
181void WeaponManager::draw()
182{
183  Weapon* w;
184  for(int i = 0; i < W_MAX_SLOTS; ++i)
185    {
186      w = this->configs[this->currConfID].slots[i];
187      if( w != NULL) w->draw();
188    }
189}
190
191
192/**
193   \brief private gets the next free slot in a certain weaponconfig
194   \param the selected weaponconfig
195*/
196int WeaponManager::getNextFreeSlot(int configID)
197{
198  for( int i = 0; i < W_MAX_SLOTS; ++i)
199    {
200      if( this->configs[configID].slots[i] == NULL)
201        return i;
202    }
203  return -1;
204}
205
206
207
208
209
210
211/**
212   \brief standard constructor
213
214   creates a new weapon
215*/
216Weapon::Weapon (PNode* parent, const Vector& coordinate, const Quaternion& direction) 
217  : WorldEntity()
218{
219  parent->addChild(this, PNODE_ALL);
220  this->setRelCoor(coordinate);
221  this->setRelDir(direction);
222  WorldInterface* wi = WorldInterface::getInstance();
223  this->worldEntities = wi->getEntityList();
224
225  this->objectComponent1 = NULL;
226  this->objectComponent2 = NULL;
227  this->objectComponent3 = NULL;
228
229  this->animation1 = NULL;
230  this->animation2 = NULL;
231  this->animation3 = NULL;
232}
233
234
235/**
236   \brief standard deconstructor
237*/
238Weapon::~Weapon () 
239{
240  // model will be deleted from WorldEntity-destructor
241  //this->worldEntities = NULL;
242 
243  /* dont delete objectComponentsX here, they will be killed when the pnodes are cleaned out */
244
245  /* all animations are deleted via the animation player*/
246}
247
248
249/**
250    \brief enables the weapon
251
252    a weapon can be enabled/disabled because of various reasons. if a weapon is
253    been enabled, it can interact in a world. elswhere it wont react to any
254    action.
255*/
256void Weapon::enable()
257{
258  this->enabled = true;
259}
260
261
262/**
263    \brief disables the weapon
264
265    a weapon can be enabled/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*/
269void Weapon::disable()
270{
271  this->enabled = false;
272}
273
274
275/**
276    \brief checks if the weapon is enabled
277    \returns true if enabled
278
279    a weapon can be ebabled/disabled because of various reasons. if a weapon is
280    been enabled, it can interact in a world. elswhere it wont react to any
281    action.
282*/
283bool Weapon::isEnabled()
284{
285  return this->enabled;
286}
287
288
289/**
290   \brief sets a new projectile to the weapon
291   \param new projectile for this weapon
292
293   weapon an projectile are independent, so you can combine them as you want
294*/
295void Weapon::setProjectile(Projectile* projectile)
296{
297  this->projectile = projectile;
298}
299
300
301/**
302   \brief sets a new projectile to the weapon
303   \returns the current projectile of this weapon
304
305   weapon an projectile are independent, so you can combine them as you want
306*/
307Projectile* Weapon::getProjectile()
308{
309  return this->projectile;
310}
311
312
313/**
314   \brief this activates the weapon
315
316   This is needed, since there can be more than one weapon on a ship. the
317   activation can be connected with an animation. for example the weapon is
318   been armed out.
319*/
320void Weapon::activate()
321{}
322
323
324/**
325   \brief this deactivates the weapon
326
327   This is needed, since there can be more than one weapon on a ship. the
328   activation can be connected with an animation. for example the weapon is
329   been armed out.
330*/
331void Weapon::deactivate()
332{}
333
334/**
335   \brief asks if the current weapon is active
336   \returns true if it the weapon is active
337*/
338bool Weapon::isActive()
339{}
340
341
342
343
344
345
346/**
347   \brief is called, when the weapon gets hit (=collide with something)
348   \param from which entity it is been hit
349   \param where it is been hit
350
351   this may not be used, since it would make the game relay complicated when one
352   can destroy the weapons of enemies or vice versa.
353*/
354void Weapon::hit (WorldEntity* entity, Vector* position) 
355{}
356
357
358/**
359   \brief is called, when the weapon is destroyed
360
361   this is in conjunction with the hit function, so when a weapon is able to get
362   hit, it can also be destoryed.
363*/
364void Weapon::destroy () 
365{}
366
367
368/**
369   \brief tick signal for time dependent/driven stuff
370*/
371void Weapon::tick (float time) 
372{}
373
374
375/**
376   \brief is called, when there is no fire button pressed
377*/
378void Weapon::weaponIdle()
379{}
380
381
382/**
383   \brief this will draw the weapon
384*/
385void Weapon::draw () 
386{}
387
Note: See TracBrowser for help on using the repository browser.