ADD: Connect PlayerInventory to UI

This commit is contained in:
sebastian 2026-02-13 16:34:53 +01:00
parent 00e7826209
commit c3a0fa662c
18 changed files with 81 additions and 37 deletions

View File

@ -107,8 +107,8 @@ add_executable(Dicewars_Siedler src/main.cpp
src/game/hexWorld/ecs/systems/BuildingPlacementSystem.h src/game/hexWorld/ecs/systems/BuildingPlacementSystem.h
src/game/GameMode.cpp src/game/GameMode.cpp
src/game/GameMode.h src/game/GameMode.h
src/game/PlayerInventory.cpp src/game/player/PlayerInventory.cpp
src/game/PlayerInventory.h src/game/player/PlayerInventory.h
src/game/player/Player.cpp src/game/player/Player.cpp
src/game/player/Player.h src/game/player/Player.h
src/game/hexWorld/ecs/components/OwnerComponent.cpp src/game/hexWorld/ecs/components/OwnerComponent.cpp

View File

@ -8,9 +8,10 @@
#include "../../../platform/glfw/InputManager.h" #include "../../../platform/glfw/InputManager.h"
#include "GLFW/glfw3.h" #include "GLFW/glfw3.h"
void UiComponent::addChild(std::unique_ptr<UiComponent> child) { size_t UiComponent::addChild(std::unique_ptr<UiComponent> child) {
child->parent = this; child->parent = this;
children.emplace_back(std::move(child)); children.emplace_back(std::move(child));
return children.size() - 1;
} }
void UiComponent::setVisible(const bool v) { void UiComponent::setVisible(const bool v) {
@ -46,6 +47,10 @@ bool UiComponent::isMouseOver(float mouseX, float mouseY) {
return uiPositioner.screenSpace.contains(mouseX, mouseY); return uiPositioner.screenSpace.contains(mouseX, mouseY);
} }
UiComponent* UiComponent::getChildAtIndex(size_t t) const {
return children[t].get();
}
void UiComponent::onUpdate(float) { void UiComponent::onUpdate(float) {
glm::vec2 mousePos = InputManager::getMousePositionNormalized(); glm::vec2 mousePos = InputManager::getMousePositionNormalized();
bool hoveredNow = isMouseOver(mousePos.x, mousePos.y); bool hoveredNow = isMouseOver(mousePos.x, mousePos.y);

View File

@ -21,7 +21,7 @@ public:
UiComponent() :uiPositioner(*this) {}; UiComponent() :uiPositioner(*this) {};
UiComponent(const LayoutStyle& layout) : uiPositioner(*this, layout) {}; UiComponent(const LayoutStyle& layout) : uiPositioner(*this, layout) {};
virtual ~UiComponent() = default; virtual ~UiComponent() = default;
void addChild(std::unique_ptr<UiComponent> child); size_t addChild(std::unique_ptr<UiComponent> child);
void setVisible(bool visible); void setVisible(bool visible);
[[nodiscard]] bool isVisible() const; [[nodiscard]] bool isVisible() const;
void update(float delta); void update(float delta);
@ -40,6 +40,8 @@ public:
[[nodiscard]] bool isHovered() const { return state == UiState::HOVERED; } [[nodiscard]] bool isHovered() const { return state == UiState::HOVERED; }
virtual void onClick() {}; virtual void onClick() {};
UiComponent* getChildAtIndex(size_t t) const;
protected: protected:
bool visible = true; bool visible = true;
UiState state = UiState::NORMAL; UiState state = UiState::NORMAL;

View File

@ -4,6 +4,10 @@
#ifndef DICEWARS_SIEDLER_LAYER_H #ifndef DICEWARS_SIEDLER_LAYER_H
#define DICEWARS_SIEDLER_LAYER_H #define DICEWARS_SIEDLER_LAYER_H
#include <memory>
#include <utility>
#include "../../game/GameMode.h"
class Layer { class Layer {
@ -12,6 +16,12 @@ public:
virtual void onAttach() {} virtual void onAttach() {}
virtual void onDetach() {} virtual void onDetach() {}
virtual void onUpdate() {} virtual void onUpdate() {}
void setGameMode(std::shared_ptr<GameMode> gameMode) {this->gameMode = std::move(gameMode);}
[[nodiscard]] std::shared_ptr<GameMode> getGameMode() const {return gameMode;}
protected:
std::shared_ptr<GameMode> gameMode;
}; };

View File

@ -12,5 +12,7 @@ DicewarsApp::DicewarsApp() {
GameLayer* gamelayer = new GameLayer(); GameLayer* gamelayer = new GameLayer();
gamelayer->setGameMode(gameMode); gamelayer->setGameMode(gameMode);
pushLayer(gamelayer); pushLayer(gamelayer);
pushLayer(new UILayer()); UILayer* uiLayer = new UILayer();
uiLayer->setGameMode(gameMode);
pushLayer(uiLayer);
} }

View File

@ -74,6 +74,10 @@ void GameLayer::onUpdate()
if (InputManager::isKeyPressed(GLFW_KEY_A)) moveDir.x -= 1.0f; 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_D)) moveDir.x += 1.0f;
if (InputManager::isKeyPressed(GLFW_KEY_F)) {
gameMode->getPlayerInventory(gameMode->getCurrentPlayer()).add(RessourceType::FOOD, 10);
}
camera->move(moveDir, 0.5f); camera->move(moveDir, 0.5f);
tileHighlightSystem->update(*entityManager, *mousePicker, *camera); tileHighlightSystem->update(*entityManager, *mousePicker, *camera);
@ -83,8 +87,3 @@ void GameLayer::onUpdate()
renderer->render(*light, *camera); renderer->render(*light, *camera);
} }
void GameLayer::setGameMode(std::shared_ptr<GameMode> gameMode) {
this->gameMode = gameMode;
}

