diff --git a/include/gui/InventoryWidget.hpp b/include/gui/InventoryWidget.hpp index 7d93061f..c8ee8d62 100644 --- a/include/gui/InventoryWidget.hpp +++ b/include/gui/InventoryWidget.hpp @@ -14,7 +14,6 @@ #ifndef INVENTORYWIDGET_HPP_ #define INVENTORYWIDGET_HPP_ -#include "Inventory.hpp" #include "MouseItemWidget.hpp" #include "SDLHeaders.hpp" @@ -22,7 +21,7 @@ class InventoryWidget : public Widget { public: InventoryWidget(Widget *parent = nullptr) : Widget(parent) {} - void init(const Inventory &inventory); + void init(Inventory &inventory); void onEvent(const SDL_Event &event, MouseItemWidget &mouseItemWidget); diff --git a/include/gui/ItemWidget.hpp b/include/gui/ItemWidget.hpp index 80150fb9..ea139e07 100644 --- a/include/gui/ItemWidget.hpp +++ b/include/gui/ItemWidget.hpp @@ -15,19 +15,25 @@ #define ITEMWIDGET_HPP_ #include "Image.hpp" +#include "Inventory.hpp" #include "Widget.hpp" class ItemWidget : public Widget { public: - ItemWidget(u16 id, Widget *parent = nullptr); + ItemWidget(Inventory &inventory, u16 x, u16 y, Widget *parent = nullptr); - u16 item() const { return m_id; } - void setItem(u16 id); + void update(); + + u16 item() const { return m_inventory.getItem(m_x, m_y); } + void setItem(unsigned int id); private: void draw(RenderTarget &target, RenderStates states) const override; - u16 m_id = 0; + Inventory &m_inventory; + + unsigned int m_x = 0; + unsigned int m_y = 0; Image m_image; }; diff --git a/include/gui/MouseItemWidget.hpp b/include/gui/MouseItemWidget.hpp index 24992dc8..cf8ea1be 100644 --- a/include/gui/MouseItemWidget.hpp +++ b/include/gui/MouseItemWidget.hpp @@ -18,7 +18,7 @@ class MouseItemWidget : public ItemWidget { public: - MouseItemWidget(Widget *parent) : ItemWidget(0, parent) {} + MouseItemWidget(Widget *parent) : ItemWidget(m_inventory, 0, 0, parent) { update(); } void onEvent(const SDL_Event &event); @@ -26,6 +26,8 @@ class MouseItemWidget : public ItemWidget { private: void updatePosition(float x, float y); + + Inventory m_inventory{1, 1}; }; #endif // MOUSEITEMWIDGET_HPP_ diff --git a/include/gui/WorkbenchWidget.hpp b/include/gui/WorkbenchWidget.hpp index 1c5323ae..69f51684 100644 --- a/include/gui/WorkbenchWidget.hpp +++ b/include/gui/WorkbenchWidget.hpp @@ -18,7 +18,7 @@ class WorkbenchWidget : public Widget { public: - WorkbenchWidget(Widget *parent = nullptr); + WorkbenchWidget(Inventory &playerInventory, Inventory &hotbarInventory, Widget *parent = nullptr); void onEvent(const SDL_Event &event); @@ -30,10 +30,10 @@ class WorkbenchWidget : public Widget { Inventory m_craftingInventory{3, 3}; InventoryWidget m_craftingInventoryWidget{this}; - Inventory m_inventory{9, 3}; - InventoryWidget m_inventoryWidget{this}; + Inventory &m_playerInventory; + InventoryWidget m_playerInventoryWidget{this}; - Inventory m_hotbarInventory{9, 1}; + Inventory &m_hotbarInventory; InventoryWidget m_hotbarInventoryWidget{this}; MouseItemWidget m_mouseItemWidget{this}; diff --git a/include/hud/Hotbar.hpp b/include/hud/Hotbar.hpp index 004d2731..997eae70 100644 --- a/include/hud/Hotbar.hpp +++ b/include/hud/Hotbar.hpp @@ -16,16 +16,19 @@ #include +#include "Inventory.hpp" #include "ItemWidget.hpp" #include "SDLHeaders.hpp" class Hotbar : public IDrawable { public: - Hotbar(); + Hotbar(Inventory &inventory); + + void update(); void onEvent(const SDL_Event &event); - int cursorPos() const { return m_cursorPos; } + u16 currentItem() const { return m_inventory.getItem(m_cursorPos, 0); } private: void draw(RenderTarget &target, RenderStates states) const override; @@ -35,6 +38,8 @@ class Hotbar : public IDrawable { Image m_cursor; int m_cursorPos = 0; + Inventory &m_inventory; + std::vector m_items; }; diff --git a/include/inventory/Inventory.hpp b/include/inventory/Inventory.hpp index 35259ef3..e1575eca 100644 --- a/include/inventory/Inventory.hpp +++ b/include/inventory/Inventory.hpp @@ -23,6 +23,7 @@ class Inventory { Inventory(u16 width, u16 height) : m_width(width), m_height(height) { m_items.resize(width * height); } + u16 getItem(u16 x, u16 y) { return m_items.at(x + y * m_width); } void setItem(u16 x, u16 y, u16 id); u16 width() const { return m_width; } diff --git a/include/states/GameState.hpp b/include/states/GameState.hpp index bc86e4f5..8bccbb5e 100644 --- a/include/states/GameState.hpp +++ b/include/states/GameState.hpp @@ -57,7 +57,9 @@ class GameState : public ApplicationState { Crosshair m_crosshair; Texture m_widgetTexture; - Hotbar m_hotbar; + Inventory m_playerInventory{9, 3}; + Inventory m_hotbarInventory{9, 1}; + Hotbar m_hotbar{m_hotbarInventory}; }; #endif // GAMESTATE_HPP_ diff --git a/include/states/InventoryState.hpp b/include/states/InventoryState.hpp index c08ccfa2..222fab04 100644 --- a/include/states/InventoryState.hpp +++ b/include/states/InventoryState.hpp @@ -20,7 +20,7 @@ class InventoryState : public ApplicationState { public: - InventoryState(ApplicationState *parent = nullptr); + InventoryState(Inventory &playerInventory, Inventory &hotbarInventory, ApplicationState *parent = nullptr); void onEvent(const SDL_Event &event) override; @@ -31,7 +31,10 @@ class InventoryState : public ApplicationState { Shader m_shader; - WorkbenchWidget m_widget; + Inventory &m_playerInventory; + Inventory &m_hotbarInventory; + + WorkbenchWidget m_widget{m_playerInventory, m_hotbarInventory}; }; #endif // INVENTORYSTATE_HPP_ diff --git a/source/gui/InventoryWidget.cpp b/source/gui/InventoryWidget.cpp index 7c25a604..65791a01 100644 --- a/source/gui/InventoryWidget.cpp +++ b/source/gui/InventoryWidget.cpp @@ -13,12 +13,16 @@ */ #include "InventoryWidget.hpp" -void InventoryWidget::init(const Inventory &inventory) { +void InventoryWidget::init(Inventory &inventory) { m_itemWidgets.clear(); - for (u16 y = 0 ; y < inventory.height() ; ++y) - for (u16 x = 0 ; x < inventory.width() ; ++x) - m_itemWidgets.emplace_back(inventory.items().at(x + y * inventory.width()), this).setPosition(x * 18, y * 18, 0); + for (u16 y = 0 ; y < inventory.height() ; ++y) { + for (u16 x = 0 ; x < inventory.width() ; ++x) { + ItemWidget &widget = m_itemWidgets.emplace_back(inventory, x, y, this); + widget.update(); + widget.setPosition(x * 18, y * 18, 0); + } + } m_width = inventory.width() * 18; m_height = inventory.height() * 18; diff --git a/source/gui/ItemWidget.cpp b/source/gui/ItemWidget.cpp index 1b4b2a30..15e6bfb0 100644 --- a/source/gui/ItemWidget.cpp +++ b/source/gui/ItemWidget.cpp @@ -13,21 +13,21 @@ */ #include "ItemWidget.hpp" -ItemWidget::ItemWidget(u16 id, Widget *parent) : Widget(16, 16, parent) { +ItemWidget::ItemWidget(Inventory &inventory, u16 x, u16 y, Widget *parent) + : Widget(16, 16, parent), m_inventory(inventory), m_x(x), m_y(y) +{ m_image.load("texture-blocks"); m_image.setScale(2.0f / 3.0f, 2.0f / 3.0f, 1.0f); m_image.setPosition(3, 3, 0); - - setItem(id); } -void ItemWidget::setItem(u16 id) { - m_id = id; +void ItemWidget::update() { + m_image.setClipRect(item() * 16, 0, 16, 16); +} - // if (id == 0) - // id = 15; - - m_image.setClipRect(id * 16, 0, 16, 16); +void ItemWidget::setItem(unsigned int id) { + m_inventory.setItem(m_x, m_y, id); + update(); } void ItemWidget::draw(RenderTarget &target, RenderStates states) const { diff --git a/source/gui/WorkbenchWidget.cpp b/source/gui/WorkbenchWidget.cpp index 3bdf3b28..9efd4126 100644 --- a/source/gui/WorkbenchWidget.cpp +++ b/source/gui/WorkbenchWidget.cpp @@ -14,22 +14,17 @@ #include "Config.hpp" #include "WorkbenchWidget.hpp" -WorkbenchWidget::WorkbenchWidget(Widget *parent) : Widget(176, 166, parent) { +WorkbenchWidget::WorkbenchWidget(Inventory &playerInventory, Inventory &hotbarInventory, Widget *parent) + : Widget(176, 166, parent), m_playerInventory(playerInventory), m_hotbarInventory(hotbarInventory) +{ m_background.load("texture-workbench"); m_background.setClipRect(0, 0, 176, 166); - for (u16 i = 0 ; i < 12 ; ++i) { - if (i < 9) - m_hotbarInventory.setItem(i, 0, i + 1); - else - m_inventory.setItem(i - 9, 0, i + 1); - } - m_craftingInventoryWidget.init(m_craftingInventory); m_craftingInventoryWidget.setPosition(29, 16, 0); - m_inventoryWidget.init(m_inventory); - m_inventoryWidget.setPosition(7, 83, 0); + m_playerInventoryWidget.init(m_playerInventory); + m_playerInventoryWidget.setPosition(7, 83, 0); m_hotbarInventoryWidget.init(m_hotbarInventory); m_hotbarInventoryWidget.setPosition(7, 141, 0); @@ -41,7 +36,7 @@ WorkbenchWidget::WorkbenchWidget(Widget *parent) : Widget(176, 166, parent) { void WorkbenchWidget::onEvent(const SDL_Event &event) { m_craftingInventoryWidget.onEvent(event, m_mouseItemWidget); - m_inventoryWidget.onEvent(event, m_mouseItemWidget); + m_playerInventoryWidget.onEvent(event, m_mouseItemWidget); m_hotbarInventoryWidget.onEvent(event, m_mouseItemWidget); m_mouseItemWidget.onEvent(event); @@ -53,7 +48,7 @@ void WorkbenchWidget::draw(RenderTarget &target, RenderStates states) const { target.draw(m_background, states); target.draw(m_craftingInventoryWidget, states); - target.draw(m_inventoryWidget, states); + target.draw(m_playerInventoryWidget, states); target.draw(m_hotbarInventoryWidget, states); target.draw(m_mouseItemWidget, states); diff --git a/source/hud/BlockCursor.cpp b/source/hud/BlockCursor.cpp index bf67f9ac..d2ccee90 100644 --- a/source/hud/BlockCursor.cpp +++ b/source/hud/BlockCursor.cpp @@ -106,7 +106,7 @@ void BlockCursor::onEvent(const SDL_Event &event, const Hotbar &hotbar) { m_selectedBlock.y / Chunk::height, m_selectedBlock.z / Chunk::depth); - if (block && !block->onClickEvent(chunk)) { + if (block && !block->onClickEvent(chunk) && hotbar.currentItem()) { int face = m_selectedBlock.w; int x = m_selectedBlock.x; @@ -120,7 +120,7 @@ void BlockCursor::onEvent(const SDL_Event &event, const Hotbar &hotbar) { if(face == 2) z++; if(face == 5) z--; - m_world.setBlock(x, y, z, hotbar.cursorPos() + 1); + m_world.setBlock(x, y, z, hotbar.currentItem()); } } } diff --git a/source/hud/Hotbar.cpp b/source/hud/Hotbar.cpp index 7bbba4ea..42f66cb0 100644 --- a/source/hud/Hotbar.cpp +++ b/source/hud/Hotbar.cpp @@ -18,7 +18,7 @@ static const auto backgroundX = SCREEN_WIDTH / 2 - 182 * 3 / 2; static const auto backgroundY = SCREEN_HEIGHT - 22 * 3; -Hotbar::Hotbar() { +Hotbar::Hotbar(Inventory &inventory) : m_inventory(inventory) { m_background.load("texture-widgets"); m_background.setClipRect(0, 0, 182, 22); m_background.setPosition(backgroundX, backgroundY, 0); @@ -29,13 +29,20 @@ Hotbar::Hotbar() { m_cursor.setPosition(backgroundX - 3, backgroundY - 3, 0); m_cursor.setScale(3, 3, 1); - for (u16 i = 1 ; i < 10 ; ++i) { - ItemWidget &widget = m_items.emplace_back(i); - widget.setPosition(backgroundX + 16 + 180 / 3.0 * (i - 1) - 8, backgroundY + 7, 0); + for (u16 i = 0 ; i < 9 ; ++i) { + ItemWidget &widget = m_items.emplace_back(m_inventory, i, 0); + widget.update(); + widget.setPosition(backgroundX + 16 + 180 / 3.0 * i - 8.5, backgroundY + 7.5, 0); widget.setScale(3, 3, 1); } } +void Hotbar::update() { + for (u16 i = 0 ; i < 9 ; ++i) { + m_items[i].setItem(m_inventory.getItem(i, 0)); + } +} + void Hotbar::onEvent(const SDL_Event &event) { if (event.type == SDL_MOUSEWHEEL) { if (event.wheel.y == -1) diff --git a/source/inventory/Inventory.cpp b/source/inventory/Inventory.cpp index 2c845e31..e1adf773 100644 --- a/source/inventory/Inventory.cpp +++ b/source/inventory/Inventory.cpp @@ -14,6 +14,6 @@ #include "Inventory.hpp" void Inventory::setItem(u16 x, u16 y, u16 id) { - m_items.at(x + y * m_width) = id; + m_items[x + y * m_width] = id; } diff --git a/source/states/GameState.cpp b/source/states/GameState.cpp index 262582e5..ffb655fa 100644 --- a/source/states/GameState.cpp +++ b/source/states/GameState.cpp @@ -25,6 +25,13 @@ #include "Mouse.hpp" GameState::GameState() { + for (u16 i = 0 ; i < 11 ; ++i) { + if (i < 9) + m_hotbarInventory.setItem(i, 0, i + 1); + else + m_playerInventory.setItem(i - 9, 0, i + 1); + } + initShaders(); } @@ -77,10 +84,13 @@ void GameState::update() { if (&m_stateStack->top() == this) m_viewMatrix = m_camera.processInputs(m_world); + // FIXME: Shouldn't be called every tick + m_hotbar.update(); + m_blockCursor.update(false); if (Keyboard::isKeyPressedOnce(Keyboard::E) && &m_stateStack->top() == this) { - m_stateStack->push(this); + m_stateStack->push(m_playerInventory, m_hotbarInventory, this); } } diff --git a/source/states/InventoryState.cpp b/source/states/InventoryState.cpp index 3d87902b..3ecf9f16 100644 --- a/source/states/InventoryState.cpp +++ b/source/states/InventoryState.cpp @@ -15,7 +15,9 @@ #include "InventoryState.hpp" #include "Mouse.hpp" -InventoryState::InventoryState(ApplicationState *parent) : ApplicationState(parent) { +InventoryState::InventoryState(Inventory &playerInventory, Inventory &hotbarInventory, ApplicationState *parent) + : ApplicationState(parent), m_playerInventory{playerInventory}, m_hotbarInventory{hotbarInventory} +{ m_shader.createProgram(); m_shader.addShader(GL_VERTEX_SHADER, "shaders/basic.v.glsl"); m_shader.addShader(GL_FRAGMENT_SHADER, "shaders/basic.f.glsl");