source: GTP/trunk/App/Games/Jungle_Rumble/src/ParticleEmitter.cpp @ 1378

Revision 1378, 8.4 KB checked in by giegl, 18 years ago (diff)

GTPD - Jungle Rumble - integrate into GTP SVN structure

Line 
1#include "dxstdafx.h"
2#include ".\particleemitter.h"
3#include "ParticleGroup.h"
4#include "GameManager.h"
5
6ParticleEmitter::ParticleEmitter(void) : ParticleCube() {
7        this->width = 2;
8        this->height = 2;
9        this->depth = 1;
10        this->pid = 0;
11        this->updatePlanes();
12        this->setEMBirthRate(10);
13        this->setEMEmissionDuration(10);
14        this->setEMParticleVelocity(10);
15        this->setEMHorizontalDegree(0.3f);
16        this->setEMVerticalDegree(0.3f);
17        this->setEMParticleLifeTime(5);
18        this->setEMRotationalDegree(50, 50, 50);
19        this->firstFrame = true;
20        this->deleteMe = true;
21        this->emitting = false;
22        this->useHeatHaze = false;
23        this->nodeType |= Scene::NODE_PARTICLEEMITTER;
24        this->sound = NULL;
25}
26
27ParticleEmitter::~ParticleEmitter(void) {
28}
29
30
31void ParticleEmitter::setEMBirthRate(float _birthRate) {
32        this->birthRate = _birthRate;
33}
34
35void ParticleEmitter::setEMEmissionDuration(float _emissionDur) {
36        this->lifeTime = 0;
37        this->nextEmit = 0;
38        this->pid = 0;
39        this->particleEmitted = 1;
40        this->emissionDur = _emissionDur;
41}
42
43void ParticleEmitter::setEMParticleLifeTime(float _particleLifeTime) {
44        this->particleLifeTime = _particleLifeTime;
45}
46
47void ParticleEmitter::setEMParticleVelocity(float _velocity) {
48        this->velocity = _velocity;
49}
50
51void ParticleEmitter::setEMHorizontalDegree(float _horDegree) {
52        this->horDegree = _horDegree;
53}
54
55void ParticleEmitter::setEMVerticalDegree(float _verDegree) {
56        this->verDegree = _verDegree;
57}
58
59void ParticleEmitter::setEMRotationalDegree(float _rX, float _rY, float _rZ) {
60        this->rotDegree.setXYZ(_rX, _rY, _rZ);
61}
62
63void ParticleEmitter::setDeleteMeAfterEmission(bool _deleteMe) {
64        this->deleteMe = _deleteMe;
65}
66
67void ParticleEmitter::addRefNode(Node &node) {
68        node.setVisible(false);
69        node.setStandBy(true);
70        this->refNodeVector.push_back(&node);
71}
72
73/*void ParticleEmitter::attachRefEmitter(PartikelEmitter &emitter) {
74        this->refEmitter = &emitter;
75}*/
76
77void ParticleEmitter::update(float dt) {
78        //PartikelCube::update(dt);
79        if(this->firstFrame) {
80                this->emitting = true;
81                this->firstFrame = false;
82                srand((UINT)time(NULL));
83                if(this->sound) {
84                        this->sound->play();
85                }
86                return;
87        }
88        this->lifeTime+=dt;
89
90        if(this->lifeTime < this->emissionDur) {
91                if(this->sound) {
92                        this->sound->setPosition(this->getAbsolutePosition());
93                }
94                float dtEmission = 1/this->birthRate; //this->emissionDur/this->birthRate;
95                int pCount = (int) (this->lifeTime/dtEmission);
96                int n = pCount-this->particleEmitted;
97                //ParticleEmitter *pEmitter;
98                for(int i=0; i<n;i++) {
99                        Node *node;
100                        Node *refNode;
101
102                        //pid = (int)((((float)rand())/RAND_MAX)*this->refNodeVector.size())-1;
103                        pid = (int) ((((float) rand())/(RAND_MAX+1))*(this->refNodeVector.size()-1));
104                        if(pid<0)
105                                pid = 0;
106                        if(pid>this->refNodeVector.size()-1)
107                                pid = 0;
108
109                        /*char temp[100];
110                        sprintf(temp, "refNodeVector.size = %i", this->refNodeVector.size());
111                        this->myScene->manager->printToConsole(temp);*/
112
113                        if(this->refNodeVector.size()==0) {
114                                this->myScene->manager->printToConsole("ParticleEmitter::refNodeVector.size = 0!!!");
115                                return;
116                        }
117       
118                        refNode = this->refNodeVector.at(pid);
119                        node = refNode->clone();
120                        this->myScene->addToParticleList(node);
121                        node->setStandBy(false);
122
123                        /*if(this->refEmitter!=NULL) {
124                                pEmitter = this->sMgr->createPartikelEmitter(*node);
125                                //Clone refEmitter
126                                pEmitter->setEMBirthRate(this->refEmitter->birthRate);
127                                pEmitter->setEMEmissionDuration(this->refEmitter->emissionDur);
128                                pEmitter->setEMPartikelVelocity(this->refEmitter->velocity);
129                                pEmitter->setEMHorizontalDegree(this->refEmitter->horDegree);
130                                pEmitter->setEMVerticalDegree(this->refEmitter->verDegree);
131                                pEmitter->setEMRotationalDegree(this->refEmitter->rotDegree.x, this->refEmitter->rotDegree.y, this->refEmitter->rotDegree.z);
132                                pEmitter->setEMPartikelLifeTime(this->refEmitter->partikelLifeTime);
133                                pEmitter->setDeleteMeAfterEmission(this->refEmitter->deleteEmitter);
134                                pEmitter->refNodeVector = this->refEmitter->refNodeVector;
135                                pEmitter->setDimensions(this->width, this->height, this->depth);
136                        }*/
137
138                        node->setVisible(true);
139                        //node->setParticleGroup(this->getParticleGroup());
140                        node->setTimeToLive(this->particleLifeTime);
141                        this->myParticleGroup->addParticle(this->myScene->getSmartPointer(node));
142
143                        node->setColDetGroup(this->pColDetGroup);
144
145                        //Set Velocity
146                        Vector v;
147                        v.x = (FLOAT)(((float)rand())/RAND_MAX - 0.5)*2*this->horDegree;
148                        v.y = (FLOAT)(((float)rand())/RAND_MAX - 0.5)*2*this->verDegree;
149                        v.z = 1;        //StandardDirection
150                        v.normalize();
151                        v = v*this->velocity;
152                        this->addVelocityToParticle(node, v);
153
154                        //Set EmitPosition
155                        Vector p;
156                        p = this->getAbsolutePosition();
157                        p.x += (FLOAT)(((float)rand())/RAND_MAX - 0.5)*this->width;
158                        p.y += (FLOAT)(((float)rand())/RAND_MAX - 0.5)*this->height;
159                        node->setPosition(p);
160
161                        //Set Rotation
162                        Vector rotP;
163                        rotP.x = (FLOAT)(((float)rand())/RAND_MAX - 0.5)*this->rotDegree.x;
164                        rotP.y = (FLOAT)(((float)rand())/RAND_MAX - 0.5)*this->rotDegree.y;
165                        rotP.z = (FLOAT)(((float)rand())/RAND_MAX - 0.5)*this->rotDegree.z;
166                       
167                        node->getActor()->addLocalTorque(rotP.getNxVector());
168                        node->getActor()->setSolverIterationCount(1);
169
170                        this->particleEmitted++;
171                }
172        } else if(this->deleteMe) {
173                this->killMe();
174        } else {
175                if(this->sound) {
176                        this->sound->stop();
177                }
178                this->setStandBy(true);
179        }
180        if(this->lifeTime >= this->emissionDur) {
181                if(this->sound) {
182                        this->sound->stop();
183                }
184                this->emitting = false;
185        }
186}
187
188void ParticleEmitter::restartEmitting()
189{
190        this->firstFrame = true;
191        this->lifeTime = 0;
192        this->nextEmit = 0;
193        this->pid = 0;
194        this->particleEmitted = 1;
195        this->setStandBy(false);
196}
197
198bool ParticleEmitter::isEmitting() {
199        return this->emitting;
200}
201
202void ParticleEmitter::killMe() {
203        //this->myScene->manager->printToConsole("deleteEmitter!");
204        for(UINT i=0;i<this->refNodeVector.size();i++) {
205                //this->myScene->setTrigger(Scene::TRIGGER_KILLNODE, this->refNodeVector.at(i), sceneLifeTime + this->particleLifeTime + 0.5);
206                this->refNodeVector.at(i)->killMe();
207        }
208        this->refNodeVector.clear();
209        this->myParticleGroup->removeParticleEmitter(this);
210        ParticleCube::killMe();
211}
212
213void ParticleEmitter::setParticleGroup(ParticleGroup* _particleGroup)
214{
215        this->myParticleGroup = _particleGroup;
216        this->particleGroup = this->myParticleGroup->getParticleGroup(); //Diese Zeile könnte falsch sein!!
217}
218
219void ParticleEmitter::setUseHeatHaze(bool _use)
220{
221        this->useHeatHaze = _use;
222}
223bool ParticleEmitter::getUseHeatHaze()
224{
225        return this->useHeatHaze;
226}
227
228Node* ParticleEmitter::clone()
229{
230        //this->myScene->manager->printToConsole("   cloning Emitter");
231        ParticleGroup* pg = (ParticleGroup*) this->myScene->createNode(Scene::NODE_PARTICLEGROUP, this->myParticleGroup->hasRenderer());
232
233        /*char temp[100];
234        sprintf(temp, "      new ParticleGroupID = %i", pg->getParticleGroup());
235        this->myScene->manager->printToConsole(temp);*/
236
237        ParticleEmitter* obj = (ParticleEmitter*) this->myScene->createNode(Scene::NODE_PARTICLEEMITTER);
238       
239        obj->setDimensions(this->width, this->height, this->depth);
240        obj->setEMBirthRate(this->birthRate);
241        obj->setEMEmissionDuration(this->emissionDur);
242        obj->setEMHorizontalDegree(this->horDegree);
243        obj->setEMParticleLifeTime(this->particleLifeTime);
244        obj->setEMParticleVelocity(this->velocity);
245        obj->setEMRotationalDegree(this->rotDegree.x, this->rotDegree.y, this->rotDegree.z);
246        obj->setDeleteMeAfterEmission(this->deleteMe);
247        obj->setUseHeatHaze(this->useHeatHaze);
248        obj->firstFrame = true;
249        obj->lifeTime = 0;
250        obj->pColDetGroup = this->pColDetGroup;
251
252        //RefNodes!
253        Node* node = NULL;
254        for(UINT i=0;i<this->refNodeVector.size();i++) {
255                //this->myScene->manager->printToConsole("      refNode cloned!");
256                node = this->refNodeVector.at(i)->clone();
257                node->setBehaveAs(Node::RIGIDBODY);
258                node->setColDetGroup(this->pColDetGroup);
259                obj->addRefNode(*node);
260                //obj->addRefNode(*this->refNodeVector.at(i)->clone());
261                //obj->addRefNode(*this->refNodeVector.at(i));
262        }
263        //ParticleGroup
264        //this->myParticleGroup->addParticleEmitter(this->myScene->getSmartPointer(obj));
265        pg->addParticleEmitter(this->myScene->getSmartPointer(obj));
266
267        obj->pActorDesc = this->pActorDesc;
268        obj->pBodyDesc = this->pBodyDesc;
269        obj->pActorDesc.body = &obj->pBodyDesc;
270       
271        return obj;
272}
273
274void ParticleEmitter::addEmittingSound(SoundNode* _sound)
275{
276        this->sound = _sound;
277        this->addChild(this->myScene->getSmartPointer(this->sound));
278}
Note: See TracBrowser for help on using the repository browser.