View File

@ -25,12 +25,7 @@ public:
void onAttach() override; void onAttach() override;
void onDetach() override; void onDetach() override;
void onUpdate() override; void onUpdate() override;
void setGameMode(std::shared_ptr<GameMode> gameMode);
private: private:
std::shared_ptr<GameMode> gameMode;
TexturedModel texturedModel; TexturedModel texturedModel;
std::unique_ptr<Camera> camera; std::unique_ptr<Camera> camera;
std::unique_ptr<Light> light; std::unique_ptr<Light> light;

View File

@ -36,3 +36,9 @@ void GameMode::addPlayer(PlayerID playerID, std::string name) {
PlayerID GameMode::getCurrentPlayer() { PlayerID GameMode::getCurrentPlayer() {
return 0; return 0;
} }
PlayerInventory& GameMode::getPlayerInventory(PlayerID playerID) {
return *players[playerID].getInventory();
}

View File

@ -18,6 +18,7 @@ public:
void addPlayer(PlayerID playerID, std::string name); void addPlayer(PlayerID playerID, std::string name);
PlayerID getCurrentPlayer(); PlayerID getCurrentPlayer();
PlayerInventory& getPlayerInventory(PlayerID playerID);
private: private:
std::unordered_map<PlayerID, Player> players; std::unordered_map<PlayerID, Player> players;

View File

@ -67,18 +67,29 @@ void UILayer::onAttach() {
auto inventoryContainer = std::make_unique<UiInventoryContainer>(*smallFont); auto inventoryContainer = std::make_unique<UiInventoryContainer>(*smallFont);
inventoryContainer->addRessource("assets/ui/ressource-icons/wood-log.png", 10); inventoryContainer->addRessource("assets/ui/ressource-icons/wood-log.png", 10, RessourceType::WOOD);
inventoryContainer->addRessource("assets/ui/ressource-icons/granite.png", 1139); inventoryContainer->addRessource("assets/ui/ressource-icons/granite.png", 1139, RessourceType::STONE);
inventoryContainer->addRessource("assets/ui/ressource-icons/humans.png", 523); inventoryContainer->addRessource("assets/ui/ressource-icons/humans.png", 523, RessourceType::PEOPLE);
inventoryContainer->addRessource("assets/ui/ressource-icons/bread.png", 89); inventoryContainer->addRessource("assets/ui/ressource-icons/bread.png", 89, RessourceType::FOOD);
inventoryContainer->addRessource("assets/ui/ressource-icons/swords.png", 45); 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() { void UILayer::onUpdate() {
Layer::onUpdate(); Layer::onUpdate();
//Update inventory texts
PlayerInventory playerInventory = gameMode->getPlayerInventory(gameMode->getCurrentPlayer());
auto inventoryContainer = dynamic_cast<UiInventoryContainer*>(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}; Dimensions rootParent {0.0, 0.0, 1.0, 1.0f};
rootContainer->uiPositioner.compute(rootParent); rootContainer->uiPositioner.compute(rootParent);
rootContainer->update(0.f); //Todo: Determine frame time rootContainer->update(0.f); //Todo: Determine frame time

View File

@ -6,11 +6,13 @@
#define UILAYER_H #define UILAYER_H
#include <memory> #include <memory>
#include "GameMode.h"
#include "../engine/core/gui/text/Font.h" #include "../engine/core/gui/text/Font.h"
#include "../engine/core/gui/uiMain/UiContainer.h" #include "../engine/core/gui/uiMain/UiContainer.h"
#include "../engine/layer/Layer.h" #include "../engine/layer/Layer.h"
#include "../engine/renderer/GUIRenderer.h" #include "../engine/renderer/GUIRenderer.h"
#include "../engine/renderer/TextRenderer.h" #include "../engine/renderer/TextRenderer.h"
#include "ui/components/UiInventoryContainer.h"
class UILayer : public Layer{ class UILayer : public Layer{
@ -20,6 +22,8 @@ private:
std::unique_ptr<Font> font; std::unique_ptr<Font> font;
std::unique_ptr<Font> smallFont; std::unique_ptr<Font> smallFont;
std::unique_ptr<UiContainer> rootContainer; std::unique_ptr<UiContainer> rootContainer;
size_t inventoryContainerID;
public: public:
UILayer(); UILayer();
void onAttach() override; void onAttach() override;

View File

@ -4,5 +4,5 @@
#ifndef RESSOURCETYPE_H #ifndef RESSOURCETYPE_H
#define RESSOURCETYPE_H #define RESSOURCETYPE_H
enum class RessourceType {NONE, WOOD, STONE}; enum class RessourceType {NONE, WOOD, STONE, PEOPLE, FOOD, SOLDIERS};
#endif //RESSOURCETYPE_H #endif //RESSOURCETYPE_H

View File

@ -8,7 +8,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include "../PlayerInventory.h" #include "PlayerInventory.h"
using PlayerID = std::uint32_t; using PlayerID = std::uint32_t;

View File

@ -20,3 +20,9 @@ void PlayerInventory::spend(RessourceType resourceType, int amount) {
void PlayerInventory::add(RessourceType resourceType, int amount) { void PlayerInventory::add(RessourceType resourceType, int amount) {
resources[resourceType] += amount; resources[resourceType] += amount;
} }
int PlayerInventory::getAmount(RessourceType resourceType) const {
if (auto it = resources.find(resourceType); it != resources.end())
return it->second;
return 0;
}

View File

@ -6,7 +6,7 @@
#define PLAYERINVENTORY_H #define PLAYERINVENTORY_H
#include <unordered_map> #include <unordered_map>
#include "hexWorld/RessourceType.h" #include "../hexWorld/RessourceType.h"
class PlayerInventory { class PlayerInventory {
@ -17,6 +17,7 @@ public:
bool hasEnough(RessourceType resourceType, int amount) const; bool hasEnough(RessourceType resourceType, int amount) const;
void spend(RessourceType resourceType, int amount); void spend(RessourceType resourceType, int amount);
void add(RessourceType resourceType, int amount); void add(RessourceType resourceType, int amount);
int getAmount(RessourceType resourceType) const;
}; };

View File

@ -7,6 +7,7 @@
#include "UiRessourceWidget.h" #include "UiRessourceWidget.h"
#include "../../../engine/core/gui/uiComponent/UiComponent.h" #include "../../../engine/core/gui/uiComponent/UiComponent.h"
#include "../../../engine/renderer/loader/Loader.h" #include "../../../engine/renderer/loader/Loader.h"
#include "../../hexWorld/RessourceType.h"
#include "factorys/RessourceWidgetFactory.h" #include "factorys/RessourceWidgetFactory.h"
// UiInventoryContainer.h // UiInventoryContainer.h
@ -23,22 +24,22 @@ public:
uiPositioner.setLayout(containerStyle); 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; float marginLeft = (widgets.empty()) ? 0.0f : 10.0f;
auto widget = RessourceWidgetFactory::create(iconPath, amount, font, marginLeft); auto widget = RessourceWidgetFactory::create(iconPath, amount, font, marginLeft);
widgets.push_back(widget.get()); widgets.emplace(widgetID, widget.get());
addChild(std::move(widget)); addChild(std::move(widget));
} }
void updateRessource(size_t index, int newAmount) { void updateRessource(RessourceType type, int newAmount) {
if (index < widgets.size()) { if (auto it = widgets.find(type); it != widgets.end()) {
// widgets[index]->updateAmount(newAmount); // falls du so eine Methode implementierst it->second->setAmount(newAmount);
} }
} }
private: private:
Font& font; Font& font;
std::vector<UiRessourceWidget*> widgets; // raw pointers für Zugriff std::unordered_map<RessourceType, UiRessourceWidget*> widgets;
}; };
#endif //DICEWARS_SIEDLER_UIINVENTORYCONTAINER_H #endif //DICEWARS_SIEDLER_UIINVENTORYCONTAINER_H

View File

@ -9,13 +9,14 @@
UiRessourceWidget::UiRessourceWidget(GLuint iconTextureID, int amount, Font &font, const LayoutStyle &containerStyle, UiRessourceWidget::UiRessourceWidget(GLuint iconTextureID, int amount, Font &font, const LayoutStyle &containerStyle,
LayoutStyle &iconStyle, LayoutStyle &textStyle) : UiComponent(containerStyle) { LayoutStyle &iconStyle, LayoutStyle &textStyle) : UiComponent(containerStyle) {
icon = std::make_unique<UiImage>(iconTextureID, iconStyle); auto icon = std::make_unique<UiImage>(iconTextureID, iconStyle);
text = std::make_unique<UiText>(font, std::to_string(amount), textStyle); auto text = std::make_unique<UiText>(font, std::to_string(amount), textStyle);
addChild(std::move(icon)); iconIndex = addChild(std::move(icon));
addChild(std::move(text)); textIndex = addChild(std::move(text));
} }
void UiRessourceWidget::setAmount(int newAmount) { void UiRessourceWidget::setAmount(int newAmount) {
text->setText(std::to_string(newAmount)); auto text = dynamic_cast<UiText*>(getChildAtIndex(textIndex));
if (text) text->setText(std::to_string(newAmount));
} }

View File

@ -15,8 +15,8 @@ public:
UiRessourceWidget(GLuint iconTextureID, int amount, Font& font, const LayoutStyle& containerStyle, LayoutStyle& iconStyle, LayoutStyle& textStyle); UiRessourceWidget(GLuint iconTextureID, int amount, Font& font, const LayoutStyle& containerStyle, LayoutStyle& iconStyle, LayoutStyle& textStyle);
void setAmount(int newAmount); void setAmount(int newAmount);
private: private:
std::unique_ptr<UiImage> icon; size_t iconIndex;
std::unique_ptr<UiText> text; size_t textIndex;
}; };