Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10712 was 10712, checked in by rennerc, 17 years ago

AdmWeapon sound, ActionboxEnemy weapons

File size: 7.2 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        damage = 10;
122        fireRate = 0.5;
123        alwaysHits = true;
124
125        this->registerObject(this, BspWeapon::_objectList);
126        this->aimingSystem = new AimingSystem( this );
127        this->aimingSystem->setParent( this );
128        this->aimingSystem->toList(list);
129       
130        this->soundSource_shoot.setSourceNode(this);
131        this->soundBuffer_shoot = OrxSound::ResourceSoundBuffer("sounds/explosions/doulette.wav");
132       
133}
134
135void BspWeapon::shoot()
136{
137
138  soundSource_shoot.play( soundBuffer_shoot, OrxSound::SoundEngine::getInstance()->getEffectsVolume(), 0.5);
139 
140for ( std::list<MuzzleFlash*>::iterator it = gunFire.begin(); it!=gunFire.end(); it++)
141{
142  (*it)->explode( 0.2 );
143}
144
145std::list<WorldEntity*>::iterator entityIterator;
146// for all bsp managers check all entities
147Vector pos = this->getAbsCoor();
148Vector dir = pos + this->getAbsDir().apply( Vector( 1, 0, 0 ) )*range;
149
150
151float shortestDist = range;
152for( ObjectList<BspEntity>::const_iterator bspIterator = BspEntity::objectList().begin();
153     bspIterator != BspEntity::objectList().end();
154     bspIterator++)
155{
156  float res = (dynamic_cast<BspEntity*>(*bspIterator)->getBspManager())->checkCollisionRay( pos, dir, dir.len() + 1 );
157
158  if ( res < shortestDist )
159    shortestDist = res;
160}
161
162WorldEntity* target = aimingSystem->getNearestTarget();
163aimingSystem->flushList();
164
165if ( target != NULL )
166{
167  if ( shortestDist < (pos - target->getAbsCoor()).len() )
168  {
169    printf("HIT WALL\n");
170    return;
171  }
172  if (!alwaysHits)
173  {
174    float r = rand();
175    r = r/RAND_MAX;
176    float res = (target->getAbsCoor() - this->getAbsCoor()).len();
177    float p = 1 - res*res/range/range;
178    if (r < p )
179    {
180      printf( "HIT %s\n", target->getClassName().c_str() );
181      target->hit( this->damage, this );
182    }
183    else
184      printf( "MISHIT %s\n", target->getClassName().c_str() );
185
186  }
187  else
188  {
189    printf( "HIT %s\n", target->getClassName().c_str() );
190    target->hit( this->damage, this );
191  }
192}
193
194
195
196       
197}
198
199void BspWeapon::draw() const
200{
201  WorldEntity::draw();
202
203  for ( std::list<MuzzleFlash*>::const_iterator it = gunFire.begin(); it!=gunFire.end(); it++)
204  {
205    (*it)->draw();
206  }
207#if 0
208  glMatrixMode(GL_MODELVIEW);
209  glPushMatrix();
210
211  glPushAttrib(GL_ENABLE_BIT);
212
213  glDisable(GL_LIGHTING);
214  glDisable(GL_TEXTURE_2D);
215  glDisable(GL_BLEND);
216  glLineWidth(2.0);
217
218
219  Vector mp = this->getAbsCoor();
220  Vector op = this->getAbsDir().apply( Vector(1, 0, 0) );
221  op *= 1000;
222  op += mp;
223
224  glColor3f(1.0, 1.0, 1.0 );
225  glBegin(GL_LINE_STRIP);
226    glVertex3f(mp.x, mp.y, mp.z);
227    glVertex3f(op.x, op.y, op.z);
228  glEnd();
229
230 
231  glPopAttrib();
232  glPopMatrix();
233#endif
234}
235
236void MuzzleFlash::draw( ) const
237{
238  if (explosionParticles) explosionParticles->draw();
239}
240
241void MuzzleFlash::activate()
242{
243        if (unlikely(explosionParticles == NULL))
244        {
245                explosionParticles = new SpriteParticles(5000);
246                explosionParticles->setName("MuzzleFlashExplosionParticles");
247                explosionParticles->setMaterialTexture("textures/radial-trans-noise.png");
248                explosionParticles->setLifeSpan(0.1, 0);
249                explosionParticles->setRadius(0.0, 8);
250                explosionParticles->setRadius(.5, 6.0);
251                explosionParticles->setRadius(1.0, 2.0);
252                explosionParticles->setColor(0.0, 1,0.7,0,1);
253                explosionParticles->setColor(0.4, 0.8,.5,0,1);
254                explosionParticles->setColor(0.8, 0.5,0,0,.8);
255                explosionParticles->setColor(1.0, 0,0,0,.6);
256    explosionParticles->toList(OM_DEAD_TICK);
257  }
258                       
259        this->emitter->setSystem(explosionParticles);
260        this->emitter->updateNode(.01);
261        this->emitter->updateNode(.01);
262        this->toList(OM_DEAD_TICK);
263        this->lifeCycle = 0.0;
264}
265
266void MuzzleFlash::explode(float lifetime)
267{
268        MuzzleFlash* explosion = this;
269        //explosion->setAbsCoor(this->getAbsCoor());
270        explosion->emitter->setSize(1, 1, 1);
271        explosion->activate();
272        explosion->lifeTime = lifetime;
273}
274
275MuzzleFlash::MuzzleFlash ()
276{
277  this->explosionParticles = NULL;
278  this->registerObject(this, MuzzleFlash::_objectList);
279  this->toList(OM_DEAD_TICK);
280
281  this->emitter = new BoxEmitter(Vector(10,10,10), 200, 45, M_2_PI);
282  this->emitter->addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
283  this->emitter->setParent(this);
284  this->emitter->setSpread(M_PI, M_PI);
285
286  this->lifeCycle = 0.0f;
287  this->lifeTime = .5f;
288
289}
290
291
292/**
293 *  standard deconstructor
294*/
295MuzzleFlash::~MuzzleFlash ()
296{
297  delete this->emitter;
298
299  /* this is normaly done by World.cc by deleting the ParticleEngine */
300  if (explosionParticles != NULL)
301  {
302    delete explosionParticles;
303    MuzzleFlash::explosionParticles = NULL;
304  }
305}
306
307void MuzzleFlash::deactivate()
308{
309  this->emitter->setSystem(NULL);
310  this->toList(OM_DEAD);
311}
312
313
314/**
315 *  signal tick, time dependent things will be handled here
316 * @param time since last tick
317*/
318void MuzzleFlash::tick (float dt)
319{
320  this->lifeCycle += dt;
321  if(this->lifeTime < this->lifeCycle)
322    this->deactivate();
323}
324
325
326
Note: See TracBrowser for help on using the repository browser.