Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

player cannot shoot throu walls
per default worldentities cannot be killed
weapon correct again

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