source: GTP/trunk/App/Games/Jungle_Rumble/src/GameScene.cpp @ 1401

Revision 1401, 33.8 KB checked in by giegl, 18 years ago (diff)

GTPD - Jungle Rumble - fixed all warnings (except performance)

Line 
1#include "dxstdafx.h"
2#include "GameScene.h"
3#include "Player.h"
4#include "UserPlayer.h"
5#include "AIPlayer.h"
6#include "SimpleWeapon.h"
7#include "SimpleBullet.h"
8#include "Bullet.h"
9#include "Weapon.h"
10#include "Terrain.h"
11#include "HUD.h"
12#include "Ocean.h"
13#include "SkyBox.h"
14#include "GameSceneLoader.h"
15#include "Goodie.h"
16#include "UniversalWeapon.h"
17#include "SoundNode.h"
18#include <cstdlib>
19#include <ctime>
20#include <typeinfo>
21#include <string>
22
23#define M_PI 3.1415
24
25GameScene::GameScene(void) : Scene()
26{
27        srand( (unsigned)time( NULL ) );
28        this->terrain = 0;
29        this->ocean = 0;
30        this->skybox = 0;
31        this->bgSoundChannel = -1;
32        this->cameraFixedCount = 0;
33        this->flyCamEnabled = false;
34
35        this->sadSound = NULL;
36
37        this->happySound = NULL;
38
39        this->alreadyUsed = false;
40        this->aiPlayerFireEnable = true;
41        this->showFPS = false;
42
43        this->maxEnemyCount = 1;
44
45        this->recalcTransformations = true;
46
47        this->currentlypaused = false;
48        this->preparepaused = false;
49}
50
51GameScene::~GameScene(void)
52{
53        //this->clearScene();
54        /*if(this->defaultRenderTarget) {
55                delete this->defaultRenderTarget;
56                this->defaultRenderTarget = NULL;
57        }*/
58}
59
60void GameScene::clearScene()
61{
62        //this->fetchPhysicResults();
63
64       
65        this->terrain = 0;
66        this->ocean = 0;
67        this->skybox = 0;
68        this->bgSoundChannel = -1;
69        this->cameraFixedCount = 0;
70        this->flyCamEnabled = false;
71        this->firstFrame = true;
72
73        /*if(this->happySound) {
74                this->happySound->killMe();
75                this->happySound=NULL;
76        }
77        if(this->sadSound) {
78                this->sadSound->killMe();
79                this->sadSound=NULL;
80        }*/
81       
82               
83        //Game cleanup
84       
85        /*std::list<SPTR<Node> >::iterator it;
86        Player* p;
87        exception e;
88        for(it=this->playerList.begin();it!=this->playerList.end();it++) {
89                p = (Player*) (*it).get();
90                if(p) {
91                        try {
92                                if(p->upperSpringActor) {
93                                        this->pScene->releaseActor(*p->upperSpringActor);
94                                        this->pScene->releaseJoint(*p->upperJoint);
95                                        p->upperJoint = NULL;
96                                        p->upperSpringActor = NULL;
97                                }
98                        } catch(exception e) {
99                        }
100                }
101        }*/
102       
103        this->cleanUpScene();
104
105        playerList.clear();
106        goodieList.clear();
107        bulletList.clear();
108        materialXMLVector.clear();
109        materialPhysicVector.clear();
110        refWeaponVector.clear();                        //List of all Weapons
111        refBulletVector.clear();
112        this->cameraFixedCount = 0;
113        this->sadSound = NULL;
114        this->happySound = NULL;
115        this->currentlypaused = false;
116        this->preparepaused = false;
117
118        Scene::clearScene();
119
120}
121
122void GameScene::initScene(GameManager &_manager) {
123        this->resultVisible = false;
124        this->won = false;
125        this->recalcTransformations = true;
126        this->resultShowingTimer = 0;
127        this->setContactReport(&this->userContactReport);
128        this->setTriggerReport(&this->userTriggerReport);
129        this->setNotifyReport(&this->userNotify);
130        this->descSet = false;
131        this->currentlypaused = false;
132        this->preparepaused = false;
133        Scene::initScene(_manager);
134       
135        //Define ColDetGroups
136
137        //Terrain Stuff
138        this->pScene->setActorGroupPairFlags(UserContactReport::COLGROUP_TERRAIN, UserContactReport::COLGROUP_TERRAIN, NX_IGNORE_PAIR);
139        //this->pScene->setActorGroupPairFlags(UserContactReport::COLGROUP_TERRAIN, UserContactReport::COLGROUP_PLAYER, NX_IGNORE_PAIR);
140        this->pScene->setActorGroupPairFlags(UserContactReport::COLGROUP_TERRAIN, UserContactReport::COLGROUP_BULLET, NX_NOTIFY_ON_START_TOUCH);
141        this->pScene->setActorGroupPairFlags(UserContactReport::COLGROUP_TERRAIN, UserContactReport::COLGROUP_GOODIE, NX_IGNORE_PAIR);
142        this->pScene->setActorGroupPairFlags(UserContactReport::COLGROUP_TERRAIN, UserContactReport::COLGROUP_OTHER, NX_IGNORE_PAIR);
143       
144        //Bullet Stuff
145        this->pScene->setActorGroupPairFlags(UserContactReport::COLGROUP_BULLET, UserContactReport::COLGROUP_GOODIE, NX_NOTIFY_ON_START_TOUCH);
146        this->pScene->setActorGroupPairFlags(UserContactReport::COLGROUP_BULLET, UserContactReport::COLGROUP_OTHER, NX_NOTIFY_ON_START_TOUCH);
147        this->pScene->setActorGroupPairFlags(UserContactReport::COLGROUP_BULLET, UserContactReport::COLGROUP_OBSTACLE, NX_NOTIFY_ON_START_TOUCH);
148        this->pScene->setActorGroupPairFlags(UserContactReport::COLGROUP_BULLET, UserContactReport::COLGROUP_ONEPLAYER, NX_NOTIFY_ON_START_TOUCH);
149        this->pScene->setActorGroupPairFlags(UserContactReport::COLGROUP_BULLET, UserContactReport::COLGROUP_MOREPLAYER, NX_NOTIFY_ON_START_TOUCH);
150
151    //Player Stuff
152        this->pScene->setGroupCollisionFlag(UserContactReport::COLGROUP_PLAYER, UserContactReport::COLGROUP_TERRAIN, false);
153        this->pScene->setActorGroupPairFlags(UserContactReport::COLGROUP_PLAYER, UserContactReport::COLGROUP_BULLET, NX_NOTIFY_ON_START_TOUCH);
154        this->pScene->setActorGroupPairFlags(UserContactReport::COLGROUP_PLAYER, UserContactReport::COLGROUP_GOODIE, NX_NOTIFY_ON_START_TOUCH);
155        this->pScene->setActorGroupPairFlags(UserContactReport::COLGROUP_PLAYER, UserContactReport::COLGROUP_PLAYER, NX_NOTIFY_ON_START_TOUCH | NX_NOTIFY_ON_END_TOUCH);
156        this->pScene->setActorGroupPairFlags(UserContactReport::COLGROUP_PLAYER, UserContactReport::COLGROUP_FIRE, NX_NOTIFY_ON_START_TOUCH);
157        this->pScene->setActorGroupPairFlags(UserContactReport::COLGROUP_PLAYER, UserContactReport::COLGROUP_ICE, NX_NOTIFY_ON_START_TOUCH);
158
159        this->pScene->setGroupCollisionFlag(UserContactReport::COLGROUP_NOSELFCOL, UserContactReport::COLGROUP_NOSELFCOL, false);
160        this->pScene->setGroupCollisionFlag(UserContactReport::COLGROUP_NOCOL, UserContactReport::COLGROUP_NOSELFCOL, false);
161
162        //Disable flag for noCol group!
163        this->pScene->setActorGroupPairFlags(UserContactReport::COLGROUP_NOCOL, UserContactReport::COLGROUP_TERRAIN, NX_IGNORE_PAIR);
164        this->pScene->setActorGroupPairFlags(UserContactReport::COLGROUP_NOCOL, UserContactReport::COLGROUP_PLAYER, NX_IGNORE_PAIR);
165        this->pScene->setActorGroupPairFlags(UserContactReport::COLGROUP_NOCOL, UserContactReport::COLGROUP_BULLET, NX_IGNORE_PAIR);
166        this->pScene->setActorGroupPairFlags(UserContactReport::COLGROUP_NOCOL, UserContactReport::COLGROUP_GOODIE, NX_IGNORE_PAIR);
167        this->pScene->setActorGroupPairFlags(UserContactReport::COLGROUP_NOCOL, UserContactReport::COLGROUP_OTHER, NX_IGNORE_PAIR);
168        this->pScene->setActorGroupPairFlags(UserContactReport::COLGROUP_NOCOL, UserContactReport::COLGROUP_NOCOL, NX_IGNORE_PAIR);
169
170        this->pScene->setGroupCollisionFlag(UserContactReport::COLGROUP_NOCOL, UserContactReport::COLGROUP_TERRAIN, false);
171        this->pScene->setGroupCollisionFlag(UserContactReport::COLGROUP_NOCOL, UserContactReport::COLGROUP_PLAYER, false);
172        this->pScene->setGroupCollisionFlag(UserContactReport::COLGROUP_NOCOL, UserContactReport::COLGROUP_BULLET, false);
173        this->pScene->setGroupCollisionFlag(UserContactReport::COLGROUP_NOCOL, UserContactReport::COLGROUP_GOODIE, false);
174        this->pScene->setGroupCollisionFlag(UserContactReport::COLGROUP_NOCOL, UserContactReport::COLGROUP_OTHER, false);
175        this->pScene->setGroupCollisionFlag(UserContactReport::COLGROUP_NOCOL, UserContactReport::COLGROUP_NOCOL, false);
176        this->pScene->setGroupCollisionFlag(UserContactReport::COLGROUP_NOCOL, UserContactReport::COLGROUP_FIRE, false);
177        this->pScene->setGroupCollisionFlag(UserContactReport::COLGROUP_NOCOL, UserContactReport::COLGROUP_ONEPLAYER, false);
178        this->pScene->setGroupCollisionFlag(UserContactReport::COLGROUP_NOCOL, UserContactReport::COLGROUP_MOREPLAYER, false);
179        this->pScene->setGroupCollisionFlag(UserContactReport::COLGROUP_NOCOL, 0, false);
180
181        //Disable Fire fire collision
182        this->pScene->setGroupCollisionFlag(UserContactReport::COLGROUP_FIRE, UserContactReport::COLGROUP_FIRE, false);
183
184        if(!this->happySound) {
185                this->happySound = (SoundNode*) this->createNode(Scene::NODE_SOUND);
186                this->happySound->loadFile("./media/sound/win.mp3", false);
187        }
188        if(!this->sadSound) {
189                this->sadSound = (SoundNode*) this->createNode(Scene::NODE_SOUND);
190                this->sadSound->loadFile("./media/sound/lose.mp3", false);
191        }
192        this->cameraFixedCount=0;
193}
194
195void GameScene::renderScene(float fElapsedTime)
196{
197        if(this->isVisible()) {
198                if(this->device==0) {
199                        this->device = DXUTGetD3DDevice();
200                        this->physxRenderer.setDevice(*this->device);
201                }
202
203                if(this->preparepaused) {
204                        this->currentlypaused = true;
205                }
206
207                //this->device->SetRenderTarget(0, this->finalImageSurface);
208                //this->manager->printToConsole("renderScene");
209                //Execute RenderChain
210                /*std::list<void(Scene::*)()>::iterator chainIt;
211                for(chainIt=this->renderChain.begin(); chainIt!=this->renderChain.end(); chainIt++) {
212                        (*this.**chainIt)();
213                }*/
214                if(this->firstFrame) {
215                        this->playBackgroundSound();
216                }
217
218                if(!this->currentlypaused) {
219                        this->takeTime();
220                }
221               
222                //ClearScreen
223                //this->clearRenderTargetAndZBuffer();
224                //Clear SharedResources
225                this->clearSharedResources();
226                //Fetch Physic Results
227        if(this->recalcTransformations && !this->currentlypaused) {
228                        this->fetchPhysicResults();
229                }
230                //Fading Stuff
231        if(!this->currentlypaused) {
232                        this->doFade();
233                        //FIRST GAME UPDATE
234                        this->updateGameFirst();
235                        //Trigger updates
236                        this->updateTrigger();
237                        //Update Nodes
238                        this->updateNodes();
239                        //Calculate Transformations
240                        if(this->recalcTransformations) {
241                                this->calculateTransformations();
242                        }
243                        //SECOND GAME UPDATE
244                        this->updateGameSecond();
245                }
246
247                //startRenderPasses
248                this->startRenderPasses();
249
250                //Cleanup Memory
251                this->cleanUpScene();
252
253                if(!this->currentlypaused) {
254                        //Start Physic
255                        if(this->recalcTransformations) {
256                                this->startPhysic();
257                        }
258                }
259                this->cnt = 0;
260                this->firstFrame = false;
261        }
262}
263
264void GameScene::cleanUpScene()
265{
266        std::list<SPTR<Node> >::iterator it;
267        std::list<SPTR<Node> >::iterator nit;
268        std::vector<SPTR<Renderer> >::iterator rit;
269        Node* node;
270        for(it=this->zombieNodeList.begin();it!=this->zombieNodeList.end();it++) {
271                node = (*it).get();
272                node->getFather()->removeChild(*it);
273
274                //Delete Renderer
275                if(node->hasRenderer())
276                {
277                        Renderer *r = node->getRenderer().get();
278                        for(rit=this->rendererList.begin();rit!=this->rendererList.end();rit++) {
279                                if((*rit).get() == r)
280                                {
281                                        this->rendererList.erase(rit);
282                                        break;
283                                }
284                        }
285                }
286
287                //Delete Physic Actor
288                if(node->getActor()!=NULL)
289                {
290                        this->pScene->releaseActor(*node->getActor());
291                }
292
293                this->deleteNodeInList(node, this->nodeList);
294                this->deleteNodeInList(node, this->particleList);
295                this->deleteNodeInList(node, this->soundNodeList);
296                this->deleteNodeInList(node, this->playerList);
297                this->deleteNodeInList(node, this->goodieList);
298                this->deleteNodeInList(node, this->bulletList);
299        }
300        this->zombieNodeList.clear();
301}
302
303Terrain* GameScene::getTerrain()
304{
305        return this->terrain;
306}
307
308HUD* GameScene::getHUD()
309{
310        return this->hud;
311}
312
313Node* GameScene::createNode(int type)
314{
315        return this->createNode(type, this->root, true, false);
316}
317
318Node* GameScene::createNode(int type, Node &father)
319{
320        return this->createNode(type, father, true, false);
321}
322
323Node* GameScene::createNode(int type, bool addDefaultRenderer)
324{
325        return this->createNode(type, this->root, addDefaultRenderer, false);
326}
327
328Node* GameScene::createNode(int type, Node &father, bool addDefaultRenderer) {
329        return this->createNode(type, father, addDefaultRenderer, false);
330}
331
332Node* GameScene::createNode(int type, Node &father, bool addDefaultRenderer, bool isReference)
333{
334        switch(type)  {
335                case NODE_TERRAIN:
336                        {
337                                SPTR<Node> node(new Terrain);
338                                father.addChild(node);
339                                this->nodeList.push_back(node);
340                                node->setScene(*this);
341                                Node *temp = node.get();
342                                this->terrain = (Terrain *) temp;
343                                return temp;
344                        }
345                        break;
346                case NODE_OCEAN:
347                        {
348                                SPTR<Node> node(new Ocean);
349                                father.addChild(node);
350                                this->nodeList.push_back(node);
351                                node->setScene(*this);
352                                Node *temp = node.get();
353                                this->ocean = (Ocean *) temp;
354                                return temp;
355                        }
356                        break;
357                case NODE_HUD:
358                        {
359                                SPTR<Node> node(new HUD);
360                                father.addChild(node);
361                                this->nodeList.push_back(node);
362                                node->setScene(*this);
363                                Node *temp = node.get();
364                                this->hud = (HUD *) temp;
365                                return temp;
366                        }
367                        break;
368                case NODE_SKYBOX:
369                        {
370                                SPTR<Node> node(new SkyBox);
371                                father.addChild(node);
372                                this->nodeList.push_back(node);
373                                node->setScene(*this);
374                                Node *temp = node.get();
375                                this->skybox = (SkyBox *) temp;
376                                return temp;
377                        }
378                        break;
379                case NODE_USERPLAYER:
380                        {
381                                SPTR<Node> node(new UserPlayer);
382                                father.addChild(node);
383                                this->nodeList.push_back(node);
384                                this->playerList.push_back(node);
385                                node->setScene(*this);
386                                this->player = (UserPlayer *) node.get();
387                                return node.get();
388                        }
389                        break;
390                case NODE_AIPLAYER:
391                        {
392                                SPTR<Node> node(new AIPlayer);
393                                father.addChild(node);
394                                this->nodeList.push_back(node);
395                                this->playerList.push_back(node);
396                                node->setScene(*this);
397                                return node.get();
398                        }
399                        break;
400                case NODE_SIMPLEWEAPON:
401                        {
402                                SPTR<Node> node(new SimpleWeapon);
403                                node->setScene(*this);
404                                if(!isReference) {
405                                        father.addChild(node);
406                                        this->nodeList.push_back(node);
407                                } else {
408                                        this->refWeaponVector.push_back(node);
409                                }
410                                return node.get();
411                        }
412                        break;
413                case NODE_GOODIE:
414                        {
415                                SPTR<Node> node(new Goodie);
416                                node->setScene(*this);
417                                //if(!isReference) {
418                                        father.addChild(node);
419                                        this->nodeList.push_back(node);
420                                /*} else {
421                                        this->refWeaponList.push_back(node);
422                                }*/
423                                if(addDefaultRenderer) {
424                                        SPTR<Renderer> renderer(new SimpleMeshRenderer);
425                                        renderer->setScene(*this);
426                                        WPTR<Node> wn(node);
427                                        renderer->setNode(wn);
428                                        node->setRenderer(renderer);
429                                        renderer->init();
430                                        this->rendererList.push_back(renderer);
431                                }
432                                return node.get();
433                        }
434                        break;
435                case NODE_SIMPLEBULLET:
436                        {
437                                SPTR<Node> node(new SimpleBullet);
438                                node->setScene(*this);
439                                if(!isReference) {
440                                        father.addChild(node);
441                                        this->nodeList.push_back(node);
442                                        this->bulletList.push_back(node);
443                                } else {
444                                        this->refBulletVector.push_back(node);
445                                }
446                                return node.get();
447                        }
448                        break;
449                case NODE_UNIVERSALWEAPON:
450                        {
451                                SPTR<Node> node(new UniversalWeapon);
452                                node->setScene(*this);
453                                if(!isReference) {
454                                        father.addChild(node);
455                                        this->nodeList.push_back(node);
456                                } else {
457                                        this->refWeaponVector.push_back(node);
458                                }
459                                return node.get();
460                        }
461                        break;
462                default:
463                        return Scene::createNode(type, father, addDefaultRenderer, isReference);
464                        break;
465        }
466}
467
468void GameScene::loadGame(std::string filename)
469{
470        GameSceneLoader loader;
471        if(!loader.loadGameScene(*this, filename, this->manager)) {//loadGameScene(*this, filename)) {
472                this->manager->printToConsole("Loading failed!");
473
474                //Initialize rest
475                //TODO Object3d preloading, sound preloading usw.
476        }
477}
478
479/*void GameScene::setMaterialId(int desiredId, int realId) {
480        int ids[2];
481        ids[0] = desiredId;
482        ids[1] = realId;
483        this->desiredToRealMaterialId.push_back(ids);
484}*/
485
486
487std::list<SPTR<Node> > * GameScene::getPlayerList()
488{
489        return &this->playerList;
490}
491
492
493std::list<SPTR<Node> > * GameScene::getGoodieList()
494{
495        return &this->goodieList;
496}
497
498/*void GameScene::registerHealthPackage(Goodie* hp, float arrivalTime)
499{
500
501        this->setTrigger(this->TRIGGER_UNSETSTANDBY, hp, this->getRandomTime(arrivalTime, timeVariation));
502}
503
504void GameScene::registerWeaponPackage(Goodie* wp, float arrivalTime)
505{
506       
507}
508
509void GameScene::registerBulletPackage(Goodie* mp, float arrivalTime)
510{
511       
512}*/
513
514float GameScene::getRandomTime(float timeBase, float timeVariation)
515{
516        return timeBase + ((((float) rand())/RAND_MAX)-0.5f)*timeVariation*2;
517}
518
519void GameScene::executeTrigger(SPTR<Trigger> trigger)
520{
521        Scene::executeTrigger(trigger);
522
523        Trigger *t = trigger.get();
524        Node* node;
525        Node* secondNode;
526        Player* tempPlayer;
527        Vector normal;
528        Vector pos;
529       
530        switch(t->getType())
531        {
532                case this->TRIGGER_IMPACTTERRAIN:
533                        node = t->getNode().get();      //Bullet
534                        normal = t->getNormal();
535                        ((Bullet*) node)->impactTerrain(normal);
536                        break;
537                case this->TRIGGER_IMPACTPLAYER:
538                        node = t->getNode().get();      //Bullet
539                        secondNode = t->getSecondNode().get();  //Player
540                        normal = t->getNormal();
541                        ((Bullet*) node)->impactPlayer((Player*) secondNode, normal);
542                        break;
543                case this->TRIGGER_IMPACTGOODIE:
544                        node = t->getNode().get();
545                        secondNode = t->getSecondNode().get();  //Goodie
546                        normal = t->getNormal();
547                        ((Bullet*) node)->impactGoodie((Goodie*) secondNode, normal);
548                        break;
549                case this->TRIGGER_IMPACTOTHER:
550                        node = t->getNode().get();
551                        secondNode = t->getSecondNode().get();  //Object3d
552                        normal = t->getNormal();
553                        ((Bullet*) node)->impactOther((Object3d*) secondNode, normal);
554                        break;
555                case this->TRIGGER_GOODIECATCH:
556                        {
557                                node = t->getNode().get();                              //Player
558                                secondNode = t->getSecondNode().get();  //Goddie
559                                Goodie *goodie = (Goodie*) secondNode;
560                                Player *p = (Player*) node;
561                                this->manager->printToConsole("General Goodie catch!");
562                                switch(goodie->getGoodieType()) {
563                                        case Goodie::GOODIE_HEALTH:
564                                                this->manager->printToConsole("  health!");
565                                                p->addHealth(goodie->getHealthAmount());
566                                                break;
567                                        case Goodie::GOODIE_WEAPON:
568                                                //this->manager->printToConsole("GOODIE_WEAPON adding not yet implemented!");
569                                                this->manager->printToConsole("  weapon!");
570                                                if(goodie->getWeaponType()<=3) {
571                                                        SimpleWeapon *weapon = (SimpleWeapon *) this->createNode(this->NODE_SIMPLEWEAPON, *p->schale, true);
572                                                        weapon->setWeaponType(goodie->getWeaponType());
573                                                        weapon->addMunition(goodie->getArmorAmount());
574                                                        weapon->initWeapon();
575                                                        p->addWeapon(*weapon);
576                                                } else {
577                                                        p->addWeapon(*this->cloneWeaponFromReference(goodie->getWeaponType()));
578                                                }
579                                                break;
580                                        case Goodie::GOODIE_AMO:
581                                                this->manager->printToConsole("  amo!");
582                                                p->addMunition(goodie->getWeaponType(), goodie->getArmorAmount());
583                                                break;
584                                }
585                                SoundNode* goodiePick = (SoundNode*) this->createNode(GameScene::NODE_SOUND);
586                                goodiePick->loadFile("./media/sound/goodiePick.mp3", false);
587                                goodiePick->setKillSoundNodeAfterPlayed(true);
588                                goodiePick->play();
589                                secondNode->killMe();
590                        }
591                        break;
592                case this->TRIGGER_IMPACTFIRE:
593                        node = t->getNode().get();
594                        secondNode = t->getSecondNode().get();
595                        if(node->userData != secondNode) {
596                                Player* p = (Player*) secondNode;
597                                p->setHealth(p->getRealHealth() - 0.1f);
598                                p->hitByFire();
599                                if(p->isToasted()) {
600                                        this->hud->displayToasted(0.5, 0.01f);
601                                }
602                        }
603                        break;
604                case this->TRIGGER_IMPACTICE:
605                        node = t->getNode().get();
606                        secondNode = t->getSecondNode().get();
607                        if(node->userData != secondNode) {
608                                Player* p = (Player*) secondNode;
609                                p->setHealth(p->getRealHealth() - 0.05f);
610                                p->hitByIce();
611                                /*if(p->isToasted()) {
612                                        this->hud->displayToasted(1.0, 0.01f);
613                                }*/
614                        }
615                        break;
616                case this->TRIGGER_CREATEPLAYER:
617                case this->TRIGGER_CREATEMOREPLAYER:
618                        this->manager->printToConsole("Create ONE Player");
619                        if(this->gameMode==1) {
620                                node = t->getNode().get();                              //Create Trigger Object
621                                secondNode = t->getSecondNode().get();  //Bullet
622                                tempPlayer = ((Bullet*)secondNode)->myPlayer;   //Player of bullet
623                                //if(tempPlayer == this->player && this->playerList.size()-1 < this->maxEnemyCount) {
624                                if(tempPlayer == this->player && int(this->playerList.size())-1 < this->maxEnemyCount) {
625                                        pos = node->getAbsolutePosition();
626                                        pos.y++;
627                                        SimpleWeapon* weapon = NULL;
628                                        Player* p = (Player*) this->createNode(GameScene::NODE_AIPLAYER);
629                                        p->setAvatareFileName(this->enemyModel);
630                                        p->setTeam(this->player->getTeam()+1);
631                                        p->initPlayer(pos.x, pos.y, pos.z);
632                                        //Add Standard Weapon
633                                        weapon = (SimpleWeapon *) this->createNode(this->NODE_SIMPLEWEAPON, *p->schale, true);
634                                        weapon->setWeaponType(0);
635                                        weapon->addMunition(1000);
636                                        weapon->initWeapon();
637                                        p->addWeapon(*weapon);
638                                        p->setActiveWeapon(0);
639                                        if(t->getType()==this->TRIGGER_CREATEPLAYER) {
640                                                node->killMe();
641                                        }
642                                }
643                                secondNode->killMe();
644                        }
645                        break;
646        }       
647}
648
649void GameScene::updateGameFirst()
650{
651        //this->manager->printToConsole("updateGameFirst");
652        //Send Keys to UserPlayer
653        /*player->accelerate(this->keyDown['W']);
654        player->stop(this->keyDown['S']);
655        player->toLeft(this->keyDown['A']);
656        player->toRight(this->keyDown['D']);*/
657
658        /*if(this->keyDown['O']) {
659                this->manager->printToConsole("adding upforce!");
660                this->player->addUpForce();
661        }*/
662
663        if(this->descSet) {
664                this->descSet = false;
665                this->hud->message(this->description, this->descDuration, 0.01f);
666        }
667
668        //switch to menue scene:
669        if(this->keyDown[VK_ESCAPE]) {
670                this->keyDown[VK_ESCAPE] = false;
671                manager->switchToMenueScene();
672        }
673
674        //fly around cam:
675       
676        /*if(this->keyDown['F']) {
677                this->flyCamEnabled = true;
678        }
679        if(this->keyDown['G'])
680                this->flyCamEnabled = false;*/
681
682        //Update Camera
683        int screenWidth = this->manager->screenWidth; //TODO rausnehmen und durch richtigen wert ersetzen
684        int screenHeight = this->manager->screenHeight;
685
686        /*if(this->flyCamEnabled) {
687                Vector viewPos;
688                Vector lookAtPos;
689                Vector dir;
690                viewPos = this->activeCamera->getAbsolutePosition();
691                lookAtPos = this->activeCamera->getLookAtTarget();
692                dir = lookAtPos - viewPos;
693                dir.normalize();
694                float rX = 0;
695                float rY = 0;
696                float dZ = 0;
697                float dX = 0;
698                float dY = 0;
699
700                if(this->keyDown[VK_RIGHT]) {
701                        rY++;
702                }
703                if(this->keyDown[VK_LEFT]) {
704                        rY--;
705                }
706                if(this->keyDown[VK_UP]) {
707                        rX++;
708                }
709                if(this->keyDown[VK_DOWN]) {
710                        rX--;
711                }
712                if(this->keyDown['W']) {
713                        dZ++;
714                }
715                if(this->keyDown['S']) {
716                        dZ--;
717                }
718                if(this->keyDown['D']) {
719                        dX--;
720                }
721                if(this->keyDown['A']) {
722                        dX++;
723                }
724                if(this->keyDown['E']) {
725                        dY++;
726                }
727                if(this->keyDown['C']) {
728                        dY--;
729                }
730
731                float rotSpeed = 0.05f;
732                D3DXQUATERNION quat;
733                D3DXMATRIX rotMat;
734                D3DXQuaternionIdentity(&quat);
735                D3DXQuaternionRotationYawPitchRoll(&quat, rY*rotSpeed, rX*rotSpeed, 0);
736                D3DXMatrixRotationQuaternion(&rotMat, &quat);
737                D3DXVec4Transform(&dir, &dir, &rotMat);
738
739                lookAtPos = viewPos + dir;
740
741                Vector upVec(0, 1, 0);
742                Vector rightVec;
743                rightVec = dir.crossProd(upVec);
744                float speed = 1;
745                dir = dir*dZ*speed;
746        rightVec = rightVec*dX*speed;
747                upVec = upVec*dY*speed;
748               
749                lookAtPos = lookAtPos+dir+rightVec+upVec;
750                viewPos = viewPos+dir+rightVec+upVec;
751
752                this->activeCamera->setLookAtTarget(lookAtPos.x, lookAtPos.y, lookAtPos.z);
753                this->activeCamera->setPosition(viewPos.x, viewPos.y, viewPos.z);
754                this->skybox->getBoxObject()->setPosition(viewPos.x, 1, viewPos.z);
755                return;
756        }*/
757
758        //Update Player instructions
759        if(this->keyDown['W']) player->accelerate();
760        if(this->keyDown['S']) player->stop();
761        if(this->keyDown['A']) player->toLeft();
762        if(this->keyDown['D']) player->toRight();
763
764        //Set active Weapons
765        if(this->keyDown['1']) player->setActiveWeapon(0);
766        if(this->keyDown['2']) player->setActiveWeapon(1);
767        if(this->keyDown['3']) player->setActiveWeapon(2);
768        if(this->keyDown['4']) player->setActiveWeapon(3);
769        if(this->keyDown['5']) player->setActiveWeapon(4);
770        if(this->keyDown['6']) player->setActiveWeapon(5);
771        if(this->keyDown['7']) player->setActiveWeapon(6);
772        if(this->keyDown['8']) player->setActiveWeapon(7);
773        if(this->keyDown['9']) player->setActiveWeapon(8);
774
775        /*std::list<SPTR<Node> >::iterator it;
776        if(this->keyDown['Z']) {
777                for(it = this->playerList.begin(); it!=this->playerList.end();it++) {
778                        ((Player*) (*it).get())->switchToPublicPhysic();
779                }
780        }
781        //this->player->switchToPublicPhysic();
782        if(this->keyDown['H']) {
783                for(it = this->playerList.begin(); it!=this->playerList.end();it++) {
784                        ((Player*) (*it).get())->switchToPrivatePhysic();
785                }
786        }*/
787        //this->player->switchToPrivatePhysic();
788               
789
790        /*if(this->keyDown['J']) this->sObj->rotate(0,0,-0.2);
791        if(this->keyDown['L']) this->sObj->rotate(0,0,0.2);
792        if(this->keyDown['I']) this->sObj->rotate(-0.2,0,0);
793        if(this->keyDown['K']) this->sObj->rotate(0.2,0,0);
794        if(this->keyDown['U']) this->sObj->rotate(0,-0.2,0);
795        if(this->keyDown['M']) this->sObj->rotate(0,0.2,0);
796        if(this->keyDown['Y']) this->sObj->translate(0,0,6);
797        if(this->keyDown['X']) this->sObj->translate(0,0,-6);*/
798
799       
800        if(this->cameraFixedCount>5) {
801                /*char temp[100];
802                sprintf(temp, "1/dt = %f", 1/dt);
803                this->manager->printToConsole(temp);*/
804                int camFactor = 12;
805               
806                Vector diff;
807                D3DXMATRIX mYaw;
808                D3DXMATRIX mXaw;
809                D3DXMATRIX bla;
810
811                //dLookAtPoint = oldDLookAtPoint;
812                //dViewPoint = oldDViewPoint;
813                dViewPoint = player->getDesiredViewPoint();
814                dLookAtPoint = player->getDesiredLookAtPoint();
815
816                D3DXMatrixRotationY(&mYaw, (FLOAT)-M_PI/3*(0.5f-((float)this->mousePos[0])/screenWidth)*2);
817               
818                //Yaw berechnen
819                diff = dViewPoint - dLookAtPoint;
820                Vector tDiff;
821
822                //if (this->flyCamEnabled) {
823                //      D3DXVec4Transform(&tDiff, &diff, &bla);
824                //} else {
825                        D3DXVec4Transform(&tDiff, &diff, &mYaw);
826                //}
827                       
828                dViewPoint = dLookAtPoint + tDiff;
829               
830                //Falls Terrain höher als Cameraposition
831                Vector tempView = dViewPoint;
832                tempView.y += 5;
833                float h= this->getTerrainHeight(tempView);
834                int dHeight = 3;
835                if(h>dViewPoint.y) {
836                        dViewPoint.y = h+dHeight;
837                }
838                if(!this->player->gotHit) {
839                        lookAtPoint = lookAtPoint + (dLookAtPoint-lookAtPoint)*this->dt*(float)camFactor;
840                        viewPoint = viewPoint + (dViewPoint-viewPoint)*this->dt*(float)camFactor;
841                } else {
842                        lookAtPoint = lookAtPoint + (dLookAtPoint-lookAtPoint)*this->dt*(float)camFactor;
843                        viewPoint = viewPoint + (dViewPoint-viewPoint)*this->dt*(float)camFactor/5;
844                }
845
846                //Test if ViewPoint eventually collides with objects in scene
847
848                if(viewPoint.y < h+dHeight/2) {
849                        /*if(viewPoint.y-h==0) {
850                                h = viewPoint.y-0.5f;
851                        }
852                        viewPoint.y += (dHeight/2)/(viewPoint.y-h);*/
853                        viewPoint.y = dViewPoint.y;
854                }
855                //lookAtPoint.setXYZ(100, 30, 100);
856                //viewPoint.setXYZ(0, 100, 0);
857                this->activeCamera->setLookAtTarget(lookAtPoint.x, lookAtPoint.y, lookAtPoint.z);
858                this->activeCamera->setPosition(viewPoint.x, viewPoint.y, viewPoint.z);
859               
860
861                // update Skybox - hier richtig?
862                this->skybox->getBoxObject()->setPosition(viewPoint.x, 1, viewPoint.z);
863        } else {
864                this->activeCamera->setLookAtTarget(this->player->getAbsolutePosition());
865                this->cameraFixedCount++;
866        }
867}
868
869void GameScene::updateGameSecond()
870{
871        /*D3DXVECTOR3 * D3DXVec3Unproject(
872                D3DXVECTOR3 * pOut,
873                CONST D3DXVECTOR3 * pV,
874                CONST D3DVIEWPORT9 * pViewport,
875                CONST D3DXMATRIX * pProjection,
876                CONST D3DXMATRIX * pView,
877                CONST D3DXMATRIX * pWorld
878        );*/
879        if(this->cameraFixedCount>2) {
880                D3DXVECTOR3 mousePosSource;
881                mousePosSource.x = (float)this->mousePos[0];
882                mousePosSource.y = (float)this->mousePos[1];
883                mousePosSource.z = 0;
884
885                D3DXVECTOR3 unprojectedMouseNear, unprojectedMouseFar;
886                D3DVIEWPORT9 pViewport;
887               
888                this->device->GetViewport(&pViewport);
889
890                D3DXVec3Unproject(&unprojectedMouseNear, &mousePosSource, &pViewport, &this->getProjectionMatrix(), &this->getViewMatrix(), &this->worldMatrix);
891                mousePosSource.z = 1;
892                D3DXVec3Unproject(&unprojectedMouseFar, &mousePosSource, &pViewport, &this->getProjectionMatrix(), &this->getViewMatrix(), &this->worldMatrix);
893
894                NxVec3 NxNear(unprojectedMouseNear.x, unprojectedMouseNear.y, unprojectedMouseNear.z);
895                NxVec3 NxFar(unprojectedMouseFar.x, unprojectedMouseFar.y, unprojectedMouseFar.z);
896
897                NxVec3 direction;
898                direction.subtract(NxFar, NxNear);
899                direction.normalize();
900                NxRay ray(NxNear, direction);
901
902                NxRaycastHit hit;
903                Vector target;
904
905                // Get the closest shape
906                NxU32 mask = 0;
907                /*mask |= UserContactReport::COLGROUP_NOCOL;
908                mask |= UserContactReport::COLGROUP_TERRAIN;*/
909                mask = 0xFFFFFFFF;
910                NxShape* closestShape = this->pScene->raycastClosestShape(ray, NX_ALL_SHAPES, hit, mask);
911                if (closestShape && closestShape->getActor().userData != this->player)
912                {
913                        //this->manager->printToConsole("HIT");
914                        //NxVec3& worldImpact = hit.worldImpact;
915                        target.setNxVector(hit.worldImpact);
916                        //dist = hit.distance;
917                } else {
918                        //this->manager->printToConsole("Not HIT");
919                        target.setXYZ(unprojectedMouseFar.x, unprojectedMouseFar.y, unprojectedMouseFar.z);
920                }
921               
922                this->player->getActiveWeapon()->setFireAt(target);
923                if(this->mouseButton[this->MOUSE_LEFT]) {
924                        player->getActiveWeapon()->fire();
925                }
926        }
927
928        //Set HUD Time
929        int minutes, seconds, intDuration;
930        this->gameDuration-=this->dt;
931        intDuration = (int) this->gameDuration;
932        seconds = intDuration%60;
933        minutes = (intDuration-seconds)/60;
934        this->hud->setTime(minutes, seconds);
935
936
937        //Check if Challenge is over!
938        std::list<SPTR<Node> >::iterator it;
939        std::list<SPTR<Node> >::iterator tit;
940        Player* p;
941        Player* tp;
942        if(!this->resultVisible) {
943                if(this->gameDuration>0) {
944                        if(this->player->getHealth()<=0) {
945                                this->initLose();
946                        } else {
947                                for(it = this->playerList.begin();it!=this->playerList.end();it++) {
948                                        p = (Player*) (*it).get();
949                                        if(p != this->player && p->getHealth()<=0) {
950                                                //Reset activeOpponent from a player if that opponent should be deleted!
951                                                for(tit = this->playerList.begin();tit!=this->playerList.end();tit++) {
952                                                        tp = (Player*) (*tit).get();
953                                                        if(p != tp && p->isA(GameScene::NODE_AIPLAYER) && ((AIPlayer*) tp)->activeOpponent == p) {
954                                                                ((AIPlayer*) tp)->activeOpponent = NULL;
955                                                        }
956                                                }
957                                                if(!p->getSoftKill()) {
958                                                        p->setSoftKill(true,4);
959                                                }
960                                        }
961                                }
962                                if(this->playerList.size()<=1) {
963                                        this->initWin();
964                                }
965                        }
966                } else {        //Time's up
967                        float sumHealth = 0;
968                        for(it = this->playerList.begin();it!=this->playerList.end();it++) {
969                                p = (Player*) (*it).get();
970                                if(p != this->player) {
971                                        sumHealth += max(0.0f, p->getHealth());
972                                }
973                        }
974                        if(this->player->getHealth()>sumHealth) {
975                                this->initWin();
976                        } else {
977                                this->initLose();
978                        }
979                }
980        } else {
981                this->resultShowingTimer-=this->dt;
982                if(!this->won && this->resultShowingTimer<=5.75f) {
983                        this->recalcTransformations = false;
984                }
985                if(this->resultShowingTimer<=0) {
986                        this->manager->switchToMenueScene();
987                }
988        }
989        if(this->showFPS) {
990                char temp[16];
991                sprintf(temp, "FPS: %f", DXUTGetFPS());
992                this->manager->printToConsole(temp);
993                this->hud->message(temp, 1.0f, 1.0f);
994        }
995        this->hud->setAmmo(this->player->getAmo());
996}
997
998void GameScene::initWin()
999{
1000        this->won=true;
1001        this->player->initWin();
1002        this->resultVisible = true;
1003        this->hud->displayWin(6, 0.01f);
1004        this->resultShowingTimer = 6;
1005
1006        if(this->happySound) {
1007                this->happySound->play();
1008        }
1009}
1010
1011void GameScene::initLose()
1012{
1013        this->player->initLose();
1014        this->won = false;
1015        this->resultVisible = true;
1016        this->hud->displayLose(6, 0.01f);
1017        this->resultShowingTimer = 6;
1018        if(this->sadSound) {
1019                this->sadSound->play();
1020        }
1021}
1022
1023//void GameScene::setBackgroundSound(std::string filename) {
1024//      this->bgMusicStream = FSOUND_Stream_Open(filename.c_str(), FSOUND_LOOP_NORMAL, 0, 0);
1025//}
1026//
1027//void GameScene::setBackgroundSoundVolume(float vol) {
1028//      this->bgSoundVolume = vol;
1029//      if(this->bgSoundChannel!=-1) {
1030//              FSOUND_SetVolume(this->bgSoundChannel, (int)(255*this->bgSoundVolume));
1031//      }
1032//}
1033//
1034//void GameScene::playBackgroundSound() {
1035//      if(this->bgMusicStream!=NULL) {
1036//              this->bgSoundChannel = FSOUND_Stream_Play(FSOUND_FREE, this->bgMusicStream);
1037//              FSOUND_SetVolume(this->bgSoundChannel, (int)(255*this->bgSoundVolume));
1038//      }
1039//}
1040
1041void GameScene::connectMaterialIds(int physicId, int xmlId)
1042{
1043        //this->materialConnectVector.push_back(temp);
1044        this->materialXMLVector.push_back(xmlId);
1045        this->materialPhysicVector.push_back(physicId);
1046}
1047
1048int GameScene::getPhysicMaterialId(int xmlId)
1049{
1050        for(UINT i=0;i<this->materialXMLVector.size();i++) {
1051                if(this->materialXMLVector.at(i) == xmlId) {
1052                        return this->materialPhysicVector.at(i);
1053                }
1054        }
1055        return 0;
1056}
1057
1058Bullet* GameScene::cloneBulletFromReference(int type)
1059{
1060        Bullet* temp;
1061        for(UINT i=0;i<this->refBulletVector.size();i++) {
1062                temp = (Bullet*) this->refBulletVector.at(i).get();
1063                if(type == temp->getBulletType()) {
1064                        return (Bullet*) temp->clone();
1065                }
1066        }
1067        return NULL;
1068}
1069
1070Weapon* GameScene::cloneWeaponFromReference(int type)
1071{
1072        Weapon* temp;
1073        for(UINT i=0;i<this->refWeaponVector.size();i++) {
1074                temp = (Weapon*) this->refWeaponVector.at(i).get();
1075                if(type == temp->getWeaponType()) {
1076                        this->manager->printToConsole("GameScene::cloneWeaponFromReference.....clone in weapon initialized?");
1077                        return (Weapon*) temp->clone();
1078                }
1079        }
1080        return NULL;
1081}
1082
1083void GameScene::reduceHealthOnPlayers(Bullet* bullet)
1084{
1085        //this->manager->printToConsole("REDUCING HEALTH");
1086        Vector dist;
1087        Vector bulletPos;
1088        bulletPos = bullet->getAbsolutePosition();
1089        Player* p;
1090        float forceFactor;
1091        for(UINT i=0;i<bullet->playerList.size();i++) {
1092                p = bullet->playerList.at(i);
1093                dist = p->getAbsolutePosition() - bulletPos;
1094                forceFactor = max(0.0f, (bullet->impactRadius - dist.length())/bullet->impactRadius);
1095                p->setHealth(p->getHealth()- forceFactor*bullet->impactDamage);
1096        }
1097}
1098
1099/*void GameScene::updateSoundNodeVolume()
1100{
1101        Scene::updateSoundNodeVolume();
1102        if(this->bgSoundChannel!=-1) {
1103                FSOUND_SetVolume(this->bgSoundChannel, (int)(255*this->bgSoundVolume*this->sceneAlpha));
1104        }
1105}*/
1106
1107float GameScene::getTerrainHeight(Vector v)
1108{
1109        NxVec3 direction(0, -1, 0);
1110        NxRay ray(v.getNxVector(), direction);
1111        NxRaycastHit hit;
1112        Vector hv = v;
1113
1114        // Get the closest shape
1115        NxShape* closestShape = this->pScene->raycastClosestShape(ray, NX_ALL_SHAPES, hit, 0xFFFFFFFF, NX_MAX_F32);//0xffffff);
1116        if (closestShape)
1117        {
1118                hv.y = hit.worldImpact.y;
1119        } else {
1120                hv.y = 0;
1121        }
1122        float h = this->terrain->getHeight(v.x, v.z);
1123        hv.y = (hv.y>=h) ? hv.y : h;
1124        return hv.y;
1125}
1126
1127void GameScene::setChallengeDescription(std::string _description, float _descDuration)
1128{
1129        this->descSet = true;
1130        this->description = _description;
1131        this->descDuration = _descDuration;
1132}
1133
1134void GameScene::setMaxEnemyCount(int maxEnemyCount)
1135{
1136        this->maxEnemyCount = maxEnemyCount;
1137}
1138
1139void GameScene::hasWon() {
1140        std::list<SPTR<Node> >::iterator it;
1141        Player* p;
1142        for(it = this->playerList.begin();it!=this->playerList.end();it++) {
1143                p = (Player*) (*it).get();
1144                if(p != this->player) {
1145                        //this->manager->printToConsole("setting health = 0");
1146                        p->setHealth(0);
1147                }
1148        }
1149}
Note: See TracBrowser for help on using the repository browser.