SHARE
TWEET

CTileManager

a guest Mar 29th, 2017 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include    <tgSystem.h>
  2. #include    "CTile.h"
  3.  
  4. #include    "CTileManager.h"
  5. #include    "CLevelEditor.h"
  6. #include    "Managers/CModelManager.h"
  7. #include    "Managers/CLightManager.h"
  8. #include    "CPhysicsManager.h"
  9. #include    "tgCTextureManager.h"
  10. #include    "GameStateMachine/CGameStates.h"
  11. #include    "tgCFileSystem.h"
  12. #include <tgMemoryDisable.h>
  13. #include <fstream>
  14. #include <ctime>
  15. #include <string>
  16. #include <tgMemoryEnable.h>
  17. #include "CStringParser.h"
  18. #include "Managers/CWorldManager.h"
  19.  
  20. std::vector<tgCString> CTileManager::GetFilesInFolders(std::string folder)
  21. {
  22. #if defined(TG_WINDOWS)
  23.  
  24.     std::vector<tgCString> names;
  25.     char search_path[200];
  26.     sprintf(search_path, "%s/*.*", folder.c_str());
  27.     WIN32_FIND_DATA fd;
  28.     HANDLE hFind = ::FindFirstFile(search_path, &fd);
  29.     if(hFind != INVALID_HANDLE_VALUE) {
  30.         do {
  31.             // read all (real) files in current folder
  32.             // , delete '!' read other 2 default folder . and ..
  33.             if(! (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) {
  34.                 names.push_back(fd.cFileName);
  35.             }
  36.         }while(::FindNextFile(hFind, &fd));
  37.         ::FindClose(hFind);
  38.     }
  39.     return names;
  40. #endif
  41. }
  42.  
  43. CTileManager::CTileManager( )
  44. {
  45.  
  46.     m_SizeX = 16;
  47.     m_SizeY = 14;
  48.  
  49.     m_Spacing = 2.0f;
  50.     m_StartingPos = tgCV3D(0.0f, 0.0f, 0.0f);
  51.  
  52.     m_TileDestructionMode = DESTRUCTIONMODE_FALLING;
  53.  
  54.     // If not level editor, load selected level.
  55.     if( !CGameStates::GetInstance().GetStateMenu()->GetIsEditor() || CGameStates::GetInstance().GetStateMenu()->GetIsEditingLevel() ){
  56.         LoadLevel();
  57.     }
  58.     else{
  59.         for (tgUInt32 i = 0; i < (m_SizeX * m_SizeY); i++)
  60.         {
  61.             AddTile(i, CGameStates::GetInstance().GetStateMenu()->GetSelectedTheme());
  62.         }
  63.     }
  64.  
  65.     CPhysicsManager::GetInstance().GetWorld()->optimizeBroadPhase();
  66.  
  67. }
  68.  
  69. CTileManager::~CTileManager( void )
  70. {
  71.  
  72.     for (tgUInt32 i = 0; i < m_Tiles.size(); i++)
  73.     {
  74.         delete m_Tiles[i];
  75.     }
  76.  
  77.    
  78. }
  79.  
  80.  
  81. tgBool CTileManager::LoadLevel( void ){
  82.     tgCString FileName = "";
  83.  
  84. #if defined(TG_WINDOWS)
  85.     SGameInfo GameInfo;
  86.  
  87.     GameInfo = CGameStates::GetInstance().GetStateCharSel()->GetGameInfo();
  88.     tgCString LevelName = "";
  89.  
  90.     if(GameInfo.SelectedLevel == 0){
  91.         LevelName = "FUMP_Level_1.txt";
  92.     }
  93.     if(GameInfo.SelectedLevel == 1){
  94.         LevelName = "FUMP_Level_2.txt";
  95.     }
  96.     if(GameInfo.SelectedLevel == 2){
  97.         LevelName = "FUMP_Level_3.txt";
  98.     }
  99.     if(GameInfo.SelectedLevel == 3){
  100.         LevelName = "FUMP_Level_4.txt";
  101.     }
  102.     if(CGameStates::GetInstance().GetStateGame()->GetPlayFromDebugMenu())
  103.         FileName = tgCString("data/levels/%s", GetFilesInFolders("data/levels").at(CGameStates::GetInstance().GetStateGame()->GetSelectedLevel()-1).String());
  104.     else if(CGameStates::GetInstance().GetStateGame()->GetPlayFromCharacterSelect())
  105.         FileName = tgCString("data/levels/%s", LevelName.String());
  106.     else
  107.         FileName = tgCString("data/levels/%s", GetFilesInFolders("data/levels").at(0).String());
  108.  
  109. #elif defined(TG_PS4)
  110.     tgUInt8*    pData   = NULL;
  111.     tgUInt32    Size    = 0;
  112.     SGameInfo GameInfo;
  113.  
  114.     GameInfo = CGameStates::GetInstance().GetStateCharSel()->GetGameInfo();
  115.     tgCString LevelName = "";
  116.  
  117.     if(GameInfo.SelectedLevel == 0){
  118.         LevelName = "FUMP_Level_1.txt";
  119.     }
  120.     if(GameInfo.SelectedLevel == 1){
  121.         LevelName = "FUMP_Level_2.txt";
  122.     }
  123.     if(GameInfo.SelectedLevel == 2){
  124.         LevelName = "FUMP_Level_3.txt";
  125.     }
  126.     if(GameInfo.SelectedLevel == 3){
  127.         LevelName = "FUMP_Level_4.txt";
  128.     }
  129.  
  130.     if(CGameStates::GetInstance().GetStateGame()->GetPlayFromCharacterSelect())
  131.         FileName = tgCString("/app0/data/levels/%s", LevelName.String());
  132.     else
  133.         FileName = tgCString("/app0/data/levels/PERFORMANCE_TEST_LEVEL.txt");
  134. #endif
  135.  
  136.     std::string Line;
  137.     size_t Pos = 0;
  138.     size_t Pos1 = 1;
  139.  
  140.     tgUInt32 Index = 0;
  141.     std::string Delimiter = "|";
  142.     std::string DimensionDelimiter = "x";
  143.  
  144.     std::ifstream LevelFile;
  145.     std::string Tile;
  146.  
  147.     LevelFile.open(FileName.String());
  148.     // Loop through file, split strings by delimiter and get type and pos
  149.     if (LevelFile.is_open())
  150.     {
  151.         while ( getline (LevelFile,Line) )
  152.         {
  153.             if((Pos = Line.find(DimensionDelimiter) != std::string::npos)){
  154.                 if(Line.length() < 3)
  155.                     m_SizeX = std::stoi(Line.substr(0, Pos));
  156.                 else if(Line.length() < 6)
  157.                 {
  158.                     m_SizeX = std::stoi(Line.substr(0, Pos) + Line.substr(1, Pos));
  159.                     if(Line.length() >= 4)
  160.                         m_Theme = std::stoi(Line.substr(3, Pos));
  161.                     else
  162.                         m_Theme = 0;
  163.                 }
  164.             }
  165.             while ((Pos = Line.find(Delimiter)) != std::string::npos) {
  166.                 Tile = Line.substr(0, Pos);
  167.                 AddTileFromFile(Index, std::stoi(Tile), m_Theme);
  168.                 Line.erase(0, Pos + Delimiter.length());
  169.                 Index++;
  170.             }
  171.         }
  172.         LevelFile.close();
  173.     }
  174.     return true;
  175.    
  176. }
  177.  
  178. void CTileManager::AddTile( tgUInt32 Index, tgUInt32 Theme  )
  179. {
  180.     tgUInt32 Row, Col;
  181.     Col = Index % m_SizeX;
  182.     Row = Index / m_SizeX;
  183.     tgCV3D Position = tgCV3D(-(m_StartingPos.x + ((Index * m_Spacing) - (Row * (m_Spacing * m_SizeX)))), m_StartingPos.y, (Row * m_Spacing));
  184.  
  185.     CTile *Tile = new CTile(Position, 1.0f, 1.0f, 0, Theme);
  186.  
  187.     m_Tiles.push_back(Tile);
  188. }
  189.  
  190. void CTileManager::AddTileFromFile( tgUInt32 Index, tgUInt32 Type, tgUInt32 Theme )
  191. {
  192.     tgUInt32 Row, Col;
  193.     Col = Index % m_SizeX;
  194.     Row = Index / m_SizeX;
  195.     tgCV3D Position = tgCV3D(-(m_StartingPos.x + ((Index * m_Spacing) - (Row * (m_Spacing * m_SizeX)))), m_StartingPos.y, (Row * m_Spacing));
  196.  
  197.     CTile *Tile = new CTile(Position, 1.0f, 1.0f, Type, Theme);
  198.     m_Tiles.push_back(Tile);
  199.    
  200. }
  201.  
  202. void CTileManager::Update( tgFloat DeltaTime )
  203. {
  204.     for (tgUInt32 i = 0; i < m_Tiles.size(); i++)
  205.     {
  206.         m_Tiles[i]->Update(DeltaTime);
  207.     }
  208. }
  209.  
  210. void CTileManager::RandomTileFall(  )
  211. {
  212.     for (tgUInt32 i = 0; i < m_Tiles.size(); i++)
  213.     {
  214.         m_Tiles[i]->GetWorld()->setBodyMotionType(m_Tiles[i]->GetBodyID(), hknpMotionType::DYNAMIC);
  215.     }
  216. }
  217.  
  218. void CTileManager::ApplyForceToAllTiles(  )
  219. {
  220.     for (tgUInt32 i = 0; i < m_Tiles.size(); i++)
  221.     {
  222.  
  223.         m_Tiles[i]->GetWorld()->applyBodyLinearImpulse(m_Tiles[i]->GetBodyID(), hkVector4(tgMathRandom(-120.0f, 120.0f), 120.0f, tgMathRandom(-120.0f, 120.0f)));
  224.     }
  225. }
  226.  
  227. void CTileManager::Render( void )
  228. {
  229.     const CLightManager::SLight*    pLightArray     = CLightManager::GetInstance().GetLightArray();
  230.     tgCLightManager&                rLightManager   = tgCLightManager::GetInstance();
  231.     CLightManager&  LightManager        = CLightManager::GetInstance();
  232.  
  233.  
  234.     // Render all solid meshes
  235.     for( tgUInt32 LightIndex = 0; LightIndex < MAX_NUM_LIGHTS; ++LightIndex )
  236.     {
  237.         if( pLightArray[ LightIndex ].pLight )
  238.         {
  239.             rLightManager.SetCurrentLight( *pLightArray[ LightIndex ].pLight );
  240.  
  241.             for (tgUInt32 i = 0; i < m_Tiles.size(); i++)
  242.             {
  243.                 m_Tiles[i]->Render();
  244.             }
  245.  
  246.         }
  247.     }
  248.  
  249.  
  250. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top