Permalink
Browse files

Added Minecraft-style inventory hotbar.

Full inventory system will come when I get around to it, but this will do for now.
  • Loading branch information...
1 parent b52a5dc commit a5a4fc8f99dcbedc57d3bd31f4785c40c126a11e @Middlerun committed Aug 20, 2011
Showing with 65 additions and 56 deletions.
  1. +11 −6 entity.lua
  2. BIN gfx/highlight.png
  3. BIN gfx/hotbar.png
  4. BIN gfx/xcf/hotbar.xcf
  5. +4 −0 loadgraphics.lua
  6. +15 −33 main.lua
  7. +35 −17 player.lua
View
@@ -6,7 +6,6 @@ function Entity:new(id, y, x)
self.__index = self
o.id = id
- assert (o.id ~= nil)
o.x = x
o.y = y
o.falling = true
@@ -20,9 +19,15 @@ function Entity:new(id, y, x)
end
function Entity:draw(view)
- love.graphics.draw(images[self.id][1],
- (self.x - view.x)*view.zoom + love.graphics.getWidth()/2,
- (self.y - view.y - 0.5 + 0.2 * math.sin(2 * love.timer.getMicroTime() + self.angle1)) * view.zoom + love.graphics.getHeight()/2,
- 4 * love.timer.getMicroTime() + self.angle2,
- view.zoom/32, view.zoom/32, view.zoom/4, view.zoom/4)
+ local base = tileBase(self.id)
+ if base ~= nil then love.graphics.draw(images[base][1],
+ (self.x - view.x)*view.zoom + love.graphics.getWidth()/2,
+ (self.y - view.y - 0.5 + 0.2 * math.sin(2 * love.timer.getMicroTime() + self.angle1)) * view.zoom + love.graphics.getHeight()/2,
+ 4 * love.timer.getMicroTime() + self.angle2,
+ view.zoom/32, view.zoom/32, images[base][1]:getWidth()/2, images[base][1]:getHeight()/2) end
+ love.graphics.draw(images[self.id][1],
+ (self.x - view.x)*view.zoom + love.graphics.getWidth()/2,
+ (self.y - view.y - 0.5 + 0.2 * math.sin(2 * love.timer.getMicroTime() + self.angle1)) * view.zoom + love.graphics.getHeight()/2,
+ 4 * love.timer.getMicroTime() + self.angle2,
+ view.zoom/32, view.zoom/32, images[self.id][1]:getWidth()/2, images[self.id][1]:getHeight()/2)
end
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Binary file not shown.
View
@@ -58,7 +58,11 @@ for i = 1, 8 do
breakImage[i] = love.graphics.newImage("gfx/tiles/break" .. i .. ".png")
breakImage[i]:setFilter("linear", "nearest")
end
+
genChunk = love.graphics.newImage("gfx/genChunk.png")
genChunk:setFilter("linear", "nearest")
sky = love.graphics.newImage("gfx/sky.png")
+
+hotbar = love.graphics.newImage("gfx/hotbar.png")
+highlight = love.graphics.newImage("gfx/highlight.png")
View
@@ -21,7 +21,6 @@ selected = 1
mineBlock = {r = nil, c = nil}
mineProgress = 0
placeTime = 0
-landTime = 0
instamine = false
debug = false
hookRelease = false
@@ -133,22 +132,12 @@ function love.update(dt)
mineProgress = dt / durability[block]
end
elseif love.mouse.isDown("r") and block == AIR and placeTime > 0.2 then
-
- -- Temporary hack, change later
- if selected == 1 then block = DIRT
- elseif selected == 2 then block = STONE
- elseif selected == 3 then block = COBBLESTONE
- elseif selected == 4 then block = COAL_ORE
- elseif selected == 5 then block = WOOD
- end
- -- end hack
-
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:take(block) then
- terrain:setBlock(y, x, block)
+ if player:checkSlot(selected) then
+ terrain:setBlock(y, x, player:takeSlot(selected))
placeTime = 0
end
end
@@ -182,9 +171,6 @@ function love.draw()
love.graphics.setColor(255, 255, 255, 255)
player:draw(view)
- -- Can't remember what this was for:
- --love.graphics.line((player.x-view.x)*view.zoom + love.graphics.getWidth()/2, (player.y-view.y-1.5)*view.zoom+love.graphics.getHeight()/2, (cursor.x-view.x)*view.zoom + love.graphics.getWidth()/2, (cursor.y-view.y)*view.zoom+love.graphics.getHeight()/2)
-
love.graphics.setColor(0, 0, 0, cursorAlpha)
if inreach then
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)
@@ -194,21 +180,17 @@ function love.draw()
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(0, 0, 0, 127)
- if selected == 1 then love.graphics.setColor(0, 0, 0, 255) end
- love.graphics.print("Dirt: " .. player:checkInventory(DIRT), 50, 50)
- love.graphics.setColor(0, 0, 0, 127)
- if selected == 2 then love.graphics.setColor(0, 0, 0, 255) end
- love.graphics.print("Stone: " .. player:checkInventory(STONE), 50, 80)
- love.graphics.setColor(0, 0, 0, 127)
- if selected == 3 then love.graphics.setColor(0, 0, 0, 255) end
- love.graphics.print("Cobblestone: " .. player:checkInventory(COBBLESTONE), 50, 110)
- love.graphics.setColor(0, 0, 0, 127)
- if selected == 4 then love.graphics.setColor(0, 0, 0, 255) end
- love.graphics.print("Coal ore: " .. player:checkInventory(COAL_ORE), 50, 140)
- love.graphics.setColor(0, 0, 0, 127)
- if selected == 5 then love.graphics.setColor(0, 0, 0, 255) end
- love.graphics.print("Wood: " .. player:checkInventory(WOOD), 50, 170)
+ 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)
+ end
+ end
if debug then
love.graphics.setColor(0, 0, 0, 255)
@@ -255,10 +237,10 @@ function love.mousepressed(x, y, button)
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 == 6 then selected = 1 end
+ if selected == 10 then selected = 1 end
elseif button == "wu" then
selected = selected - 1
- if selected == 0 then selected = 5 end
+ if selected == 0 then selected = 9 end
end
end
View
@@ -33,34 +33,52 @@ function Player:new()
o.direction = 1
o.hook = GrapplingHook:new(o)
o.landTime = 0
+ o.hotbar = {}
+ for i = 1, 9 do
+ o.hotbar[i] = {}
+ end
return o
end
-function Player:give(block)
- if block == nil then return false end
- if self.inventory[block] == nil then self.inventory[block] = 1
- else self.inventory[block] = self.inventory[block] + 1
+function Player:give(thing)
+ if thing == nil then return false end
+
+ for i = 1, 9 do
+ if self.hotbar[i].id == thing and self.hotbar[i].count ~= nil and self.hotbar[i].count < 64 then
+ self.hotbar[i].count = self.hotbar[i].count + 1
+ return true
+ end
end
- return true
+ for i = 1, 9 do
+ if self.hotbar[i].id == nil then
+ self.hotbar[i].id = thing
+ self.hotbar[i].count = 1
+ return true
+ end
+ end
+ return false
end
-function Player:take(block)
- if block == nil then return false end
- if self.inventory[block] == nil then return false end
- if self.inventory[block] > 0 then
- self.inventory[block] = self.inventory[block] - 1
- return true
+function Player:takeSlot(slot)
+ assert (slot ~= nil)
+ assert (slot >= 1)
+ assert (slot <= 9)
+ if self.hotbar[slot].id == nil or self.hotbar[slot].count == nil or self.hotbar[slot].count <= 0 then return nil
else
- return false
+ self.hotbar[slot].count = self.hotbar[slot].count - 1
+ local id = self.hotbar[slot].id
+ if self.hotbar[slot].count == 0 then self.hotbar[slot].id = nil end
+ return id
end
end
-function Player:checkInventory(block)
- if block == nil then return 0 end
- if self.inventory[block] == nil then return 0
- else return self.inventory[block]
- end
+function Player:checkSlot(slot)
+ assert (slot ~= nil)
+ assert (slot >= 1)
+ assert (slot <= 9)
+ if self.hotbar[slot].id == nil or self.hotbar[slot].count == nil or self.hotbar[slot].count <= 0 then return nil
+ else return self.hotbar[slot].id end
end
function Player:update(dt)

0 comments on commit a5a4fc8

Please sign in to comment.