Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/world_entities/weapons/bsp_weapon.cc @ 10713

Last change on this file since 10713 was 10713, checked in by rennerc, 17 years ago
File size: 7.4 KB
Line 
1/*
2* orxonox - the future of 3D-vertical-scrollers
3*
4* Copyright (C) 2004 orx
5*
6* This program is free software; you can redistribute it and/or modify
7* it under the terms of the GNU General Public License as published by
8* the Free Software Foundation; either version 2, or (at your option)
9* any later version.
10*
11*     ### File Specific:
12*     main-programmer: Reto Luechinger
13*/
14
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
17#include "bsp_weapon.h"
18#include "loading/load_param.h"
19#include "debug.h"
20#include "loading/load_param_xml.h"
21
22#include "environments/bsp_entity.h"
23#include "loading/fast_factory.h"
24#include "sound_engine.h"
25
26ObjectListDefinition(BspWeapon);
27CREATE_FACTORY(BspWeapon);
28
29ObjectListDefinition(MuzzleFlash);
30
31/**
32*  Standard constructor
33*/
34BspWeapon::BspWeapon ( OM_LIST list )
35{
36        this->init( list );
37}
38
39/**
40* destructs the BspWeapon, deletes allocated memory
41*/
42BspWeapon::~BspWeapon ()
43{
44  if ( soundSource_shoot.isPlaying() )
45    soundSource_shoot.stop();
46}
47
48/**
49* Constructor with XML Element
50*/
51BspWeapon::BspWeapon (const  TiXmlElement* root)
52{
53        this->init( OM_GROUP_00 );
54        if (root != NULL)
55        {
56                this->loadParams(root);
57        }
58}
59/**
60* XML Loader
61*/
62void BspWeapon::loadParams(const TiXmlElement* root)
63{
64        if (root != NULL)
65        {
66                WorldEntity::loadParams(root);
67
68                LoadParam(root, "Range", this, BspWeapon, setRange)
69                        .describe("the range after which the Weapon hits no more")
70                .defaultValues("");
71                LoadParam(root, "Damage", this, BspWeapon, setDamage)
72                        .describe("Damage that the weapon inflicts"); 
73                LoadParam(root, "FireRate", this, BspWeapon, setFireRate)
74                        .describe("how fast it shoots");
75                LoadParam(root, "AlwaysHits", this, BspWeapon, setAlwaysHits)
76                        .describe("No, if the weapon should hit with a probability");
77
78                LOAD_PARAM_START_CYCLE(root, element);
79                {
80                        if (root != 0){
81                                LoadParam_CYCLE(element, "addPoint", this, BspWeapon, addPoint)
82                                .describe("Adds a new Point for Gunfire");
83                        }
84                }
85                LOAD_PARAM_END_CYCLE(element);
86        }
87}
88
89void BspWeapon::addPoint(float x, float y, float z)
90{
91  gunFire.push_back( new MuzzleFlash() );
92        gunFire.back()->setParent( this->getParent() );
93        gunFire.back()->setRelCoor(x, y, z);
94}
95
96void BspWeapon::tick( float dt )
97{
98  //PRINTF(0)("BSPWEAPON TICK: %d %f %f\n", bFire, bRate, dt );
99        if (bFire) {
100                if (bRate <= 0) {
101                        bRate += fireRate;
102                        this->shoot();
103                }
104                else {
105                        bRate = bRate - dt;
106                }       
107        }
108        else {
109                bRate -= dt;
110                if (bRate < 0)
111                        bRate = 0;
112        }
113
114}
115
116void BspWeapon::init( OM_LIST list )
117{
118        bRate = 0.0;
119        bFire = false;
120        range = 1000;
121#ifdef DEBUG_AIMING
122        debugDist = 1000;
123#endif
124        damage = 10;
125        fireRate = 0.5;
126        alwaysHits = true;
127
128        this->registerObject(this, BspWeapon::_objectList);
129        this->aimingSystem = new AimingSystem( this );
130        this->aimingSystem->setParent( this );
131        this->aimingSystem->toList(list);
132       
133        this->soundSource_shoot.setSourceNode(this);
134        this->soundBuffer_shoot = OrxSound::ResourceSoundBuffer("sounds/explosions/doulette.wav");
135       
136}
137
138void BspWeapon::shoot()
139{
140
141  soundSource_shoot.play( soundBuffer_shoot, OrxSound::SoundEngine::getInstance()->getEffectsVolume(), 0.5);
142 
143for ( std::list<MuzzleFlash*>::iterator it = gunFire.begin(); it!=gunFire.end(); it++)
144{
145  (*it)->explode( 0.2 );
146}
147
148std::list<WorldEntity*>::iterator entityIterator;
149// for all bsp managers check all entities
150Vector pos = this->getAbsCoor();
151Vector dir = pos + this->getAbsDir().apply( Vector( 1, 0, 0 ) )*range;
152
153
154float shortestDist = range;
155for( ObjectList<BspEntity>::const_iterator bspIterator = BspEntity::objectList().begin();
156     bspIterator != BspEntity::objectList().end();
157     bspIterator++)
158{
159  float res = (dynamic_cast<BspEntity*>(*bspIterator)->getBspManager())->checkCollisionRay( pos, dir, dir.len() + 1 );
160
161  if ( res < shortestDist )
162    shortestDist = res;
163}
164
165WorldEntity* target = aimingSystem->getNearestTarget();
166aimingSystem->flushList();
167
168if ( target != NULL )
169{
170  if ( shortestDist < (pos - target->getAbsCoor()).len() )
171  {
172    printf("HIT WALL\n");
173#ifdef DEBUG_AIMING
174    this->debugDist = shortestDist;
175#endif
176    return;
177  }
178#ifdef DEBUG_AIMING
179  else
180  {
181    this->debugDist = 1000;
182  }
183#endif
184  if (!alwaysHits)
185  {
186    float r = rand();
187    r = r/RAND_MAX;
188    float res = (target->getAbsCoor() - this->getAbsCoor()).len();
189    float p = 1 - res*res/range/range;
190    if (r < p )
191    {
192      printf( "HIT %s\n", target->getClassName().c_str() );
193      target->hit( this->damage, this );
194    }
195    else
196      printf( "MISHIT %s\n", target->getClassName().c_str() );
197
198  }
199  else
200  {
201    printf( "HIT %s\n", target->getClassName().c_str() );
202    target->hit( this->damage, this );
203  }
204}
205
206
207
208       
209}
210
211void BspWeapon::draw() const
212{
213  WorldEntity::draw();
214
215  for ( std::list<MuzzleFlash*>::const_iterator it = gunFire.begin(); it!=gunFire.end(); it++)
216  {
217    (*it)->draw();
218  }
219#if 1
220  glMatrixMode(GL_MODELVIEW);
221  glPushMatrix();
222
223  glPushAttrib(GL_ENABLE_BIT);
224
225  glDisable(GL_LIGHTING);
226  glDisable(GL_TEXTURE_2D);
227  glDisable(GL_BLEND);
228  glLineWidth(2.0);
229
230
231  Vector mp = this->getAbsCoor();
232  Vector op = this->getAbsDir().apply( Vector(1, 0, 0) );
233#ifdef DEBUG_AIMING
234  op *= debugDist;
235#else
236  op *= 1000;
237#endif
238  op += mp;
239
240  glColor3f(1.0, 1.0, 1.0 );
241  glBegin(GL_LINE_STRIP);
242    glVertex3f(mp.x, mp.y, mp.z);
243    glVertex3f(op.x, op.y, op.z);
244  glEnd();
245
246 
247  glPopAttrib();
248  glPopMatrix();
249#endif
250}
251
252void MuzzleFlash::draw( ) const
253{
254  if (explosionParticles) explosionParticles->draw();
255}
256
257void MuzzleFlash::activate()
258{
259        if (unlikely(explosionParticles == NULL))
260        {
261                explosionParticles = new SpriteParticles(5000);
262                explosionParticles->setName("MuzzleFlashExplosionParticles");
263                explosionParticles->setMaterialTexture("textures/radial-trans-noise.png");
264                explosionParticles->setLifeSpan(0.1, 0);
265                explosionParticles->setRadius(0.0, 8);
266                explosionParticles->setRadius(.5, 6.0);
267                explosionParticles->setRadius(1.0, 2.0);
268                explosionParticles->setColor(0.0, 1,0.7,0,1);
269                explosionParticles->setColor(0.4, 0.8,.5,0,1);
270                explosionParticles->setColor(0.8, 0.5,0,0,.8);
271                explosionParticles->setColor(1.0, 0,0,0,.6);
272    explosionParticles->toList(OM_DEAD_TICK);
273  }
274                       
275        this->emitter->setSystem(explosionParticles);
276        this->emitter->updateNode(.01);
277        this->emitter->updateNode(.01);
278        this->toList(OM_DEAD_TICK);
279        this->lifeCycle = 0.0;
280}
281
282void MuzzleFlash::explode(float lifetime)
283{
284        MuzzleFlash* explosion = this;
285        //explosion->setAbsCoor(this->getAbsCoor());
286        explosion->emitter->setSize(1, 1, 1);
287        explosion->activate();
288        explosion->lifeTime = lifetime;
289}
290
291MuzzleFlash::MuzzleFlash ()
292{
293  this->explosionParticles = NULL;
294  this->registerObject(this, MuzzleFlash::_objectList);
295  this->toList(OM_DEAD_TICK);
296
297  this->emitter = new BoxEmitter(Vector(10,10,10), 200, 45, M_2_PI);
298  this->emitter->addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
299  this->emitter->setParent(this);
300  this->emitter->setSpread(M_PI, M_PI);
301
302  this->lifeCycle = 0.0f;
303  this->lifeTime = .5f;
304
305}
306
307
308/**
309 *  standard deconstructor
310*/
311MuzzleFlash::~MuzzleFlash ()
312{
313  delete this->emitter;
314
315  /* this is normaly done by World.cc by deleting the ParticleEngine */
316  if (explosionParticles != NULL)
317  {
318    delete explosionParticles;
319    MuzzleFlash::explosionParticles = NULL;
320  }
321}
322
323void MuzzleFlash::deactivate()
324{
325  this->emitter->setSystem(NULL);
326  this->toList(OM_DEAD);
327}
328
329
330/**
331 *  signal tick, time dependent things will be handled here
332 * @param time since last tick
333*/
334void MuzzleFlash::tick (float dt)
335{
336  this->lifeCycle += dt;
337  if(this->lifeTime < this->lifeCycle)
338    this->deactivate();
339}
340
341
342
Note: See TracBrowser for help on using the repository browser.