/* * orxonox - the future of 3D-vertical-scrollers * * Copyright (C) 2004 orx * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * ### File Specific: * main-programmer: Reto Luechinger */ #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY #include "bsp_weapon.h" #include "loading/load_param.h" #include "debug.h" #include "loading/load_param_xml.h" #include "environments/bsp_entity.h" #include "loading/fast_factory.h" #include "sound_engine.h" ObjectListDefinition(BspWeapon); CREATE_FACTORY(BspWeapon); ObjectListDefinition(MuzzleFlash); /** * Standard constructor */ BspWeapon::BspWeapon ( OM_LIST list ) { this->init( list ); } /** * destructs the BspWeapon, deletes allocated memory */ BspWeapon::~BspWeapon () { if ( soundSource_shoot.isPlaying() ) soundSource_shoot.stop(); } /** * Constructor with XML Element */ BspWeapon::BspWeapon (const TiXmlElement* root) { this->init( OM_GROUP_00 ); if (root != NULL) { this->loadParams(root); } } /** * XML Loader */ void BspWeapon::loadParams(const TiXmlElement* root) { if (root != NULL) { WorldEntity::loadParams(root); LoadParam(root, "Range", this, BspWeapon, setRange) .describe("the range after which the Weapon hits no more") .defaultValues(""); LoadParam(root, "Damage", this, BspWeapon, setDamage) .describe("Damage that the weapon inflicts"); LoadParam(root, "FireRate", this, BspWeapon, setFireRate) .describe("how fast it shoots"); LoadParam(root, "AlwaysHits", this, BspWeapon, setAlwaysHits) .describe("No, if the weapon should hit with a probability"); LOAD_PARAM_START_CYCLE(root, element); { if (root != 0){ LoadParam_CYCLE(element, "addPoint", this, BspWeapon, addPoint) .describe("Adds a new Point for Gunfire"); } } LOAD_PARAM_END_CYCLE(element); } } void BspWeapon::addPoint(float x, float y, float z) { gunFire.push_back( new MuzzleFlash() ); gunFire.back()->setParent( this->getParent() ); gunFire.back()->setRelCoor(x, y, z); } void BspWeapon::tick( float dt ) { //PRINTF(0)("BSPWEAPON TICK: %d %f %f\n", bFire, bRate, dt ); if (bFire) { if (bRate <= 0) { bRate += fireRate; this->shoot(); } else { bRate = bRate - dt; } } else { bRate -= dt; if (bRate < 0) bRate = 0; } } void BspWeapon::init( OM_LIST list ) { bRate = 0.0; bFire = false; range = 1000; damage = 10; fireRate = 0.5; alwaysHits = true; this->registerObject(this, BspWeapon::_objectList); this->aimingSystem = new AimingSystem( this ); this->aimingSystem->setParent( this ); this->aimingSystem->toList(list); this->soundSource_shoot.setSourceNode(this); this->soundBuffer_shoot = OrxSound::ResourceSoundBuffer("sounds/explosions/doulette.wav"); } void BspWeapon::shoot() { soundSource_shoot.play( soundBuffer_shoot, OrxSound::SoundEngine::getInstance()->getEffectsVolume(), 0.5); for ( std::list::iterator it = gunFire.begin(); it!=gunFire.end(); it++) { (*it)->explode( 0.2 ); } std::list::iterator entityIterator; // for all bsp managers check all entities Vector pos = this->getAbsCoor(); Vector dir = pos + this->getAbsDir().apply( Vector( 1, 0, 0 ) )*range; float shortestDist = range; for( ObjectList::const_iterator bspIterator = BspEntity::objectList().begin(); bspIterator != BspEntity::objectList().end(); bspIterator++) { float res = (dynamic_cast(*bspIterator)->getBspManager())->checkCollisionRay( pos, dir, dir.len() + 1 ); if ( res < shortestDist ) shortestDist = res; } WorldEntity* target = aimingSystem->getNearestTarget(); aimingSystem->flushList(); if ( target != NULL ) { if ( shortestDist < (pos - target->getAbsCoor()).len() ) { printf("HIT WALL\n"); return; } if (!alwaysHits) { float r = rand(); r = r/RAND_MAX; float res = (target->getAbsCoor() - this->getAbsCoor()).len(); float p = 1 - res*res/range/range; if (r < p ) { printf( "HIT %s\n", target->getClassName().c_str() ); target->hit( this->damage, this ); } else printf( "MISHIT %s\n", target->getClassName().c_str() ); } else { printf( "HIT %s\n", target->getClassName().c_str() ); target->hit( this->damage, this ); } } } void BspWeapon::draw() const { WorldEntity::draw(); for ( std::list::const_iterator it = gunFire.begin(); it!=gunFire.end(); it++) { (*it)->draw(); } #if 0 glMatrixMode(GL_MODELVIEW); glPushMatrix(); glPushAttrib(GL_ENABLE_BIT); glDisable(GL_LIGHTING); glDisable(GL_TEXTURE_2D); glDisable(GL_BLEND); glLineWidth(2.0); Vector mp = this->getAbsCoor(); Vector op = this->getAbsDir().apply( Vector(1, 0, 0) ); op *= 1000; op += mp; glColor3f(1.0, 1.0, 1.0 ); glBegin(GL_LINE_STRIP); glVertex3f(mp.x, mp.y, mp.z); glVertex3f(op.x, op.y, op.z); glEnd(); glPopAttrib(); glPopMatrix(); #endif } void MuzzleFlash::draw( ) const { if (explosionParticles) explosionParticles->draw(); } void MuzzleFlash::activate() { if (unlikely(explosionParticles == NULL)) { explosionParticles = new SpriteParticles(5000); explosionParticles->setName("MuzzleFlashExplosionParticles"); explosionParticles->setMaterialTexture("textures/radial-trans-noise.png"); explosionParticles->setLifeSpan(0.1, 0); explosionParticles->setRadius(0.0, 8); explosionParticles->setRadius(.5, 6.0); explosionParticles->setRadius(1.0, 2.0); explosionParticles->setColor(0.0, 1,0.7,0,1); explosionParticles->setColor(0.4, 0.8,.5,0,1); explosionParticles->setColor(0.8, 0.5,0,0,.8); explosionParticles->setColor(1.0, 0,0,0,.6); explosionParticles->toList(OM_DEAD_TICK); } this->emitter->setSystem(explosionParticles); this->emitter->updateNode(.01); this->emitter->updateNode(.01); this->toList(OM_DEAD_TICK); this->lifeCycle = 0.0; } void MuzzleFlash::explode(float lifetime) { MuzzleFlash* explosion = this; //explosion->setAbsCoor(this->getAbsCoor()); explosion->emitter->setSize(1, 1, 1); explosion->activate(); explosion->lifeTime = lifetime; } MuzzleFlash::MuzzleFlash () { this->explosionParticles = NULL; this->registerObject(this, MuzzleFlash::_objectList); this->toList(OM_DEAD_TICK); this->emitter = new BoxEmitter(Vector(10,10,10), 200, 45, M_2_PI); this->emitter->addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); this->emitter->setParent(this); this->emitter->setSpread(M_PI, M_PI); this->lifeCycle = 0.0f; this->lifeTime = .5f; } /** * standard deconstructor */ MuzzleFlash::~MuzzleFlash () { delete this->emitter; /* this is normaly done by World.cc by deleting the ParticleEngine */ if (explosionParticles != NULL) { delete explosionParticles; MuzzleFlash::explosionParticles = NULL; } } void MuzzleFlash::deactivate() { this->emitter->setSystem(NULL); this->toList(OM_DEAD); } /** * signal tick, time dependent things will be handled here * @param time since last tick */ void MuzzleFlash::tick (float dt) { this->lifeCycle += dt; if(this->lifeTime < this->lifeCycle) this->deactivate(); }