Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

FPSSniperRifle uses BspWeapon now

File size: 7.0 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
25ObjectListDefinition(BspWeapon);
26CREATE_FACTORY(BspWeapon);
27
28ObjectListDefinition(MuzzleFlash);
29
30/**
31*  Standard constructor
32*/
33BspWeapon::BspWeapon ( OM_LIST list )
34{
35        this->init( list );
36}
37
38/**
39* destructs the BspWeapon, deletes allocated memory
40*/
41BspWeapon::~BspWeapon ()
42{
43          // will be deleted
44}
45
46/**
47* Constructor with XML Element
48*/
49BspWeapon::BspWeapon (const  TiXmlElement* root)
50{
51        this->init( OM_GROUP_00 );
52        if (root != NULL)
53        {
54                this->loadParams(root);
55        }
56}
57/**
58* XML Loader
59*/
60void BspWeapon::loadParams(const TiXmlElement* root)
61{
62        if (root != NULL)
63        {
64                WorldEntity::loadParams(root);
65
66                LoadParam(root, "Range", this, BspWeapon, setRange)
67                        .describe("the range after which the Weapon hits no more")
68                .defaultValues("");
69                LoadParam(root, "Damage", this, BspWeapon, setDamage)
70                        .describe("Damage that the weapon inflicts"); 
71                LoadParam(root, "FireRate", this, BspWeapon, setFireRate)
72                        .describe("how fast it shoots");
73                LoadParam(root, "AlwaysHits", this, BspWeapon, setAlwaysHits)
74                        .describe("No, if the weapon should hit with a probability");
75
76                LOAD_PARAM_START_CYCLE(root, element);
77                {
78                        if (root != 0){
79                                LoadParam_CYCLE(element, "addPoint", this, BspWeapon, addPoint)
80                                .describe("Adds a new Point for Gunfire");
81                        }
82                }
83                LOAD_PARAM_END_CYCLE(element);
84        }
85}
86
87void BspWeapon::addPoint(float x, float y, float z)
88{
89  gunFire.push_back( new MuzzleFlash() );
90        gunFire.back()->setParent( this->getParent() );
91        gunFire.back()->setRelCoor(x, y, z);
92}
93
94void BspWeapon::tick( float dt )
95{
96  PRINTF(0)("BSPWEAPON TICK: %d %f %f\n", bFire, bRate, dt );
97        if (bFire) {
98                if (bRate <= 0) {
99                        bRate += fireRate;
100                        PRINTF(0)("BSPWEAPON SHOOT\n");
101                        this->shoot();
102                }
103                else {
104                        bRate = bRate - dt;
105                }       
106        }
107        else {
108                bRate -= dt;
109                if (bRate < 0)
110                        bRate = 0;
111        }
112
113}
114
115void BspWeapon::init( OM_LIST list )
116{
117        bRate = 0.0;
118        bFire = false;
119        range = 1000;
120        damage = 10;
121        fireRate = 0.5;
122        alwaysHits = true;
123
124        this->registerObject(this, BspWeapon::_objectList);
125        this->aimingSystem = new AimingSystem( this );
126        this->aimingSystem->setParent( this );
127        this->aimingSystem->toList(list);
128       
129}
130
131void BspWeapon::shoot()
132{
133        //gunFirExpl.explode(gunFire1,Vector(2,2,2));
134        //gunFirExpl.explode(gunFire2,Vector(2,2,2));
135
136  for ( std::list<MuzzleFlash*>::iterator it = gunFire.begin(); it!=gunFire.end(); it++)
137        {
138    (*it)->explode( 0.2 );
139        }
140
141        std::list<WorldEntity*>::iterator entityIterator;
142        // for all bsp managers check all entities
143        Vector pos = this->getAbsCoor();
144        Vector dir = pos + this->getAbsDir().apply( Vector( 1, 0, 0 ) )*range;
145       
146       
147        float shortestDist = range;
148        for( ObjectList<BspEntity>::const_iterator bspIterator = BspEntity::objectList().begin();
149                bspIterator != BspEntity::objectList().end();
150                bspIterator++) {
151                float res = (dynamic_cast<BspEntity*>(*bspIterator)->getBspManager())->checkCollisionRay( pos, dir, dir.len() + 1 );
152       
153                if ( res < shortestDist )
154                        shortestDist = res;
155        }
156
157        WorldEntity* target = aimingSystem->getNearestTarget();
158        aimingSystem->flushList();
159
160        bool hit = false;
161       
162        PRINTF(0)("groups: %d %d %d %d\n", this->getOMListNumber(), this->aimingSystem->getOMListNumber(), OM_GROUP_01, OM_GROUP_00);
163
164        if ( target == NULL )
165                printf("NO TARGET\n");
166        else
167        {
168                if (!alwaysHits){
169                        float r = rand();
170                        r = r/RAND_MAX;
171                        float res = (target->getAbsCoor() - this->getAbsCoor()).len();
172                        float p = 1 - res*res/range/range;
173                        if (r < p ){
174                                hit = true;
175                                printf( "HIT %s\n", target->getClassName().c_str() );
176                                target->hit( this->damage, this );
177                        }
178                        else
179                                printf( "MISHIT %s\n", target->getClassName().c_str() );
180                       
181                }
182                else hit = true;
183        }
184
185       
186       
187       
188}
189
190void BspWeapon::draw() const
191{
192  WorldEntity::draw();
193
194  for ( std::list<MuzzleFlash*>::const_iterator it = gunFire.begin(); it!=gunFire.end(); it++)
195  {
196    (*it)->draw();
197  }
198#if 1
199  glMatrixMode(GL_MODELVIEW);
200  glPushMatrix();
201
202  glPushAttrib(GL_ENABLE_BIT);
203
204  glDisable(GL_LIGHTING);
205  glDisable(GL_TEXTURE_2D);
206  glDisable(GL_BLEND);
207  glLineWidth(2.0);
208
209
210  Vector mp = this->getAbsCoor();
211  Vector op = this->getAbsDir().apply( Vector(1, 0, 0) );
212  op *= 100;
213  op += mp;
214
215  glColor3f(1.0, 1.0, 1.0 );
216  glBegin(GL_LINE_STRIP);
217    glVertex3f(mp.x, mp.y, mp.z);
218    glVertex3f(op.x, op.y, op.z);
219  glEnd();
220
221 
222  glPopAttrib();
223  glPopMatrix();
224#endif
225}
226
227void MuzzleFlash::draw( ) const
228{
229  if (explosionParticles) explosionParticles->draw();
230}
231
232void MuzzleFlash::activate()
233{
234        if (unlikely(explosionParticles == NULL))
235        {
236                explosionParticles = new SpriteParticles(5000);
237                explosionParticles->setName("MuzzleFlashExplosionParticles");
238                explosionParticles->setMaterialTexture("textures/radial-trans-noise.png");
239                explosionParticles->setLifeSpan(0.1, 0);
240                explosionParticles->setRadius(0.0, 8);
241                explosionParticles->setRadius(.5, 6.0);
242                explosionParticles->setRadius(1.0, 2.0);
243                explosionParticles->setColor(0.0, 1,0.7,0,1);
244                explosionParticles->setColor(0.4, 0.8,.5,0,1);
245                explosionParticles->setColor(0.8, 0.5,0,0,.8);
246                explosionParticles->setColor(1.0, 0,0,0,.6);
247    explosionParticles->toList(OM_DEAD_TICK);
248  }
249                       
250        this->emitter->setSystem(explosionParticles);
251        this->emitter->updateNode(.01);
252        this->emitter->updateNode(.01);
253        this->toList(OM_DEAD_TICK);
254        this->lifeCycle = 0.0;
255}
256
257void MuzzleFlash::explode(float lifetime)
258{
259        MuzzleFlash* explosion = this;
260        //explosion->setAbsCoor(this->getAbsCoor());
261        explosion->emitter->setSize(1, 1, 1);
262        explosion->activate();
263        explosion->lifeTime = lifetime;
264}
265
266MuzzleFlash::MuzzleFlash ()
267{
268  this->explosionParticles = NULL;
269  this->registerObject(this, MuzzleFlash::_objectList);
270  this->toList(OM_DEAD_TICK);
271
272  this->emitter = new BoxEmitter(Vector(10,10,10), 200, 45, M_2_PI);
273  this->emitter->addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
274  this->emitter->setParent(this);
275  this->emitter->setSpread(M_PI, M_PI);
276
277  this->lifeCycle = 0.0f;
278  this->lifeTime = .5f;
279
280}
281
282
283/**
284 *  standard deconstructor
285*/
286MuzzleFlash::~MuzzleFlash ()
287{
288  delete this->emitter;
289
290  /* this is normaly done by World.cc by deleting the ParticleEngine */
291  if (explosionParticles != NULL)
292  {
293    delete explosionParticles;
294    MuzzleFlash::explosionParticles = NULL;
295  }
296}
297
298void MuzzleFlash::deactivate()
299{
300  this->emitter->setSystem(NULL);
301  this->toList(OM_DEAD);
302}
303
304
305/**
306 *  signal tick, time dependent things will be handled here
307 * @param time since last tick
308*/
309void MuzzleFlash::tick (float dt)
310{
311  this->lifeCycle += dt;
312  if(this->lifeTime < this->lifeCycle)
313    this->deactivate();
314}
315
316
317
Note: See TracBrowser for help on using the repository browser.