statetornado.cpp

00001 #include "statemachine.h"
00002 #include "debug.h"
00003 #include "boid.h"
00004 #include "terrain.h"
00005 #include "WindStorm.h"
00006 #include "flockanimatortornado.h"
00007 #include "statetornado.h"
00008 
00009 using namespace irr;
00010 using namespace core;
00011 using namespace video;
00012 using namespace scene;
00013 using namespace gui;
00014 
00015 extern StateMachine* sm;
00016 extern Debug* dbg;
00017 
00018 StateTornado::StateTornado(c8* n, IrrlichtDevice* d) : State(n, d)
00019 {
00020     //ctor
00021 }
00022 
00023 StateTornado::~StateTornado()
00024 {
00025     //dtor
00026 }
00027 
00028 bool StateTornado::OnEvent(SEvent event)
00029 {
00030     if (event.EventType == EET_KEY_INPUT_EVENT)
00031     {
00032         if (event.KeyInput.PressedDown)
00033         {
00034             switch(event.KeyInput.Key)
00035             {
00036                 case KEY_ESCAPE:
00037                 {
00038                     if (gui->isVisible())
00039                     {
00040                         gui->setVisible(false);
00041                         // disable mouse
00042                         device->getCursorControl()->setVisible(false);
00043                         device->getSceneManager()->getActiveCamera()->setInputReceiverEnabled(true);
00044                     }
00045                     else
00046                     {
00047                         gui->setVisible(true);
00048                         // enable mouse
00049                         device->getCursorControl()->setVisible(true);
00050                         device->getSceneManager()->getActiveCamera()->setInputReceiverEnabled(false);
00051                     }
00052                     return true;
00053                 }
00054                 case KEY_KEY_Q:
00055                 {
00056                     // exit app
00057                     exit();
00058                     device->closeDevice();
00059                     return true;
00060                 }
00061             }
00062         }
00063     }
00064 
00065     if (event.EventType == EET_GUI_EVENT)
00066     {
00067         s32 id = event.GUIEvent.Caller->getID();
00068         switch(event.GUIEvent.EventType)
00069         {
00070             case EGET_BUTTON_CLICKED:
00071             {
00072                 switch (id)
00073                 {
00074                     case 100:
00075                     {
00076                         // unpause
00077                         gui->setVisible(false);
00078                         // disable mouse
00079                         device->getCursorControl()->setVisible(false);
00080                         device->getSceneManager()->getActiveCamera()->setInputReceiverEnabled(true);
00081                         return true;
00082                     }
00083                     case 199:
00084                     {
00085                         // exit app
00086                         exit();
00087                         device->closeDevice();
00088                         return true;
00089                     }
00090                 }
00091             }
00092 
00093             case EGET_SCROLL_BAR_CHANGED:
00094             {
00095                 switch (id)
00096                 {
00097                     case 101:
00098                     {
00099                         g_fS0 += 1.0f - ((f32)gui->getSliderValue(101)) / 100.0f;
00100                         break;
00101                     }
00102                     default:
00103                     {
00104                         dbg->log("StateFlockSimulation::OnEvent() unhandelt event from id", id);
00105                         return false;
00106                     }
00107                 }
00108                 return true;
00109             }
00110 
00111         }
00112     }
00113 
00114     return false;
00115 }
00116 
00117 void StateTornado::render(u32 deltaTime)
00118 {
00119     device->getVideoDriver()->beginScene(true, true, SColor(0, 255, 255, 0));
00120     dbg->show("this is TornadoState");
00121     device->getSceneManager()->drawAll();
00122     UpdatePhysics();
00123         for (unsigned int i = 0; i < g_uiNumParticles; i++)
00124         {
00125         float x = g_pParticles[i].m_pfVelocity[0];
00126         float y = g_pParticles[i].m_pfVelocity[2];
00127         float z = g_pParticles[i].m_pfVelocity[1];
00128         boids[i]->velocity = vector3df(x,y,z);
00129         //aabbox3d<f32> box(vector3df(x,y,z), vector3df(x+3, y+3, z+3));
00130         //device->getVideoDriver()->draw3DBox(box);
00131         boids[i]->update(deltaTime);
00132         }
00133         device->getSceneManager()->getActiveCamera()->setTarget(vector3df(5000,1000,5000));
00134     dbg->show("polyCount", device->getVideoDriver()->getPrimitiveCountDrawn());
00135     dbg->show("fps", device->getVideoDriver()->getFPS());
00136     dbg->show("camPos", device->getSceneManager()->getActiveCamera()->getPosition());
00137     dbg->show("boidCount", g_uiNumParticles);
00138     device->getGUIEnvironment()->drawAll();
00139     device->getVideoDriver()->endScene();
00140 }
00141 
00142 void StateTornado::entry()
00143 {
00144     dbg->log("State", name, "entry");
00145 
00146     // init members
00147 
00148     // disable mouse
00149     device->getCursorControl()->setVisible(false);
00150 
00151     // create camera
00152     ICameraSceneNode* camera = device->getSceneManager()->addCameraSceneNode();
00153     camera->setFarValue(20000);
00154     camera->setPosition(vector3df(5000,1000,5000));
00155     s32 startTime = 0;
00156     array<vector3df> points;
00157     f32 sc = 500.0f;
00158     points.push_back(camera->getPosition() + vector3df(5,0,5) * sc);
00159     points.push_back(camera->getPosition() + vector3df(5,0,0) * sc);
00160     points.push_back(camera->getPosition() + vector3df(0,0,5) * sc);
00161     points.push_back(camera->getPosition() + vector3df(0,5,5) * sc);
00162     points.push_back(camera->getPosition() + vector3df(-5,0,0) * sc);
00163     points.push_back(camera->getPosition() + vector3df(0,0,-5) * sc);
00164     f32 speed = 0.1f;
00165     f32 tightness = 0.5f;
00166     ISceneNodeAnimator* anim = device->getSceneManager()->createFollowSplineAnimator(startTime,
00167         points, speed, tightness);
00168     camera->addAnimator(anim);
00169     anim->drop();
00170 
00171     // get that messy terrain
00172     terrain = new Terrain(device, "media/terrain/terrain-heightmap.bmp",
00173                                     "media/terrain/terrain-texture.jpg",
00174                                     "media/terrain/detailmap3.jpg");
00175 
00176     // create tornado
00177     init();
00178     for (u32 i=0; i<g_uiGoalNumParticles; i++)
00179     {
00180         Boid* b = new Boid(device, "media/fly/satelite40Faces1Texture.3DS");
00181         b->setScale(0.3f);
00182         float x = g_pParticles[i].m_pfPosition[0];
00183         float y = g_pParticles[i].m_pfPosition[2];
00184         float z = g_pParticles[i].m_pfPosition[1];
00185         x += 5000;
00186         y += 1000;
00187         z += 5000;
00188         b->position = vector3df(x,y,z);
00189         boids.push_back(b);
00190     }
00191 
00192     // create GUI
00193     gui = new Gui(device, stringc("Tornado"), rect<s32>(300, 200, 500, 400));
00194     gui->addButton(100, "Back");
00195     gui->addButton(199, "Exit");
00196     gui->setVisible(false);
00197 }
00198 
00199 void StateTornado::exit()
00200 {
00201     dbg->log("State", name, "exit");
00202 
00203     // deinit members
00204     delete gui;
00205     delete terrain;
00206     deInit();
00207     for (u32 i=0; i<boids.size(); i++)
00208     {
00209         delete boids[i];
00210     }
00211     boids.clear();
00212     device->getSceneManager()->clear();
00213 }

Generated on Sun Dec 2 17:09:58 2007 for Swarm by  doxygen 1.4.6-NO