Advertisement
Guest User

WTLD Manager Interaction Example

a guest
May 20th, 2013
568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.70 KB | None | 0 0
  1. #include "StdAfx.h"
  2. #include "Arena_Locker.h"
  3.  
  4. #include "LevelData.h"
  5.  
  6. #include "RecastInterface.h"
  7. #include "DetourInterface.h"
  8. #include "LuaManager.h"
  9.  
  10. #include "AI\npc_character.h"
  11.  
  12. #include "CrowdManager.h"
  13.  
  14. #include "SoundManager.h"
  15.  
  16. #include "interfaces\soundlist.hxx"
  17.  
  18. ArenaLocker::ArenaLocker()
  19.     : _camera(nullptr),
  20.       _view(nullptr),
  21.       _rootNode(nullptr),
  22.       _stateShutdown(nullptr),
  23.       _scene(nullptr),
  24.       _cameraMovementTime(0.0f)
  25. {
  26.     _returnValue = State::GAME_LOBBY;
  27. }
  28.  
  29. void ArenaLocker::Setup(InputManager* Input,GraphicsManager* Graphics,GUIManager* Gui,SoundManager* Sound)
  30. {
  31.     _scene = Graphics->createSceneManager(Ogre::ST_INTERIOR,"arenaLocker");
  32.     _rootNode = _scene->getRootSceneNode();
  33.  
  34.     _camera = _scene->createCamera("arenaLockerCam");
  35.     _camera->setAspectRatio(16.0f / 9.0f);
  36.     _camera->setNearClipDistance(0.001f);
  37.     _view = Graphics->getRenderWindow()->addViewport(_camera);
  38.     _view->setBackgroundColour(Ogre::ColourValue(0,0,0));
  39.     std::cout << "Arena Locker - scene manager and camera/viewport created" << std::endl;
  40.  
  41.     _physics.reset(new PhysicsManager());
  42.     btVector3 grav(0.0f,-9.8f,0.0f);
  43.     _physics->Setup(grav);
  44.     std::cout << "Arena Locker - physics setup" << std::endl;
  45.  
  46.     //list of physics-based entities and such
  47.     _loadPhysicsEntities("resource\\xml\\lists\\arenalocker_list.xml");
  48.     std::cout << "Arena Locker - models loaded" << std::endl;
  49.  
  50.     std::vector<LevelData::Waypoint> waypoints;
  51.     LevelData::LevelParser parser;
  52.     parser.setFile("resource\\models\\arena_locker\\arenalocker_test.ent");
  53.     parser.parseWaypoints(&waypoints);
  54.     std::cout << "Arena Locker - parser finished" << std::endl;
  55.  
  56.     Ogre::Entity* levelEnt = static_cast<Ogre::Entity*>(_pairs.begin()->ogreNode->getAttachedObject(0));
  57.     _navigationMeshSetup(levelEnt);
  58.  
  59.     _AIManager.reset(new AIManager());
  60.     _AIManager->loadNPCs("resource\\xml\\lists\\arenalocker_npc_list.xml",_crowd.get(),_scene,.9f);
  61.     std::cout << "NPCs loaded" << std::endl;
  62.  
  63.     _loadSounds("resource\\xml\\arena_locker\\locker_soundlist.xml",Sound);
  64.     std::cout << "Sounds loaded" << std::endl;
  65.  
  66.     _pauseMenu.reset(new PauseMenu(State::GAME_LOCKER));
  67.     _pauseMenu->Setup(Input,Graphics,Gui,Sound);
  68.     std::cout << "Pause menu initialized" << std::endl;
  69.  
  70.     _oldCameraPositionTarget = Ogre::Vector3(-7,1.0f,-8.0f);
  71.     LuaManager::getSingleton().callFunction("Arena_InitCameraMovement",2);
  72.     _cameraPositionTarget = LuaManager::getVectorFromLua(LuaManager::getSingletonPtr()->getLuaState(),1);
  73.     Ogre::Vector3 startingLook = LuaManager::getVectorFromLua(LuaManager::getSingletonPtr()->getLuaState(),2);
  74.     _oldCameraLookTarget = startingLook;
  75.     _cameraLookTarget = startingLook;
  76.     _camera->rotate(_camera->getDirection().getRotationTo(startingLook));
  77.     _oldCameraOrientation = _camera->getOrientation();
  78.     LuaManager::getSingleton().clearLuaStack();
  79.  
  80.     //TEST
  81.     _sphere = Graphics->createSceneNode(_scene,object("resource/xml/test_sphere.xml").get(),_rootNode);
  82.     //TEST
  83. }
  84.  
  85. int ArenaLocker::Run(InputManager* Input,GraphicsManager* Graphics,GUIManager* Gui,SoundManager* Sound)
  86. {
  87.     //_camera->setPosition(Ogre::Vector3(-7.0f,1.0f,-8.07f));
  88.     _camera->setPosition(Ogre::Vector3(-20.0f,10.0f,-10.0f));
  89.     _camera->setFarClipDistance(1000);
  90.     _camera->lookAt(Ogre::Vector3::ZERO);
  91.  
  92.     Ogre::Light* light = _scene->createLight("arenaLockerLight");
  93.     light->setType(Ogre::Light::LT_POINT);
  94.     light->setPosition(Ogre::Vector3(1.5,1.5,2.0));
  95.     Graphics->setLightRange(light,15);
  96.  
  97.     bool exitNow = _stateShutdown;
  98.     bool paused = false;
  99.  
  100.     float time; _oldTime = Graphics->getCurrentTimeMs();
  101.     while(!_stateShutdown)
  102.     {
  103.         //updates input manager.
  104.         if(!Input->Update(false))
  105.         {
  106.             _stateShutdown = true;
  107.         }
  108.  
  109.         time = Graphics->getCurrentTimeMs();
  110.         _deltaTime = time - _oldTime;
  111.         _oldTime = time;
  112.  
  113.         if(paused)
  114.         {
  115.             paused = false;
  116.             _deltaTime = 16.6f;
  117.         }
  118.  
  119.         //_handleScript();
  120.         //_handleCamera();
  121.  
  122.         //Update the crowd manager
  123.         _crowd->updateTick(_deltaTime / 1000.0f);
  124.  
  125.         _AIManager->update(_deltaTime);
  126.  
  127.         if(!GameManager::UpdateManagers(Graphics,_physics.get(),_deltaTime))
  128.         {
  129.             _stateShutdown = true;
  130.         }
  131.  
  132.         _handleSoundEvents(LuaManager::getSingletonPtr()->getSoundEventQueue(),Sound);
  133.         Sound->Update(Input->getConfiguration(),_camera);
  134.  
  135.         int numOfChannels;
  136.         Sound->_getSystem()->getChannelsPlaying(&numOfChannels);
  137.         //std::cout << numOfChannels << std::endl;
  138.  
  139.         if(Input->escapePressed() && !paused)
  140.         {
  141.             int ret = _pauseMenu->Run(Input,Graphics,Gui,Sound);
  142.             if(ret == State::END)
  143.             {
  144.                 _stateShutdown = true;
  145.                 _returnValue = ret;
  146.             }
  147.             else
  148.             {
  149.                 paused = true;
  150.             }
  151.         }
  152.     }
  153.  
  154.     if(exitNow)
  155.     {
  156.         MessageBoxA(NULL,"Check the WTLD.log file or the debug console for information on this error.","Error",MB_OK);
  157.         return State::END;
  158.     }
  159.  
  160.     //Eventually will be GAME_LOBBY or something.
  161.     return _returnValue;
  162. }
  163.  
  164. void ArenaLocker::Shutdown(InputManager* Input,GraphicsManager* Graphics,GUIManager* Gui,SoundManager* Sound)
  165. {
  166.     std::for_each(_pairs.begin(),_pairs.end(),[] (OgreBulletPair ipair) {
  167.         if(ipair.btBody->getCollisionShape()->getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE)
  168.         {
  169.             btBvhTriangleMeshShape* mesh = static_cast<btBvhTriangleMeshShape*>(ipair.btBody->getCollisionShape());
  170.             btTriangleMesh* trimesh = static_cast<btTriangleMesh*>(mesh->getUserPointer());
  171.             delete trimesh;
  172.         }
  173.     });
  174.  
  175.     std::for_each(_sounds.begin(),_sounds.end(),[Sound] (sSound snd) {
  176.         Sound->destroySound(snd);
  177.     });
  178.  
  179.     _pauseMenu->Shutdown(Input,Graphics,Gui,Sound);
  180.  
  181.     Graphics->getRenderWindow()->removeAllViewports();
  182.  
  183.     Graphics->cleanAndDestroySceneManager(_scene);
  184.  
  185.     LuaManager::getSingletonPtr()->deepClean();
  186. }
  187.  
  188. void ArenaLocker::_handleScript()
  189. {
  190.     LuaManager* lua = LuaManager::getSingletonPtr();
  191.     lua->clearLuaStack();
  192.     lua->prepFunction("Arena_CameraMovement");
  193.     //lua->pushFunctionArgVector(_camera->getPosition());
  194.  
  195.     if(_deltaTime == 0)
  196.     {
  197.         lua->pushFunctionArg(16.0f);
  198.     }
  199.     else
  200.     {
  201.         lua->pushFunctionArg(_deltaTime);
  202.     }
  203.     lua->callFunction(1,3);
  204.  
  205.     //check for returned values
  206.     lua_State* luaS = lua->getLuaState();
  207.     //assume it's a vector that's being returned. Otherwise some seriously fucked up shit is going on.
  208.     Ogre::Vector3 targetVector = Ogre::Vector3::UNIT_X;
  209.     Ogre::Vector3 lookVector = Ogre::Vector3::UNIT_X;
  210.     if(lua_istable(luaS,1))
  211.     {
  212.         targetVector = LuaManager::getVectorFromLua(luaS,1);
  213.     }
  214.  
  215.     if(lua_istable(luaS,2))
  216.     {
  217.         lookVector = LuaManager::getVectorFromLua(luaS,2);
  218.     }
  219.  
  220.     //Movement time
  221.     if(lua_isnumber(luaS,3))
  222.     {
  223.         _cameraMovementTime = lua_tonumber(luaS,3);
  224.     }
  225.     else
  226.     {
  227.         _cameraMovementTime = 0.0f;
  228.     }
  229.  
  230.     //Target Vector
  231.     if(targetVector == Ogre::Vector3::ZERO)
  232.     {
  233.         _stateShutdown = true;
  234.     }
  235.     else
  236.     {
  237.         _cameraPositionTarget = targetVector;
  238.     }
  239.  
  240.     //Look vector
  241.     if(lookVector != Ogre::Vector3::ZERO)
  242.     {
  243.         //lookVector = _camera->getPosition() + lookVector;
  244.         if(lookVector != _oldCameraLookTarget)
  245.         {
  246.             std::cout << "CameraLookTarget changed!" << std::endl;
  247.             std::cout << "New LookTarget:" << lookVector << std::endl;
  248.             _oldCameraLookTarget = _cameraLookTarget;
  249.             _cameraLookTarget = lookVector;
  250.             _oldCameraOrientation = _camera->getOrientation();
  251.         }
  252.     }
  253.  
  254.    
  255.  
  256.     /*
  257.     _cameraMovementTime += .001f;
  258.     if(_cameraMovementTime > 1.0f)
  259.     {
  260.         _cameraMovementTime = 0.0f;
  261.     }
  262.  
  263.     LuaManager::getSingletonPtr()->clearLuaStack();
  264.     LuaManager::getSingletonPtr()->prepFunction("Bezier_Test");
  265.     LuaManager::getSingletonPtr()->pushFunctionArg(_cameraMovementTime);
  266.     //LuaManager::getSingletonPtr()->_printStack();
  267.     LuaManager::getSingletonPtr()->callFunction(1,1);
  268.  
  269.     //get the vector from it.
  270.     Ogre::Vector2 tmp = Ogre::Vector2::ZERO;
  271.     lua_State* l = LuaManager::getSingleton().getLuaState();
  272.     if(lua_istable(l,1))
  273.     {
  274.         for(int i = 1; i < 3; ++i)
  275.         {
  276.             lua_pushnumber(l,i);
  277.             lua_gettable(l,1);
  278.             if(lua_isnumber(l,-1))
  279.             {
  280.                 tmp[i-1] = static_cast<float>(lua_tonumber(l,-1));
  281.             }
  282.             else
  283.             {
  284.                 tmp[i-1] = 0;
  285.             }
  286.             lua_pop(l,1);
  287.         }
  288.     }
  289.  
  290.     if(tmp != Ogre::Vector2::ZERO)
  291.     {
  292.         _sphere->setPosition(tmp.x,1.0f,tmp.y);
  293.     }
  294.     LuaManager::getSingletonPtr()->clearLuaStack();
  295.     */
  296.  
  297.     //handle updating the camera in another function.
  298.     //keeps this one cleaner.
  299.  
  300.     return;
  301. }
  302.  
  303. void ArenaLocker::_handleCamera()
  304. {
  305.     //position...
  306.     //_camera->setPosition(_cameraPositionTarget);
  307.    
  308.     //rotation...
  309.     //in the script, the cameraMovementTime is divided into three to give the LookProgress counter access to all of the lookProg states.
  310.     float progress = _cameraMovementTime;
  311.     if(progress > .33 && progress <= .67f)
  312.     {
  313.         progress -= .33f;
  314.         progress *= 3;
  315.     }
  316.     else if(progress > .67f)
  317.     {
  318.         progress -= .67f;
  319.         progress *= 3;
  320.     }
  321.     else
  322.     {
  323.         progress *= 3;
  324.     }
  325.     Ogre::Vector3 look = Utility::vector3_lerp(_oldCameraLookTarget,_cameraLookTarget,progress);
  326.     _camera->setDirection(look);
  327.  
  328.     return;
  329. }
  330.  
  331. void ArenaLocker::_handleSoundEvents(std::vector<SoundEvent>& events, SoundManager* Sound)
  332. {
  333.     if(events.size() == 0)
  334.     {
  335.         return;
  336.     }
  337.  
  338.     sSound sndTmp;
  339.     FMOD_VECTOR vel; vel.x = 0; vel.y = 0; vel.z = 0;
  340.     for(auto itr = events.begin(); itr != events.end(); ++itr)
  341.     {
  342.         auto sndItr = std::find(_sounds.begin(),_sounds.end(),(*itr).name);
  343.         if(sndItr != _sounds.end())
  344.         {
  345.             sndTmp = *sndItr;
  346.         }
  347.         else
  348.         {
  349.             continue;
  350.         }
  351.  
  352.         if(itr->is3D)
  353.         {
  354.             Sound->playSound(sndTmp,itr->position,vel);
  355.         }
  356.         else
  357.         {
  358.             Sound->playSound(sndTmp);
  359.         }
  360.     }
  361.  
  362.     events.clear();
  363.  
  364.     return;
  365. }
  366.  
  367. void ArenaLocker::_loadPhysicsEntities(std::string fileName)
  368. {
  369.     auto objList = list(fileName.c_str());
  370.     for(auto itr = objList->file().begin(); itr != objList->file().end(); ++itr)
  371.     {
  372.         _pairs.push_back(GameManager::createObject(_scene,(*itr),_physics.get()));
  373.     }
  374. }
  375.  
  376. void ArenaLocker::_loadSounds(std::string fileName, SoundManager* Sound)
  377. {
  378.     auto sndList = soundList(fileName.c_str());
  379.     sSound sound;
  380.     for(auto itr = sndList->sound().begin(); itr != sndList->sound().end(); ++itr)
  381.     {
  382.         sound.is3D = itr->is3D();
  383.         sound.isLooping = itr->looping();
  384.         sound.name = itr->name();
  385.         Sound->createSound(sound,itr->filename().c_str());
  386.         _sounds.push_back(sound);
  387.     }
  388.  
  389.     return;
  390. }
  391.  
  392. void ArenaLocker::_navigationMeshSetup(Ogre::Entity* levelEntity)
  393. {
  394.     InputGeometry levelGeometry(levelEntity);
  395.  
  396.     RecastConfiguration params(.2f,2.5f);
  397.  
  398.     _recast.reset(new RecastInterface(_scene,params));
  399.     _recast->getRecastConfig().walkableRadius = static_cast<int>(.2f); // zero?
  400.     _recast->buildNavMesh(&levelGeometry);
  401.     _recast->exportPolygonMeshToObj("ARENALOCKER_RECAST_MESH.obj");
  402.  
  403.     rcdtConfig config;
  404.     config.recastConfig = &_recast->getRecastConfig();
  405.     config.userConfig = &_recast->getRecastBuildConfiguration();
  406.  
  407.     _detour.reset(new DetourInterface(_recast->getPolyMesh(),_recast->getDetailMesh(),config));
  408.  
  409.     _crowd.reset(new CrowdManager(_detour.get(),&config));
  410. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement