Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4597 was 4597, checked in by bensch, 19 years ago

orxonox/trunk: setClassID implemented in all files

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