From f301cede218eb6be467956a089f127d8003509c2 Mon Sep 17 00:00:00 2001 From: sebastian Date: Sat, 14 Feb 2026 08:35:32 +0100 Subject: [PATCH] ADD: Update ModelStages based in ModelParams --- src/engine/core/ECS/ModelComponent.h | 10 ++++--- src/engine/core/ECS/ModelStateComponent.h | 2 +- src/engine/core/ECS/RenderSystem.cpp | 27 +++++++++++++++++++ src/engine/core/ECS/RenderSystem.h | 5 +++- src/engine/layer/entities/Entity.h | 5 +++- src/game/GameLayer.cpp | 19 ++++++------- src/game/hexWorld/MapGenerator.cpp | 5 +++- .../hexWorld/building/BuildingFactory.cpp | 2 +- .../tileGenerator/ForestTileGenerator.cpp | 2 +- 9 files changed, 57 insertions(+), 20 deletions(-) diff --git a/src/engine/core/ECS/ModelComponent.h b/src/engine/core/ECS/ModelComponent.h index 8c5dcf6..4fdac99 100644 --- a/src/engine/core/ECS/ModelComponent.h +++ b/src/engine/core/ECS/ModelComponent.h @@ -16,13 +16,13 @@ class ModelComponent: public Component { public: - explicit ModelComponent(const ModelStages& modelStages): currentStage(0) { - for (const auto& stage : modelStages.getModelStages()) { + ModelComponent(const std::shared_ptr &modelStages, std::string modelname): modelName(std::move(modelname)), currentStage(0) { + for (const auto& stage : modelStages->getModelStages()) { stages.insert(stage.getStageName(), stage.getModelOfModelStage()); } }; - explicit ModelComponent(std::shared_ptr model): currentStage(0) { + ModelComponent(std::shared_ptr model, std::string modelname): modelName(std::move(modelname)), currentStage(0) { stages["default"] = std::move(model); } @@ -38,7 +38,11 @@ public: [[nodiscard]] std::shared_ptr getActiveModel() const{ return stages.atIndex(currentStage); } + + [[nodiscard]] const std::string& getModelName() const {return modelName;} + private: + std::string modelName; IndexedMap> stages; size_t currentStage; }; diff --git a/src/engine/core/ECS/ModelStateComponent.h b/src/engine/core/ECS/ModelStateComponent.h index dc41c50..49b5e7e 100644 --- a/src/engine/core/ECS/ModelStateComponent.h +++ b/src/engine/core/ECS/ModelStateComponent.h @@ -9,7 +9,7 @@ #include "Component.h" -class ModelStateComponent : public Component { +class ModelStateComponent : public Component{ public: std::unordered_map params; }; diff --git a/src/engine/core/ECS/RenderSystem.cpp b/src/engine/core/ECS/RenderSystem.cpp index c29d4de..d60ca8d 100644 --- a/src/engine/core/ECS/RenderSystem.cpp +++ b/src/engine/core/ECS/RenderSystem.cpp @@ -4,12 +4,20 @@ #include "RenderSystem.h" +#include "ModelStateComponent.h" +#include "../../renderer/loader/AssetManager.h" + void RenderSystem::render(EntityManager &entityManager, MasterRenderer &renderer) { for (auto id : entityManager.getAllEntities()) { auto transform = entityManager.getComponent(id); auto model = entityManager.getComponent(id); auto tileRenderingComponent = entityManager.getComponent(id); + auto modelStateComponent = entityManager.getComponent(id); + + if (modelStateComponent) { + updateModelStage(model.get(), modelStateComponent.get()); + } if (!transform || !model) continue; @@ -22,3 +30,22 @@ void RenderSystem::render(EntityManager &entityManager, MasterRenderer &renderer } } + +void RenderSystem::updateModelStage(ModelComponent *model, ModelStateComponent *state) { + std::string bestStage = "default"; + + auto modelStages = AssetManager::getModelStages(model->getModelName()); + + + for (const auto& stage : modelStages->getModelStages()) { + auto it = state->params.find(stage.getCondition().key); + if (it == state->params.end()) continue; + + float value = it->second; + if (value >= stage.getCondition().min && value < stage.getCondition().max) { + bestStage = stage.getStageName(); + } + } + + model->updateStage(bestStage); +} diff --git a/src/engine/core/ECS/RenderSystem.h b/src/engine/core/ECS/RenderSystem.h index e2c515f..13bcf4a 100644 --- a/src/engine/core/ECS/RenderSystem.h +++ b/src/engine/core/ECS/RenderSystem.h @@ -5,12 +5,15 @@ #ifndef RENDERSYSTEM_H #define RENDERSYSTEM_H #include "EntityManager.h" +#include "ModelStateComponent.h" #include "../../renderer/MasterRenderer.h" class RenderSystem { public: - void render(EntityManager &entityManager, MasterRenderer &renderer); + static void render(EntityManager &entityManager, MasterRenderer &renderer); + + static void updateModelStage(ModelComponent* model, ModelStateComponent* state); }; diff --git a/src/engine/layer/entities/Entity.h b/src/engine/layer/entities/Entity.h index c1106b4..be6e41f 100644 --- a/src/engine/layer/entities/Entity.h +++ b/src/engine/layer/entities/Entity.h @@ -5,19 +5,22 @@ #ifndef ENTITY_H #define ENTITY_H #include +#include #include "../../renderer/model/TexturedModel.h" #include "glm/vec3.hpp" class Entity { private: + std::shared_ptr model; glm::vec3 position; float rotX, rotY, rotZ; float scale; public: - Entity(std::shared_ptr model, const glm::vec3 position, const float rotX, const float rotY, const float rotZ, const float scale) : model(std::move(model)), position(position), rotX(rotX), rotY(rotY), rotZ(rotZ), scale(scale) {}; + std::string modelName; + Entity(std::shared_ptr model, const glm::vec3 position, const float rotX, const float rotY, const float rotZ, const float scale, std::string modelname="") : modelName(std::move(modelname)), model(std::move(model)), position(position), rotX(rotX), rotY(rotY), rotZ(rotZ), scale(scale) {}; void increasePosition(const float dx, const float dy, const float dz) {position += glm::vec3(dx, dy, dz);} //( dx, dy, dz void increaseRotation(const float dx, const float dy, const float dz) {rotX += dx; rotY += dy; rotZ += dz;} diff --git a/src/game/GameLayer.cpp b/src/game/GameLayer.cpp index 755d2fe..c979aa1 100644 --- a/src/game/GameLayer.cpp +++ b/src/game/GameLayer.cpp @@ -4,6 +4,7 @@ #include "GameLayer.h" +#include "../engine/core/ECS/ModelStateComponent.h" #include "../engine/platform/glfw/InputManager.h" #include "../engine/renderer/Renderer.h" #include "../engine/renderer/loader/OBJLoader.h" @@ -43,14 +44,7 @@ void GameLayer::onAttach() MapGenerator::generateHexMap(*map, 10,10, *entityManager); //printf("Generated Terrain with %lu Tiles!\n", map->tiles.size()); - for (const auto& entity : mapEntities) { - EntityID entityID = entityManager->createEntity(); - glm::vec3 pos = entity->getPosition(); - glm::vec3 rot = glm::vec3(entity->getRotX(), entity->getRotY(), entity->getRotZ()); - float scale = entity->getScale(); - entityManager->addComponent(entityID, std::make_shared(pos, rot, scale)); - entityManager->addComponent(entityID, std::make_shared(entity->getModel())); - } + const std::string stoneMasonPath = "assets/buildings/stone_mason/"; @@ -71,11 +65,14 @@ void GameLayer::onAttach() auto modelStages = AssetManager::getModelStages("stoneMason"); auto transformComponent = std::make_shared(glm::vec3(0,0,0), glm::vec3(0), 1.f); - auto modelComponent = std::make_shared(*modelStages); + auto modelComponent = std::make_shared(modelStages, "stoneMason"); + auto modelStateComponent = std::make_shared(); + modelStateComponent->params["fillRatio"] = 0.f; modelComponent->updateStage("stone_mason_empty"); EntityID entityID = entityManager->createEntity(); entityManager->addComponent(entityID, transformComponent); entityManager->addComponent(entityID, modelComponent); + entityManager->addComponent(entityID, modelStateComponent); testEntity = entityID; } @@ -105,8 +102,8 @@ void GameLayer::onUpdate() } if (InputManager::isKeyPressed(GLFW_KEY_U)) { - auto model_component = entityManager->getComponent(testEntity); - model_component->updateStage("stone_mason_full"); + auto modelStateComponent = entityManager->getComponent(testEntity); + modelStateComponent->params["fillRatio"] += 0.1f; } diff --git a/src/game/hexWorld/MapGenerator.cpp b/src/game/hexWorld/MapGenerator.cpp index 41cf5a0..d5c4bb1 100644 --- a/src/game/hexWorld/MapGenerator.cpp +++ b/src/game/hexWorld/MapGenerator.cpp @@ -37,19 +37,22 @@ void MapGenerator::generateHexMap(Map &map, int width, int height, EntityManager RessourceType resourceType = RessourceType::NONE; std::shared_ptr hexModel = AssetManager::getModel("hexModelNone"); std::vector tileEntities; + std::string modelName = "hexModelNone"; if (randomValue < 0.5f) { resourceType = RessourceType::WOOD; ForestTileGenerator forestTileGenerator; tileEntities = forestTileGenerator.generateHexTile(entityManager, random, worldPos); hexModel = AssetManager::getModel("hexModelWood"); + modelName = "hexModelWood"; } else if (randomValue < 0.75f) { resourceType = RessourceType::STONE; hexModel = AssetManager::getModel("hexModelStone"); + modelName = "hexModelStone"; } EntityID entityID = entityManager.createEntity(); const auto transformComponent = std::make_shared(worldPos, glm::vec3(0), 1.0f); - const auto modelComponent = std::make_shared(hexModel); + const auto modelComponent = std::make_shared(hexModel, modelName); const auto tileHighlightComponent = std::make_shared(false); const auto tileGameplayComponent = std::make_shared(q, r, radius, resourceType); tileGameplayComponent->entitiesOnTile = tileEntities; diff --git a/src/game/hexWorld/building/BuildingFactory.cpp b/src/game/hexWorld/building/BuildingFactory.cpp index a3b030a..a0b698e 100644 --- a/src/game/hexWorld/building/BuildingFactory.cpp +++ b/src/game/hexWorld/building/BuildingFactory.cpp @@ -13,7 +13,7 @@ EntityID BuildingFactory::create(EntityManager &em, const BuildingDefinition &de EntityID e = em.createEntity(); - em.addComponent(e, std::make_shared(AssetManager::getModel(def.model))); + em.addComponent(e, std::make_shared(AssetManager::getModel(def.model), def.model)); if (def.type == BuildingType::FOREST_HUT) { em.addComponent(e, std::make_shared( diff --git a/src/game/hexWorld/tileGenerator/ForestTileGenerator.cpp b/src/game/hexWorld/tileGenerator/ForestTileGenerator.cpp index 6fc58a6..30b3419 100644 --- a/src/game/hexWorld/tileGenerator/ForestTileGenerator.cpp +++ b/src/game/hexWorld/tileGenerator/ForestTileGenerator.cpp @@ -47,7 +47,7 @@ std::vector ForestTileGenerator::generateHexTile(EntityManager& em, Ra int treeModelIndex = random.randomInt(0, static_cast(treeModels.size()-1)); const auto transformComponent = std::make_shared(treePos, glm::vec3(0), 1.f); - const auto modelComponent = std::make_shared(treeModels[treeModelIndex]); + const auto modelComponent = std::make_shared(treeModels[treeModelIndex], "hexModelWood"); const EntityID entityID = em.createEntity(); em.addComponent(entityID, transformComponent);