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 | |
---|
25 | ObjectListDefinition(BspWeapon); |
---|
26 | CREATE_FACTORY(BspWeapon); |
---|
27 | |
---|
28 | ObjectListDefinition(MuzzleFlash); |
---|
29 | |
---|
30 | /** |
---|
31 | * Standard constructor |
---|
32 | */ |
---|
33 | BspWeapon::BspWeapon () |
---|
34 | { |
---|
35 | this->init(); |
---|
36 | } |
---|
37 | |
---|
38 | /** |
---|
39 | * destructs the BspWeapon, deletes allocated memory |
---|
40 | */ |
---|
41 | BspWeapon::~BspWeapon () |
---|
42 | { |
---|
43 | // will be deleted |
---|
44 | } |
---|
45 | |
---|
46 | /** |
---|
47 | * Constructor with XML Element |
---|
48 | */ |
---|
49 | BspWeapon::BspWeapon (const TiXmlElement* root) |
---|
50 | { |
---|
51 | this->init(); |
---|
52 | if (root != NULL) |
---|
53 | { |
---|
54 | this->loadParams(root); |
---|
55 | } |
---|
56 | } |
---|
57 | /** |
---|
58 | * XML Loader |
---|
59 | */ |
---|
60 | void 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 | |
---|
87 | void 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 | |
---|
94 | void BspWeapon::tick( float dt ) |
---|
95 | { |
---|
96 | if (bFire) { |
---|
97 | if (bRate < 0) { |
---|
98 | bRate += fireRate; |
---|
99 | this->shoot(); |
---|
100 | } |
---|
101 | else { |
---|
102 | bRate = bRate - dt; |
---|
103 | } |
---|
104 | } |
---|
105 | else { |
---|
106 | bRate -= dt; |
---|
107 | if (bRate < 0) |
---|
108 | bRate = 0; |
---|
109 | } |
---|
110 | |
---|
111 | } |
---|
112 | |
---|
113 | void BspWeapon::init() |
---|
114 | { |
---|
115 | bRate = 0; |
---|
116 | bFire = false; |
---|
117 | range = 1000; |
---|
118 | damage = 10; |
---|
119 | fireRate = 0.5; |
---|
120 | alwaysHits = true; |
---|
121 | |
---|
122 | this->registerObject(this, BspWeapon::_objectList); |
---|
123 | this->aimingSystem = new AimingSystem( this ); |
---|
124 | this->aimingSystem->setParent( this ); |
---|
125 | this->aimingSystem->toList(OM_GROUP_00); |
---|
126 | |
---|
127 | } |
---|
128 | |
---|
129 | void BspWeapon::shoot() |
---|
130 | { |
---|
131 | //gunFirExpl.explode(gunFire1,Vector(2,2,2)); |
---|
132 | //gunFirExpl.explode(gunFire2,Vector(2,2,2)); |
---|
133 | |
---|
134 | for ( std::list<MuzzleFlash*>::iterator it = gunFire.begin(); it!=gunFire.end(); it++) |
---|
135 | { |
---|
136 | (*it)->explode( 0.2 ); |
---|
137 | } |
---|
138 | |
---|
139 | std::list<WorldEntity*>::iterator entityIterator; |
---|
140 | // for all bsp managers check all entities |
---|
141 | Vector pos = this->getAbsCoor(); |
---|
142 | Vector dir = pos + this->getAbsDir().apply( Vector( 1, 0, 0 ) )*range; |
---|
143 | |
---|
144 | |
---|
145 | float shortestDist = range; |
---|
146 | for( ObjectList<BspEntity>::const_iterator bspIterator = BspEntity::objectList().begin(); |
---|
147 | bspIterator != BspEntity::objectList().end(); |
---|
148 | bspIterator++) { |
---|
149 | float res = (dynamic_cast<BspEntity*>(*bspIterator)->getBspManager())->checkCollisionRay( pos, dir, dir.len() + 1 ); |
---|
150 | |
---|
151 | if ( res < shortestDist ) |
---|
152 | shortestDist = res; |
---|
153 | } |
---|
154 | |
---|
155 | WorldEntity* target = aimingSystem->getNearestTarget(); |
---|
156 | aimingSystem->flushList(); |
---|
157 | |
---|
158 | bool hit = false; |
---|
159 | |
---|
160 | if ( target == NULL ) |
---|
161 | printf("NO TARGET\n"); |
---|
162 | else |
---|
163 | { |
---|
164 | if (!alwaysHits){ |
---|
165 | float r = rand(); |
---|
166 | r = r/RAND_MAX; |
---|
167 | float res = (target->getAbsCoor() - this->getAbsCoor()).len(); |
---|
168 | float p = 1 - res*res/range/range; |
---|
169 | if (r < p ){ |
---|
170 | hit = true; |
---|
171 | printf( "HIT %s\n", target->getClassName().c_str() ); |
---|
172 | } |
---|
173 | else |
---|
174 | printf( "MISHIT %s\n", target->getClassName().c_str() ); |
---|
175 | |
---|
176 | } |
---|
177 | else hit = true; |
---|
178 | } |
---|
179 | |
---|
180 | if ( !hit ) |
---|
181 | { |
---|
182 | //Vector explosionPos = this->getAbsCoor() + this->getAbsDir().apply( Vector( 1, 0, 0 ) )*shortestDist; |
---|
183 | |
---|
184 | //TODO create explosion at explosionPos |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | } |
---|
189 | |
---|
190 | void 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 0 |
---|
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 | |
---|
227 | void MuzzleFlash::draw( ) const |
---|
228 | { |
---|
229 | if (explosionParticles) explosionParticles->draw(); |
---|
230 | } |
---|
231 | |
---|
232 | void 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 | |
---|
257 | void 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 | |
---|
266 | MuzzleFlash::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 | */ |
---|
286 | MuzzleFlash::~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 | |
---|
298 | void 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 | */ |
---|
309 | void MuzzleFlash::tick (float dt) |
---|
310 | { |
---|
311 | this->lifeCycle += dt; |
---|
312 | if(this->lifeTime < this->lifeCycle) |
---|
313 | this->deactivate(); |
---|
314 | } |
---|
315 | |
---|
316 | |
---|
317 | |
---|