Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

player can kill adm now

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
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        PRINTF(0)("---------------------------------------groups: %d %d %d %d\n", this->getOMListNumber(), this->aimingSystem->getOMListNumber(), OM_GROUP_01, OM_GROUP_00);
161
162        if ( target == NULL )
163                printf("NO TARGET\n");
164        else
165        {
166                if (!alwaysHits){
167                        float r = rand();
168                        r = r/RAND_MAX;
169                        float res = (target->getAbsCoor() - this->getAbsCoor()).len();
170                        float p = 1 - res*res/range/range;
171                        if (r < p ){
172                                printf( "HIT %s\n", target->getClassName().c_str() );
173                                target->hit( this->damage, this );
174                        }
175                        else
176                                printf( "MISHIT %s\n", target->getClassName().c_str() );
177                       
178                }
179                else 
180                {
181                        printf( "HIT %s\n", target->getClassName().c_str() );
182                        target->hit( this->damage, this );
183                }
184        }
185
186       
187       
188       
189}
190
191void BspWeapon::draw() const
192{
193  WorldEntity::draw();
194
195  for ( std::list<MuzzleFlash*>::const_iterator it = gunFire.begin(); it!=gunFire.end(); it++)
196  {
197    (*it)->draw();
198  }
199#if 1
200  glMatrixMode(GL_MODELVIEW);
201  glPushMatrix();
202
203  glPushAttrib(GL_ENABLE_BIT);
204
205  glDisable(GL_LIGHTING);
206  glDisable(GL_TEXTURE_2D);
207  glDisable(GL_BLEND);
208  glLineWidth(2.0);
209
210
211  Vector mp = this->getAbsCoor();
212  Vector op = this->getAbsDir().apply( Vector(1, 0, 0) );
213  op *= 100;
214  op += mp;
215
216  glColor3f(1.0, 1.0, 1.0 );
217  glBegin(GL_LINE_STRIP);
218    glVertex3f(mp.x, mp.y, mp.z);
219    glVertex3f(op.x, op.y, op.z);
220  glEnd();
221
222 
223  glPopAttrib();
224  glPopMatrix();
225#endif
226}
227
228void MuzzleFlash::draw( ) const
229{
230  if (explosionParticles) explosionParticles->draw();
231}
232
233void MuzzleFlash::activate()
234{
235        if (unlikely(explosionParticles == NULL))
236        {
237                explosionParticles = new SpriteParticles(5000);
238                explosionParticles->setName("MuzzleFlashExplosionParticles");
239                explosionParticles->setMaterialTexture("textures/radial-trans-noise.png");
240                explosionParticles->setLifeSpan(0.1, 0);
241                explosionParticles->setRadius(0.0, 8);
242                explosionParticles->setRadius(.5, 6.0);
243                explosionParticles->setRadius(1.0, 2.0);
244                explosionParticles->setColor(0.0, 1,0.7,0,1);
245                explosionParticles->setColor(0.4, 0.8,.5,0,1);
246                explosionParticles->setColor(0.8, 0.5,0,0,.8);
247                explosionParticles->setColor(1.0, 0,0,0,.6);
248    explosionParticles->toList(OM_DEAD_TICK);
249  }
250                       
251        this->emitter->setSystem(explosionParticles);
252        this->emitter->updateNode(.01);
253        this->emitter->updateNode(.01);
254        this->toList(OM_DEAD_TICK);
255        this->lifeCycle = 0.0;
256}
257
258void MuzzleFlash::explode(float lifetime)
259{
260        MuzzleFlash* explosion = this;
261        //explosion->setAbsCoor(this->getAbsCoor());
262        explosion->emitter->setSize(1, 1, 1);
263        explosion->activate();
264        explosion->lifeTime = lifetime;
265}
266
267MuzzleFlash::MuzzleFlash ()
268{
269  this->explosionParticles = NULL;
270  this->registerObject(this, MuzzleFlash::_objectList);
271  this->toList(OM_DEAD_TICK);
272
273  this->emitter = new BoxEmitter(Vector(10,10,10), 200, 45, M_2_PI);
274  this->emitter->addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
275  this->emitter->setParent(this);
276  this->emitter->setSpread(M_PI, M_PI);
277
278  this->lifeCycle = 0.0f;
279  this->lifeTime = .5f;
280
281}
282
283
284/**
285 *  standard deconstructor
286*/
287MuzzleFlash::~MuzzleFlash ()
288{
289  delete this->emitter;
290
291  /* this is normaly done by World.cc by deleting the ParticleEngine */
292  if (explosionParticles != NULL)
293  {
294    delete explosionParticles;
295    MuzzleFlash::explosionParticles = NULL;
296  }
297}
298
299void MuzzleFlash::deactivate()
300{
301  this->emitter->setSystem(NULL);
302  this->toList(OM_DEAD);
303}
304
305
306/**
307 *  signal tick, time dependent things will be handled here
308 * @param time since last tick
309*/
310void MuzzleFlash::tick (float dt)
311{
312  this->lifeCycle += dt;
313  if(this->lifeTime < this->lifeCycle)
314    this->deactivate();
315}
316
317
318
Note: See TracBrowser for help on using the repository browser.