From 95842ddc07c7dba812805954fd6c0c5a9e424270 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Fri, 29 Jun 2018 21:18:48 +0200 Subject: [PATCH] [Block] Item drop added. Now CoalOre drops Coal and Stone drops Cobblestone. --- include/world/Block.hpp | 7 +++++++ source/core/Registry.cpp | 4 ++-- source/hud/BlockCursor.cpp | 4 ++-- source/world/Block.cpp | 3 +++ 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/include/world/Block.hpp b/include/world/Block.hpp index 5dcdaab9..77d9b5c1 100644 --- a/include/world/Block.hpp +++ b/include/world/Block.hpp @@ -18,6 +18,7 @@ #include "BlockType.hpp" #include "IntTypes.hpp" +#include "ItemStack.hpp" class Chunk; class Player; @@ -45,6 +46,9 @@ class Block { bool canUpdate() const { return m_canUpdate; } + ItemStack getItemDrop() const { return ItemStack{m_itemDrop, m_itemDropAmount}; }; + void setItemDrop(u16 itemDrop, u16 itemDropAmount = 1) { m_itemDrop = itemDrop; m_itemDropAmount = itemDropAmount; } + protected: glm::vec4 getTexCoordsFromID(int textureID) const; @@ -56,6 +60,9 @@ class Block { bool m_isSelected = false; s8 m_selectedFace = -1; + + u16 m_itemDrop; + u16 m_itemDropAmount; }; #endif // BLOCK_HPP_ diff --git a/source/core/Registry.cpp b/source/core/Registry.cpp index 4d8ac0d7..5422c310 100644 --- a/source/core/Registry.cpp +++ b/source/core/Registry.cpp @@ -27,11 +27,11 @@ void Registry::registerBlocks() { registerBlock(BlockType::Grass, 226); registerBlock(BlockType::Leaves, 266); registerBlock(BlockType::Wood, 277); - registerBlock(BlockType::Stone, 402); + registerBlock(BlockType::Stone, 402).setItemDrop(ItemType::Cobblestone); registerBlock(BlockType::Sand, 369); registerBlock(); registerBlock(BlockType::Glass, 168); - registerBlock(BlockType::CoalOre, 36); + registerBlock(BlockType::CoalOre, 36).setItemDrop(ItemType::Coal); registerBlock(BlockType::Planks, 316); registerBlock(BlockType::Glowstone, 218); registerBlock(); diff --git a/source/hud/BlockCursor.cpp b/source/hud/BlockCursor.cpp index a5f5f085..86046f80 100644 --- a/source/hud/BlockCursor.cpp +++ b/source/hud/BlockCursor.cpp @@ -134,8 +134,8 @@ void BlockCursor::update(Inventory &playerInventory, bool useDepthBuffer) { m_selectedBlock = selectedBlock; if (m_animationStart && GameClock::getTicks() > m_animationStart + 1000) { - u16 block = m_world.getBlock(m_selectedBlock.x, m_selectedBlock.y, m_selectedBlock.z); - playerInventory.addStack(block); + ItemStack itemDrop = Registry::getInstance().getBlock(m_world.getBlock(m_selectedBlock.x, m_selectedBlock.y, m_selectedBlock.z)).getItemDrop(); + playerInventory.addStack(itemDrop.item().id(), itemDrop.amount()); m_world.setBlock(m_selectedBlock.x, m_selectedBlock.y, m_selectedBlock.z, 0); m_animationStart = GameClock::getTicks(); } diff --git a/source/world/Block.cpp b/source/world/Block.cpp index a4542e25..55ec06e7 100644 --- a/source/world/Block.cpp +++ b/source/world/Block.cpp @@ -16,6 +16,9 @@ Block::Block(u32 id, u32 textureID) { m_id = id; m_textureID = textureID; + + m_itemDrop = id; + m_itemDropAmount = 1; } glm::vec4 Block::getTexCoords(int face, u16) const {