[Block|Item] Block hardness and item mining level/speed handled.

This commit is contained in:
Quentin Bazin 2018-06-29 21:55:47 +02:00
parent 95842ddc07
commit 618ff96277
6 changed files with 67 additions and 20 deletions

View File

@ -28,9 +28,9 @@ class BlockCursor : public IDrawable {
: m_camera(camera), m_player(player), m_world(world), m_viewMatrix(viewMatrix), m_projectionMatrix(projectionMatrix)
{ updateVertexBuffer(); }
void onEvent(const SDL_Event &event, Inventory &hotbarInventory, const Hotbar &hotbar);
void onEvent(const SDL_Event &event, const Hotbar &hotbar);
void update(Inventory &playerInventory, bool useDepthBuffer);
void update(const Hotbar &hotbar, bool useDepthBuffer);
private:
void updateVertexBuffer(int animationPos = -1);

View File

@ -35,6 +35,12 @@ class Item {
u16 burnTime() const { return m_burnTime; }
void setBurnTime(u16 burnTime) { m_burnTime = burnTime; }
u8 harvestCapability() const { return m_harvestCapability; }
void setHarvestCapability(u8 harvestCapability) { m_harvestCapability = harvestCapability; }
float miningSpeed() const { return m_miningSpeed; }
void setMiningSpeed(float miningSpeed) { m_miningSpeed = miningSpeed; }
protected:
bool m_isBlock = false;
bool m_isFuel = false;
@ -46,6 +52,9 @@ class Item {
u32 m_id = 0;
u32 m_textureID = 0;
u8 m_harvestCapability = 0;
float m_miningSpeed = 1;
};
#endif // ITEM_HPP_

View File

@ -49,6 +49,16 @@ class Block {
ItemStack getItemDrop() const { return ItemStack{m_itemDrop, m_itemDropAmount}; };
void setItemDrop(u16 itemDrop, u16 itemDropAmount = 1) { m_itemDrop = itemDrop; m_itemDropAmount = itemDropAmount; }
u8 harvestRequirements() const { return m_harvestRequirements; }
void setHarvestRequirements(u8 harvestRequirements) { m_harvestRequirements = harvestRequirements; }
float hardness() const { return m_hardness; }
void setHardness(float hardness) { m_hardness = hardness; }
float timeToBreak(u8 harvestCapability, float miningSpeed) const {
return ((m_harvestRequirements & harvestCapability) == m_harvestRequirements) ? 1.5 * m_hardness / miningSpeed : 5 * m_hardness;
}
protected:
glm::vec4 getTexCoordsFromID(int textureID) const;
@ -63,6 +73,9 @@ class Block {
u16 m_itemDrop;
u16 m_itemDropAmount;
u8 m_harvestRequirements = 0;
float m_hardness = 1.0f;
};
#endif // BLOCK_HPP_

View File

@ -23,15 +23,29 @@ Registry *Registry::s_instance = nullptr;
void Registry::registerBlocks() {
registerBlock<Block>(BlockType::Air, 0);
registerBlock<Block>(BlockType::Dirt, 37);
registerBlock<Block>(BlockType::Cobblestone, 38);
auto &cobblestoneBlock = registerBlock<Block>(BlockType::Cobblestone, 38);
cobblestoneBlock.setHarvestRequirements(1);
cobblestoneBlock.setHardness(2);
registerBlock<Block>(BlockType::Grass, 226);
registerBlock<Block>(BlockType::Leaves, 266);
registerBlock<Block>(BlockType::Wood, 277);
registerBlock<Block>(BlockType::Stone, 402).setItemDrop(ItemType::Cobblestone);
registerBlock<Block>(BlockType::Leaves, 266).setHardness(0.5);
registerBlock<Block>(BlockType::Wood, 277).setHardness(2);
auto &stoneBlock = registerBlock<Block>(BlockType::Stone, 402);
stoneBlock.setItemDrop(ItemType::Cobblestone);
stoneBlock.setHardness(1.5);
stoneBlock.setHarvestRequirements(1);
registerBlock<Block>(BlockType::Sand, 369);
registerBlock<BlockWater>();
registerBlock<Block>(BlockType::Glass, 168);
registerBlock<Block>(BlockType::CoalOre, 36).setItemDrop(ItemType::Coal);
auto &coalBlock = registerBlock<Block>(BlockType::CoalOre, 36);
coalBlock.setItemDrop(ItemType::Coal);
coalBlock.setHardness(3);
coalBlock.setHarvestRequirements(1);
registerBlock<Block>(BlockType::Planks, 316);
registerBlock<Block>(BlockType::Glowstone, 218);
registerBlock<BlockWorkbench>();
@ -58,10 +72,18 @@ void Registry::registerItems() {
registerItem<ItemBlock>(ItemType::IronOre, BlockType::IronOre, "Iron Ore");
registerItem<Item>(ItemType::Stick, 324, "Stick");
registerItem<Item>(ItemType::StoneAxe, 325, "Stone Axe");
auto &stoneAxe = registerItem<Item>(ItemType::StoneAxe, 325, "Stone Axe");
stoneAxe.setHarvestCapability(4);
stoneAxe.setMiningSpeed(4);
registerItem<Item>(ItemType::StoneHoe, 326, "Stone Hoe");
registerItem<Item>(ItemType::StonePickaxe, 327, "Stone Pickaxe");
registerItem<Item>(ItemType::StoneShovel, 328, "Stone Shovel");
auto &stonePickaxe = registerItem<Item>(ItemType::StonePickaxe, 327, "Stone Pickaxe");
stonePickaxe.setHarvestCapability(1);
stonePickaxe.setMiningSpeed(4);
registerItem<Item>(ItemType::StoneShovel, 328, "Stone Shovel").setHarvestCapability(2);
registerItem<Item>(ItemType::StoneSword, 329, "Stone Sword");
Item &itemCoal = registerItem<Item>(ItemType::Coal, 111, "Coal");

View File

@ -87,7 +87,7 @@ void BlockCursor::updateVertexBuffer(int animationPos) {
VertexBuffer::bind(nullptr);
}
void BlockCursor::onEvent(const SDL_Event &event, Inventory &hotbarInventory, const Hotbar &hotbar) {
void BlockCursor::onEvent(const SDL_Event &event, const Hotbar &hotbar) {
if (event.type == SDL_MOUSEBUTTONDOWN && m_selectedBlock.w != -1) {
if (event.button.button == SDL_BUTTON_LEFT) {
m_animationStart = GameClock::getTicks();
@ -114,8 +114,8 @@ void BlockCursor::onEvent(const SDL_Event &event, Inventory &hotbarInventory, co
m_world.setBlock(x, y, z, hotbar.currentItem());
const ItemStack &currentStack = hotbarInventory.getStack(hotbar.cursorPos(), 0);
hotbarInventory.setStack(hotbar.cursorPos(), 0, currentStack.amount() > 1 ? currentStack.item().id() : 0, currentStack.amount() - 1);
const ItemStack &currentStack = m_player.hotbarInventory().getStack(hotbar.cursorPos(), 0);
m_player.hotbarInventory().setStack(hotbar.cursorPos(), 0, currentStack.amount() > 1 ? currentStack.item().id() : 0, currentStack.amount() - 1);
}
}
}
@ -126,21 +126,24 @@ void BlockCursor::onEvent(const SDL_Event &event, Inventory &hotbarInventory, co
}
}
void BlockCursor::update(Inventory &playerInventory, bool useDepthBuffer) {
void BlockCursor::update(const Hotbar &hotbar, bool useDepthBuffer) {
glm::vec4 selectedBlock = findSelectedBlock(useDepthBuffer);
if (selectedBlock.x != m_selectedBlock.x || selectedBlock.y != m_selectedBlock.y || selectedBlock.z != m_selectedBlock.z)
m_animationStart = (m_animationStart) ? GameClock::getTicks() : 0;
m_selectedBlock = selectedBlock;
if (m_animationStart && GameClock::getTicks() > m_animationStart + 1000) {
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());
const ItemStack &currentStack = m_player.hotbarInventory().getStack(hotbar.cursorPos(), 0);
const Block &block = Registry::getInstance().getBlock(m_world.getBlock(m_selectedBlock.x, m_selectedBlock.y, m_selectedBlock.z));
float timeToBreak = block.timeToBreak(currentStack.item().harvestCapability(), currentStack.item().miningSpeed());
if (m_animationStart && GameClock::getTicks() > m_animationStart + timeToBreak * 1000) {
ItemStack itemDrop = block.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();
}
updateVertexBuffer((GameClock::getTicks() - m_animationStart) / 100);
updateVertexBuffer((GameClock::getTicks() - m_animationStart) / (timeToBreak * 100));
}
#include "ResourceHandler.hpp"

View File

@ -68,7 +68,7 @@ void GameState::onEvent(const SDL_Event &event) {
}
m_hotbar.onEvent(event);
m_blockCursor.onEvent(event, m_player.hotbarInventory(), m_hotbar);
m_blockCursor.onEvent(event, m_hotbar);
}
void GameState::update() {
@ -84,7 +84,7 @@ void GameState::update() {
// FIXME: Shouldn't be called every tick
m_hotbar.update();
m_blockCursor.update(m_player.inventory(), false);
m_blockCursor.update(m_hotbar, false);
if (Keyboard::isKeyPressedOnce(Keyboard::E) && &m_stateStack->top() == this) {
auto &inventoryState = m_stateStack->push<InventoryState>(this);