A set of updates
- add object door - refactor log - etc
This commit is contained in:
		
							parent
							
								
									c95593af3e
								
							
						
					
					
						commit
						340ff8f519
					
				
					 12 changed files with 158 additions and 39 deletions
				
			
		
							
								
								
									
										18
									
								
								entity.lua
									
										
									
									
									
								
							
							
						
						
									
										18
									
								
								entity.lua
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -47,15 +47,15 @@ local function draw_line(l)
 | 
			
		|||
    end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function m:draw()
 | 
			
		||||
function m:draw_lines(lines)
 | 
			
		||||
    love.graphics.push('all')
 | 
			
		||||
    love.graphics.translate(self.x, self.y)
 | 
			
		||||
    love.graphics.setLineWidth(3)
 | 
			
		||||
 | 
			
		||||
    if type(self.lines[1]) == 'string' then
 | 
			
		||||
        draw_line(self.lines)
 | 
			
		||||
    if type(lines[1]) == 'string' then
 | 
			
		||||
        draw_line(lines)
 | 
			
		||||
    else
 | 
			
		||||
        for _, l in ipairs(self.lines) do
 | 
			
		||||
        for _, l in ipairs(lines) do
 | 
			
		||||
            draw_line(l)
 | 
			
		||||
        end
 | 
			
		||||
    end
 | 
			
		||||
| 
						 | 
				
			
			@ -63,4 +63,14 @@ function m:draw()
 | 
			
		|||
    love.graphics.pop()
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function m:draw()
 | 
			
		||||
    self:draw_lines(self.lines)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function m:install(room, x, y)
 | 
			
		||||
    local e = self:new()
 | 
			
		||||
    e:setpos(x, y)
 | 
			
		||||
    room:insert(e)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
return m
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
#!/bin/sh
 | 
			
		||||
 | 
			
		||||
rm garden.love
 | 
			
		||||
zip -r garden.love res/ room/ item/ *.lua
 | 
			
		||||
zip -r garden.love res/ room/ obj/ item/ *.lua
 | 
			
		||||
cp garden.love ~/storage/downloads/
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										2
									
								
								item.lua
									
										
									
									
									
								
							
							
						
						
									
										2
									
								
								item.lua
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -26,6 +26,8 @@ end
 | 
			
		|||
function m:itemMode()
 | 
			
		||||
    self.mousepressed = window.mousepressed(function (self, x, y, button)
 | 
			
		||||
        log:info(self)
 | 
			
		||||
        inventory.selected_item = self
 | 
			
		||||
        log:format("selected idx : %s", inventory.selected_item.idx)
 | 
			
		||||
    end)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										20
									
								
								item/key.lua
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								item/key.lua
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,20 @@
 | 
			
		|||
local window = require('window')
 | 
			
		||||
local item = require('item')
 | 
			
		||||
 | 
			
		||||
-- A key.
 | 
			
		||||
