Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added Inventory class to keep Player class simple.

  • Loading branch information...
commit 1843397aee8a9bf9348e9586b55a40d57db759a0 1 parent a366e72
@Middlerun authored
View
2  collision.lua
@@ -110,7 +110,7 @@ function checkCollisions(terrain, player)
if math.abs(player.x - entity.x) < player.width/2 + 0.35 and
entity.y < player.y + 0.85 and entity.y > player.y - player.height + 0.35 then
- if player:give(entity.id) then
+ if player.inventory:give(entity.id) then
remove = true
end
elseif love.timer.getTime() - entity.createTime > 300 then
View
12 gameplay.lua
@@ -57,8 +57,8 @@ function handleGameplayInput(player, terrain, dt)
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)
+ if player.inventory:checkSlot(4, selected.hotbar).id then
+ terrain:setBlock(y, x, player.inventory:takeSlot(4, selected.hotbar).id)
placeTime = 0
end
end
@@ -82,10 +82,10 @@ function handleInventoryInput(player)
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
+ if x >= love.graphics.getWidth()/2 - inventoryGraphic:getWidth()/2 + 12 + 54 * (c - 1) and
+ 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
end
View
117 inventory.lua
@@ -0,0 +1,117 @@
+Inventory = {}
+
+function Inventory:new()
+ local o = {}
+ setmetatable(o, self)
+ self.__index = self
+
+ o.slots = {}
+ for r = 1, 4 do
+ o.slots[r] = {}
+ for c = 1, 9 do
+ o.slots[r][c] = {}
+ end
+ end
+ o.hotbar = o.slots[4]
+
+ return o
+end
+
+function Inventory:give(thing)
+ if thing == nil then return false end
+
+ for r = 4, 1, -1 do
+ for c = 1, 9 do
+ if self.slots[r][c].id == thing and self.slots[r][c].count ~= nil and self.slots[r][c].count < 64 then
+ self.slots[r][c].count = self.slots[r][c].count + 1
+ return true
+ end
+ end
+ end
+ for r = 4, 1, -1 do
+ for c = 1, 9 do
+ if self.slots[r][c].id == nil then
+ self.slots[r][c].id = thing
+ self.slots[r][c].count = 1
+ return true
+ end
+ end
+ end
+ return false
+end
+
+function Inventory:takeSlot(r, c, count)
+ assert (r ~= nil and c ~= nil)
+ assert (r >= 1 and r <= 4)
+ assert (c >= 1 and c <= 9)
+ if count == nil then count = 1 end
+ if self.slots[r][c].id == nil or self.slots[r][c].count == nil or self.slots[r][c].count - count < 0 then return {id = nil, count = 0}
+ else
+ self.slots[r][c].count = self.slots[r][c].count - count
+ local id = self.slots[r][c].id
+ if self.slots[r][c].count == 0 then self.slots[r][c].id = nil end
+ return {id = id, count = count}
+ end
+end
+
+function Inventory:setSlot(r, c, stuff)
+ assert (r ~= nil and c ~= nil)
+ assert (r >= 1 and r <= 4)
+ assert (c >= 1 and c <= 9)
+ self.slots[r][c] = stuff
+end
+
+function Inventory:checkSlot(r, c)
+ assert (r ~= nil and c ~= nil)
+ assert (r >= 1 and r <= 4)
+ assert (c >= 1 and c <= 9)
+ if self.slots[r][c].id == nil or self.slots[r][c].count == nil or self.slots[r][c].count <= 0 then return {id = nil, count = 0}
+ else return {id = self.slots[r][c].id, count = self.slots[r][c].count} end
+end
+
+function Inventory:drawHotbar(selected)
+ 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 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)
+ end
+ end
+end
+
+function Inventory:draw(selected)
+ 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
+ -- 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)
+ end
+
+ local offsetY
+ for r = 1, 4 do
+ if r < 4 then offsetY = 242 + 52 * (r - 1)
+ else offsetY = 422
+ end
+ for c = 1, 9 do
+ 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
+
+ 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.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
View
2  loadgraphics.lua
@@ -65,5 +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")
+inventoryGraphic = love.graphics.newImage("gfx/inventory.png")
highlight = love.graphics.newImage("gfx/highlight.png")
View
35 main.lua
@@ -8,6 +8,7 @@ love.filesystem.load("common.lua")()
love.filesystem.load("loadgraphics.lua")()
love.filesystem.load("entity.lua")()
love.filesystem.load("gameplay.lua")()
+love.filesystem.load("inventory.lua")()
love.filesystem.load("AnAL.lua")()
love.filesystem.setIdentity("lovecraft")
@@ -124,7 +125,7 @@ function love.draw()
end
if showInventory then
- player:drawInventory(selected)
+ player.inventory:draw(selected)
if pickedItem.id ~= nil then
love.mouse.setVisible(false)
local x = love.mouse.getX()
@@ -138,7 +139,7 @@ function love.draw()
love.mouse.setVisible(true)
end
else
- player:drawHotbar(selected.hotbar)
+ player.inventory:drawHotbar(selected.hotbar)
end
if debug then
@@ -195,34 +196,34 @@ end
function love.mousepressed(x, y, button)
if showInventory then
if selected.r ~= nil and selected.c ~= nil then
- if button == "l" and player:checkSlot(selected.r, selected.c).id == pickedItem.id then
- if player:checkSlot(selected.r, selected.c).count + pickedItem.count <= 64 then
- player:setSlot(selected.r, selected.c, {id = pickedItem.id, count = player:checkSlot(selected.r, selected.c).count + pickedItem.count})
+ 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:checkSlot(selected.r, selected.c).count + pickedItem.count - 64
- player:setSlot(selected.r, selected.c, {id = pickedItem.id, count = 64})
+ 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:takeSlot(selected.r, selected.c, player:checkSlot(selected.r, selected.c).count)
- player:setSlot(selected.r, selected.c, pickedItem)
+ 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:takeSlot(selected.r, selected.c, math.ceil(player:checkSlot(selected.r, selected.c).count/2))
+ 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:checkSlot(selected.r, selected.c).id == nil then
- player:setSlot(selected.r, selected.c, {id = pickedItem.id, count = 1})
+ 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:checkSlot(selected.r, selected.c).id == pickedItem.id then
- if player:checkSlot(selected.r, selected.c).count < 64 then
- player:setSlot(selected.r, selected.c, {id = pickedItem.id, count = player:checkSlot(selected.r, selected.c).count + 1})
+ 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:takeSlot(selected.r, selected.c, player:checkSlot(selected.r, selected.c).count)
- player:setSlot(selected.r, selected.c, pickedItem)
+ 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
View
108 player.lua
@@ -33,70 +33,11 @@ function Player:new()
o.direction = 1
o.hook = GrapplingHook:new(o)
o.landTime = 0
- o.inventory = {}
- for r = 1, 4 do
- o.inventory[r] = {}
- for c = 1, 9 do
- o.inventory[r][c] = {}
- end
- end
- o.hotbar = o.inventory[4]
+ o.inventory = Inventory:new()
return o
end
-function Player:give(thing)
- if thing == nil then return false end
-
- for r = 4, 1, -1 do
- for c = 1, 9 do
- if self.inventory[r][c].id == thing and self.inventory[r][c].count ~= nil and self.inventory[r][c].count < 64 then
- self.inventory[r][c].count = self.inventory[r][c].count + 1
- return true
- end
- end
- end
- for r = 4, 1, -1 do
- for c = 1, 9 do
- if self.inventory[r][c].id == nil then
- self.inventory[r][c].id = thing
- self.inventory[r][c].count = 1
- return true
- end
- end
- end
- return false
-end
-
-function Player:takeSlot(r, c, count)
- assert (r ~= nil and c ~= nil)
- assert (r >= 1 and r <= 4)
- assert (c >= 1 and c <= 9)
- if count == nil then count = 1 end
- if self.inventory[r][c].id == nil or self.inventory[r][c].count == nil or self.inventory[r][c].count - count < 0 then return {id = nil, count = 0}
- else
- self.inventory[r][c].count = self.inventory[r][c].count - count
- local id = self.inventory[r][c].id
- if self.inventory[r][c].count == 0 then self.inventory[r][c].id = nil end
- return {id = id, count = count}
- end
-end
-
-function Player:setSlot(r, c, stuff)
- assert (r ~= nil and c ~= nil)
- assert (r >= 1 and r <= 4)
- assert (c >= 1 and c <= 9)
- self.inventory[r][c] = stuff
-end
-
-function Player:checkSlot(r, c)
- assert (r ~= nil and c ~= nil)
- assert (r >= 1 and r <= 4)
- assert (c >= 1 and c <= 9)
- if self.inventory[r][c].id == nil or self.inventory[r][c].count == nil or self.inventory[r][c].count <= 0 then return {id = nil, count = 0}
- else return {id = self.inventory[r][c].id, count = self.inventory[r][c].count} end
-end
-
function Player:update(dt)
if self.hook.hooked then
self.x, self.y = self.hook:endPoint()
@@ -158,50 +99,3 @@ function Player:draw(view)
love.graphics.draw(self.stand, (self.x-view.x)*view.zoom + love.graphics.getWidth()/2, (self.y-view.y+0.1)*view.zoom + love.graphics.getHeight()/2, 0, self.direction * view.zoom/32, view.zoom/32, 34, 103)
end
end
-
-function Player:drawHotbar(selected)
- 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
-end
-
-function Player:drawInventory(selected)
- 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(inventory, love.graphics.getWidth()/2 - inventory:getWidth()/2, love.graphics.getHeight()/2 - inventory:getHeight()/2)
- if selected.r == nil or selected.c == nil then
- -- Do nothing
- elseif selected.r >= 1 and selected.r <= 3 then
- love.graphics.draw(highlight, love.graphics.getWidth()/2 - inventory:getWidth()/2 + 20 + 54 * (selected.c - 1), love.graphics.getHeight()/2 - inventory:getHeight()/2 + 224 + 52 * (selected.r - 1))
- elseif selected.r == 4 then
- love.graphics.draw(highlight, love.graphics.getWidth()/2 - inventory:getWidth()/2 + 20 + 54 * (selected.c - 1), love.graphics.getHeight()/2 - inventory:getHeight()/2 + 404)
- end
-
- local offsetY
- for r = 1, 4 do
- if r < 4 then offsetY = 242 + 52 * (r - 1)
- else offsetY = 422
- end
- for c = 1, 9 do
- if player.inventory[r][c].id ~= nil then
- local base = tileBase(player.inventory[r][c].id)
-
- if base ~= nil then love.graphics.draw(images[base][1], love.graphics.getWidth()/2 - inventory:getWidth()/2 + 39 + 54 * (c - 1), love.graphics.getHeight()/2 - inventory:getHeight()/2 + offsetY, 0, 1, 1, images[player.inventory[r][c].id][1]:getWidth()/2, images[player.inventory[r][c].id][1]:getHeight()/2) end
-
- love.graphics.draw(images[player.inventory[r][c].id][1], love.graphics.getWidth()/2 - inventory:getWidth()/2 + 39 + 54 * (c - 1), love.graphics.getHeight()/2 - inventory:getHeight()/2 + offsetY, 0, 1, 1, images[player.inventory[r][c].id][1]:getWidth()/2, images[player.inventory[r][c].id][1]:getHeight()/2)
-
- love.graphics.print(player.inventory[r][c].count, love.graphics.getWidth()/2 - inventory:getWidth()/2 + 50 + 54 * (c - 1), love.graphics.getHeight()/2 - inventory:getHeight()/2 + offsetY + 9)
- end
- end
- end
-end
Please sign in to comment.
Something went wrong with that request. Please try again.