ADD: Connect PlayerInventory to UI
This commit is contained in:
parent
00e7826209
commit
c3a0fa662c
@ -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
|
||||
|
||||
@ -8,9 +8,10 @@
|
||||
#include "../../../platform/glfw/InputManager.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;
|
||||
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);
|
||||
|
||||
@ -21,7 +21,7 @@ public:
|
||||
UiComponent() :uiPositioner(*this) {};
|
||||
UiComponent(const LayoutStyle& layout) : uiPositioner(*this, layout) {};
|
||||
virtual ~UiComponent() = default;
|
||||
void addChild(std::unique_ptr<UiComponent> child);
|
||||
size_t addChild(std::unique_ptr<UiComponent> 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;
|
||||
|
||||
@ -4,6 +4,10 @@
|
||||
|
||||
#ifndef DICEWARS_SIEDLER_LAYER_H
|
||||
#define DICEWARS_SIEDLER_LAYER_H
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#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> gameMode) {this->gameMode = std::move(gameMode);}
|
||||
[[nodiscard]] std::shared_ptr<GameMode> getGameMode() const {return gameMode;}
|
||||
|
||||
protected:
|
||||
std::shared_ptr<GameMode> gameMode;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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> gameMode) {
|
||||
this->gameMode = gameMode;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -25,12 +25,7 @@ public:
|
||||
void onAttach() override;
|
||||
void onDetach() override;
|
||||
void onUpdate() override;
|
||||
|
||||
void setGameMode(std::shared_ptr<GameMode> gameMode);
|
||||
|
||||
private:
|
||||
std::shared_ptr<GameMode> gameMode;
|
||||
|
||||
TexturedModel texturedModel;
|
||||
std::unique_ptr<Camera> camera;
|
||||
std::unique_ptr<Light> light;
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -18,6 +18,7 @@ public:
|
||||
void addPlayer(PlayerID playerID, std::string name);
|
||||
|
||||
PlayerID getCurrentPlayer();
|
||||
PlayerInventory& getPlayerInventory(PlayerID playerID);
|
||||
|
||||
private:
|
||||
std::unordered_map<PlayerID, Player> players;
|
||||
|
||||
@ -67,18 +67,29 @@ void UILayer::onAttach() {
|
||||
|
||||
auto inventoryContainer = std::make_unique<UiInventoryContainer>(*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<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};
|
||||
rootContainer->uiPositioner.compute(rootParent);
|
||||
rootContainer->update(0.f); //Todo: Determine frame time
|
||||
|
||||
@ -6,11 +6,13 @@
|
||||
#define UILAYER_H
|
||||
#include <memory>
|
||||
|
||||
#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> font;
|
||||
std::unique_ptr<Font> smallFont;
|
||||
std::unique_ptr<UiContainer> rootContainer;
|
||||
|
||||
size_t inventoryContainerID;
|
||||
public:
|
||||
UILayer();
|
||||
void onAttach() override;
|
||||
|
||||
@ -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
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "../PlayerInventory.h"
|
||||
#include "PlayerInventory.h"
|
||||
|
||||
using PlayerID = std::uint32_t;
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
@ -6,7 +6,7 @@
|
||||
#define PLAYERINVENTORY_H
|
||||
#include <unordered_map>
|
||||
|
||||
#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;
|
||||
|
||||
};
|
||||
|
||||
@ -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<UiRessourceWidget*> widgets; // raw pointers für Zugriff
|
||||
std::unordered_map<RessourceType, UiRessourceWidget*> widgets;
|
||||
};
|
||||
|
||||
#endif //DICEWARS_SIEDLER_UIINVENTORYCONTAINER_H
|
||||
@ -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<UiImage>(iconTextureID, iconStyle);
|
||||
text = std::make_unique<UiText>(font, std::to_string(amount), textStyle);
|
||||
auto icon = std::make_unique<UiImage>(iconTextureID, iconStyle);
|
||||
auto text = std::make_unique<UiText>(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<UiText*>(getChildAtIndex(textIndex));
|
||||
if (text) text->setText(std::to_string(newAmount));
|
||||
}
|
||||
|
||||
@ -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<UiImage> icon;
|
||||
std::unique_ptr<UiText> text;
|
||||
size_t iconIndex;
|
||||
size_t textIndex;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user