[BlockInfoWidget] Waila-like widget added.

This commit is contained in:
Quentin Bazin 2018-07-06 15:08:13 +02:00
parent 4f02b3623c
commit fd4c357403
18 changed files with 147 additions and 20 deletions

View File

@ -31,6 +31,8 @@ class BlockCursor : public IDrawable {
void update(const Hotbar &hotbar, bool useDepthBuffer);
const Block *currentBlock() const { return m_currentBlock; }
private:
void updateVertexBuffer(const Block &block);
void updateAnimationVertexBuffer(const Block &block, int animationPos = -1);
@ -51,6 +53,7 @@ class BlockCursor : public IDrawable {
unsigned int m_animationStart = 0;
glm::vec4 m_selectedBlock{0, 0, 0, 0};
const Block *m_currentBlock = nullptr;
};
#endif // BLOCKCURSOR_HPP_

View File

@ -0,0 +1,42 @@
/*
* =====================================================================================
*
* Filename: BlockInfoWidget.hpp
*
* Description:
*
* Created: 06/07/2018 14:32:50
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#ifndef BLOCKINFOWIDGET_HPP_
#define BLOCKINFOWIDGET_HPP_
#include "ItemWidget.hpp"
class BlockInfoWidget : public Widget {
public:
BlockInfoWidget(Widget *parent = nullptr);
void update() override;
const Block *currentBlock() const { return m_currentBlock; }
void setCurrentBlock(const Block *block);
private:
void draw(RenderTarget &target, RenderStates states) const override;
Inventory m_inventory{1, 1};
ItemWidget m_itemWidget{m_inventory, 0, 0, this};
Sprite m_background{"texture-toasts", 160, 32};
Text m_text;
bool m_isVisible = false;
const Block *m_currentBlock = nullptr;
};
#endif // BLOCKINFOWIDGET_HPP_

View File

@ -15,6 +15,7 @@
#define HUD_HPP_
#include "BlockCursor.hpp"
#include "BlockInfoWidget.hpp"
#include "Crosshair.hpp"
#include "DebugOverlay.hpp"
#include "Hotbar.hpp"
@ -40,6 +41,8 @@ class HUD : public Transformable, public IDrawable {
DebugOverlay m_debugOverlay;
bool m_isDebugOverlayVisible = false;
BlockInfoWidget m_blockInfoWidget;
};
#endif // HUD_HPP_

View File

@ -22,7 +22,7 @@
class Hotbar : public Widget {
public:
Hotbar(Inventory &inventory);
Hotbar(Inventory &inventory, Widget *parent = nullptr);
void onEvent(const SDL_Event &event);

View File

@ -24,6 +24,7 @@ class Inventory {
: m_width(width), m_height(height) { m_items.resize(width * height); }
const ItemStack &getStack(u16 x, u16 y) const { return m_items.at(x + y * m_width); }
ItemStack &getStack(u16 x, u16 y) { return m_items.at(x + y * m_width); }
void setStack(u16 x, u16 y, u16 id, u16 amount = 1);
void addStack(u16 id, u16 amount = 1);

View File

@ -22,6 +22,7 @@ class ItemStack {
ItemStack(u16 id, u16 amount = 1);
const Item &item() const { return *m_item; }
void setItem(u16 item);
u16 amount() const { return m_amount; }
void setAmount(u16 amount) { m_amount = amount; }

View File

@ -27,7 +27,7 @@ class World;
class Block {
public:
Block(u32 id, u32 textureID);
Block(u32 id, u32 textureID, const std::string &name);
virtual ~Block() = default;
virtual void onTick(const glm::ivec3 &, Player &, Chunk &, World &) const {}
@ -40,6 +40,9 @@ class Block {
u16 data() const { return (m_id >> 16) & 0xffff; }
u32 textureID() const { return m_textureID; }
const std::string &name() const { return m_name; }
void setName(const std::string &name) { m_name = name; }
s8 selectedFace() const { return m_selectedFace; }
void setSelected(bool isSelected, s8 face) { m_isSelected = isSelected; m_selectedFace = face; }
@ -72,6 +75,8 @@ class Block {
u32 m_id;
u32 m_textureID;
std::string m_name;
bool m_isSelected = false;
s8 m_selectedFace = -1;

View File

@ -29,7 +29,7 @@ void Registry::registerBlocks() {
tinyxml2::XMLElement *blockElement = doc.FirstChildElement("blocks").FirstChildElement("block").ToElement();
while (blockElement) {
u16 id = blockElement->UnsignedAttribute("id");
// const char *name = blockElement->Attribute("name");
const char *name = blockElement->Attribute("name");
if (id == BlockType::Workbench) registerBlock<BlockWorkbench>();
else if (id == BlockType::Furnace) registerBlock<BlockFurnace>();
@ -37,7 +37,7 @@ void Registry::registerBlocks() {
else {
u16 textureID = blockElement->UnsignedAttribute("textureID");
auto &block = registerBlock<Block>(id, textureID);
auto &block = registerBlock<Block>(id, textureID, name);
float hardness = 0;
if (blockElement->QueryFloatAttribute("hardness", &hardness) == tinyxml2::XMLError::XML_SUCCESS)

View File

@ -189,7 +189,7 @@ void BlockCursor::update(const Hotbar &hotbar, bool useDepthBuffer) {
m_selectedBlock = selectedBlock;
const Block &block = Registry::getInstance().getBlock(m_world.getBlock(m_selectedBlock.x, m_selectedBlock.y, m_selectedBlock.z));
m_currentBlock = &Registry::getInstance().getBlock(m_world.getBlock(m_selectedBlock.x, m_selectedBlock.y, m_selectedBlock.z));
// if (block.boundingBox().intersects(FloatBox{m_selectedBlock.x, m_selectedBlock.y, m_selectedBlock.z, 1, 1, 1})) {
// selectedBlockChanged = false;
// m_selectedBlock.w = -1;
@ -199,19 +199,21 @@ void BlockCursor::update(const Hotbar &hotbar, bool useDepthBuffer) {
m_animationStart = (m_animationStart) ? GameClock::getTicks() : 0;
const ItemStack &currentStack = m_player.hotbarInventory().getStack(hotbar.cursorPos(), 0);
float timeToBreak = block.timeToBreak(currentStack.item().harvestCapability(), currentStack.item().miningSpeed());
float timeToBreak = m_currentBlock->timeToBreak(currentStack.item().harvestCapability(), currentStack.item().miningSpeed());
if (m_animationStart && GameClock::getTicks() > m_animationStart + timeToBreak * 1000) {
ItemStack itemDrop = block.getItemDrop();
ItemStack itemDrop = m_currentBlock->getItemDrop();
m_player.inventory().addStack(itemDrop.item().id(), itemDrop.amount());
m_world.setBlock(m_selectedBlock.x, m_selectedBlock.y, m_selectedBlock.z, 0);
m_animationStart = GameClock::getTicks();
}
if (m_selectedBlock.w != -1)
updateVertexBuffer(block);
updateVertexBuffer(*m_currentBlock);
else
m_currentBlock = nullptr;
if (m_animationStart)
updateAnimationVertexBuffer(block, (GameClock::getTicks() - m_animationStart) / (timeToBreak * 100));
if (m_animationStart && m_currentBlock)
updateAnimationVertexBuffer(*m_currentBlock, (GameClock::getTicks() - m_animationStart) / (timeToBreak * 100));
}
#include "ResourceHandler.hpp"

View File

@ -0,0 +1,52 @@
/*
* =====================================================================================
*
* Filename: BlockInfoWidget.cpp
*
* Description:
*
* Created: 06/07/2018 14:34:00
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#include "Block.hpp"
#include "BlockInfoWidget.hpp"
BlockInfoWidget::BlockInfoWidget(Widget *parent) : Widget(160, 32, parent) {
m_itemWidget.setPosition(5, m_height / 2 - m_itemWidget.height() / 2, 0);
m_background.setColor(Color{255, 255, 255, 200});
m_text.setColor(Color{240, 240, 240});
m_text.setPosition(26, 8, 0);
}
void BlockInfoWidget::update() {
m_itemWidget.update();
}
void BlockInfoWidget::setCurrentBlock(const Block *block) {
m_currentBlock = block;
if (!m_currentBlock)
m_isVisible = false;
else {
m_isVisible = true;
m_text.setText(block->name());
m_itemWidget.setStack(block->id(), 1);
}
}
void BlockInfoWidget::draw(RenderTarget &target, RenderStates states) const {
if (m_isVisible) {
applyTransform(states);
target.draw(m_background, states);
target.draw(m_itemWidget, states);
target.draw(m_text, states);
}
}

View File

@ -19,7 +19,6 @@
DebugOverlay::DebugOverlay(const Camera &camera) : m_camera(camera) {
setPosition(4, 4, 0);
setScale(GUI_SCALE, GUI_SCALE, 1);
m_versionText.setText(APP_NAME + std::string(" v0.0.1"));
m_versionText.setColor(Color::white);

View File

@ -18,11 +18,16 @@ HUD::HUD(Camera &camera, Player &player, World &world, glm::mat4 &viewMatrix, gl
m_blockCursor(camera, player, world, viewMatrix, projectionMatrix),
m_debugOverlay(camera)
{
setScale(GUI_SCALE, GUI_SCALE, 1);
m_shader.createProgram();
m_shader.addShader(GL_VERTEX_SHADER, "resources/shaders/basic.v.glsl");
m_shader.addShader(GL_FRAGMENT_SHADER, "resources/shaders/basic.f.glsl");
m_shader.linkProgram();
m_hotbar.setPosition(SCREEN_WIDTH / getScale().x / 2 - m_hotbar.width() / 2, SCREEN_HEIGHT / getScale().y - m_hotbar.height(), 0);
m_blockInfoWidget.setPosition(SCREEN_WIDTH / getScale().x / 2 - m_blockInfoWidget.width() / 2, 2, 0);
}
void HUD::onEvent(const SDL_Event &event) {
@ -39,7 +44,13 @@ void HUD::update() {
m_blockCursor.update(m_hotbar, false);
m_debugOverlay.update();
if (m_isDebugOverlayVisible)
m_debugOverlay.update();
m_blockInfoWidget.update();
if (m_blockCursor.currentBlock() != m_blockInfoWidget.currentBlock())
m_blockInfoWidget.setCurrentBlock(m_blockCursor.currentBlock());
}
void HUD::draw(RenderTarget &target, RenderStates states) const {
@ -48,10 +59,16 @@ void HUD::draw(RenderTarget &target, RenderStates states) const {
states.shader = &m_shader;
states.vertexAttributes = VertexAttribute::Only2d;
applyTransform(states);
if (m_isDebugOverlayVisible)
target.draw(m_debugOverlay, states);
target.draw(m_blockInfoWidget, states);
target.draw(m_hotbar, states);
states.modelMatrix = nullptr;
target.draw(m_crosshair, states);
}

View File

@ -14,10 +14,7 @@
#include "Config.hpp"
#include "Hotbar.hpp"
Hotbar::Hotbar(Inventory &inventory) : m_inventory(inventory) {
setPosition(SCREEN_WIDTH / 2 - 182 * 3 / 2, SCREEN_HEIGHT - 22 * 3, 0);
setScale(GUI_SCALE, GUI_SCALE, 1);
Hotbar::Hotbar(Inventory &inventory, Widget *parent) : Widget(182, 22, parent), m_inventory(inventory) {
m_background.load("texture-widgets");
m_background.setClipRect(0, 0, 182, 22);
m_background.setPosition(0, 0, 0);

View File

@ -21,3 +21,7 @@ ItemStack::ItemStack(u16 id, u16 amount) : m_item(&Registry::getInstance().getIt
m_amount = amount;
}
void ItemStack::setItem(u16 item) {
m_item = &Registry::getInstance().getItem(item);
}

View File

@ -13,9 +13,10 @@
*/
#include "Block.hpp"
Block::Block(u32 id, u32 textureID) {
Block::Block(u32 id, u32 textureID, const std::string &name) {
m_id = id;
m_textureID = textureID;
m_name = name;
m_itemDrop = id;
m_itemDropAmount = 1;

View File

@ -20,7 +20,7 @@
#include "Registry.hpp"
#include "World.hpp"
BlockFurnace::BlockFurnace() : Block(BlockType::Furnace, 164) {
BlockFurnace::BlockFurnace() : Block(BlockType::Furnace, 164, "Furnace") {
m_canUpdate = true;
}

View File

@ -14,7 +14,7 @@
#include "BlockWater.hpp"
#include "World.hpp"
BlockWater::BlockWater() : Block(BlockType::Water, 457) {
BlockWater::BlockWater() : Block(BlockType::Water, 457, "Water") {
m_canUpdate = true;
}

View File

@ -19,7 +19,7 @@
#include "WorkbenchWidget.hpp"
#include "World.hpp"
BlockWorkbench::BlockWorkbench() : Block(BlockType::Workbench, 77) {
BlockWorkbench::BlockWorkbench() : Block(BlockType::Workbench, 77, "Workbench") {
}
bool BlockWorkbench::onBlockActivated(const glm::ivec3 &position, Player &player, World &world) const {