Skip to content

Commit

Permalink
Expanded inventory system.
Browse files Browse the repository at this point in the history
  • Loading branch information
Middlerun committed Aug 29, 2011
1 parent 0c7db15 commit 8c95a49
Show file tree
Hide file tree
Showing 12 changed files with 273 additions and 142 deletions.
17 changes: 12 additions & 5 deletions README
Expand Up @@ -8,11 +8,13 @@
A game by Middlerun
http://www.middlerun.net

A 2D Minecraft-style game.
Lövecraft is a 2D mining and building sim in the vein of Minecraft and
Terraria.

Controls:
Move left: A
Move right: D
Open inventory: E
Break block: Left mouse
Place block: Right mouse
Select block: Scroll wheel
Expand All @@ -24,15 +26,20 @@ Lengthen hook line: S

Requires LÖVE 0.7.2. See http://www.love2d.org for more info.

Lövecraft is currently in a very early stage of development. The current
version can always be found at https://github.com/Middlerun/lovecraft

Despite the name, it has nothing to do with the works of H.P. Lovecraft. I'll
rename it to something less misleading when I think of a good name.


To do:
------

- Fix grappling hook bugs
- Crafting
- Tools and items
- Inventory management system
- More block types
- Crafting
- Fix grappling hook bugs
- Music and sound effects
- Menu screen
- Save/load system
Expand All @@ -49,7 +56,7 @@ Credits, attributions and other rubbish:
Made with the LÖVE framework
http://www.love2d.org

Inspired by Minecraft and Terraria.
Inspired by Minecraft, Terraria and the Worms games.

Assets used under Creative Commons license:
Cobblestone graphics by Arachne
Expand Down
1 change: 1 addition & 0 deletions chunk.lua
Expand Up @@ -69,6 +69,7 @@ function Chunk:generate(seed, chunkR, chunkC)
end
end
end
self.perlin = {}
self.generated = true
self.changed = true
self.r = chunkR
Expand Down
94 changes: 94 additions & 0 deletions gameplay.lua
@@ -0,0 +1,94 @@
function handleGameplayInput(player, terrain, dt)
cursor.x = (love.mouse.getX() - love.graphics.getWidth() / 2) / view.zoom + view.x
cursor.y = (love.mouse.getY() - love.graphics.getHeight() / 2) / view.zoom + view.y

if not player.hook.hooked and love.keyboard.isDown(" ") and not player.falling and not hookRelease then
player.falling = true
player.vy = -15
end
if player.hook.hooked then
if love.keyboard.isDown("w") then
player.hook:shorten(dt)
elseif love.keyboard.isDown("s") then
player.hook:lengthen(dt)
end
if love.keyboard.isDown("a") then
player.hook.push = 2
player.direction = -1
elseif love.keyboard.isDown("d") then
player.hook.push = -2
player.direction = 1
else
player.hook.push = 0
end
end

if love.keyboard.isDown("w") or love.keyboard.isDown("a") or love.keyboard.isDown("s") or love.keyboard.isDown("d") then
cursorFade = true
end
if love.mouse.getX() ~= oldMouse.x or love.mouse.getY() ~= oldMouse.y or love.mouse.isDown("l") or love.mouse.isDown("r") then
cursorFade = false
cursorAlpha = 255
end
if cursorFade then cursorAlpha = math.max(0, cursorAlpha - dt * 255 / 5) end
oldMouse.x = love.mouse.getX()
oldMouse.y = love.mouse.getY()

inreach = (pythag(cursor.x, cursor.y, player.x, player.y - player.height/2) < 5)
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
if math.ceil(cursor.x) == mineBlock.c and math.ceil(cursor.y) == mineBlock.r then
mineProgress = mineProgress + dt / durability[block]
if mineProgress >= 1 or instamine then
terrain:setBlock(math.ceil(cursor.y), math.ceil(cursor.x), AIR)
mineProgress = 0
mineBlock.r = nil
mineBlock.c = nil
terrain:addEntity(breakGive[block], math.ceil(cursor.y), math.ceil(cursor.x) - 0.5 - rand:num())
end
else
mineBlock.r = math.ceil(cursor.y)
mineBlock.c = math.ceil(cursor.x)
mineProgress = dt / 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:checkSlot(4, selected.hotbar).id then
terrain:setBlock(y, x, player:takeSlot(4, selected.hotbar).id)
placeTime = 0
end
end
mineProgress = 0
else
mineProgress = 0
end
end
end

