Level loading modularized. Levels auto-load/cache tilesets that are already loaded and shared between maps.

This commit is contained in:
2023-06-21 22:59:06 -07:00
parent 9247ecb528
commit 8d5d509c7c
4 changed files with 23 additions and 5 deletions
+19 -4
View File
@@ -26,10 +26,9 @@ Crawler::Crawler()
}
bool Crawler::OnUserCreate(){
TMXParser level1("assets/maps/Level1.tmx");
MAP_DATA[LEVEL1]=level1.GetData();
TMXParser level2("assets/maps/Level2.tmx");
MAP_DATA[LEVEL2]=level2.GetData();
InitializeLevel("assets/maps/Level1.tmx",LEVEL1);
InitializeLevel("assets/maps/Level2.tmx",LEVEL2);
InitializeLevel("assets/Campaigns/1_1.tmx",CAMPAIGN_1_1);
//Initialize Camera.
camera=Camera2D{WINDOW_SIZE};
@@ -796,6 +795,22 @@ void Crawler::SetupWorldShake(float duration){
camera.SetTarget(worldShake);
}
void Crawler::InitializeLevel(std::string mapFile,MapName map){
TMXParser level(mapFile);
size_t slashMarker = mapFile.find_last_of('/');
std::string baseDir=mapFile.substr(0,slashMarker+1);
MAP_DATA[map]=level.GetData();
for(XMLTag&tag:MAP_DATA[map].TilesetData){
if(tag.tag=="tileset"&&MAP_TILESETS.find(tag.data["source"])==MAP_TILESETS.end()){
Renderable*r=new Renderable();
MAP_TILESETS[tag.data["source"]]=r;
r->Load(baseDir+tag.data["source"]);
std::cout<<"Missing tileset:"<<baseDir+tag.data["source"]<<". Loaded from source."<<std::endl;
}
}
}
int main()
{
Crawler demo;