From 2c1c5b2d63649753d46a2c7339e503e68a37e527 Mon Sep 17 00:00:00 2001 From: sebastian Date: Sat, 14 Feb 2026 09:11:06 +0100 Subject: [PATCH] ADD: ProducingSystem --- CMakeLists.txt | 4 ++ src/engine/core/ECS/RenderSystem.cpp | 2 +- src/engine/layer/Layer.h | 3 ++ src/game/GameLayer.cpp | 11 +++++ src/game/UILayer.cpp | 2 +- src/game/UILayer.h | 2 +- .../hexWorld/ecs/components/OwnerComponent.h | 6 +-- .../ecs/components/ProducingComponent.cpp | 5 +++ .../ecs/components/ProducingComponent.h | 42 +++++++++++++++++++ .../hexWorld/ecs/systems/ProducingSystem.cpp | 36 ++++++++++++++++ .../hexWorld/ecs/systems/ProducingSystem.h | 18 ++++++++ 11 files changed, 124 insertions(+), 7 deletions(-) create mode 100644 src/game/hexWorld/ecs/components/ProducingComponent.cpp create mode 100644 src/game/hexWorld/ecs/components/ProducingComponent.h create mode 100644 src/game/hexWorld/ecs/systems/ProducingSystem.cpp create mode 100644 src/game/hexWorld/ecs/systems/ProducingSystem.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b8bce1a..211d909 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -181,6 +181,10 @@ add_executable(Dicewars_Siedler src/main.cpp src/engine/toolbox/IndexedMap.h src/engine/core/ECS/ModelStateComponent.cpp src/engine/core/ECS/ModelStateComponent.h + src/game/hexWorld/ecs/components/ProducingComponent.cpp + src/game/hexWorld/ecs/components/ProducingComponent.h + src/game/hexWorld/ecs/systems/ProducingSystem.cpp + src/game/hexWorld/ecs/systems/ProducingSystem.h ) target_compile_options(Dicewars_Siedler PRIVATE diff --git a/src/engine/core/ECS/RenderSystem.cpp b/src/engine/core/ECS/RenderSystem.cpp index d60ca8d..7f1b424 100644 --- a/src/engine/core/ECS/RenderSystem.cpp +++ b/src/engine/core/ECS/RenderSystem.cpp @@ -42,7 +42,7 @@ void RenderSystem::updateModelStage(ModelComponent *model, ModelStateComponent * if (it == state->params.end()) continue; float value = it->second; - if (value >= stage.getCondition().min && value < stage.getCondition().max) { + if (value >= stage.getCondition().min && value <= stage.getCondition().max) { bestStage = stage.getStageName(); } } diff --git a/src/engine/layer/Layer.h b/src/engine/layer/Layer.h index 3192238..ede5d81 100644 --- a/src/engine/layer/Layer.h +++ b/src/engine/layer/Layer.h @@ -8,10 +8,12 @@ #include #include "../../game/GameMode.h" +#include "../core/Application.h" class Layer { public: + Layer() : events(Application::getInstance().getEventBus()) {} virtual ~Layer() = default; virtual void onAttach() {} virtual void onDetach() {} @@ -22,6 +24,7 @@ public: protected: std::shared_ptr gameMode; + EventBus& events; }; diff --git a/src/game/GameLayer.cpp b/src/game/GameLayer.cpp index 0c04248..e95f4b7 100644 --- a/src/game/GameLayer.cpp +++ b/src/game/GameLayer.cpp @@ -15,6 +15,9 @@ #include "../engine/renderer/loader/AssetManager.h" #include "hexWorld/HexModelFactory.h" #include "hexWorld/MapGenerator.h" +#include "hexWorld/ecs/components/ProducingComponent.h" +#include "hexWorld/ecs/systems/ProducingSystem.h" +#include "hexWorld/events/TurnChangedEvent.h" GameLayer::GameLayer() :texturedModel(0,0) //Platzhalter, echtes Model kommt in onAttach { @@ -67,13 +70,21 @@ void GameLayer::onAttach() auto transformComponent = std::make_shared(glm::vec3(0,0,0), glm::vec3(0), 1.f); auto modelComponent = std::make_shared(modelStages, "stoneMason"); auto modelStateComponent = std::make_shared(); + auto producingComponent = std::make_shared(RessourceType::STONE, 2, false); + auto ownerComponent = std::make_shared(gameMode->getCurrentPlayer()); 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); + entityManager->addComponent(entityID, producingComponent); + entityManager->addComponent(entityID, ownerComponent); testEntity = entityID; + + events.subscribe([this](const TurnChangedEvent& event) { + ProducingSystem::onTurnEnded(*entityManager); + }); } void GameLayer::onDetach() diff --git a/src/game/UILayer.cpp b/src/game/UILayer.cpp index 7c50ac8..aab0a9f 100644 --- a/src/game/UILayer.cpp +++ b/src/game/UILayer.cpp @@ -19,7 +19,7 @@ #include "ui/components/UiRessourceWidget.h" #include "ui/components/factorys/RessourceWidgetFactory.h" -UILayer::UILayer(): events(Application::getInstance().getEventBus()) { +UILayer::UILayer() { Loader& loader = Loader::instance(); guiRenderer = std::make_unique(loader); textRenderer = std::make_unique(); diff --git a/src/game/UILayer.h b/src/game/UILayer.h index 54796eb..cf18301 100644 --- a/src/game/UILayer.h +++ b/src/game/UILayer.h @@ -28,7 +28,7 @@ private: size_t inventoryContainerID; size_t turnTextID; - EventBus& events; + public: UILayer(); void onAttach() override; diff --git a/src/game/hexWorld/ecs/components/OwnerComponent.h b/src/game/hexWorld/ecs/components/OwnerComponent.h index f1ba9d5..b48918e 100644 --- a/src/game/hexWorld/ecs/components/OwnerComponent.h +++ b/src/game/hexWorld/ecs/components/OwnerComponent.h @@ -9,11 +9,9 @@ class OwnerComponent : public Component{ -private: +public: PlayerID playerID; - - public: - OwnerComponent(PlayerID playerID) : playerID(playerID) {}; + explicit OwnerComponent(PlayerID playerID) : playerID(playerID) {}; }; diff --git a/src/game/hexWorld/ecs/components/ProducingComponent.cpp b/src/game/hexWorld/ecs/components/ProducingComponent.cpp new file mode 100644 index 0000000..29d1ba1 --- /dev/null +++ b/src/game/hexWorld/ecs/components/ProducingComponent.cpp @@ -0,0 +1,5 @@ +// +// Created by sebastian on 14.02.26. +// + +#include "ProducingComponent.h" \ No newline at end of file diff --git a/src/game/hexWorld/ecs/components/ProducingComponent.h b/src/game/hexWorld/ecs/components/ProducingComponent.h new file mode 100644 index 0000000..48def2c --- /dev/null +++ b/src/game/hexWorld/ecs/components/ProducingComponent.h @@ -0,0 +1,42 @@ +// +// Created by sebastian on 14.02.26. +// + +#ifndef DICEWARS_SIEDLER_PRODUCINGCOMPONENT_H +#define DICEWARS_SIEDLER_PRODUCINGCOMPONENT_H +#include "../../../../engine/core/ECS/Component.h" + + +enum class RessourceType; + +class ProducingComponent: public Component { +public: + ProducingComponent(RessourceType produces, int baseAmountPerTurn, bool isBlocked = false) : produces(produces), baseAmountPerTurn(baseAmountPerTurn), blocked(isBlocked) {} + + [[nodiscard]] int getStorage() const {return storage;} + [[nodiscard]] RessourceType getProduces() const {return produces;} + [[nodiscard]] bool isBlocked() const {return blocked;} + [[nodiscard]] float getEfficiency() const {return efficiency;} + [[nodiscard]] int getMaxStorage() const {return maxStorage;} + [[nodiscard]] int getAmountPerTurn() const {return baseAmountPerTurn;} + + void setBlocked(bool blocked) {this->blocked = blocked;} + void setStorage(int storage) {this->storage = storage;} + void setEfficiency(float efficiency) {this->efficiency = efficiency;} + void increaseStorage(int amount) {storage += amount;} + void decreaseStorage(int amount) {storage -= amount;} + void setMaxStorage(int maxStorage) {this->maxStorage = maxStorage;} +private: + RessourceType produces; + int baseAmountPerTurn; + + int storage = 0; + int maxStorage = 20; + + float efficiency = 1.0f; + + bool blocked; +}; + + +#endif //DICEWARS_SIEDLER_PRODUCINGCOMPONENT_H \ No newline at end of file diff --git a/src/game/hexWorld/ecs/systems/ProducingSystem.cpp b/src/game/hexWorld/ecs/systems/ProducingSystem.cpp new file mode 100644 index 0000000..59e60c8 --- /dev/null +++ b/src/game/hexWorld/ecs/systems/ProducingSystem.cpp @@ -0,0 +1,36 @@ +// +// Created by sebastian on 14.02.26. +// + +#include "ProducingSystem.h" + +#include +#include + +#include "../../../../engine/core/ECS/EntityManager.h" +#include "../../../../engine/core/ECS/ModelStateComponent.h" +#include "../components/ProducingComponent.h" + +void ProducingSystem::onTurnEnded(EntityManager &em) { + printf("Updating producing entities...\n"); + for (const EntityID e : em.getAllEntities()) { + const auto prod = em.getComponent(e); + const auto modelState = em.getComponent(e); + + if (!prod || !modelState) { + continue; + } + + if (prod->isBlocked()) { + std::cout << "Producing is blocked for entity " << e << std::endl; + continue; + } + + const int produced = prod->getAmountPerTurn(); + prod->setStorage(std::min(prod->getStorage() + produced, prod->getMaxStorage())); + + float fillRatio = static_cast(prod->getStorage()) / static_cast(prod->getMaxStorage()); + modelState->params["fillRatio"] = fillRatio; + printf("Updated fillRatio: %f\n", fillRatio); + } +} diff --git a/src/game/hexWorld/ecs/systems/ProducingSystem.h b/src/game/hexWorld/ecs/systems/ProducingSystem.h new file mode 100644 index 0000000..84bce98 --- /dev/null +++ b/src/game/hexWorld/ecs/systems/ProducingSystem.h @@ -0,0 +1,18 @@ +// +// Created by sebastian on 14.02.26. +// + +#ifndef DICEWARS_SIEDLER_PRODUCINGSYSTEM_H +#define DICEWARS_SIEDLER_PRODUCINGSYSTEM_H +#include "../../../player/Player.h" + + +class EntityManager; + +class ProducingSystem { +public: + static void onTurnEnded(EntityManager &em); +}; + + +#endif //DICEWARS_SIEDLER_PRODUCINGSYSTEM_H \ No newline at end of file