function handleInventoryInput(player)
local x = love.mouse.getX()
local y = love.mouse.getY()
local offsetY
selected.r = nil
selected.c = nil
for r = 1, 4 do
if r < 4 then
offsetY = 216 + 52 * (r - 1)
else
offsetY = 396
end
for c = 1, 9 do
if x >= love.graphics.getWidth()/2 - inventory:getWidth()/2 + 12 + 54 * (c - 1) and
x <= love.graphics.getWidth()/2 - inventory:getWidth()/2 + 66 + 54 * (c - 1) and
y >= love.graphics.getHeight()/2 - inventory:getHeight()/2 + offsetY and
y <= love.graphics.getHeight()/2 - inventory:getHeight()/2 + offsetY + 52 then
selected.r = r
selected.c = c
end
end
end
end
Binary file modified gfx/hotbar.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gfx/inventory.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified gfx/xcf/hotbar.xcf
Binary file not shown.
Binary file added gfx/xcf/inventory.xcf
Binary file not shown.
1 change: 1 addition & 0 deletions grapplinghook.lua
Expand Up @@ -188,5 +188,6 @@ end
function GrapplingHook:draw(view, x, y)
if not self.fired then return end
love.graphics.setColor(0, 0, 0, 255)
love.graphics.setLine(view.zoom/16, "smooth")
love.graphics.line((x-view.x)*view.zoom + love.graphics.getWidth()/2, (y-view.y)*view.zoom + love.graphics.getHeight()/2, (self.x-view.x)*view.zoom + love.graphics.getWidth()/2, (self.y-view.y)*view.zoom + love.graphics.getHeight()/2)
end
1 change: 1 addition & 0 deletions loadgraphics.lua
Expand Up @@ -65,4 +65,5 @@ genChunk:setFilter("linear", "nearest")
sky = love.graphics.newImage("gfx/sky.png")

hotbar = love.graphics.newImage("gfx/hotbar.png")
inventory = love.graphics.newImage("gfx/inventory.png")
highlight = love.graphics.newImage("gfx/highlight.png")
166 changes: 68 additions & 98 deletions main.lua
Expand Up @@ -7,6 +7,7 @@ love.filesystem.load("collision.lua")()
love.filesystem.load("common.lua")()
love.filesystem.load("loadgraphics.lua")()
love.filesystem.load("entity.lua")()
love.filesystem.load("gameplay.lua")()
love.filesystem.load("AnAL.lua")()
love.filesystem.setIdentity("lovecraft")

Expand All @@ -17,14 +18,19 @@ cursor = {x = 0, y = 0}
cursorFade = false
cursorAlpha = 255
inreach = true
selected = 1
selected = {}
selected.hotbar = 1
selected.r = 1
selected.c = 1
mineBlock = {r = nil, c = nil}
mineProgress = 0
placeTime = 0
instamine = false
debug = false
hookRelease = false
showInventory = false
g = 40
pickedItem = {id = nil, count = 0}



Expand Down Expand Up @@ -69,25 +75,8 @@ function love.update(dt)

checkCollisions(terrain, player)

if not player.hook.hooked and love.keyboard.isDown(" ") and not player.falling and not hookRelease then
player.falling = true
player.vy = -15
end
if player.hook.hooked then
if love.keyboard.isDown("w") then
player.hook:shorten(dt)
elseif love.keyboard.isDown("s") then
player.hook:lengthen(dt)
end
if love.keyboard.isDown("a") then
player.hook.push = 2
player.direction = -1
elseif love.keyboard.isDown("d") then
player.hook.push = -2
player.direction = 1
else
player.hook.push = 0
end
if showInventory then handleInventoryInput(player)
else handleGameplayInput(player, terrain, dt)
end

view.x = view.x + (player.x - view.x) * 0.2
Expand All @@ -100,53 +89,6 @@ function love.update(dt)
end
first = false

cursor.x = (love.mouse.getX() - love.graphics.getWidth() / 2) / view.zoom + view.x
cursor.y = (love.mouse.getY() - love.graphics.getHeight() / 2) / view.zoom + view.y
if love.keyboard.isDown("w") or love.keyboard.isDown("a") or love.keyboard.isDown("s") or love.keyboard.isDown("d") then
cursorFade = true
end
if love.mouse.getX() ~= oldMouse.x or love.mouse.getY() ~= oldMouse.y or love.mouse.isDown("l") or love.mouse.isDown("r") then
cursorFade = false
cursorAlpha = 255
end
if cursorFade then cursorAlpha = math.max(0, cursorAlpha - dt * 255 / 5) end
oldMouse.x = love.mouse.getX()
oldMouse.y = love.mouse.getY()

