Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Laid groundwork for tools, and improved inventory system.

  • Loading branch information...
commit 2c81656b1655cda848b5e28e82a56d29d8c09f5c 1 parent 6cc3965
@Middlerun authored
View
48 common.lua
@@ -14,6 +14,37 @@ WOOD = 17
LEAVES = 18
UNGENERATED = 255
+-- Item codes
+WOOD_SHOVEL = 1011
+WOOD_PICKAXE = 1012
+WOOD_AXE = 1013
+WOOD_SWORD = 1014
+STONE_SHOVEL = 1021
+STONE_PICKAXE = 1022
+STONE_AXE = 1023
+STONE_SWORD = 1024
+
+-- Tool type codes
+SHOVEL = 1
+PICKAXE = 2
+AXE = 3
+SWORD = 4
+NO_TOOL = 9
+
+-- Tool material codes
+WOOD_TOOL = 1
+STONE_TOOL = 2
+COPPER_TOOL = 3
+IRON_TOOL = 4
+
+function toolInfo(item)
+ if item == nil or item <= 1000 or item >= 2000 then return 9, nil end
+ item = item - 1000
+ toolType = item % 10
+ toolMaterial = (item - toolType) / 10
+ return toolType, toolMaterial
+end
+
durability = {}
durability[STONE] = 2
durability[GRASS] = 1
@@ -23,6 +54,23 @@ durability[COAL_ORE] = 3
durability[WOOD] = 1.5
durability[LEAVES] = 0.3
+function mineSpeedMultiplier(block, tool)
+ toolType, toolMaterial = toolInfo(tool)
+ if (block == STONE or block == COBBLESTONE or block == COAL_ORE) and toolType == PICKAXE then
+ multiplier = 1.5
+ elseif (block == GRASS or block == DIRT) and toolType == SHOVEL then
+ multiplier = 2
+ elseif (block == WOOD or block == LEAVES) and toolType == AXE then
+ multiplier = 1.5
+ else
+ return 1
+ end
+ if toolMaterial == STONE_TOOL then multiplier = multiplier * 1.3 end
+ if toolMaterial == COPPER_TOOL then multiplier = multiplier * 1.5 end
+ if toolMaterial == IRON_TOOL then multiplier = multiplier * 1.6 end
+ return multiplier
+end
+
breakGive = {}
breakGive[STONE] = COBBLESTONE
breakGive[GRASS] = DIRT
View
17 gameplay.lua
@@ -38,8 +38,9 @@ function handleGameplayInput(player, terrain, dt)
if inreach then
local block = terrain:getBlock(math.ceil(cursor.y), math.ceil(cursor.x))
if love.mouse.isDown("l") and block ~= AIR and block ~= UNGENERATED then
+ tool = player.inventory:checkCurrent().id
if math.ceil(cursor.x) == mineBlock.c and math.ceil(cursor.y) == mineBlock.r then
- mineProgress = mineProgress + dt / durability[block]
+ mineProgress = mineProgress + mineSpeedMultiplier(block, tool) * dt / durability[block]
if mineProgress >= 1 or instamine then
terrain:setBlock(math.ceil(cursor.y), math.ceil(cursor.x), AIR)
mineProgress = 0
@@ -50,15 +51,15 @@ function handleGameplayInput(player, terrain, dt)
else
mineBlock.r = math.ceil(cursor.y)
mineBlock.c = math.ceil(cursor.x)
- mineProgress = dt / durability[block]
+ mineProgress = dt / mineSpeedMultiplier(block, tool) * durability[block]
end
elseif love.mouse.isDown("r") and block == AIR and placeTime > 0.2 then
local x = math.ceil(cursor.x)
local y = math.ceil(cursor.y)
if x - 1 >= player.x + player.width / 2 or x <= player.x - player.width / 2
or y - 1 >= player.y or y <= player.y - player.height then
- if player.inventory:checkSlot(4, selected.hotbar).id then
- terrain:setBlock(y, x, player.inventory:takeSlot(4, selected.hotbar).id)
+ if player.inventory:checkCurrent().id < 1000 then
+ terrain:setBlock(y, x, player.inventory:takeCurrent().id)
placeTime = 0
end
end
@@ -73,8 +74,8 @@ function handleInventoryInput(player)
local x = love.mouse.getX()
local y = love.mouse.getY()
local offsetY
- selected.r = nil
- selected.c = nil
+ player.inventory.selected.r = nil
+ player.inventory.selected.c = nil
for r = 1, 4 do
if r < 4 then
offsetY = 216 + 52 * (r - 1)
@@ -86,8 +87,8 @@ function handleInventoryInput(player)
x <= love.graphics.getWidth()/2 - inventoryGraphic:getWidth()/2 + 66 + 54 * (c - 1) and
y >= love.graphics.getHeight()/2 - inventoryGraphic:getHeight()/2 + offsetY and
y <= love.graphics.getHeight()/2 - inventoryGraphic:getHeight()/2 + offsetY + 52 then
- selected.r = r
- selected.c = c
+ player.inventory.selected.r = r
+ player.inventory.selected.c = c
end
end
end
View
BIN  gfx/items/woodShovel.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
120 inventory.lua
@@ -13,6 +13,11 @@ function Inventory:new()
end
end
o.hotbar = o.slots[4]
+ o.selected = {}
+ o.selected.hotbar = 1
+ o.selected.r = 1
+ o.selected.c = 1
+ o.pickedItem = {id = nil, count = 0}
return o
end
@@ -54,6 +59,10 @@ function Inventory:takeSlot(r, c, count)
end
end
+function Inventory:takeCurrent(count)
+ return self:takeSlot(4, self.selected.hotbar, count)
+end
+
function Inventory:setSlot(r, c, stuff)
assert (r ~= nil and c ~= nil)
assert (r >= 1 and r <= 4)
@@ -69,32 +78,52 @@ function Inventory:checkSlot(r, c)
else return {id = self.slots[r][c].id, count = self.slots[r][c].count} end
end
-function Inventory:drawHotbar(selected)
+function Inventory:checkCurrent()
+ return self:checkSlot(4, self.selected.hotbar)
+end
+
+function Inventory:selectNext()
+ self.selected.hotbar = self.selected.hotbar + 1
+ if self.selected.hotbar == 10 then self.selected.hotbar = 1 end
+end
+
+function Inventory:selectPrev()
+ self.selected.hotbar = self.selected.hotbar - 1
+ if self.selected.hotbar == 0 then self.selected.hotbar = 9 end
+end
+
+function Inventory:drawHotbar()
love.graphics.setColor(255, 255, 255, 255)
love.graphics.draw(hotbar, love.graphics.getWidth()/2 - hotbar:getWidth()/2, love.graphics.getHeight() - hotbar:getHeight())
- love.graphics.draw(highlight, love.graphics.getWidth()/2 - hotbar:getWidth()/2 + 20 + 54 * (selected - 1), love.graphics.getHeight() - hotbar:getHeight() + 8)
+ love.graphics.draw(highlight, love.graphics.getWidth()/2 - hotbar:getWidth()/2 + 20 + 54 * (self.selected.hotbar - 1), love.graphics.getHeight() - hotbar:getHeight() + 8)
for i = 1, 9 do
if self.hotbar[i].id ~= nil then
- local base = tileBase(self.hotbar[i].id)
- if base ~= nil then love.graphics.draw(images[base][1], love.graphics.getWidth()/2 - hotbar:getWidth()/2 + 39 + 54 * (i - 1), love.graphics.getHeight() - hotbar:getHeight() + 26, 0, 1, 1, images[self.hotbar[i].id][1]:getWidth()/2, images[self.hotbar[i].id][1]:getHeight()/2) end
- love.graphics.draw(images[self.hotbar[i].id][1], love.graphics.getWidth()/2 - hotbar:getWidth()/2 + 39 + 54 * (i - 1), love.graphics.getHeight() - hotbar:getHeight() + 26, 0, 1, 1, images[self.hotbar[i].id][1]:getWidth()/2, images[self.hotbar[i].id][1]:getHeight()/2)
- love.graphics.print(self.hotbar[i].count, love.graphics.getWidth()/2 - hotbar:getWidth()/2 + 50 + 54 * (i - 1), love.graphics.getHeight() - hotbar:getHeight() + 35)
+ if self.hotbar[i].id < 1000 then
+ local base = tileBase(self.hotbar[i].id)
+ if base ~= nil then love.graphics.draw(images[base][1], love.graphics.getWidth()/2 - hotbar:getWidth()/2 + 39 + 54 * (i - 1), love.graphics.getHeight() - hotbar:getHeight() + 26, 0, 1, 1, images[self.hotbar[i].id][1]:getWidth()/2, images[self.hotbar[i].id][1]:getHeight()/2) end
+ love.graphics.draw(images[self.hotbar[i].id][1], love.graphics.getWidth()/2 - hotbar:getWidth()/2 + 39 + 54 * (i - 1), love.graphics.getHeight() - hotbar:getHeight() + 26, 0, 1, 1, images[self.hotbar[i].id][1]:getWidth()/2, images[self.hotbar[i].id][1]:getHeight()/2)
+ else
+ love.graphics.draw(images[self.hotbar[i].id], love.graphics.getWidth()/2 - hotbar:getWidth()/2 + 39 + 54 * (i - 1), love.graphics.getHeight() - hotbar:getHeight() + 26, 0, 1, 1, images[self.hotbar[i].id]:getWidth()/2, images[self.hotbar[i].id]:getHeight()/2)
+ end
+ if self.hotbar[i].count > 1 then
+ love.graphics.print(self.hotbar[i].count, love.graphics.getWidth()/2 - hotbar:getWidth()/2 + 50 + 54 * (i - 1), love.graphics.getHeight() - hotbar:getHeight() + 35)
+ end
end
end
end
-function Inventory:draw(selected)
+function Inventory:draw()
love.graphics.setColor(0, 0, 0, 128)
love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
love.graphics.setColor(255, 255, 255, 255)
love.graphics.draw(inventoryGraphic, love.graphics.getWidth()/2 - inventoryGraphic:getWidth()/2, love.graphics.getHeight()/2 - inventoryGraphic:getHeight()/2)
- if selected.r == nil or selected.c == nil then
+ if self.selected.r == nil or self.selected.c == nil then
-- Do nothing
- elseif selected.r >= 1 and selected.r <= 3 then
- love.graphics.draw(highlight, love.graphics.getWidth()/2 - inventoryGraphic:getWidth()/2 + 20 + 54 * (selected.c - 1), love.graphics.getHeight()/2 - inventoryGraphic:getHeight()/2 + 224 + 52 * (selected.r - 1))
- elseif selected.r == 4 then
- love.graphics.draw(highlight, love.graphics.getWidth()/2 - inventoryGraphic:getWidth()/2 + 20 + 54 * (selected.c - 1), love.graphics.getHeight()/2 - inventoryGraphic:getHeight()/2 + 404)
+ elseif self.selected.r >= 1 and self.selected.r <= 3 then
+ love.graphics.draw(highlight, love.graphics.getWidth()/2 - inventoryGraphic:getWidth()/2 + 20 + 54 * (self.selected.c - 1), love.graphics.getHeight()/2 - inventoryGraphic:getHeight()/2 + 224 + 52 * (self.selected.r - 1))
+ elseif self.selected.r == 4 then
+ love.graphics.draw(highlight, love.graphics.getWidth()/2 - inventoryGraphic:getWidth()/2 + 20 + 54 * (self.selected.c - 1), love.graphics.getHeight()/2 - inventoryGraphic:getHeight()/2 + 404)
end
local offsetY
@@ -106,11 +135,72 @@ function Inventory:draw(selected)
if self.slots[r][c].id ~= nil then
local base = tileBase(self.slots[r][c].id)
- if base ~= nil then love.graphics.draw(images[base][1], love.graphics.getWidth()/2 - inventoryGraphic:getWidth()/2 + 39 + 54 * (c - 1), love.graphics.getHeight()/2 - inventoryGraphic:getHeight()/2 + offsetY, 0, 1, 1, images[self.slots[r][c].id][1]:getWidth()/2, images[self.slots[r][c].id][1]:getHeight()/2) end
+ if self.slots[r][c].id < 1000 then
+ if base ~= nil then love.graphics.draw(images[base][1], love.graphics.getWidth()/2 - inventoryGraphic:getWidth()/2 + 39 + 54 * (c - 1), love.graphics.getHeight()/2 - inventoryGraphic:getHeight()/2 + offsetY, 0, 1, 1, images[self.slots[r][c].id][1]:getWidth()/2, images[self.slots[r][c].id][1]:getHeight()/2) end
+ love.graphics.draw(images[self.slots[r][c].id][1], love.graphics.getWidth()/2 - inventoryGraphic:getWidth()/2 + 39 + 54 * (c - 1), love.graphics.getHeight()/2 - inventoryGraphic:getHeight()/2 + offsetY, 0, 1, 1, images[self.slots[r][c].id][1]:getWidth()/2, images[self.slots[r][c].id][1]:getHeight()/2)
- love.graphics.draw(images[self.slots[r][c].id][1], love.graphics.getWidth()/2 - inventoryGraphic:getWidth()/2 + 39 + 54 * (c - 1), love.graphics.getHeight()/2 - inventoryGraphic:getHeight()/2 + offsetY, 0, 1, 1, images[self.slots[r][c].id][1]:getWidth()/2, images[self.slots[r][c].id][1]:getHeight()/2)
+ else
+ love.graphics.draw(images[self.slots[r][c].id], love.graphics.getWidth()/2 - inventoryGraphic:getWidth()/2 + 39 + 54 * (c - 1), love.graphics.getHeight()/2 - inventoryGraphic:getHeight()/2 + offsetY, 0, 1, 1, images[self.slots[r][c].id]:getWidth()/2, images[self.slots[r][c].id]:getHeight()/2)
+ end
- love.graphics.print(self.slots[r][c].count, love.graphics.getWidth()/2 - inventoryGraphic:getWidth()/2 + 50 + 54 * (c - 1), love.graphics.getHeight()/2 - inventoryGraphic:getHeight()/2 + offsetY + 9)
+ if self.slots[r][c].count > 1 then
+ love.graphics.print(self.slots[r][c].count, love.graphics.getWidth()/2 - inventoryGraphic:getWidth()/2 + 50 + 54 * (c - 1), love.graphics.getHeight()/2 - inventoryGraphic:getHeight()/2 + offsetY + 9)
+ end
+ end
+ end
+ end
+
+ if self.pickedItem.id ~= nil then
+ love.mouse.setVisible(false)
+ local x = love.mouse.getX()
+ local y = love.mouse.getY()
+ love.graphics.setColor(255, 255, 255, 255)
+ local base = tileBase(self.pickedItem.id)
+ if self.pickedItem.id < 1000 then
+ if base ~= nil then love.graphics.draw(images[base][1], x, y, 0, 1, 1, images[self.pickedItem.id][1]:getWidth()/2, images[self.pickedItem.id][1]:getHeight()/2) end
+ love.graphics.draw(images[self.pickedItem.id][1], x, y, 0, 1, 1, images[self.pickedItem.id][1]:getWidth()/2, images[self.pickedItem.id][1]:getHeight()/2)
+ else
+ love.graphics.draw(images[self.pickedItem.id], x, y, 0, 1, 1, images[self.pickedItem.id]:getWidth()/2, images[self.pickedItem.id]:getHeight()/2)
+ end
+ if self.pickedItem.count > 1 then
+ love.graphics.print(self.pickedItem.count, x + 11, y + 9)
+ end
+ else
+ love.mouse.setVisible(true)
+ end
+end
+
+function Inventory:handleInput(button)
+ if self.selected.r ~= nil and self.selected.c ~= nil then
+ if button == "l" and player.inventory:checkSlot(self.selected.r, self.selected.c).id == self.pickedItem.id then
+ if player.inventory:checkSlot(self.selected.r, self.selected.c).count + self.pickedItem.count <= 64 then
+ player.inventory:setSlot(self.selected.r, self.selected.c, {id = self.pickedItem.id, count = player.inventory:checkSlot(self.selected.r, self.selected.c).count + self.pickedItem.count})
+ self.pickedItem = {id = nil, count = 0}
+ else
+ self.pickedItem.count = player.inventory:checkSlot(self.selected.r, self.selected.c).count + self.pickedItem.count - 64
+ player.inventory:setSlot(self.selected.r, self.selected.c, {id = self.pickedItem.id, count = 64})
+ end
+ elseif button == "l" then
+ local tempItem = player.inventory:takeSlot(self.selected.r, self.selected.c, player.inventory:checkSlot(self.selected.r, self.selected.c).count)
+ player.inventory:setSlot(self.selected.r, self.selected.c, self.pickedItem)
+ self.pickedItem = tempItem
+ elseif button == "r" and self.pickedItem.id == nil then
+ self.pickedItem = player.inventory:takeSlot(self.selected.r, self.selected.c, math.ceil(player.inventory:checkSlot(self.selected.r, self.selected.c).count/2))
+ elseif button == "r" then
+ if player.inventory:checkSlot(self.selected.r, self.selected.c).id == nil then
+ player.inventory:setSlot(self.selected.r, self.selected.c, {id = self.pickedItem.id, count = 1})
+ self.pickedItem.count = self.pickedItem.count - 1
+ if self.pickedItem.count == 0 then self.pickedItem.id = nil end
+ elseif player.inventory:checkSlot(self.selected.r, self.selected.c).id == self.pickedItem.id then
+ if player.inventory:checkSlot(self.selected.r, self.selected.c).count < 64 then
+ player.inventory:setSlot(self.selected.r, self.selected.c, {id = self.pickedItem.id, count = player.inventory:checkSlot(self.selected.r, self.selected.c).count + 1})
+ self.pickedItem.count = self.pickedItem.count - 1
+ if self.pickedItem.count == 0 then self.pickedItem.id = nil end
+ end
+ else
+ local tempItem = player.inventory:takeSlot(self.selected.r, self.selected.c, player.inventory:checkSlot(self.selected.r, self.selected.c).count)
+ player.inventory:setSlot(self.selected.r, self.selected.c, self.pickedItem)
+ self.pickedItem = tempItem
end
end
end
View
6 loadgraphics.lua
@@ -53,6 +53,12 @@ for i = 1, 16 do
images[LEAVES][i]:setFilter("linear", "nearest")
end
+
+
+images[WOOD_SHOVEL] = love.graphics.newImage("gfx/items/woodShovel.png")
+
+
+
breakImage = {}
for i = 1, 8 do
breakImage[i] = love.graphics.newImage("gfx/tiles/break" .. i .. ".png")
View
69 main.lua
@@ -19,10 +19,6 @@ cursor = {x = 0, y = 0}
cursorFade = false
cursorAlpha = 255
inreach = true
-selected = {}
-selected.hotbar = 1
-selected.r = 1
-selected.c = 1
mineBlock = {r = nil, c = nil}
mineProgress = 0
placeTime = 0
@@ -31,7 +27,6 @@ debug = false
hookRelease = false
showInventory = false
g = 40
-pickedItem = {id = nil, count = 0}
@@ -54,6 +49,8 @@ function love.load()
view.x = player.x
view.y = player.y - player.height / 2
first = true
+
+ player.inventory:give(WOOD_SHOVEL) -- Just temporary, for testing
end
@@ -125,21 +122,9 @@ function love.draw()
end
if showInventory then
- player.inventory:draw(selected)
- if pickedItem.id ~= nil then
- love.mouse.setVisible(false)
- local x = love.mouse.getX()
- local y = love.mouse.getY()
- love.graphics.setColor(255, 255, 255, 255)
- local base = tileBase(pickedItem.id)
- if base ~= nil then love.graphics.draw(images[base][1], x, y, 0, 1, 1, images[pickedItem.id][1]:getWidth()/2, images[pickedItem.id][1]:getHeight()/2) end
- love.graphics.draw(images[pickedItem.id][1], x, y, 0, 1, 1, images[pickedItem.id][1]:getWidth()/2, images[pickedItem.id][1]:getHeight()/2)
- love.graphics.print(pickedItem.count, x + 11, y + 9)
- else
- love.mouse.setVisible(true)
- end
+ player.inventory:draw()
else
- player.inventory:drawHotbar(selected.hotbar)
+ player.inventory:drawHotbar()
end
if debug then
@@ -152,7 +137,7 @@ end
function love.keypressed(k, u)
if showInventory then
- if k == "escape" and pickedItem.id == nil then
+ if k == "escape" and player.inventory.pickedItem.id == nil then
showInventory = false
end
else
@@ -173,7 +158,7 @@ function love.keypressed(k, u)
elseif k == "f3" then
debug = not debug
instamine = debug
- elseif k == "e" and pickedItem.id == nil then
+ elseif k == "e" and player.inventory.pickedItem.id == nil then
showInventory = not showInventory
elseif k == "[" then
if view.zoom > 1 then view.zoom = view.zoom / 2 end
@@ -197,48 +182,14 @@ end
function love.mousepressed(x, y, button)
if showInventory then
- if selected.r ~= nil and selected.c ~= nil then
- if button == "l" and player.inventory:checkSlot(selected.r, selected.c).id == pickedItem.id then
- if player.inventory:checkSlot(selected.r, selected.c).count + pickedItem.count <= 64 then
- player.inventory:setSlot(selected.r, selected.c, {id = pickedItem.id, count = player.inventory:checkSlot(selected.r, selected.c).count + pickedItem.count})
- pickedItem = {id = nil, count = 0}
- else
- pickedItem.count = player.inventory:checkSlot(selected.r, selected.c).count + pickedItem.count - 64
- player.inventory:setSlot(selected.r, selected.c, {id = pickedItem.id, count = 64})
- end
- elseif button == "l" then
- local tempItem = player.inventory:takeSlot(selected.r, selected.c, player.inventory:checkSlot(selected.r, selected.c).count)
- player.inventory:setSlot(selected.r, selected.c, pickedItem)
- pickedItem = tempItem
- elseif button == "r" and pickedItem.id == nil then
- pickedItem = player.inventory:takeSlot(selected.r, selected.c, math.ceil(player.inventory:checkSlot(selected.r, selected.c).count/2))
- elseif button == "r" then
- if player.inventory:checkSlot(selected.r, selected.c).id == nil then
- player.inventory:setSlot(selected.r, selected.c, {id = pickedItem.id, count = 1})
- pickedItem.count = pickedItem.count - 1
- if pickedItem.count == 0 then pickedItem.id = nil end
- elseif player.inventory:checkSlot(selected.r, selected.c).id == pickedItem.id then
- if player.inventory:checkSlot(selected.r, selected.c).count < 64 then
- player.inventory:setSlot(selected.r, selected.c, {id = pickedItem.id, count = player.inventory:checkSlot(selected.r, selected.c).count + 1})
- pickedItem.count = pickedItem.count - 1
- if pickedItem.count == 0 then pickedItem.id = nil end
- end
- else
- local tempItem = player.inventory:takeSlot(selected.r, selected.c, player.inventory:checkSlot(selected.r, selected.c).count)
- player.inventory:setSlot(selected.r, selected.c, pickedItem)
- pickedItem = tempItem
- end
- end
- end
+ player.inventory:handleInput(button)
else
if button == "m" then
player.hook:fire(player.x, player.y - player.height/2, cursor.x - player.x, cursor.y - (player.y - player.height/2))
elseif button == "wd" then
- selected.hotbar = selected.hotbar + 1
- if selected.hotbar == 10 then selected.hotbar = 1 end
+ player.inventory:selectNext()
elseif button == "wu" then
- selected.hotbar = selected.hotbar - 1
- if selected.hotbar == 0 then selected.hotbar = 9 end
+ player.inventory:selectPrev()
end
end
end
@@ -258,4 +209,4 @@ function love.quit()
--This will be printed to the console on quit
print("Thanks for playing. Please play again soon!")
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.