[Hotbar|WorkbenchWidget] Inventories linked.
This commit is contained in:
parent
a9f182ec61
commit
2eecb88ced
@ -14,7 +14,6 @@
|
|||||||
#ifndef INVENTORYWIDGET_HPP_
|
#ifndef INVENTORYWIDGET_HPP_
|
||||||
#define INVENTORYWIDGET_HPP_
|
#define INVENTORYWIDGET_HPP_
|
||||||
|
|
||||||
#include "Inventory.hpp"
|
|
||||||
#include "MouseItemWidget.hpp"
|
#include "MouseItemWidget.hpp"
|
||||||
#include "SDLHeaders.hpp"
|
#include "SDLHeaders.hpp"
|
||||||
|
|
||||||
@ -22,7 +21,7 @@ class InventoryWidget : public Widget {
|
|||||||
public:
|
public:
|
||||||
InventoryWidget(Widget *parent = nullptr) : Widget(parent) {}
|
InventoryWidget(Widget *parent = nullptr) : Widget(parent) {}
|
||||||
|
|
||||||
void init(const Inventory &inventory);
|
void init(Inventory &inventory);
|
||||||
|
|
||||||
void onEvent(const SDL_Event &event, MouseItemWidget &mouseItemWidget);
|
void onEvent(const SDL_Event &event, MouseItemWidget &mouseItemWidget);
|
||||||
|
|
||||||
|
@ -15,19 +15,25 @@
|
|||||||
#define ITEMWIDGET_HPP_
|
#define ITEMWIDGET_HPP_
|
||||||
|
|
||||||
#include "Image.hpp"
|
#include "Image.hpp"
|
||||||
|
#include "Inventory.hpp"
|
||||||
#include "Widget.hpp"
|
#include "Widget.hpp"
|
||||||
|
|
||||||
class ItemWidget : public Widget {
|
class ItemWidget : public Widget {
|
||||||
public:
|
public:
|
||||||
ItemWidget(u16 id, Widget *parent = nullptr);
|
ItemWidget(Inventory &inventory, u16 x, u16 y, Widget *parent = nullptr);
|
||||||
|
|
||||||
u16 item() const { return m_id; }
|
void update();
|
||||||
void setItem(u16 id);
|
|
||||||
|
u16 item() const { return m_inventory.getItem(m_x, m_y); }
|
||||||
|
void setItem(unsigned int id);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void draw(RenderTarget &target, RenderStates states) const override;
|
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;
|
Image m_image;
|
||||||
};
|
};
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
class MouseItemWidget : public ItemWidget {
|
class MouseItemWidget : public ItemWidget {
|
||||||
public:
|
public:
|
||||||
MouseItemWidget(Widget *parent) : ItemWidget(0, parent) {}
|
MouseItemWidget(Widget *parent) : ItemWidget(m_inventory, 0, 0, parent) { update(); }
|
||||||
|
|
||||||
void onEvent(const SDL_Event &event);
|
void onEvent(const SDL_Event &event);
|
||||||
|
|
||||||
@ -26,6 +26,8 @@ class MouseItemWidget : public ItemWidget {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
void updatePosition(float x, float y);
|
void updatePosition(float x, float y);
|
||||||
|
|
||||||
|
Inventory m_inventory{1, 1};
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // MOUSEITEMWIDGET_HPP_
|
#endif // MOUSEITEMWIDGET_HPP_
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
class WorkbenchWidget : public Widget {
|
class WorkbenchWidget : public Widget {
|
||||||
public:
|
public:
|
||||||
WorkbenchWidget(Widget *parent = nullptr);
|
WorkbenchWidget(Inventory &playerInventory, Inventory &hotbarInventory, Widget *parent = nullptr);
|
||||||
|
|
||||||
void onEvent(const SDL_Event &event);
|
void onEvent(const SDL_Event &event);
|
||||||
|
|
||||||
@ -30,10 +30,10 @@ class WorkbenchWidget : public Widget {
|
|||||||
Inventory m_craftingInventory{3, 3};
|
Inventory m_craftingInventory{3, 3};
|
||||||
InventoryWidget m_craftingInventoryWidget{this};
|
InventoryWidget m_craftingInventoryWidget{this};
|
||||||
|
|
||||||
Inventory m_inventory{9, 3};
|
Inventory &m_playerInventory;
|
||||||
InventoryWidget m_inventoryWidget{this};
|
InventoryWidget m_playerInventoryWidget{this};
|
||||||
|
|
||||||
Inventory m_hotbarInventory{9, 1};
|
Inventory &m_hotbarInventory;
|
||||||
InventoryWidget m_hotbarInventoryWidget{this};
|
InventoryWidget m_hotbarInventoryWidget{this};
|
||||||
|
|
||||||
MouseItemWidget m_mouseItemWidget{this};
|
MouseItemWidget m_mouseItemWidget{this};
|
||||||
|
@ -16,16 +16,19 @@
|
|||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "Inventory.hpp"
|
||||||
#include "ItemWidget.hpp"
|
#include "ItemWidget.hpp"
|
||||||
#include "SDLHeaders.hpp"
|
#include "SDLHeaders.hpp"
|
||||||
|
|
||||||
class Hotbar : public IDrawable {
|
class Hotbar : public IDrawable {
|
||||||
public:
|
public:
|
||||||
Hotbar();
|
Hotbar(Inventory &inventory);
|
||||||
|
|
||||||
|
void update();
|
||||||
|
|
||||||
void onEvent(const SDL_Event &event);
|
void onEvent(const SDL_Event &event);
|
||||||
|
|
||||||
int cursorPos() const { return m_cursorPos; }
|
u16 currentItem() const { return m_inventory.getItem(m_cursorPos, 0); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void draw(RenderTarget &target, RenderStates states) const override;
|
void draw(RenderTarget &target, RenderStates states) const override;
|
||||||
@ -35,6 +38,8 @@ class Hotbar : public IDrawable {
|
|||||||
Image m_cursor;
|
Image m_cursor;
|
||||||
int m_cursorPos = 0;
|
int m_cursorPos = 0;
|
||||||
|
|
||||||
|
Inventory &m_inventory;
|
||||||
|
|
||||||
std::vector<ItemWidget> m_items;
|
std::vector<ItemWidget> m_items;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -23,6 +23,7 @@ class Inventory {
|
|||||||
Inventory(u16 width, u16 height)
|
Inventory(u16 width, u16 height)
|
||||||
: m_width(width), m_height(height) { m_items.resize(width * 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);
|
void setItem(u16 x, u16 y, u16 id);
|
||||||
|
|
||||||
u16 width() const { return m_width; }
|
u16 width() const { return m_width; }
|
||||||
|
@ -57,7 +57,9 @@ class GameState : public ApplicationState {
|
|||||||
Crosshair m_crosshair;
|
Crosshair m_crosshair;
|
||||||
|
|
||||||
Texture m_widgetTexture;
|
Texture m_widgetTexture;
|
||||||
Hotbar m_hotbar;
|
Inventory m_playerInventory{9, 3};
|
||||||
|
Inventory m_hotbarInventory{9, 1};
|
||||||
|
Hotbar m_hotbar{m_hotbarInventory};
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // GAMESTATE_HPP_
|
#endif // GAMESTATE_HPP_
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
|
|
||||||
class InventoryState : public ApplicationState {
|
class InventoryState : public ApplicationState {
|
||||||
public:
|
public:
|
||||||
InventoryState(ApplicationState *parent = nullptr);
|
InventoryState(Inventory &playerInventory, Inventory &hotbarInventory, ApplicationState *parent = nullptr);
|
||||||
|
|
||||||
void onEvent(const SDL_Event &event) override;
|
void onEvent(const SDL_Event &event) override;
|
||||||
|
|
||||||
@ -31,7 +31,10 @@ class InventoryState : public ApplicationState {
|
|||||||
|
|
||||||
Shader m_shader;
|
Shader m_shader;
|
||||||
|
|
||||||
WorkbenchWidget m_widget;
|
Inventory &m_playerInventory;
|
||||||
|
Inventory &m_hotbarInventory;
|
||||||
|
|
||||||
|
WorkbenchWidget m_widget{m_playerInventory, m_hotbarInventory};
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // INVENTORYSTATE_HPP_
|
#endif // INVENTORYSTATE_HPP_
|
||||||
|
@ -13,12 +13,16 @@
|
|||||||
*/
|
*/
|
||||||
#include "InventoryWidget.hpp"
|
#include "InventoryWidget.hpp"
|
||||||
|
|
||||||
void InventoryWidget::init(const Inventory &inventory) {
|
void InventoryWidget::init(Inventory &inventory) {
|
||||||
m_itemWidgets.clear();
|
m_itemWidgets.clear();
|
||||||
|
|
||||||
for (u16 y = 0 ; y < inventory.height() ; ++y)
|
for (u16 y = 0 ; y < inventory.height() ; ++y) {
|
||||||
for (u16 x = 0 ; x < inventory.width() ; ++x)
|
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);
|
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_width = inventory.width() * 18;
|
||||||
m_height = inventory.height() * 18;
|
m_height = inventory.height() * 18;
|
||||||
|
@ -13,21 +13,21 @@
|
|||||||
*/
|
*/
|
||||||
#include "ItemWidget.hpp"
|
#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.load("texture-blocks");
|
||||||
m_image.setScale(2.0f / 3.0f, 2.0f / 3.0f, 1.0f);
|
m_image.setScale(2.0f / 3.0f, 2.0f / 3.0f, 1.0f);
|
||||||
m_image.setPosition(3, 3, 0);
|
m_image.setPosition(3, 3, 0);
|
||||||
|
|
||||||
setItem(id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ItemWidget::setItem(u16 id) {
|
void ItemWidget::update() {
|
||||||
m_id = id;
|
m_image.setClipRect(item() * 16, 0, 16, 16);
|
||||||
|
}
|
||||||
|
|
||||||
// if (id == 0)
|
void ItemWidget::setItem(unsigned int id) {
|
||||||
// id = 15;
|
m_inventory.setItem(m_x, m_y, id);
|
||||||
|
update();
|
||||||
m_image.setClipRect(id * 16, 0, 16, 16);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ItemWidget::draw(RenderTarget &target, RenderStates states) const {
|
void ItemWidget::draw(RenderTarget &target, RenderStates states) const {
|
||||||
|
@ -14,22 +14,17 @@
|
|||||||
#include "Config.hpp"
|
#include "Config.hpp"
|
||||||
#include "WorkbenchWidget.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.load("texture-workbench");
|
||||||
m_background.setClipRect(0, 0, 176, 166);
|
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.init(m_craftingInventory);
|
||||||
m_craftingInventoryWidget.setPosition(29, 16, 0);
|
m_craftingInventoryWidget.setPosition(29, 16, 0);
|
||||||
|
|
||||||
m_inventoryWidget.init(m_inventory);
|
m_playerInventoryWidget.init(m_playerInventory);
|
||||||
m_inventoryWidget.setPosition(7, 83, 0);
|
m_playerInventoryWidget.setPosition(7, 83, 0);
|
||||||
|
|
||||||
m_hotbarInventoryWidget.init(m_hotbarInventory);
|
m_hotbarInventoryWidget.init(m_hotbarInventory);
|
||||||
m_hotbarInventoryWidget.setPosition(7, 141, 0);
|
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) {
|
void WorkbenchWidget::onEvent(const SDL_Event &event) {
|
||||||
m_craftingInventoryWidget.onEvent(event, m_mouseItemWidget);
|
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_hotbarInventoryWidget.onEvent(event, m_mouseItemWidget);
|
||||||
|
|
||||||
m_mouseItemWidget.onEvent(event);
|
m_mouseItemWidget.onEvent(event);
|
||||||
@ -53,7 +48,7 @@ void WorkbenchWidget::draw(RenderTarget &target, RenderStates states) const {
|
|||||||
target.draw(m_background, states);
|
target.draw(m_background, states);
|
||||||
|
|
||||||
target.draw(m_craftingInventoryWidget, states);
|
target.draw(m_craftingInventoryWidget, states);
|
||||||
target.draw(m_inventoryWidget, states);
|
target.draw(m_playerInventoryWidget, states);
|
||||||
target.draw(m_hotbarInventoryWidget, states);
|
target.draw(m_hotbarInventoryWidget, states);
|
||||||
|
|
||||||
target.draw(m_mouseItemWidget, states);
|
target.draw(m_mouseItemWidget, states);
|
||||||
|
@ -106,7 +106,7 @@ void BlockCursor::onEvent(const SDL_Event &event, const Hotbar &hotbar) {
|
|||||||
m_selectedBlock.y / Chunk::height,
|
m_selectedBlock.y / Chunk::height,
|
||||||
m_selectedBlock.z / Chunk::depth);
|
m_selectedBlock.z / Chunk::depth);
|
||||||
|
|
||||||
if (block && !block->onClickEvent(chunk)) {
|
if (block && !block->onClickEvent(chunk) && hotbar.currentItem()) {
|
||||||
int face = m_selectedBlock.w;
|
int face = m_selectedBlock.w;
|
||||||
|
|
||||||
int x = m_selectedBlock.x;
|
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 == 2) z++;
|
||||||
if(face == 5) z--;
|
if(face == 5) z--;
|
||||||
|
|
||||||
m_world.setBlock(x, y, z, hotbar.cursorPos() + 1);
|
m_world.setBlock(x, y, z, hotbar.currentItem());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
static const auto backgroundX = SCREEN_WIDTH / 2 - 182 * 3 / 2;
|
static const auto backgroundX = SCREEN_WIDTH / 2 - 182 * 3 / 2;
|
||||||
static const auto backgroundY = SCREEN_HEIGHT - 22 * 3;
|
static const auto backgroundY = SCREEN_HEIGHT - 22 * 3;
|
||||||
|
|
||||||
Hotbar::Hotbar() {
|
Hotbar::Hotbar(Inventory &inventory) : m_inventory(inventory) {
|
||||||
m_background.load("texture-widgets");
|
m_background.load("texture-widgets");
|
||||||
m_background.setClipRect(0, 0, 182, 22);
|
m_background.setClipRect(0, 0, 182, 22);
|
||||||
m_background.setPosition(backgroundX, backgroundY, 0);
|
m_background.setPosition(backgroundX, backgroundY, 0);
|
||||||
@ -29,13 +29,20 @@ Hotbar::Hotbar() {
|
|||||||
m_cursor.setPosition(backgroundX - 3, backgroundY - 3, 0);
|
m_cursor.setPosition(backgroundX - 3, backgroundY - 3, 0);
|
||||||
m_cursor.setScale(3, 3, 1);
|
m_cursor.setScale(3, 3, 1);
|
||||||
|
|
||||||
for (u16 i = 1 ; i < 10 ; ++i) {
|
for (u16 i = 0 ; i < 9 ; ++i) {
|
||||||
ItemWidget &widget = m_items.emplace_back(i);
|
ItemWidget &widget = m_items.emplace_back(m_inventory, i, 0);
|
||||||
widget.setPosition(backgroundX + 16 + 180 / 3.0 * (i - 1) - 8, backgroundY + 7, 0);
|
widget.update();
|
||||||
|
widget.setPosition(backgroundX + 16 + 180 / 3.0 * i - 8.5, backgroundY + 7.5, 0);
|
||||||
widget.setScale(3, 3, 1);
|
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) {
|
void Hotbar::onEvent(const SDL_Event &event) {
|
||||||
if (event.type == SDL_MOUSEWHEEL) {
|
if (event.type == SDL_MOUSEWHEEL) {
|
||||||
if (event.wheel.y == -1)
|
if (event.wheel.y == -1)
|
||||||
|
@ -14,6 +14,6 @@
|
|||||||
#include "Inventory.hpp"
|
#include "Inventory.hpp"
|
||||||
|
|
||||||
void Inventory::setItem(u16 x, u16 y, u16 id) {
|
void Inventory::setItem(u16 x, u16 y, u16 id) {
|
||||||
m_items.at(x + y * m_width) = id;
|
m_items[x + y * m_width] = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,6 +25,13 @@
|
|||||||
#include "Mouse.hpp"
|
#include "Mouse.hpp"
|
||||||
|
|
||||||
GameState::GameState() {
|
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();
|
initShaders();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,10 +84,13 @@ void GameState::update() {
|
|||||||
if (&m_stateStack->top() == this)
|
if (&m_stateStack->top() == this)
|
||||||
m_viewMatrix = m_camera.processInputs(m_world);
|
m_viewMatrix = m_camera.processInputs(m_world);
|
||||||
|
|
||||||
|
// FIXME: Shouldn't be called every tick
|
||||||
|
m_hotbar.update();
|
||||||
|
|
||||||
m_blockCursor.update(false);
|
m_blockCursor.update(false);
|
||||||
|
|
||||||
if (Keyboard::isKeyPressedOnce(Keyboard::E) && &m_stateStack->top() == this) {
|
if (Keyboard::isKeyPressedOnce(Keyboard::E) && &m_stateStack->top() == this) {
|
||||||
m_stateStack->push<InventoryState>(this);
|
m_stateStack->push<InventoryState>(m_playerInventory, m_hotbarInventory, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,7 +15,9 @@
|
|||||||
#include "InventoryState.hpp"
|
#include "InventoryState.hpp"
|
||||||
#include "Mouse.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.createProgram();
|
||||||
m_shader.addShader(GL_VERTEX_SHADER, "shaders/basic.v.glsl");
|
m_shader.addShader(GL_VERTEX_SHADER, "shaders/basic.v.glsl");
|
||||||
m_shader.addShader(GL_FRAGMENT_SHADER, "shaders/basic.f.glsl");
|
m_shader.addShader(GL_FRAGMENT_SHADER, "shaders/basic.f.glsl");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user