local k = item:from{
 | 
			
		||||
    width = 20,
 | 
			
		||||
    height = 20,
 | 
			
		||||
    lines = {
 | 
			
		||||
        {'fill', {0, 0, 0, 5, 5, 5, 5, 0}},
 | 
			
		||||
        {'line', {5, 5, 20, 20}},
 | 
			
		||||
        {'line', {10, 10, 5, 15}},
 | 
			
		||||
        {'line', {15, 15, 10, 20}},
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
    name = "key",
 | 
			
		||||
    description = "A key for locked containers with varying volume",
 | 
			
		||||
    code = 0,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
return k
 | 
			
		||||
| 
						 | 
				
			
			@ -1,5 +0,0 @@
 | 
			
		|||
local m = {
 | 
			
		||||
    ['crowbar'] = require('item/crowbar')
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
return m
 | 
			
		||||
							
								
								
									
										55
									
								
								main.lua
									
										
									
									
									
								
							
							
						
						
									
										55
									
								
								main.lua
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -1,4 +1,5 @@
 | 
			
		|||
-- Imports
 | 
			
		||||
local util = require("util")
 | 
			
		||||
local window = require("window")
 | 
			
		||||
local button = require("button")
 | 
			
		||||
local rooms = require("rooms")
 | 
			
		||||
| 
						 | 
				
			
			@ -29,23 +30,31 @@ inventory = {
 | 
			
		|||
    x = 0, y = 0,
 | 
			
		||||
    width = screen.width,
 | 
			
		||||
    height = 60,
 | 
			
		||||
    items = {}
 | 
			
		||||
    selected_item = nil,
 | 
			
		||||
    items = {},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inventory.mousepressed = window.propagate{'items'}
 | 
			
		||||
 | 
			
		||||
inventory.draw = window.draw(function (self)
 | 
			
		||||
    local x = 0
 | 
			
		||||
    for _, item in pairs(self.items) do
 | 
			
		||||
        item:setpos(x, 0)
 | 
			
		||||
    for i, item in pairs(self.items) do
 | 
			
		||||
        if self.selected_item and self.selected_item.idx == i then
 | 
			
		||||
            love.graphics.circle('line', 60*i - 30, 30, 25)
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        item:draw()
 | 
			
		||||
        x = item.width
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    window.draw_border(self.width, self.height)
 | 
			
		||||
end)
 | 
			
		||||
 | 
			
		||||
function inventory:pickup(item)
 | 
			
		||||
    table.insert(self.items, item)
 | 
			
		||||
    util.add_entity(self.items, item)
 | 
			
		||||
 | 
			
		||||
    local x = (#self.items - 1) * 60 + (60 - item.width) / 2
 | 
			
		||||
    local y = (60 - item.height) / 2
 | 
			
		||||
 | 
			
		||||
    item:setpos(x, y)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
log = {
 | 
			
		||||
| 
						 | 
				
			
			@ -55,44 +64,39 @@ log = {
 | 
			
		|||
    lines = {},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
log.limit = log.width - 10
 | 
			
		||||
 | 
			
		||||
local font = love.graphics.getFont()
 | 
			
		||||
local font_height = font:getHeight('L')
 | 
			
		||||
local line_limit = log.height / font_height
 | 
			
		||||
 | 
			
		||||
log.limit = log.width - 10
 | 
			
		||||
 | 
			
		||||
log.draw = window.draw(function (self)
 | 
			
		||||
    local i = 0
 | 
			
		||||
    for _, l in ipairs(self.lines) do
 | 
			
		||||
        local rw, wl = font:getWrap(l, self.limit)
 | 
			
		||||
    local h = 0
 | 
			
		||||
    for i, l in ipairs(self.lines) do
 | 
			
		||||
        local th = font:getHeight(l)
 | 
			
		||||
        love.graphics.printf(l, 5, 5 + i*th, self.limit)
 | 
			
		||||
        i = i + #wl
 | 
			
		||||
        love.graphics.printf(l, 5, 5 + h, self.limit)
 | 
			
		||||
        h = h + th
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    window.draw_border(self.width, self.height)
 | 
			
		||||
end)
 | 
			
		||||
 | 
			
		||||
function log:log(str)
 | 
			
		||||
    table.insert(self.lines, str)
 | 
			
		||||
    local rw, wl = font:getWrap(str, self.limit)
 | 
			
		||||
 | 
			
		||||
    local i = 0
 | 
			
		||||
    for _, l in ipairs(self.lines) do
 | 
			
		||||
        local rw, wl = font:getWrap(l, self.limit)
 | 
			
		||||
        local th = font:getHeight(l)
 | 
			
		||||
        i = i + #wl
 | 
			
		||||
    for _, l in ipairs(wl) do
 | 
			
		||||
        table.insert(self.lines, l)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    while i > line_limit do
 | 
			
		||||
        local l = self.lines[1]
 | 
			
		||||
        local rw, wl = font:getWrap(l, self.limit)
 | 
			
		||||
        local th = font:getHeight(l)
 | 
			
		||||
 | 
			
		||||
        i = i - #wl
 | 
			
		||||
    while #self.lines > line_limit do
 | 
			
		||||
        table.remove(self.lines, 1)
 | 
			
		||||
    end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function log:format(...)
 | 
			
		||||
    log:log(string.format(...))
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function log:info(o)
 | 
			
		||||
    if o._t == 'item' then
 | 
			
		||||
        self:log(string.format("It's %s. %s", o.name, o.description))
 | 
			
		||||
| 
						 | 
				
			
			@ -116,6 +120,7 @@ end
 | 
			
		|||
 | 
			
		||||
function love.mousepressed(x, y, button)
 | 
			
		||||
    local x, y = (x - mw) / MULTIPLIER, (y - mh) / MULTIPLIER
 | 
			
		||||
 | 
			
		||||
    screen:mousepressed(x, y, button)
 | 
			
		||||
    inventory:mousepressed(x, y, button)
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										50
									
								
								obj/door.lua
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								obj/door.lua
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,50 @@
 | 
			
		|||
local window = require('window')
 | 
			
		||||
local entity = require('entity')
 | 
			
		||||
 | 
			
		||||
local locked_door = {
 | 
			
		||||
    {'fill', {0, 0, 0, 120, 60, 120, 60, 0}},
 | 
			
		||||
    {'fill', {10, 10, 50, 10, 50, 50, 10, 50}},
 | 
			
		||||
    {'line', {30, 10, 30, 50}},
 | 
			
		||||
    {'line', {10, 30, 50, 30}},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
local open_door = {
 | 
			
		||||
    {'fill', {0, 0, 0, 120, 60, 120, 60, 0}},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
local m = entity:from{
 | 
			
		||||
    width = 60,
 | 
			
		||||
    height = 120,
 | 
			
		||||
    locked = true,
 | 
			
		||||
    code = 0,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function m:draw()
 | 
			
		||||
    if self.locked then
 | 
			
		||||
        self:draw_lines(locked_door)
 | 
			
		||||
    else
 | 
			
		||||
        self:draw_lines(open_door)
 | 
			
		||||
    end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
m.mousepressed = window.mousepressed(function (self)
 | 
			
		||||
    if self.locked then
 | 
			
		||||
        local i = inventory.selected_item
 | 
			
		||||
        if i and i.code then
 | 
			
		||||
            if i.code == self.code then
 | 
			
		||||
                self.locked = false
 | 
			
		||||
                log:log("Door opened.")
 | 
			
		||||
            else
 | 
			
		||||
                log:log("This seems not a matching key.")
 | 
			
		||||
                inventory.selected_item = nil
 | 
			
		||||
            end
 | 
			
		||||
        else
 | 
			
		||||
            log:log("It's locked.")
 | 
			
		||||
        end
 | 
			
		||||
    else
 | 
			
		||||
        log:format("You enter %s", self.destination)
 | 
			
		||||
        set_room(self.destination)
 | 
			
		||||
    end
 | 
			
		||||
end)
 | 
			
		||||
 | 
			
		||||
return m
 | 
			
		||||
							
								
								
									
										10
									
								
								room.lua
									
										
									
									
									
								
							
							
						
						
									
										10
									
								
								room.lua
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -6,12 +6,20 @@ local entity = require('entity')
 | 
			
		|||
-- - objects
 | 
			
		||||
-- - edges
 | 
			
		||||
local room = entity:from{
 | 
			
		||||
    x = 0, y = 0,
 | 
			
		||||
    background = love.graphics.newImage("res/defaultbg.png"),
 | 
			
		||||
    edges = {},
 | 
			
		||||
    objects = {},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
local oldnew = room.new
 | 
			
		||||
function room:new()
 | 
			
		||||
    local r = oldnew(room)
 | 
			
		||||
    r.edges = {}
 | 
			
		||||
    r.objects = {}
 | 
			
		||||
 | 
			
		||||
    return r
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function room:insert(obj)
 | 
			
		||||
    table.insert(self.objects, obj)
 | 
			
		||||
    obj.idx = #self.objects
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4,12 +4,10 @@ local crowbar = require('item/crowbar')
 | 
			
		|||
 | 
			
		||||
local r = room:new()
 | 
			
		||||
 | 
			
		||||
r.background = love.graphics.newImage("res/defaultbg.png")
 | 
			
		||||
 | 
			
		||||
crowbar:install(r, 100, 160)
 | 
			
		||||
edge:set(r, 'up', "default")
 | 
			
		||||
edge:set(r, 'down', "default")
 | 
			
		||||
edge:set(r, 'left', "default")
 | 
			
		||||
edge:set(r, 'right', "default")
 | 
			
		||||
edge:set(r, 'right', "doortest")
 | 
			
		||||
 | 
			
		||||
return r
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										17
									
								
								room/doortest.lua
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								room/doortest.lua
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,17 @@
 | 
			
		|||
local room = require('room')
 | 
			
		||||
local door = require('obj/door')
 | 
			
		||||
local key = require('item/key')
 | 
			
		||||
 | 
			
		||||
local r = room:new()
 | 
			
		||||
 | 
			
		||||
door:from{
 | 
			
		||||
    destination = "default",
 | 
			
		||||
    locked = true,
 | 
			
		||||
    code = 45100,
 | 
			
		||||
}
 | 
			
		||||
    :install(r, 40, 80)
 | 
			
		||||
 | 
			
		||||
key:from{ code = 45100 }
 | 
			
		||||
    :install(r, 180, 160)
 | 
			
		||||
 | 
			
		||||
return r
 | 
			
		||||
| 
						 | 
				
			
			@ -1,5 +1,6 @@
 | 
			
		|||
local m = {
 | 
			
		||||
    ['default'] = require('room/default'),
 | 
			
		||||
    ['doortest'] = require('room/doortest'),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
return m
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										13
									
								
								util.lua
									
										
									
									
									
								
							
							
						
						
									
										13
									
								
								util.lua
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -21,4 +21,17 @@ function m.inside(px, py, x, y, w, h)
 | 
			
		|||
    m.between(py, y, y+h)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function m.add_entity(a, x)
 | 
			
		||||
    table.insert(a, x)
 | 
			
		||||
    x.idx = #a
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function m.del_entity(a, x)
 | 
			
		||||
    table.remove(a, x)
 | 
			
		||||
 | 
			
		||||
    for i, v in ipairs(a) do
 | 
			
		||||
        v.idx = i
 | 
			
		||||
    end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
return m
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue