From c3a0fa662c79eeb1281b04056f3cecd796360ee6 Mon Sep 17 00:00:00 2001 From: sebastian Date: Fri, 13 Feb 2026 16:34:53 +0100 Subject: [PATCH] ADD: Connect PlayerInventory to UI --- CMakeLists.txt | 4 ++-- .../core/gui/uiComponent/UiComponent.cpp | 7 +++++- src/engine/core/gui/uiComponent/UiComponent.h | 4 +++- src/engine/layer/Layer.h | 10 ++++++++ src/game/DicewarsApp.cpp | 4 +++- src/game/GameLayer.cpp | 9 ++++---- src/game/GameLayer.h | 5 ---- src/game/GameMode.cpp | 6 +++++ src/game/GameMode.h | 1 + src/game/UILayer.cpp | 23 ++++++++++++++----- src/game/UILayer.h | 4 ++++ src/game/hexWorld/RessourceType.h | 2 +- src/game/player/Player.h | 2 +- src/game/{ => player}/PlayerInventory.cpp | 6 +++++ src/game/{ => player}/PlayerInventory.h | 3 ++- src/game/ui/components/UiInventoryContainer.h | 13 ++++++----- src/game/ui/components/UiRessourceWidget.cpp | 11 +++++---- src/game/ui/components/UiRessourceWidget.h | 4 ++-- 18 files changed, 81 insertions(+), 37 deletions(-) rename src/game/{ => player}/PlayerInventory.cpp (75%) rename src/game/{ => player}/PlayerInventory.h (82%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7f20dad..fffd4e7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -107,8 +107,8 @@ add_executable(Dicewars_Siedler src/main.cpp src/game/hexWorld/ecs/systems/BuildingPlacementSystem.h src/game/GameMode.cpp src/game/GameMode.h - src/game/PlayerInventory.cpp - src/game/PlayerInventory.h + src/game/player/PlayerInventory.cpp + src/game/player/PlayerInventory.h src/game/player/Player.cpp src/game/player/Player.h src/game/hexWorld/ecs/components/OwnerComponent.cpp diff --git a/src/engine/core/gui/uiComponent/UiComponent.cpp b/src/engine/core/gui/uiComponent/UiComponent.cpp index ac4ec25..ddbb732 100644 --- a/src/engine/core/gui/uiComponent/UiComponent.cpp +++ b/src/engine/core/gui/uiComponent/UiComponent.cpp @@ -8,9 +8,10 @@ #include "../../../platform/glfw/InputManager.h" #include "GLFW/glfw3.h" -void UiComponent::addChild(std::unique_ptr child) { +size_t UiComponent::addChild(std::unique_ptr child) { child->parent = this; children.emplace_back(std::move(child)); + return children.size() - 1; } void UiComponent::setVisible(const bool v) { @@ -46,6 +47,10 @@ bool UiComponent::isMouseOver(float mouseX, float mouseY) { return uiPositioner.screenSpace.contains(mouseX, mouseY); } +UiComponent* UiComponent::getChildAtIndex(size_t t) const { + return children[t].get(); +} + void UiComponent::onUpdate(float) { glm::vec2 mousePos = InputManager::getMousePositionNormalized(); bool hoveredNow = isMouseOver(mousePos.x, mousePos.y); diff --git a/src/engine/core/gui/uiComponent/UiComponent.h b/src/engine/core/gui/uiComponent/UiComponent.h index b47bc22..c380754 100644 --- a/src/engine/core/gui/uiComponent/UiComponent.h +++ b/src/engine/core/gui/uiComponent/UiComponent.h @@ -21,7 +21,7 @@ public: UiComponent() :uiPositioner(*this) {}; UiComponent(const LayoutStyle& layout) : uiPositioner(*this, layout) {}; virtual ~UiComponent() = default; - void addChild(std::unique_ptr child); + size_t addChild(std::unique_ptr child); void setVisible(bool visible); [[nodiscard]] bool isVisible() const; void update(float delta); @@ -40,6 +40,8 @@ public: [[nodiscard]] bool isHovered() const { return state == UiState::HOVERED; } virtual void onClick() {}; + + UiComponent* getChildAtIndex(size_t t) const; protected: bool visible = true; UiState state = UiState::NORMAL; diff --git a/src/engine/layer/Layer.h b/src/engine/layer/Layer.h index ee671b9..3192238 100644 --- a/src/engine/layer/Layer.h +++ b/src/engine/layer/Layer.h @@ -4,6 +4,10 @@ #ifndef DICEWARS_SIEDLER_LAYER_H #define DICEWARS_SIEDLER_LAYER_H +#include +#include + +#include "../../game/GameMode.h" class Layer { @@ -12,6 +16,12 @@ public: virtual void onAttach() {} virtual void onDetach() {} virtual void onUpdate() {} + + void setGameMode(std::shared_ptr gameMode) {this->gameMode = std::move(gameMode);} + [[nodiscard]] std::shared_ptr getGameMode() const {return gameMode;} + +protected: + std::shared_ptr gameMode; }; diff --git a/src/game/DicewarsApp.cpp b/src/game/DicewarsApp.cpp index 669f042..f9aca08 100644 --- a/src/game/DicewarsApp.cpp +++ b/src/game/DicewarsApp.cpp @@ -12,5 +12,7 @@ DicewarsApp::DicewarsApp() { GameLayer* gamelayer = new GameLayer(); gamelayer->setGameMode(gameMode); pushLayer(gamelayer); - pushLayer(new UILayer()); + UILayer* uiLayer = new UILayer(); + uiLayer->setGameMode(gameMode); + pushLayer(uiLayer); } diff --git a/src/game/GameLayer.cpp b/src/game/GameLayer.cpp index 4090e06..7e63258 100644 --- a/src/game/GameLayer.cpp +++ b/src/game/GameLayer.cpp @@ -74,6 +74,10 @@ void GameLayer::onUpdate() if (InputManager::isKeyPressed(GLFW_KEY_A)) moveDir.x -= 1.0f; if (InputManager::isKeyPressed(GLFW_KEY_D)) moveDir.x += 1.0f; + if (InputManager::isKeyPressed(GLFW_KEY_F)) { + gameMode->getPlayerInventory(gameMode->getCurrentPlayer()).add(RessourceType::FOOD, 10); + } + camera->move(moveDir, 0.5f); tileHighlightSystem->update(*entityManager, *mousePicker, *camera); @@ -83,8 +87,3 @@ void GameLayer::onUpdate() renderer->render(*light, *camera); } -void GameLayer::setGameMode(std::shared_ptr gameMode) { - this->gameMode = gameMode; -} - - diff --git a/src/game/GameLayer.h b/src/game/GameLayer.h index 2776d78..0e225b8 100644 --- a/src/game/GameLayer.h +++ b/src/game/GameLayer.h @@ -25,12 +25,7 @@ public: void onAttach() override; void onDetach() override; void onUpdate() override; - - void setGameMode(std::shared_ptr gameMode); - private: - std::shared_ptr gameMode; - TexturedModel texturedModel; std::unique_ptr camera; std::unique_ptr light; diff --git a/src/game/GameMode.cpp b/src/game/GameMode.cpp index cf7fe8c..2259f86 100644 --- a/src/game/GameMode.cpp +++ b/src/game/GameMode.cpp @@ -36,3 +36,9 @@ void GameMode::addPlayer(PlayerID playerID, std::string name) { PlayerID GameMode::getCurrentPlayer() { return 0; } + +PlayerInventory& GameMode::getPlayerInventory(PlayerID playerID) { + return *players[playerID].getInventory(); +} + + diff --git a/src/game/GameMode.h b/src/game/GameMode.h index 337327e..9928bdd 100644 --- a/src/game/GameMode.h +++ b/src/game/GameMode.h @@ -18,6 +18,7 @@ public: void addPlayer(PlayerID playerID, std::string name); PlayerID getCurrentPlayer(); + PlayerInventory& getPlayerInventory(PlayerID playerID); private: std::unordered_map players; diff --git a/src/game/UILayer.cpp b/src/game/UILayer.cpp index aec7173..ccc9ea6 100644 --- a/src/game/UILayer.cpp +++ b/src/game/UILayer.cpp @@ -67,18 +67,29 @@ void UILayer::onAttach() { auto inventoryContainer = std::make_unique(*smallFont); - inventoryContainer->addRessource("assets/ui/ressource-icons/wood-log.png", 10); - inventoryContainer->addRessource("assets/ui/ressource-icons/granite.png", 1139); - inventoryContainer->addRessource("assets/ui/ressource-icons/humans.png", 523); - inventoryContainer->addRessource("assets/ui/ressource-icons/bread.png", 89); - inventoryContainer->addRessource("assets/ui/ressource-icons/swords.png", 45); + inventoryContainer->addRessource("assets/ui/ressource-icons/wood-log.png", 10, RessourceType::WOOD); + inventoryContainer->addRessource("assets/ui/ressource-icons/granite.png", 1139, RessourceType::STONE); + inventoryContainer->addRessource("assets/ui/ressource-icons/humans.png", 523, RessourceType::PEOPLE); + inventoryContainer->addRessource("assets/ui/ressource-icons/bread.png", 89, RessourceType::FOOD); + inventoryContainer->addRessource("assets/ui/ressource-icons/swords.png", 45, RessourceType::SOLDIERS); - rootContainer->addChild(std::move(inventoryContainer)); + inventoryContainerID = rootContainer->addChild(std::move(inventoryContainer)); } void UILayer::onUpdate() { Layer::onUpdate(); + //Update inventory texts + PlayerInventory playerInventory = gameMode->getPlayerInventory(gameMode->getCurrentPlayer()); + auto inventoryContainer = dynamic_cast(rootContainer->getChildAtIndex(inventoryContainerID)); + if (inventoryContainer) { + inventoryContainer->updateRessource(RessourceType::WOOD, playerInventory.getAmount(RessourceType::WOOD)); + inventoryContainer->updateRessource(RessourceType::STONE, playerInventory.getAmount(RessourceType::STONE)); + inventoryContainer->updateRessource(RessourceType::PEOPLE, playerInventory.getAmount(RessourceType::PEOPLE)); + inventoryContainer->updateRessource(RessourceType::FOOD, playerInventory.getAmount(RessourceType::FOOD)); + inventoryContainer->updateRessource(RessourceType::SOLDIERS, playerInventory.getAmount(RessourceType::SOLDIERS)); + } + Dimensions rootParent {0.0, 0.0, 1.0, 1.0f}; rootContainer->uiPositioner.compute(rootParent); rootContainer->update(0.f); //Todo: Determine frame time diff --git a/src/game/UILayer.h b/src/game/UILayer.h index fd7a8d9..67de6b0 100644 --- a/src/game/UILayer.h +++ b/src/game/UILayer.h @@ -6,11 +6,13 @@ #define UILAYER_H #include +#include "GameMode.h" #include "../engine/core/gui/text/Font.h" #include "../engine/core/gui/uiMain/UiContainer.h" #include "../engine/layer/Layer.h" #include "../engine/renderer/GUIRenderer.h" #include "../engine/renderer/TextRenderer.h" +#include "ui/components/UiInventoryContainer.h" class UILayer : public Layer{ @@ -20,6 +22,8 @@ private: std::unique_ptr font; std::unique_ptr smallFont; std::unique_ptr rootContainer; + + size_t inventoryContainerID; public: UILayer(); void onAttach() override; diff --git a/src/game/hexWorld/RessourceType.h b/src/game/hexWorld/RessourceType.h index f140a14..81bfd17 100644 --- a/src/game/hexWorld/RessourceType.h +++ b/src/game/hexWorld/RessourceType.h @@ -4,5 +4,5 @@ #ifndef RESSOURCETYPE_H #define RESSOURCETYPE_H -enum class RessourceType {NONE, WOOD, STONE}; +enum class RessourceType {NONE, WOOD, STONE, PEOPLE, FOOD, SOLDIERS}; #endif //RESSOURCETYPE_H diff --git a/src/game/player/Player.h b/src/game/player/Player.h index 9d706d4..cc9054c 100644 --- a/src/game/player/Player.h +++ b/src/game/player/Player.h @@ -8,7 +8,7 @@ #include #include -#include "../PlayerInventory.h" +#include "PlayerInventory.h" using PlayerID = std::uint32_t; diff --git a/src/game/PlayerInventory.cpp b/src/game/player/PlayerInventory.cpp similarity index 75% rename from src/game/PlayerInventory.cpp rename to src/game/player/PlayerInventory.cpp index fc6aabb..3a6d8b0 100644 --- a/src/game/PlayerInventory.cpp +++ b/src/game/player/PlayerInventory.cpp @@ -20,3 +20,9 @@ void PlayerInventory::spend(RessourceType resourceType, int amount) { void PlayerInventory::add(RessourceType resourceType, int amount) { resources[resourceType] += amount; } + +int PlayerInventory::getAmount(RessourceType resourceType) const { + if (auto it = resources.find(resourceType); it != resources.end()) + return it->second; + return 0; +} diff --git a/src/game/PlayerInventory.h b/src/game/player/PlayerInventory.h similarity index 82% rename from src/game/PlayerInventory.h rename to src/game/player/PlayerInventory.h index d3f2263..a654eb9 100644 --- a/src/game/PlayerInventory.h +++ b/src/game/player/PlayerInventory.h @@ -6,7 +6,7 @@ #define PLAYERINVENTORY_H #include -#include "hexWorld/RessourceType.h" +#include "../hexWorld/RessourceType.h" class PlayerInventory { @@ -17,6 +17,7 @@ public: bool hasEnough(RessourceType resourceType, int amount) const; void spend(RessourceType resourceType, int amount); void add(RessourceType resourceType, int amount); + int getAmount(RessourceType resourceType) const; }; diff --git a/src/game/ui/components/UiInventoryContainer.h b/src/game/ui/components/UiInventoryContainer.h index 898fb55..94b4351 100644 --- a/src/game/ui/components/UiInventoryContainer.h +++ b/src/game/ui/components/UiInventoryContainer.h @@ -7,6 +7,7 @@ #include "UiRessourceWidget.h" #include "../../../engine/core/gui/uiComponent/UiComponent.h" #include "../../../engine/renderer/loader/Loader.h" +#include "../../hexWorld/RessourceType.h" #include "factorys/RessourceWidgetFactory.h" // UiInventoryContainer.h @@ -23,22 +24,22 @@ public: uiPositioner.setLayout(containerStyle); } - void addRessource(const std::string& iconPath, int amount) { + void addRessource(const std::string& iconPath, int amount, RessourceType widgetID) { float marginLeft = (widgets.empty()) ? 0.0f : 10.0f; auto widget = RessourceWidgetFactory::create(iconPath, amount, font, marginLeft); - widgets.push_back(widget.get()); + widgets.emplace(widgetID, widget.get()); addChild(std::move(widget)); } - void updateRessource(size_t index, int newAmount) { - if (index < widgets.size()) { - // widgets[index]->updateAmount(newAmount); // falls du so eine Methode implementierst + void updateRessource(RessourceType type, int newAmount) { + if (auto it = widgets.find(type); it != widgets.end()) { + it->second->setAmount(newAmount); } } private: Font& font; - std::vector widgets; // raw pointers für Zugriff + std::unordered_map widgets; }; #endif //DICEWARS_SIEDLER_UIINVENTORYCONTAINER_H \ No newline at end of file diff --git a/src/game/ui/components/UiRessourceWidget.cpp b/src/game/ui/components/UiRessourceWidget.cpp index 9a8d206..36cdffe 100644 --- a/src/game/ui/components/UiRessourceWidget.cpp +++ b/src/game/ui/components/UiRessourceWidget.cpp @@ -9,13 +9,14 @@ UiRessourceWidget::UiRessourceWidget(GLuint iconTextureID, int amount, Font &font, const LayoutStyle &containerStyle, LayoutStyle &iconStyle, LayoutStyle &textStyle) : UiComponent(containerStyle) { - icon = std::make_unique(iconTextureID, iconStyle); - text = std::make_unique(font, std::to_string(amount), textStyle); + auto icon = std::make_unique(iconTextureID, iconStyle); + auto text = std::make_unique(font, std::to_string(amount), textStyle); - addChild(std::move(icon)); - addChild(std::move(text)); + iconIndex = addChild(std::move(icon)); + textIndex = addChild(std::move(text)); } void UiRessourceWidget::setAmount(int newAmount) { - text->setText(std::to_string(newAmount)); + auto text = dynamic_cast(getChildAtIndex(textIndex)); + if (text) text->setText(std::to_string(newAmount)); } diff --git a/src/game/ui/components/UiRessourceWidget.h b/src/game/ui/components/UiRessourceWidget.h index 8369878..93af735 100644 --- a/src/game/ui/components/UiRessourceWidget.h +++ b/src/game/ui/components/UiRessourceWidget.h @@ -15,8 +15,8 @@ public: UiRessourceWidget(GLuint iconTextureID, int amount, Font& font, const LayoutStyle& containerStyle, LayoutStyle& iconStyle, LayoutStyle& textStyle); void setAmount(int newAmount); private: - std::unique_ptr icon; - std::unique_ptr text; + size_t iconIndex; + size_t textIndex; };