|
@@ -7,13 +7,12 @@
|
|
|
* Created by Manoël Trapier on 28/02/2021.
|
|
|
*/
|
|
|
|
|
|
-
|
|
|
+#include <math.h>
|
|
|
#include <fstream>
|
|
|
|
|
|
#include <sol/sol.hpp>
|
|
|
|
|
|
#include <LevelLoader.h>
|
|
|
-
|
|
|
#include <Game.h>
|
|
|
|
|
|
#include <Components/Animation.h>
|
|
@@ -37,8 +36,7 @@ LevelLoader::~LevelLoader()
|
|
|
|
|
|
}
|
|
|
|
|
|
-void LevelLoader::loadAssetTable(sol::state &lua,
|
|
|
- sol::table &assetsTable,
|
|
|
+void LevelLoader::loadAssetTable(sol::table &assetsTable,
|
|
|
const std::unique_ptr<AssetStore> &assetStore,
|
|
|
SDL_Renderer *renderer)
|
|
|
{
|
|
@@ -68,6 +66,141 @@ void LevelLoader::loadAssetTable(sol::state &lua,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+void LevelLoader::loadTileMap(sol::table &tileMap,
|
|
|
+ const std::unique_ptr<Registry> ®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<TransformComponent>(glm::vec2(x * (tileScale * tileSize), y * (tileScale * tileSize)), glm::vec2(tileScale, tileScale), 0.0);
|
|
|
+ tile.addComponent<SpriteComponent>(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<Registry> ®istry)
|
|
|
+{
|
|
|
+ uint32_t i;
|
|
|
+ for(i = 0; i <= entitiesTable.size(); i++)
|
|
|
+ {
|
|
|
+ sol::table entity = entitiesTable[i];
|
|
|
+ Entity newEntity = registry->createEntity();
|
|
|
+
|
|
|
+ sol::optional<std::string> tag = entity["tag"];
|
|
|
+ if (tag != sol::nullopt)
|
|
|
+ {
|
|
|
+ newEntity.tag(tag->c_str());
|
|
|
+ }
|
|
|
+
|
|
|
+ sol::optional<std::string> 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<std::string>();
|
|
|
+
|
|
|
+ if (componentName == "transform")
|
|
|
+ {
|
|
|
+ newEntity.addComponent<TransformComponent>(glm::vec2(component["position"]["x"], component["position"]["y"]),
|
|
|
+ glm::vec2(component["scale"]["x"], component["scale"]["y"]),
|
|
|
+ component["rotation"]);
|
|
|
+ }
|
|
|
+ else if (componentName == "rigidbody")
|
|
|
+ {
|
|
|
+ newEntity.addComponent<RigidBodyComponent>(glm::vec2(component["velocity"]["x"], component["velocity"]["y"]));
|
|
|
+ }
|
|
|
+ else if (componentName == "sprite")
|
|
|
+ {
|
|
|
+ newEntity.addComponent<SpriteComponent>(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<AnimationComponent>(component["num_frames"].get_or(1),
|
|
|
+ component["speed_rate"].get_or(1),
|
|
|
+ true);
|
|
|
+ }
|
|
|
+ else if (componentName == "boxcollider")
|
|
|
+ {
|
|
|
+ newEntity.addComponent<BoxColliderComponent>(component["width"],
|
|
|
+ component["height"],
|
|
|
+ glm::vec2(component["offset"]["x"].get_or(0), component["offset"]["y"].get_or(0)));
|
|
|
+ }
|
|
|
+ else if (componentName == "health")
|
|
|
+ {
|
|
|
+ newEntity.addComponent<HealthComponent>(component["health_percentage"].get_or(100));
|
|
|
+ }
|
|
|
+ else if (componentName == "projectile_emitter")
|
|
|
+ {
|
|
|
+ double projectileDuration = component["projectile_duration"];
|
|
|
+ double repeatFrequency = component["repeat_frequency"];
|
|
|
+ newEntity.addComponent<ProjectileEmitterComponent>(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<KeyboardControlComponent>(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<CameraFollowComponent>();
|
|
|
+ }
|
|
|
+ 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<Registry> ®istry,
|
|
|
const std::unique_ptr<AssetStore> &assetStore,
|
|
@@ -91,8 +224,14 @@ void LevelLoader::LoadLevel(sol::state &lua,
|
|
|
Logger::Info("Opened level #%d", levelNumber);
|
|
|
|
|
|
sol::table level = lua["Level"];
|
|
|
+
|
|
|
sol::table assetsTable = level["assets"];
|
|
|
- this->loadAssetTable(lua, assetsTable, assetStore, renderer);
|
|
|
+ sol::table tileMap = level["tilemap"];
|
|
|
+ sol::table entitiesTable = level["entities"];
|
|
|
+
|
|
|
+ this->loadAssetTable(assetsTable, assetStore, renderer);
|
|
|
+ this->loadTileMap(tileMap, registry);
|
|
|
+ this->loadEntities(entitiesTable, registry);
|
|
|
|
|
|
#if 0
|
|
|
assetStore->addTexture(renderer, "tank-image", "assets/images/tank-panther-right.png");
|