stateterrain.cpp

00001 #include "statemachine.h"
00002 #include "debug.h"
00003 #include "flock.h"
00004 #include "flockanimatordefault.h"
00005 #include "flockanimatortornado.h"
00006 #include "terrain.h"
00007 #include "roller.h"
00008 #include "stateterrain.h"
00009 
00010 using namespace irr;
00011 using namespace core;
00012 using namespace video;
00013 using namespace scene;
00014 using namespace gui;
00015 
00016 extern StateMachine* sm;
00017 extern Debug* dbg;
00018 
00019 StateTerrain::StateTerrain(c8* n, IrrlichtDevice* d) : State(n, d)
00020 {
00021     //ctor
00022 }
00023 
00024 StateTerrain::~StateTerrain()
00025 {
00026     //dtor
00027 }
00028 
00029 bool StateTerrain::OnEvent(SEvent event)
00030 {
00031     if (event.EventType == EET_KEY_INPUT_EVENT)
00032     {
00033         if (event.KeyInput.PressedDown)
00034         {
00035             switch(event.KeyInput.Key)
00036             {
00037                 case KEY_ESCAPE:
00038                 {
00039                     if (gui->isVisible())
00040                     {
00041                         gui->setVisible(false);
00042                         // disable mouse
00043                         device->getCursorControl()->setVisible(false);
00044                         device->getSceneManager()->getActiveCamera()->setInputReceiverEnabled(true);
00045                     }
00046                     else
00047                     {
00048                         gui->setVisible(true);
00049                         // enable mouse
00050                         device->getCursorControl()->setVisible(true);
00051                         device->getSceneManager()->getActiveCamera()->setInputReceiverEnabled(false);
00052                     }
00053                     return true;
00054                 }
00055                 case KEY_KEY_Q:
00056                 {
00057                     // exit app
00058                     exit();
00059                     device->closeDevice();
00060                     return true;
00061                 }
00062                 case KEY_KEY_W:
00063                 {
00064                     roller->addVelocity(vector3df(0, 0, 100));
00065                     return true;
00066                 }
00067                 case KEY_KEY_A:
00068                 {
00069                     roller->addVelocity(vector3df(-100, 0, 0));
00070                     return true;
00071                 }
00072                 case KEY_KEY_S:
00073                 {
00074                     roller->addVelocity(vector3df(0, 0, -100));
00075                     return true;
00076                 }
00077                 case KEY_KEY_D:
00078                 {
00079                     roller->addVelocity(vector3df(+100, 0, 0));
00080                     return true;
00081                 }
00082 
00083             }
00084         }
00085     }
00086 
00087     if (event.EventType == EET_GUI_EVENT)
00088     {
00089         switch(event.GUIEvent.EventType)
00090         {
00091             case EGET_BUTTON_CLICKED:
00092             {
00093                 s32 id = event.GUIEvent.Caller->getID();
00094                 switch (id)
00095                 {
00096                     case 100:
00097                     {
00098                         // unpause
00099                         gui->setVisible(false);
00100                         // disable mouse
00101                         device->getCursorControl()->setVisible(false);
00102                         device->getSceneManager()->getActiveCamera()->setInputReceiverEnabled(true);
00103                         return true;
00104                     }
00105                     case 199:
00106                     {
00107                         sm->pushEvent("STATE_MENU");
00108                         return true;
00109                     }
00110                 }
00111             }
00112         }
00113     }
00114     return false;
00115 }
00116 
00117 void StateTerrain::render(u32 deltaTime)
00118 {
00119     device->getVideoDriver()->beginScene(true, true, SColor(0, 255, 255, 0));
00120     dbg->show("this is TerreinState");
00121     flock->update(deltaTime);
00122     roller->update(deltaTime);
00123     device->getSceneManager()->drawAll();
00124     dbg->show("polyCount", device->getVideoDriver()->getPrimitiveCountDrawn());
00125     dbg->show("fps", device->getVideoDriver()->getFPS());
00126     dbg->show("camPos", device->getSceneManager()->getActiveCamera()->getPosition());
00127     device->getGUIEnvironment()->drawAll();
00128     device->getVideoDriver()->endScene();
00129 }
00130 
00131 void StateTerrain::entry()
00132 {
00133     dbg->log("State", name, "entry");
00134 
00135     // init members
00136 
00137     // disable mouse
00138     device->getCursorControl()->setVisible(false);
00139 
00140     // get that messy terrain
00141     terrain = new Terrain(device, "media/terrain/terrain-heightmap.bmp",
00142                                     "media/terrain/terrain-texture.jpg",
00143                                     "media/terrain/detailmap3.jpg");
00144 
00145     vector3df terrCntr = terrain->getCenter();
00146 
00147     // create flock
00148     flock = new Flock(device, 100, "media/fly/satelite40Faces1Texture.3DS",
00149                       device->getSceneManager()->getRootSceneNode(),
00150                       0,
00151                       terrCntr + vector3df(0, 1000, 0));
00152     // animatorDefault behavior
00153     flock->setSeparationWeight(32);
00154     flock->setAlignmentWeight(0);
00155     flock->setCohesionWeight(33);
00156     flock->setNeighborRadius(500);
00157     flock->setTarget(terrCntr + vector3df(0, 1000, 0));
00158     flock->setSeekTargetWeight(2);
00159     // animatorTornado behavior
00160     flock->setTornadoWeight(1);
00161     flock->setScale(0.3);
00162 /*
00163     FlockAnimatorDefault* flockAnimDefault = new FlockAnimatorDefault(device);
00164     flock->addAnimator(flockAnimDefault);
00165     flockAnimDefault->drop();
00166 */
00167     FlockAnimatorTornado* flockAnimTornado = new FlockAnimatorTornado(device);
00168     flock->addAnimator(flockAnimTornado);
00169     flockAnimTornado->drop();
00170 
00171     // create player
00172     roller = new Roller(device, "media/roller/roller99.3ds", terrain->getCenter() +
00173                                                              vector3df(0, 200, 0) );
00174     roller->setScale(10);
00175     roller->setCollisionTriangles(terrain->getCollisionTriangles());
00176 
00177     // create camera
00178     ICameraSceneNode* camera = device->getSceneManager()->addCameraSceneNodeFPS(0, 100, 1000);
00179     camera->setFarValue(20000);
00180     camera->setPosition(terrCntr + vector3df(1000, 2000, 0));
00181     camera->setTarget(flock->getPosition());
00182 
00183     // create light
00184     //ILightSceneNode* light = device->getSceneManager()->addLightSceneNode(camera);
00185     //light->getLightData().Radius *= 30;
00186 
00187     // create GUI
00188     gui = new Gui(device, stringc("Terrain"), rect<s32>(300, 200, 500, 400));
00189     gui->addButton(100, "back");
00190     gui->addButton(199, "exit");
00191     gui->setVisible(false);
00192 }
00193 
00194 void StateTerrain::exit()
00195 {
00196     dbg->log("State", name, "exit");
00197 
00198     // deinit members
00199     delete terrain;
00200     delete roller;
00201     flock->drop();
00202     delete gui;
00203     device->getSceneManager()->clear();
00204 }

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