[10689] | 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" |
---|
[10694] | 23 | #include "loading/fast_factory.h" |
---|
[10689] | 24 | |
---|
| 25 | ObjectListDefinition(BspWeapon); |
---|
| 26 | CREATE_FACTORY(BspWeapon); |
---|
| 27 | |
---|
[10694] | 28 | ObjectListDefinition(MuzzleFlash); |
---|
| 29 | |
---|
[10689] | 30 | /** |
---|
| 31 | * Standard constructor |
---|
| 32 | */ |
---|
[10704] | 33 | BspWeapon::BspWeapon ( OM_LIST list ) |
---|
[10689] | 34 | { |
---|
[10704] | 35 | this->init( list ); |
---|
[10689] | 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 | { |
---|
[10704] | 51 | this->init( OM_GROUP_00 ); |
---|
[10689] | 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) |
---|
[10690] | 72 | .describe("how fast it shoots"); |
---|
[10689] | 73 | LoadParam(root, "AlwaysHits", this, BspWeapon, setAlwaysHits) |
---|
[10690] | 74 | .describe("No, if the weapon should hit with a probability"); |
---|
| 75 | |
---|
| 76 | LOAD_PARAM_START_CYCLE(root, element); |
---|
| 77 | { |
---|
[10694] | 78 | if (root != 0){ |
---|
| 79 | LoadParam_CYCLE(element, "addPoint", this, BspWeapon, addPoint) |
---|
| 80 | .describe("Adds a new Point for Gunfire"); |
---|
| 81 | } |
---|
[10690] | 82 | } |
---|
| 83 | LOAD_PARAM_END_CYCLE(element); |
---|
[10689] | 84 | } |
---|
| 85 | } |
---|
| 86 | |
---|
[10690] | 87 | void BspWeapon::addPoint(float x, float y, float z) |
---|
| 88 | { |
---|
[10694] | 89 | gunFire.push_back( new MuzzleFlash() ); |
---|
| 90 | gunFire.back()->setParent( this->getParent() ); |
---|
| 91 | gunFire.back()->setRelCoor(x, y, z); |
---|
[10690] | 92 | } |
---|
[10689] | 93 | |
---|
| 94 | void BspWeapon::tick( float dt ) |
---|
| 95 | { |
---|
[10705] | 96 | //PRINTF(0)("BSPWEAPON TICK: %d %f %f\n", bFire, bRate, dt ); |
---|
[10689] | 97 | if (bFire) { |
---|
[10704] | 98 | if (bRate <= 0) { |
---|
[10689] | 99 | bRate += fireRate; |
---|
[10704] | 100 | PRINTF(0)("BSPWEAPON SHOOT\n"); |
---|
[10689] | 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 | } |
---|
[10694] | 112 | |
---|
[10689] | 113 | } |
---|
| 114 | |
---|
[10704] | 115 | void BspWeapon::init( OM_LIST list ) |
---|
[10689] | 116 | { |
---|
[10704] | 117 | bRate = 0.0; |
---|
[10694] | 118 | bFire = false; |
---|
[10689] | 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 ); |
---|
[10704] | 127 | this->aimingSystem->toList(list); |
---|
[10694] | 128 | |
---|
[10689] | 129 | } |
---|
| 130 | |
---|
| 131 | void BspWeapon::shoot() |
---|
| 132 | { |
---|
[10694] | 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 | |
---|
[10689] | 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(); |
---|
[10704] | 159 | |
---|
[10707] | 160 | PRINTF(0)("---------------------------------------groups: %d %d %d %d\n", this->getOMListNumber(), this->aimingSystem->getOMListNumber(), OM_GROUP_01, OM_GROUP_00); |
---|
[10689] | 161 | |
---|
| 162 | if ( target == NULL ) |
---|
[10694] | 163 | printf("NO TARGET\n"); |
---|
[10689] | 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; |
---|
[10694] | 171 | if (r < p ){ |
---|
| 172 | printf( "HIT %s\n", target->getClassName().c_str() ); |
---|
[10704] | 173 | target->hit( this->damage, this ); |
---|
[10694] | 174 | } |
---|
| 175 | else |
---|
| 176 | printf( "MISHIT %s\n", target->getClassName().c_str() ); |
---|
| 177 | |
---|
[10689] | 178 | } |
---|
[10707] | 179 | else |
---|
| 180 | { |
---|
| 181 | printf( "HIT %s\n", target->getClassName().c_str() ); |
---|
| 182 | target->hit( this->damage, this ); |
---|
| 183 | } |
---|
[10689] | 184 | } |
---|
| 185 | |
---|
| 186 | |
---|
[10694] | 187 | |
---|
[10704] | 188 | |
---|
[10689] | 189 | } |
---|
| 190 | |
---|
| 191 | void BspWeapon::draw() const |
---|
| 192 | { |
---|
| 193 | WorldEntity::draw(); |
---|
| 194 | |
---|
[10694] | 195 | for ( std::list<MuzzleFlash*>::const_iterator it = gunFire.begin(); it!=gunFire.end(); it++) |
---|
| 196 | { |
---|
| 197 | (*it)->draw(); |
---|
| 198 | } |
---|
[10704] | 199 | #if 1 |
---|
[10689] | 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(); |
---|
[10694] | 225 | #endif |
---|
[10689] | 226 | } |
---|
[10694] | 227 | |
---|
| 228 | void MuzzleFlash::draw( ) const |
---|
| 229 | { |
---|
| 230 | if (explosionParticles) explosionParticles->draw(); |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | void 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 | |
---|
| 258 | void 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 | |
---|
| 267 | MuzzleFlash::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 | */ |
---|
| 287 | MuzzleFlash::~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 | |
---|
| 299 | void 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 | */ |
---|
| 310 | void MuzzleFlash::tick (float dt) |
---|
| 311 | { |
---|
| 312 | this->lifeCycle += dt; |
---|
| 313 | if(this->lifeTime < this->lifeCycle) |
---|
| 314 | this->deactivate(); |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | |
---|
| 318 | |
---|