1010#include " TimeManager.h"
1111
1212static const char * InputsConfigPath = " config/inputs.json" ;
13- static const char * GTA1MapFileExtension = " .CMP" ;
1413
1514// ////////////////////////////////////////////////////////////////////////
1615
@@ -26,115 +25,29 @@ bool CarnageGame::Initialize()
2625
2726 gGameParams .LoadDefaults ();
2827
29- // scan all gta1 maps
30- std::vector<std::string> gtaMapNames;
31- for (const std::string& currSearchPlace: gFiles .mSearchPlaces )
32- {
33- cxx::enum_files (currSearchPlace, [>aMapNames](const std::string& curr)
34- {
35- if (cxx::get_file_extension (curr) == GTA1MapFileExtension)
36- {
37- gtaMapNames.push_back (curr);
38- }
39- });
40- }
41-
42- if (gtaMapNames.size ())
43- {
44- gConsole .LogMessage (eLogMessage_Info, " Found GTA1 maps:" );
45- for (const std::string& currMapname: gtaMapNames)
46- {
47- gConsole .LogMessage (eLogMessage_Info, " - %s" , currMapname.c_str ());
48- }
49- }
50- else
51- {
52- gConsole .LogMessage (eLogMessage_Warning, " No GTA1 maps found within search places" );
53- }
54-
5528 if (gSystem .mStartupParams .mDebugMapName .empty ())
5629 {
5730 // try load first found map
58- if (gtaMapNames.size ())
59- {
60- gSystem .mStartupParams .mDebugMapName = gtaMapNames[0 ].c_str ();
61- }
31+ debug_assert (!gFiles .mGameMapsList .empty ());
32+ gSystem .mStartupParams .mDebugMapName = gFiles .mGameMapsList [0 ];
6233 }
6334
6435 if (gSystem .mStartupParams .mDebugMapName .empty ())
6536 return false ;
6637
67- gGameMap .LoadFromFile (gSystem .mStartupParams .mDebugMapName .c_str ());
68- gSpriteManager .Cleanup ();
69- gRenderManager .mMapRenderer .BuildMapMesh ();
70- if (!gSpriteManager .InitLevelSprites ())
38+ if (!StartScenario (gSystem .mStartupParams .mDebugMapName ))
7139 {
72- debug_assert (false );
73- }
74- // gSpriteManager.DumpSpriteDeltas("D:/Temp/gta1_deltas");
75- // gSpriteCache.DumpBlocksTexture("D:/Temp/gta1_blocks");
76- // gSpriteManager.DumpSpriteTextures("D:/Temp/gta1_sprites");
77- // gSpriteManager.DumpCarsTextures("D:/Temp/gta_cars");
78- gPhysics .Initialize ();
79-
80- gGameObjectsManager .Initialize ();
81-
82- // temporary
83- // glm::vec3 pos { 108.0f, 2.0f, 25.0f };
84- // glm::vec3 pos { 14.0, 2.0f, 38.0f };
85- // glm::vec3 pos { 91.0f, 2.0f, 236.0f };
86- // glm::vec3 pos { 121.0f, 2.0f, 200.0f };
87- // glm::vec3 pos { 174.0f, 2.0f, 230.0f };
88-
89- mNumPlayers = glm::clamp (gSystem .mStartupParams .mPlayersCount , 1 , GAME_MAX_PLAYERS);
90- gConsole .LogMessage (eLogMessage_Info, " Num players: %d" , mNumPlayers );
91-
92- glm::vec3 pos[GAME_MAX_PLAYERS];
40+ ShutdownCurrentScenario ();
9341
94- // choose spawn point
95- // it is temporary!
96- int currFindPosIter = 0 ;
97- for (int yBlock = 10 ; yBlock < 20 && currFindPosIter < mNumPlayers ; ++yBlock)
98- {
99- for (int xBlock = 10 ; xBlock < 20 && currFindPosIter < mNumPlayers ; ++xBlock)
100- {
101- pos[currFindPosIter] = glm::ivec3 (xBlock, MAP_LAYERS_COUNT - 1 , yBlock);
102-
103- float currHeight = gGameMap .GetHeightAtPosition (pos[currFindPosIter]);
104- int zBlock = static_cast <int >(currHeight);
105- if (zBlock > MAP_LAYERS_COUNT - 1 )
106- continue ;
107-
108- BlockStyle* currBlock = gGameMap .GetBlock (xBlock, yBlock, zBlock);
109- if (currBlock->mGroundType == eGroundType_Field ||
110- currBlock->mGroundType == eGroundType_Pawement ||
111- currBlock->mGroundType == eGroundType_Road)
112- {
113- pos[currFindPosIter].x += MAP_BLOCK_LENGTH * 0 .5f ;
114- pos[currFindPosIter].z += MAP_BLOCK_LENGTH * 0 .5f ;
115- pos[currFindPosIter].y = currHeight;
116- ++currFindPosIter;
117- }
118- }
119- }
120-
121- for (int icurr = 0 ; icurr < mNumPlayers ; ++icurr)
122- {
123- float randomAngle = 360 .0f * gCarnageGame .mGameRand .generate_float ();
124-
125- Pedestrian* pedestrian = gGameObjectsManager .CreatePedestrian (pos[icurr], cxx::angle_t::from_degrees (randomAngle));
126- SetupHumanCharacter (icurr, pedestrian);
42+ gConsole .LogMessage (eLogMessage_Warning, " Fail to start game" );
43+ return false ;
12744 }
128-
129- SetupScreenLayout (mNumPlayers );
13045 return true ;
13146}
13247
13348void CarnageGame::Deinit ()
13449{
135- gGameObjectsManager .Deinit ();
136- gPhysics .Deinit ();
137- gGameMap .Cleanup ();
50+ ShutdownCurrentScenario ();
13851}
13952
14053void CarnageGame::UpdateFrame ()
@@ -278,7 +191,7 @@ bool CarnageGame::SetInputActionsFromConfig()
278191 }
279192
280193 cxx::json_document configDocument;
281- if (!configDocument.parse_document (jsonContent. c_str () ))
194+ if (!configDocument.parse_document (jsonContent))
282195 {
283196 gConsole .LogMessage (eLogMessage_Warning, " Cannot parse input config document" );
284197 return false ;
@@ -360,3 +273,109 @@ int CarnageGame::GetPlayerIndex(const HumanCharacterController* controller) cons
360273 }
361274 return -1 ;
362275}
276+
277+ void CarnageGame::DebugChangeMap (const std::string& mapName)
278+ {
279+ gConsole .LogMessage (eLogMessage_Debug, " Changing to next map '%s'" , mapName.c_str ());
280+
281+ if (!StartScenario (mapName))
282+ {
283+ gConsole .LogMessage (eLogMessage_Warning, " Fail to change map" );
284+ return ;
285+ }
286+ }
287+
288+ bool CarnageGame::StartScenario (const std::string& mapName)
289+ {
290+ ShutdownCurrentScenario ();
291+
292+ if (!gGameMap .LoadFromFile (mapName))
293+ {
294+ gConsole .LogMessage (eLogMessage_Warning, " Cannot load map '%s'" , mapName.c_str ());
295+ return false ;
296+ }
297+ gSpriteManager .Cleanup ();
298+ gRenderManager .mMapRenderer .BuildMapMesh ();
299+ if (!gSpriteManager .InitLevelSprites ())
300+ {
301+ debug_assert (false );
302+ }
303+ // gSpriteManager.DumpSpriteDeltas("D:/Temp/gta1_deltas");
304+ // gSpriteCache.DumpBlocksTexture("D:/Temp/gta1_blocks");
305+ // gSpriteManager.DumpSpriteTextures("D:/Temp/gta1_sprites");
306+ // gSpriteManager.DumpCarsTextures("D:/Temp/gta_cars");
307+ if (!gPhysics .InitPhysicsWorld ())
308+ {
309+ debug_assert (false );
310+ }
311+
312+ gGameObjectsManager .InitGameObjects ();
313+
314+ // temporary
315+ // glm::vec3 pos { 108.0f, 2.0f, 25.0f };
316+ // glm::vec3 pos { 14.0, 2.0f, 38.0f };
317+ // glm::vec3 pos { 91.0f, 2.0f, 236.0f };
318+ // glm::vec3 pos { 121.0f, 2.0f, 200.0f };
319+ // glm::vec3 pos { 174.0f, 2.0f, 230.0f };
320+
321+ mNumPlayers = glm::clamp (gSystem .mStartupParams .mPlayersCount , 1 , GAME_MAX_PLAYERS);
322+ gConsole .LogMessage (eLogMessage_Info, " Num players: %d" , mNumPlayers );
323+
324+ glm::vec3 pos[GAME_MAX_PLAYERS];
325+
326+ // choose spawn point
327+ // it is temporary!
328+ int currFindPosIter = 0 ;
329+ for (int yBlock = 10 ; yBlock < 20 && currFindPosIter < mNumPlayers ; ++yBlock)
330+ {
331+ for (int xBlock = 10 ; xBlock < 20 && currFindPosIter < mNumPlayers ; ++xBlock)
332+ {
333+ pos[currFindPosIter] = glm::ivec3 (xBlock, MAP_LAYERS_COUNT - 1 , yBlock);
334+
335+ float currHeight = gGameMap .GetHeightAtPosition (pos[currFindPosIter]);
336+ int zBlock = static_cast <int >(currHeight);
337+ if (zBlock > MAP_LAYERS_COUNT - 1 )
338+ continue ;
339+
340+ BlockStyle* currBlock = gGameMap .GetBlock (xBlock, yBlock, zBlock);
341+ if (currBlock->mGroundType == eGroundType_Field ||
342+ currBlock->mGroundType == eGroundType_Pawement ||
343+ currBlock->mGroundType == eGroundType_Road)
344+ {
345+ pos[currFindPosIter].x += MAP_BLOCK_LENGTH * 0 .5f ;
346+ pos[currFindPosIter].z += MAP_BLOCK_LENGTH * 0 .5f ;
347+ pos[currFindPosIter].y = currHeight;
348+ ++currFindPosIter;
349+ }
350+ }
351+ }
352+
353+ for (int icurr = 0 ; icurr < mNumPlayers ; ++icurr)
354+ {
355+ float randomAngle = 360 .0f * gCarnageGame .mGameRand .generate_float ();
356+
357+ Pedestrian* pedestrian = gGameObjectsManager .CreatePedestrian (pos[icurr], cxx::angle_t::from_degrees (randomAngle));
358+ SetupHumanCharacter (icurr, pedestrian);
359+ }
360+
361+ SetupScreenLayout (mNumPlayers );
362+ return true ;
363+ }
364+
365+ void CarnageGame::ShutdownCurrentScenario ()
366+ {
367+ // todo: cleanup
368+ for (int ihuman = 0 ; ihuman < GAME_MAX_PLAYERS; ++ihuman)
369+ {
370+ if (mHumanSlot [ihuman].mCharPedestrian == nullptr )
371+ continue ;
372+
373+ gRenderManager .DetachRenderView (&mHumanSlot [ihuman].mCharView );
374+ mHumanSlot [ihuman].mCharController .SetCharacter (nullptr );
375+ mHumanSlot [ihuman].mCharView .SetCameraController (nullptr );
376+ mHumanSlot [ihuman].mCharPedestrian = nullptr ;
377+ }
378+ gGameObjectsManager .FreeGameObjects ();
379+ gPhysics .FreePhysicsWorld ();
380+ gGameMap .Cleanup ();
381+ }
0 commit comments