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

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

GTPD - Jungle Rumble - integrate into GTP SVN structure

Line 
1#include "dxstdafx.h"
2#include ".\bullet.h"
3#include "Player.h"
4#include "SoundNode.h"
5#include "GameManager.h"
6
7Bullet::Bullet(void) : Node() {
8        this->bulletType = 0;
9        this->exploded = false;
10        this->explodeAtDeath = false;
11        this->impactDamage = 15;
12        this->impactRadius = 2;
13        this->impactForce = 2;
14        this->impactSound = NULL;
15        this->flySound = NULL;
16        this->impactSound = NULL;
17        this->terrainHit = false;
18        this->impactNormal.setXYZ(0, 1, 0);
19}
20
21Bullet::~Bullet(void) {
22        int asdf=0;
23        asdf++;
24}
25
26void Bullet::setWeaponDirection(Vector _dir)
27{
28        this->dir = _dir;
29}
30
31Vector Bullet::getDirection()
32{
33        return this->dir;
34}
35
36void Bullet::setImpactForce(float _impactForce)
37{
38        this->impactForce = _impactForce;
39}
40
41float Bullet::getImpactForce()
42{
43        return this->impactForce;
44}
45
46void Bullet::setImpactDamage(float _impactDamage)
47{
48        this->impactDamage = _impactDamage;
49}
50
51float Bullet::getImpactDamage()
52{
53        return this->impactDamage;
54}
55
56void Bullet::setImpactRadius(float _radius)
57{
58        this->impactRadius = _radius;
59}
60
61float Bullet::getImpactRadius()
62{
63        return this->impactRadius;
64}
65
66void Bullet::setFlySound(std::string flySoundName)
67{
68        if(flySoundName.compare("")!=0) {
69                this->flySound = (SoundNode*) this->myScene->createNode(this->myScene->NODE_SOUND, *this);
70                this->flySound->loadFile(flySoundName, true);
71        }
72}
73
74void Bullet::setImpactSound(std::string impactSoundName)
75{
76        if(impactSoundName.compare("")!=0) {
77                this->impactSound = (SoundNode*) this->myScene->createNode(this->myScene->NODE_SOUND);
78                this->impactSound->loadFile(impactSoundName, false);
79        }
80}
81
82/*void Bullet::setNeighbour(WPTR<Node> _neighbour)
83{
84        this->neighbour = _neighbour;
85}*/
86
87void Bullet::setPlayer(Player* _myPlayer)
88{
89        this->myPlayer = _myPlayer;
90}
91
92void Bullet::setBulletType(int type)
93{
94        this->bulletType = type;
95}
96
97int Bullet::getBulletType()
98{
99        return this->bulletType;
100}
101
102void Bullet::setExplodeAtDeath(bool _explodeAtDeath)
103{
104        this->explodeAtDeath = _explodeAtDeath;
105}
106
107void Bullet::update(float dt) {
108        if(this->explodeAtDeath) {
109                this->timeToLive-=dt;
110                //Read collisiondata and execute impact***
111                /*char temp[100];
112                sprintf(temp, "timetolive = %f",this->timeToLive);
113                this->myScene->manager->printToConsole(temp);*/
114                if(this->timeToLive<=0 && this->timeToLive!=-100) {
115                        this->exploded = true;
116                        //this->impactTerrain();
117                        this->impactTimeOut();
118                }
119        } else {
120                Node::update(dt);
121        }
122}
123
124void Bullet::collectEnvironmentInformation()
125{
126        this->terrainHit = false;
127        this->shapeList.clear();
128        NxSphere worldSphere(this->getAbsolutePosition().getNxVector(), this->impactRadius);
129    NxU32 nbShapes = this->myScene->pScene->getNbDynamicShapes();
130    NxShape** shapes = new NxShape* [nbShapes];
131    for (NxU32 i = 0; i < nbShapes; i++)
132                shapes[i] = NULL;
133    this->myScene->pScene->overlapSphereShapes(worldSphere, NX_DYNAMIC_SHAPES, nbShapes, shapes, NULL);
134        for(UINT i=0;i<nbShapes;i++) {
135                if(shapes[i]!=NULL) {
136                        if(shapes[i]->getActor().getGroup()!=UserContactReport::COLGROUP_BULLET) {
137                                this->shapeList.push_back(shapes[i]);
138                        }
139                        switch(shapes[i]->getActor().getGroup()) {
140                                case UserContactReport::COLGROUP_PLAYER:
141                                        this->playerList.push_back((Player*) shapes[i]->getActor().userData);
142                                        break;
143                                case UserContactReport::COLGROUP_TERRAIN:
144                                        this->terrainHit = true;
145                                        break;
146                                case UserContactReport::COLGROUP_BULLET:
147                                        this->bulletList.push_back((Bullet*) shapes[i]->getActor().userData);
148                                case UserContactReport::COLGROUP_GOODIE:
149                                        this->goodieList.push_back((Goodie*) shapes[i]->getActor().userData);
150                                        break;
151                        }
152                } else {
153                        break;
154                }
155        }
156        delete[] shapes;
157}
158
159
160void Bullet::setStandardExplosionForces()
161{
162        //std::vector<NxShape*> shapeList = this->getShapesInSphere(radius);
163        Vector globalPos = this->getAbsolutePosition();
164        Vector dir;
165        NxShape *shape;
166        Vector sPos;
167        for(UINT i=0;i<this->shapeList.size();i++) {
168                shape = this->shapeList.at(i);
169                if(shape->getGroup()!=UserContactReport::COLGROUP_NOCOL &&
170                   shape->getGroup()!=UserContactReport::COLGROUP_TERRAIN &&
171                   !shape->getActor().readBodyFlag(NX_BF_KINEMATIC)) {
172                        sPos.setNxVector(shape->getGlobalPosition());
173                        dir = sPos - globalPos;
174                        if(dir.length()<this->impactRadius) {
175                                //Object in Explosionsradius
176                                dir = dir*((this->impactRadius - dir.length())/this->impactRadius);
177                                if(shape->getGroup()!=UserContactReport::COLGROUP_PLAYER) {
178                                        dir = dir*this->impactForce*50;
179                                } else {
180                                        dir = dir*this->impactForce*5;
181                                }
182                                shape->getActor().addForceAtPos(dir.getNxVector(), globalPos.getNxVector(), NX_IMPULSE);
183                        }
184                }
185        }
186}
187
188void Bullet::addParticleEmitter(ParticleEmitter *_emitter)
189{
190        _emitter->setStandBy(true);
191        _emitter->setVisible(false);
192        this->particleEmitterList.push_back(_emitter);
193}
194
195void Bullet::addParticleCube(ParticleCube *_cube)
196{
197        this->setStandBy(true);
198        this->particleCubeList.push_back(_cube);
199}
200
201void Bullet::startParticleEmitter()
202{
203        //Show Explosionsprite
204        Sprite* expSprite = (Sprite*) this->myScene->createNode(Scene::NODE_SPRITE);
205        float size = 16*this->impactForce/3;
206        expSprite->setDimension(size, size);
207        expSprite->setTexture("./media/textures/explosion.png");
208        expSprite->setLookAtCamera(true);
209        expSprite->setAlpha(0.5f);
210        expSprite->generatePhysicMesh();
211        expSprite->setFPS(35);
212        expSprite->setRowColumnCount(8, 8);
213        expSprite->setFrameCount(64);
214        expSprite->setLoopAnimation(true);
215        expSprite->setTimeToLive(2);
216        expSprite->setPosition(this->getAbsolutePosition());
217
218        //Start Emitter and ParticleCubes
219        Vector globalPos;
220        Vector rightVec(1, 0, 0);
221        Vector upVec;
222        upVec = impactNormal.crossProd(rightVec);
223        globalPos = this->getAbsolutePosition();
224        for(UINT i=0;i<this->particleEmitterList.size();i++) {
225                this->particleEmitterList.at(i)->orientAndMove(impactNormal, upVec, globalPos, false);
226                this->particleEmitterList.at(i)->setDeleteMeAfterEmission(true);
227                this->particleEmitterList.at(i)->setStandBy(false);
228        }
229        for(UINT i=0;i<this->particleCubeList.size();i++) {
230                this->setStandBy(false);       
231                this->particleCubeList.at(i)->orientAndMove(impactNormal, upVec, globalPos, false);
232        }
233}
234
235void Bullet::killMe()
236{
237        //if(this->explodeAtDeath) {
238                this->startParticleEmitter();
239        //}
240        /*for(UINT i=0;i<this->particleEmitterList.size();i++) {
241                this->particleEmitterList.at(i)->killMe();
242        }
243        for(UINT i=0;i<this->particleCubeList.size();i++) {
244                this->particleCubeList.at(i)->killMe();
245        }
246        this->particleCubeList.clear();
247        this->particleEmitterList.clear();*/
248        if(this->flySound)
249                this->flySound->killMe();
250        if(this->impactSound) {
251                this->impactSound->setPosition(this->getAbsolutePosition());
252                this->impactSound->setKillSoundNodeAfterPlayed(true);
253                //this->impactSound->killMe();
254        }
255        Node::killMe();
256}
257
258/*void Bullet::cloneParticleEmitterAndCubes(Bullet* newBullet) {
259        for(int i=0;i<this->particleEmitterList.size();i++) {
260                newBullet->addParticleEmitter((ParticleEmitter*) this->particleEmitterList.at(i)->clone());
261        }
262        for(int i=0;i<this->particleCubeList.size();i++) {
263                newBullet->addParticleCube((ParticleCube*) this->particleCubeList.at(i)->clone());
264        }
265}*/
Note: See TracBrowser for help on using the repository browser.