Add inventort scroll
This commit is contained in:
		
							parent
							
								
									e45a0f4135
								
							
						
					
					
						commit
						3a6c9ec3ad
					
				
					 1 changed files with 101 additions and 30 deletions
				
			
		
							
								
								
									
										131
									
								
								main.lua
									
										
									
									
									
								
							
							
						
						
									
										131
									
								
								main.lua
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -1,8 +1,25 @@
 | 
			
		|||
-- Imports
 | 
			
		||||
local util = require("util")
 | 
			
		||||
local window = require("window")
 | 
			
		||||
local entity = require("entity")
 | 
			
		||||
local button = require("button")
 | 
			
		||||
 | 
			
		||||
function min(a, b)
 | 
			
		||||
    if a < b then
 | 
			
		||||
        return a
 | 
			
		||||
    else
 | 
			
		||||
        return b
 | 
			
		||||
    end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function max(a, b)
 | 
			
		||||
    if a > b then
 | 
			
		||||
        return a
 | 
			
		||||
    else
 | 
			
		||||
        return b
 | 
			
		||||
    end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
-- entities that has update method
 | 
			
		||||
active_entities = {}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -38,7 +55,6 @@ screen = {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
screen.mousepressed = window.mousepressed(function (self, x, y, button)
 | 
			
		||||
    log:log(string.format("%d, %d", x, y))
 | 
			
		||||
    current_room:mousepressed(x, y, button)
 | 
			
		||||
end)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -53,11 +69,85 @@ inventory = {
 | 
			
		|||
    height = 60,
 | 
			
		||||
    selected_item = nil,
 | 
			
		||||
    items = {},
 | 
			
		||||
    item_idx = 1,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
local cells = math.floor((inventory.width - 60) / 60)
 | 
			
		||||
 | 
			
		||||
function inventory:setpos(idx, item)
 | 
			
		||||
    local x = 30 + (idx - 1) * 60 + (60 - item.width) / 2
 | 
			
		||||
    local y = (60 - item.height) / 2
 | 
			
		||||
 | 
			
		||||
    item:setpos(x, y)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function inventory:move(d)
 | 
			
		||||
    self.item_idx = self.item_idx + d
 | 
			
		||||
 | 
			
		||||
    if self.item_idx < 1 then
 | 
			
		||||
        self.item_idx = 1
 | 
			
		||||
    elseif self.item_idx + cells - 1 > #self.items then
 | 
			
		||||
        self.item_idx = max(1, #self.items - cells + 1)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    for i = 1, min(#self.items, cells) do
 | 
			
		||||
        local idx = self.item_idx + i - 1
 | 
			
		||||
        self:setpos(i, self.items[idx])
 | 
			
		||||
    end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function inventory:pickup(item)
 | 
			
		||||
    util.add_entity(self.items, item)
 | 
			
		||||
    self:move(#self.items)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function inventory:drop(item)
 | 
			
		||||
    util.del_entity(self.items, item)
 | 
			
		||||
    self:move(0)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function inventory:use()
 | 
			
		||||
    self:drop(self.selected_item)
 | 
			
		||||
    self.selected_item = nil
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local move_left = entity:from{
 | 
			
		||||
    x = 0, y = 0,
 | 
			
		||||
    width = 20,
 | 
			
		||||
    height = 60,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
move_left.draw = window.draw(function (self)
 | 
			
		||||
    love.graphics.polygon('line', 15, 5, 5, 30, 15, 55)
 | 
			
		||||
end)
 | 
			
		||||
 | 
			
		||||
move_left.mousepressed = window.mousepressed(function (self, x, y, button)
 | 
			
		||||
    inventory:move(-1)
 | 
			
		||||
end)
 | 
			
		||||
 | 
			
		||||
local move_right = entity:from{
 | 
			
		||||
    x = inventory.width - 20,
 | 
			
		||||
    y = 0,
 | 
			
		||||
    width = 20,
 | 
			
		||||
    height = 60,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
move_right.draw = window.draw(function (self)
 | 
			
		||||
    love.graphics.polygon('line', 5, 5, 15, 30, 5, 55)
 | 
			
		||||
end)
 | 
			
		||||
 | 
			
		||||
move_right.mousepressed = window.mousepressed(function (self, x, y, button)
 | 
			
		||||
    inventory:move(1)
 | 
			
		||||
end)
 | 
			
		||||
 | 
			
		||||
inventory.mousepressed = window.mousepressed(function (self, x, y, button)
 | 
			
		||||
    for i, v in ipairs(self.items) do
 | 
			
		||||
        if v:mousepressed(x, y, button) then
 | 
			
		||||
    move_left:mousepressed(x, y, button)
 | 
			
		||||
    move_right:mousepressed(x, y, button)
 | 
			
		||||
 | 
			
		||||
    for i = 1, min(cells, #self.items) do
 | 
			
		||||
        local item = self.items[self.item_idx + i - 1]
 | 
			
		||||
 | 
			
		||||
        if item:mousepressed(x, y, button) then
 | 
			
		||||
            return true
 | 
			
		||||
        end
 | 
			
		||||
    end
 | 
			
		||||
| 
						 | 
				
			
			@ -66,9 +156,15 @@ inventory.mousepressed = window.mousepressed(function (self, x, y, button)
 | 
			
		|||
end)
 | 
			
		||||
 | 
			
		||||
inventory.draw = window.draw(function (self)
 | 
			
		||||
    for i, item in pairs(self.items) do
 | 
			
		||||
    move_left:draw()
 | 
			
		||||
    move_right:draw()
 | 
			
		||||
 | 
			
		||||
    for i = 1, min(#self.items, cells) do
 | 
			
		||||
        local idx = i + self.item_idx - 1
 | 
			
		||||
        local item = self.items[idx]
 | 
			
		||||
 | 
			
		||||
        if self.selected_item and self.selected_item == item then
 | 
			
		||||
            love.graphics.circle('line', 60*i - 30, 30, 25)
 | 
			
		||||
            love.graphics.circle('line', 60*i, 30, 25)
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        item:draw()
 | 
			
		||||
| 
						 | 
				
			
			@ -77,31 +173,6 @@ inventory.draw = window.draw(function (self)
 | 
			
		|||
    window.draw_border(self.width, self.height)
 | 
			
		||||
end)
 | 
			
		||||
 | 
			
		||||
function inventory:set_pos(idx, item)
 | 
			
		||||
    local x = (idx - 1) * 60 + (60 - item.width) / 2
 | 
			
		||||
    local y = (60 - item.height) / 2
 | 
			
		||||
 | 
			
		||||
    item:setpos(x, y)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function inventory:pickup(item)
 | 
			
		||||
    util.add_entity(self.items, item)
 | 
			
		||||
    self:set_pos(item.idx[self.items], item)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function inventory:drop(item)
 | 
			
		||||
    util.del_entity(self.items, item)
 | 
			
		||||
 | 
			
		||||
    for i, v in ipairs(self.items) do
 | 
			
		||||
        self:set_pos(i, v)
 | 
			
		||||
    end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function inventory:use()
 | 
			
		||||
    self:drop(self.selected_item)
 | 
			
		||||
    self.selected_item = nil
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
log = {
 | 
			
		||||
    x = inventory.width , y = 0,
 | 
			
		||||
    width = WIDTH - screen.width,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue