#include "dxstdafx.h" #include ".\bullet.h" #include "Player.h" #include "SoundNode.h" #include "GameManager.h" Bullet::Bullet(void) : Node() { this->bulletType = 0; this->exploded = false; this->explodeAtDeath = false; this->impactDamage = 15; this->impactRadius = 2; this->impactForce = 2; this->impactSound = NULL; this->flySound = NULL; this->impactSound = NULL; this->terrainHit = false; this->impactNormal.setXYZ(0, 1, 0); } Bullet::~Bullet(void) { int asdf=0; asdf++; } void Bullet::setWeaponDirection(Vector _dir) { this->dir = _dir; } Vector Bullet::getDirection() { return this->dir; } void Bullet::setImpactForce(float _impactForce) { this->impactForce = _impactForce; } float Bullet::getImpactForce() { return this->impactForce; } void Bullet::setImpactDamage(float _impactDamage) { this->impactDamage = _impactDamage; } float Bullet::getImpactDamage() { return this->impactDamage; } void Bullet::setImpactRadius(float _radius) { this->impactRadius = _radius; } float Bullet::getImpactRadius() { return this->impactRadius; } void Bullet::setFlySound(std::string flySoundName) { if(flySoundName.compare("")!=0) { this->flySound = (SoundNode*) this->myScene->createNode(this->myScene->NODE_SOUND, *this); this->flySound->loadFile(flySoundName, true); } } void Bullet::setImpactSound(std::string impactSoundName) { if(impactSoundName.compare("")!=0) { this->impactSound = (SoundNode*) this->myScene->createNode(this->myScene->NODE_SOUND); this->impactSound->loadFile(impactSoundName, false); } } /*void Bullet::setNeighbour(WPTR _neighbour) { this->neighbour = _neighbour; }*/ void Bullet::setPlayer(Player* _myPlayer) { this->myPlayer = _myPlayer; } void Bullet::setBulletType(int type) { this->bulletType = type; } int Bullet::getBulletType() { return this->bulletType; } void Bullet::setExplodeAtDeath(bool _explodeAtDeath) { this->explodeAtDeath = _explodeAtDeath; } void Bullet::update(float dt) { if(this->explodeAtDeath) { this->timeToLive-=dt; //Read collisiondata and execute impact*** /*char temp[100]; sprintf(temp, "timetolive = %f",this->timeToLive); this->myScene->manager->printToConsole(temp);*/ if(this->timeToLive<=0 && this->timeToLive!=-100) { this->exploded = true; //this->impactTerrain(); this->impactTimeOut(); } } else { Node::update(dt); } } void Bullet::collectEnvironmentInformation() { this->terrainHit = false; this->shapeList.clear(); NxSphere worldSphere(this->getAbsolutePosition().getNxVector(), this->impactRadius); NxU32 nbShapes = this->myScene->pScene->getNbDynamicShapes(); NxShape** shapes = new NxShape* [nbShapes]; for (NxU32 i = 0; i < nbShapes; i++) shapes[i] = NULL; this->myScene->pScene->overlapSphereShapes(worldSphere, NX_DYNAMIC_SHAPES, nbShapes, shapes, NULL); for(UINT i=0;igetActor().getGroup()!=UserContactReport::COLGROUP_BULLET) { this->shapeList.push_back(shapes[i]); } switch(shapes[i]->getActor().getGroup()) { case UserContactReport::COLGROUP_PLAYER: this->playerList.push_back((Player*) shapes[i]->getActor().userData); break; case UserContactReport::COLGROUP_TERRAIN: this->terrainHit = true; break; case UserContactReport::COLGROUP_BULLET: this->bulletList.push_back((Bullet*) shapes[i]->getActor().userData); case UserContactReport::COLGROUP_GOODIE: this->goodieList.push_back((Goodie*) shapes[i]->getActor().userData); break; } } else { break; } } delete[] shapes; } void Bullet::setStandardExplosionForces() { //std::vector shapeList = this->getShapesInSphere(radius); Vector globalPos = this->getAbsolutePosition(); Vector dir; NxShape *shape; Vector sPos; for(UINT i=0;ishapeList.size();i++) { shape = this->shapeList.at(i); if(shape->getGroup()!=UserContactReport::COLGROUP_NOCOL && shape->getGroup()!=UserContactReport::COLGROUP_TERRAIN && !shape->getActor().readBodyFlag(NX_BF_KINEMATIC)) { sPos.setNxVector(shape->getGlobalPosition()); dir = sPos - globalPos; if(dir.length()impactRadius) { //Object in Explosionsradius dir = dir*((this->impactRadius - dir.length())/this->impactRadius); if(shape->getGroup()!=UserContactReport::COLGROUP_PLAYER) { dir = dir*this->impactForce*50; } else { dir = dir*this->impactForce*5; } shape->getActor().addForceAtPos(dir.getNxVector(), globalPos.getNxVector(), NX_IMPULSE); } } } } void Bullet::addParticleEmitter(ParticleEmitter *_emitter) { _emitter->setStandBy(true); _emitter->setVisible(false); this->particleEmitterList.push_back(_emitter); } void Bullet::addParticleCube(ParticleCube *_cube) { this->setStandBy(true); this->particleCubeList.push_back(_cube); } void Bullet::startParticleEmitter() { //Show Explosionsprite Sprite* expSprite = (Sprite*) this->myScene->createNode(Scene::NODE_SPRITE); float size = 16*this->impactForce/3; expSprite->setDimension(size, size); expSprite->setTexture("./media/textures/explosion.png"); expSprite->setLookAtCamera(true); expSprite->setAlpha(0.5f); expSprite->generatePhysicMesh(); expSprite->setFPS(35); expSprite->setRowColumnCount(8, 8); expSprite->setFrameCount(64); expSprite->setLoopAnimation(true); expSprite->setTimeToLive(2); expSprite->setPosition(this->getAbsolutePosition()); //Start Emitter and ParticleCubes Vector globalPos; Vector rightVec(1, 0, 0); Vector upVec; upVec = impactNormal.crossProd(rightVec); globalPos = this->getAbsolutePosition(); for(UINT i=0;iparticleEmitterList.size();i++) { this->particleEmitterList.at(i)->orientAndMove(impactNormal, upVec, globalPos, false); this->particleEmitterList.at(i)->setDeleteMeAfterEmission(true); this->particleEmitterList.at(i)->setStandBy(false); } for(UINT i=0;iparticleCubeList.size();i++) { this->setStandBy(false); this->particleCubeList.at(i)->orientAndMove(impactNormal, upVec, globalPos, false); } } void Bullet::killMe() { //if(this->explodeAtDeath) { this->startParticleEmitter(); //} /*for(UINT i=0;iparticleEmitterList.size();i++) { this->particleEmitterList.at(i)->killMe(); } for(UINT i=0;iparticleCubeList.size();i++) { this->particleCubeList.at(i)->killMe(); } this->particleCubeList.clear(); this->particleEmitterList.clear();*/ if(this->flySound) this->flySound->killMe(); if(this->impactSound) { this->impactSound->setPosition(this->getAbsolutePosition()); this->impactSound->setKillSoundNodeAfterPlayed(true); //this->impactSound->killMe(); } Node::killMe(); } /*void Bullet::cloneParticleEmitterAndCubes(Bullet* newBullet) { for(int i=0;iparticleEmitterList.size();i++) { newBullet->addParticleEmitter((ParticleEmitter*) this->particleEmitterList.at(i)->clone()); } for(int i=0;iparticleCubeList.size();i++) { newBullet->addParticleCube((ParticleCube*) this->particleCubeList.at(i)->clone()); } }*/