inreach = (pythag(cursor.x, cursor.y, player.x, player.y - player.height/2) < 5)
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
if math.ceil(cursor.x) == mineBlock.c and math.ceil(cursor.y) == mineBlock.r then
mineProgress = mineProgress + dt / durability[block]
if mineProgress >= 1 or instamine then
terrain:setBlock(math.ceil(cursor.y), math.ceil(cursor.x), AIR)
mineProgress = 0
mineBlock.r = nil
mineBlock.c = nil
terrain:addEntity(breakGive[block], math.ceil(cursor.y), math.ceil(cursor.x) - 0.5 - rand:num())
end
else
mineBlock.r = math.ceil(cursor.y)
mineBlock.c = math.ceil(cursor.x)
mineProgress = dt / 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:checkSlot(selected) then
terrain:setBlock(y, x, player:takeSlot(selected))
placeTime = 0
end
end
mineProgress = 0
else
mineProgress = 0
end
end

placeTime = placeTime + dt
player.walk:update(dt)
player.hook:update(terrain, dt)
Expand All @@ -172,24 +114,31 @@ function love.draw()
player:draw(view)

love.graphics.setColor(0, 0, 0, cursorAlpha)
if inreach then
if inreach and not showInventory then
love.graphics.setLine(view.zoom/32, "rough")
love.graphics.rectangle("line", (math.ceil(cursor.x)-1-view.x)*view.zoom + love.graphics.getWidth()/2, (math.ceil(cursor.y)-1-view.y)*view.zoom+love.graphics.getHeight()/2, view.zoom, view.zoom)
end

if mineProgress > 0 and mineProgress <= 1 then
love.graphics.draw(breakImage[math.ceil(mineProgress * 8)], (mineBlock.c-1-view.x)*view.zoom + love.graphics.getWidth()/2, (mineBlock.r-1-view.y)*view.zoom+love.graphics.getHeight()/2, 0, view.zoom/16, view.zoom/16)
end

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)
for i = 1, 9 do
if player.hotbar[i].id ~= nil then
local base = tileBase(player.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[player.hotbar[i].id][1]:getWidth()/2, images[player.hotbar[i].id][1]:getHeight()/2) end
love.graphics.draw(images[player.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[player.hotbar[i].id][1]:getWidth()/2, images[player.hotbar[i].id][1]:getHeight()/2)
love.graphics.print(player.hotbar[i].count, love.graphics.getWidth()/2 - hotbar:getWidth()/2 + 50 + 54 * (i - 1), love.graphics.getHeight() - hotbar:getHeight() + 35)
if showInventory then
player:drawInventory(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
else
player:drawHotbar(selected.hotbar)
end

if debug then
Expand All @@ -201,25 +150,36 @@ end


function love.keypressed(k, u)
if showInventory then
if k == "escape" and pickedItem.id == nil then
showInventory = false
end
else
if k == "escape" then
generator:send("command", "quit")
generator:wait()
love.event.push("q")
elseif k == " " then
if player.hook.fired then
player.hook:reset()
hookRelease = true
end
end
end

if k == "p" then
showPerlin = not showPerlin
elseif k == "escape" then
generator:send("command", "quit")
generator:wait()
love.event.push("q")
--showPerlin = not showPerlin
elseif k == "f3" then
debug = not debug
instamine = debug
elseif k == "e" and pickedItem.id == nil then
showInventory = not showInventory
elseif k == "[" then
if view.zoom > 1 then view.zoom = view.zoom / 2 end
elseif k == "]" then
if view.zoom < 256 then view.zoom = view.zoom * 2 end
elseif k == "f3" then
debug = not debug
instamine = debug
elseif k == " " then
if player.hook.fired then
player.hook:reset()
hookRelease = true
end
end

end


Expand All @@ -233,14 +193,24 @@ end


function love.mousepressed(x, y, button)
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 = selected + 1
if selected == 10 then selected = 1 end
elseif button == "wu" then
selected = selected - 1
if selected == 0 then selected = 9 end
if showInventory then
if selected.r ~= nil and selected.c ~= nil then
if button == "l" then
local tempItem = player:takeSlot(selected.r, selected.c, player:checkSlot(selected.r, selected.c).count)
player:setSlot(selected.r, selected.c, pickedItem)
pickedItem = tempItem
end
end
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
elseif button == "wu" then
selected.hotbar = selected.hotbar - 1
if selected.hotbar == 0 then selected.hotbar = 9 end
end
end
end

Expand Down

0 comments on commit 8c95a49

Please sign in to comment.