/* * 2D Game Engine * LevelLoader.cpp.c: * Based on pikuma.com 2D game engine in C++ and Lua course * Copyright (c) 2021 986-Studio. All rights reserved. * * Created by ManoĆ«l Trapier on 28/02/2021. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include LevelLoader::LevelLoader() { } LevelLoader::~LevelLoader() { } void LevelLoader::loadAssetTable(sol::table &assetsTable, const std::unique_ptr &assetStore, SDL_Renderer *renderer) { int i = 0; while(true) { sol::optional hasAsset = assetsTable[i]; if (hasAsset == sol::nullopt) { break; } sol::table asset = assetsTable[i]; std::string assetType = asset["type"]; if (assetType == "texture") { assetStore->addTexture(renderer, asset["id"], asset["file"]); } else if (assetType == "font") { assetStore->addFont(asset["id"], asset["file"], asset["font_size"]); } i++; } } void LevelLoader::loadTileMap(sol::table &tileMap, const std::unique_ptr ®istry) { int tileSize = tileMap["tile_size"]; double tileScale = tileMap["scale"]; int mapNumCols = tileMap["num_cols"]; int mapNumRows = tileMap["num_rows"]; std::string tilesetId = tileMap["texture_asset_id"]; std::fstream mapFile; std::string mapFilePath = tileMap["map_file"]; mapFile.open(mapFilePath.c_str()); if (!mapFile.is_open()) { Logger::Error("Cannot open the mapfile '%s'!!", mapFilePath.c_str()); } for (int y = 0; y < mapNumRows; y++) { for (int x = 0; x < mapNumCols; x++) { char ch[2] = {0, 0}; mapFile.get(ch[0]); int srcRectY = atoi(ch) * tileSize; mapFile.get(ch[0]); int srcRectX = atoi(ch) * tileSize; mapFile.ignore(); Entity tile = registry->createEntity(); tile.addComponent(glm::vec2(x * (tileScale * tileSize), y * (tileScale * tileSize)), glm::vec2(tileScale, tileScale), 0.0); tile.addComponent(tilesetId, 0, tileSize, tileSize, false, srcRectX, srcRectY); tile.group("tiles"); } } mapFile.close(); Game::mapWidth = mapNumCols * tileSize * tileScale; Game::mapHeight = mapNumRows * tileSize * tileScale; } void LevelLoader::loadEntities(sol::table &entitiesTable, const std::unique_ptr ®istry) { uint32_t i; for(i = 0; i <= entitiesTable.size(); i++) { sol::table entity = entitiesTable[i]; Entity newEntity = registry->createEntity(); sol::optional tag = entity["tag"]; if (tag != sol::nullopt) { newEntity.tag(tag->c_str()); } sol::optional group = entity["group"]; if (group != sol::nullopt) { newEntity.group(group->c_str()); } sol::table componentTable = entity["components"]; for (const auto& key_value_pair : componentTable ) { sol::object key = key_value_pair.first; sol::table component = key_value_pair.second; std::string componentName = key.as(); if (componentName == "transform") { newEntity.addComponent(glm::vec2(component["position"]["x"], component["position"]["y"]), glm::vec2(component["scale"]["x"], component["scale"]["y"]), component["rotation"]); } else if (componentName == "rigidbody") { newEntity.addComponent(glm::vec2(component["velocity"]["x"], component["velocity"]["y"])); } else if (componentName == "sprite") { newEntity.addComponent(component["texture_asset_id"], component["z_index"], component["width"], component["height"], component["fixed"].get_or(false), component["src_rect_x"].get_or(0), component["src_rect_y"].get_or(0)); } else if (componentName == "animation") { newEntity.addComponent(component["num_frames"].get_or(1), component["speed_rate"].get_or(1), true); } else if (componentName == "boxcollider") { newEntity.addComponent(component["width"], component["height"], glm::vec2(component["offset"]["x"].get_or(0), component["offset"]["y"].get_or(0))); } else if (componentName == "health") { newEntity.addComponent(component["health_percentage"].get_or(100)); } else if (componentName == "projectile_emitter") { double projectileDuration = component["projectile_duration"]; double repeatFrequency = component["repeat_frequency"]; newEntity.addComponent(glm::vec2(component["projectile_velocity"]["x"], component["projectile_velocity"]["y"]), repeatFrequency * 1000, projectileDuration * 1000, component["hit_percentage_damage"], component["friendly"]); } else if (componentName == "keyboard_controller") { newEntity.addComponent(glm::vec2(component["up_velocity"]["x"], component["up_velocity"]["y"]), glm::vec2(component["right_velocity"]["x"], component["right_velocity"]["y"]), glm::vec2(component["down_velocity"]["x"], component["down_velocity"]["y"]), glm::vec2(component["left_velocity"]["x"], component["left_velocity"]["y"])); } else if (componentName == "camera_follow") { newEntity.addComponent(); } else if (componentName == "on_update_script") { sol::function func = component[0]; newEntity.addComponent(func); } else { Logger::Error("Unknown component: %s for entity id#%d", componentName.c_str(), newEntity.getId()); } } } } void LevelLoader::LoadLevel(sol::state &lua, const std::unique_ptr ®istry, const std::unique_ptr &assetStore, SDL_Renderer *renderer, int levelNumber) { char luaFileName[50]; sprintf(luaFileName, "assets/scripts/Level%d.lua", levelNumber); sol::load_result script = lua.load_file(luaFileName); if (!script.valid()) { Logger::Error("Error in the Lua script for level #%d:", levelNumber); sol::error err = script; std::string errorMessage = err.what(); Logger::Error(errorMessage.c_str()); return; } lua.script_file(luaFileName); Logger::Info("Opened level #%d", levelNumber); sol::table level = lua["Level"]; sol::table assetsTable = level["assets"]; sol::table tileMap = level["tilemap"]; sol::table entitiesTable = level["entities"]; this->loadAssetTable(assetsTable, assetStore, renderer); this->loadTileMap(tileMap, registry); this->loadEntities(entitiesTable, registry); }