#include "dxstdafx.h" #include ".\particleemitter.h" #include "ParticleGroup.h" #include "GameManager.h" ParticleEmitter::ParticleEmitter(void) : ParticleCube() { this->width = 2; this->height = 2; this->depth = 1; this->pid = 0; this->updatePlanes(); this->setEMBirthRate(10); this->setEMEmissionDuration(10); this->setEMParticleVelocity(10); this->setEMHorizontalDegree(0.3f); this->setEMVerticalDegree(0.3f); this->setEMParticleLifeTime(5); this->setEMRotationalDegree(50, 50, 50); this->firstFrame = true; this->deleteMe = true; this->emitting = false; this->useHeatHaze = false; this->nodeType |= Scene::NODE_PARTICLEEMITTER; this->sound = NULL; } ParticleEmitter::~ParticleEmitter(void) { } void ParticleEmitter::setEMBirthRate(float _birthRate) { this->birthRate = _birthRate; } void ParticleEmitter::setEMEmissionDuration(float _emissionDur) { this->lifeTime = 0; this->nextEmit = 0; this->pid = 0; this->particleEmitted = 1; this->emissionDur = _emissionDur; } void ParticleEmitter::setEMParticleLifeTime(float _particleLifeTime) { this->particleLifeTime = _particleLifeTime; } void ParticleEmitter::setEMParticleVelocity(float _velocity) { this->velocity = _velocity; } void ParticleEmitter::setEMHorizontalDegree(float _horDegree) { this->horDegree = _horDegree; } void ParticleEmitter::setEMVerticalDegree(float _verDegree) { this->verDegree = _verDegree; } void ParticleEmitter::setEMRotationalDegree(float _rX, float _rY, float _rZ) { this->rotDegree.setXYZ(_rX, _rY, _rZ); } void ParticleEmitter::setDeleteMeAfterEmission(bool _deleteMe) { this->deleteMe = _deleteMe; } void ParticleEmitter::addRefNode(Node &node) { node.setVisible(false); node.setStandBy(true); this->refNodeVector.push_back(&node); } /*void ParticleEmitter::attachRefEmitter(PartikelEmitter &emitter) { this->refEmitter = &emitter; }*/ void ParticleEmitter::update(float dt) { //PartikelCube::update(dt); if(this->firstFrame) { this->emitting = true; this->firstFrame = false; srand((UINT)time(NULL)); if(this->sound) { this->sound->play(); } return; } this->lifeTime+=dt; if(this->lifeTime < this->emissionDur) { if(this->sound) { this->sound->setPosition(this->getAbsolutePosition()); } float dtEmission = 1/this->birthRate; //this->emissionDur/this->birthRate; int pCount = (int) (this->lifeTime/dtEmission); int n = pCount-this->particleEmitted; //ParticleEmitter *pEmitter; for(int i=0; irefNodeVector.size())-1; pid = (int) ((((float) rand())/(RAND_MAX+1))*(this->refNodeVector.size()-1)); if(pid<0) pid = 0; if(pid>this->refNodeVector.size()-1) pid = 0; /*char temp[100]; sprintf(temp, "refNodeVector.size = %i", this->refNodeVector.size()); this->myScene->manager->printToConsole(temp);*/ if(this->refNodeVector.size()==0) { this->myScene->manager->printToConsole("ParticleEmitter::refNodeVector.size = 0!!!"); return; } refNode = this->refNodeVector.at(pid); node = refNode->clone(); this->myScene->addToParticleList(node); node->setStandBy(false); /*if(this->refEmitter!=NULL) { pEmitter = this->sMgr->createPartikelEmitter(*node); //Clone refEmitter pEmitter->setEMBirthRate(this->refEmitter->birthRate); pEmitter->setEMEmissionDuration(this->refEmitter->emissionDur); pEmitter->setEMPartikelVelocity(this->refEmitter->velocity); pEmitter->setEMHorizontalDegree(this->refEmitter->horDegree); pEmitter->setEMVerticalDegree(this->refEmitter->verDegree); pEmitter->setEMRotationalDegree(this->refEmitter->rotDegree.x, this->refEmitter->rotDegree.y, this->refEmitter->rotDegree.z); pEmitter->setEMPartikelLifeTime(this->refEmitter->partikelLifeTime); pEmitter->setDeleteMeAfterEmission(this->refEmitter->deleteEmitter); pEmitter->refNodeVector = this->refEmitter->refNodeVector; pEmitter->setDimensions(this->width, this->height, this->depth); }*/ node->setVisible(true); //node->setParticleGroup(this->getParticleGroup()); node->setTimeToLive(this->particleLifeTime); this->myParticleGroup->addParticle(this->myScene->getSmartPointer(node)); node->setColDetGroup(this->pColDetGroup); //Set Velocity Vector v; v.x = (FLOAT)(((float)rand())/RAND_MAX - 0.5)*2*this->horDegree; v.y = (FLOAT)(((float)rand())/RAND_MAX - 0.5)*2*this->verDegree; v.z = 1; //StandardDirection v.normalize(); v = v*this->velocity; this->addVelocityToParticle(node, v); //Set EmitPosition Vector p; p = this->getAbsolutePosition(); p.x += (FLOAT)(((float)rand())/RAND_MAX - 0.5)*this->width; p.y += (FLOAT)(((float)rand())/RAND_MAX - 0.5)*this->height; node->setPosition(p); //Set Rotation Vector rotP; rotP.x = (FLOAT)(((float)rand())/RAND_MAX - 0.5)*this->rotDegree.x; rotP.y = (FLOAT)(((float)rand())/RAND_MAX - 0.5)*this->rotDegree.y; rotP.z = (FLOAT)(((float)rand())/RAND_MAX - 0.5)*this->rotDegree.z; node->getActor()->addLocalTorque(rotP.getNxVector()); node->getActor()->setSolverIterationCount(1); this->particleEmitted++; } } else if(this->deleteMe) { this->killMe(); } else { if(this->sound) { this->sound->stop(); } this->setStandBy(true); } if(this->lifeTime >= this->emissionDur) { if(this->sound) { this->sound->stop(); } this->emitting = false; } } void ParticleEmitter::restartEmitting() { this->firstFrame = true; this->lifeTime = 0; this->nextEmit = 0; this->pid = 0; this->particleEmitted = 1; this->setStandBy(false); } bool ParticleEmitter::isEmitting() { return this->emitting; } void ParticleEmitter::killMe() { //this->myScene->manager->printToConsole("deleteEmitter!"); for(UINT i=0;irefNodeVector.size();i++) { //this->myScene->setTrigger(Scene::TRIGGER_KILLNODE, this->refNodeVector.at(i), sceneLifeTime + this->particleLifeTime + 0.5); this->refNodeVector.at(i)->killMe(); } this->refNodeVector.clear(); this->myParticleGroup->removeParticleEmitter(this); ParticleCube::killMe(); } void ParticleEmitter::setParticleGroup(ParticleGroup* _particleGroup) { this->myParticleGroup = _particleGroup; this->particleGroup = this->myParticleGroup->getParticleGroup(); //Diese Zeile könnte falsch sein!! } void ParticleEmitter::setUseHeatHaze(bool _use) { this->useHeatHaze = _use; } bool ParticleEmitter::getUseHeatHaze() { return this->useHeatHaze; } Node* ParticleEmitter::clone() { //this->myScene->manager->printToConsole(" cloning Emitter"); ParticleGroup* pg = (ParticleGroup*) this->myScene->createNode(Scene::NODE_PARTICLEGROUP, this->myParticleGroup->hasRenderer()); /*char temp[100]; sprintf(temp, " new ParticleGroupID = %i", pg->getParticleGroup()); this->myScene->manager->printToConsole(temp);*/ ParticleEmitter* obj = (ParticleEmitter*) this->myScene->createNode(Scene::NODE_PARTICLEEMITTER); obj->setDimensions(this->width, this->height, this->depth); obj->setEMBirthRate(this->birthRate); obj->setEMEmissionDuration(this->emissionDur); obj->setEMHorizontalDegree(this->horDegree); obj->setEMParticleLifeTime(this->particleLifeTime); obj->setEMParticleVelocity(this->velocity); obj->setEMRotationalDegree(this->rotDegree.x, this->rotDegree.y, this->rotDegree.z); obj->setDeleteMeAfterEmission(this->deleteMe); obj->setUseHeatHaze(this->useHeatHaze); obj->firstFrame = true; obj->lifeTime = 0; obj->pColDetGroup = this->pColDetGroup; //RefNodes! Node* node = NULL; for(UINT i=0;irefNodeVector.size();i++) { //this->myScene->manager->printToConsole(" refNode cloned!"); node = this->refNodeVector.at(i)->clone(); node->setBehaveAs(Node::RIGIDBODY); node->setColDetGroup(this->pColDetGroup); obj->addRefNode(*node); //obj->addRefNode(*this->refNodeVector.at(i)->clone()); //obj->addRefNode(*this->refNodeVector.at(i)); } //ParticleGroup //this->myParticleGroup->addParticleEmitter(this->myScene->getSmartPointer(obj)); pg->addParticleEmitter(this->myScene->getSmartPointer(obj)); obj->pActorDesc = this->pActorDesc; obj->pBodyDesc = this->pBodyDesc; obj->pActorDesc.body = &obj->pBodyDesc; return obj; } void ParticleEmitter::addEmittingSound(SoundNode* _sound) { this->sound = _sound; this->addChild(this->myScene->getSmartPointer(this->sound)); }