Code improvements
This commit is contained in:
		
							parent
							
								
									d2610ee690
								
							
						
					
					
						commit
						82c0976e3c
					
				
					 5 changed files with 87 additions and 42 deletions
				
			
		
							
								
								
									
										10
									
								
								entity.lua
									
										
									
									
									
								
							
							
						
						
									
										10
									
								
								entity.lua
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -11,15 +11,13 @@ local m = {
 | 
			
		|||
 | 
			
		||||
m.__index = m
 | 
			
		||||
 | 
			
		||||
function m:new()
 | 
			
		||||
    local t = {}
 | 
			
		||||
function m:from(t)
 | 
			
		||||
    t.__index = t
 | 
			
		||||
    return setmetatable(t, self)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function m:from(t)
 | 
			
		||||
    t.__index = t
 | 
			
		||||
    return setmetatable(t, self)
 | 
			
		||||
function m:new()
 | 
			
		||||
    return self:from({})
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function m:setpos(x, y)
 | 
			
		||||
| 
						 | 
				
			
			@ -50,7 +48,7 @@ end
 | 
			
		|||
function m:draw_lines(lines)
 | 
			
		||||
    love.graphics.push('all')
 | 
			
		||||
    love.graphics.translate(self.x, self.y)
 | 
			
		||||
    love.graphics.setLineWidth(3)
 | 
			
		||||
    love.graphics.setLineWidth(2)
 | 
			
		||||
 | 
			
		||||
    if type(lines[1]) == 'string' then
 | 
			
		||||
        draw_line(lines)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										34
									
								
								main.lua
									
										
									
									
									
								
							
							
						
						
									
										34
									
								
								main.lua
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -51,7 +51,15 @@ inventory = {
 | 
			
		|||
    items = {},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inventory.mousepressed = window.propagate{'items'}
 | 
			
		||||
inventory.mousepressed = window.mousepressed(function (self, x, y, button)
 | 
			
		||||
    for i, v in ipairs(self.items) do
 | 
			
		||||
        if v:mousepressed(x, y, button) then
 | 
			
		||||
            return true
 | 
			
		||||
        end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    inventory.selected_item = nil
 | 
			
		||||
end)
 | 
			
		||||
 | 
			
		||||
inventory.draw = window.draw(function (self)
 | 
			
		||||
    for i, item in pairs(self.items) do
 | 
			
		||||
| 
						 | 
				
			
			@ -65,15 +73,31 @@ inventory.draw = window.draw(function (self)
 | 
			
		|||
    window.draw_border(self.width, self.height)
 | 
			
		||||
end)
 | 
			
		||||
 | 
			
		||||
function inventory:pickup(item)
 | 
			
		||||
    util.add_entity(self.items, item)
 | 
			
		||||
 | 
			
		||||
    local x = (#self.items - 1) * 60 + (60 - item.width) / 2
 | 
			
		||||
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, 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,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -33,6 +33,8 @@ m.mousepressed = window.mousepressed(function (self)
 | 
			
		|||
        if i and i.code then
 | 
			
		||||
            if i.code == self.code then
 | 
			
		||||
                self.locked = false
 | 
			
		||||
                inventory:use()
 | 
			
		||||
                
 | 
			
		||||
                log:log("Door opened.")
 | 
			
		||||
            else
 | 
			
		||||
                log:log("This seems not a matching key.")
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										26
									
								
								room.lua
									
										
									
									
									
								
							
							
						
						
									
										26
									
								
								room.lua
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -1,3 +1,4 @@
 | 
			
		|||
local util = require('util')
 | 
			
		||||
local window = require('window')
 | 
			
		||||
local entity = require('entity')
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -7,13 +8,12 @@ local entity = require('entity')
 | 
			
		|||
-- - edges
 | 
			
		||||
local room = entity:from{
 | 
			
		||||
    background = love.graphics.newImage("res/defaultbg.png"),
 | 
			
		||||
    edges = {},
 | 
			
		||||
    objects = {},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
local oldnew = room.new
 | 
			
		||||
function room:new()
 | 
			
		||||
    local r = oldnew(room)
 | 
			
		||||
local from = room.from
 | 
			
		||||
function room:from(t)
 | 
			
		||||
    local r = from(self, t)
 | 
			
		||||
    
 | 
			
		||||
    r.edges = {}
 | 
			
		||||
    r.objects = {}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -21,16 +21,11 @@ function room:new()
 | 
			
		|||
end
 | 
			
		||||
 | 
			
		||||
function room:insert(obj)
 | 
			
		||||
    table.insert(self.objects, obj)
 | 
			
		||||
    obj.idx = #self.objects
 | 
			
		||||
    util.add_entity(self.objects, obj)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function room:remove(obj)
 | 
			
		||||
    table.remove(self.objects, obj.idx)
 | 
			
		||||
 | 
			
		||||
    for i, v in ipairs(self.objects) do
 | 
			
		||||
        v.idx = i
 | 
			
		||||
    end
 | 
			
		||||
    util.del_entity(self.objects, obj)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
room.mousepressed = window.propagate{'edges', 'objects'}
 | 
			
		||||
| 
						 | 
				
			
			@ -47,4 +42,11 @@ function room:draw()
 | 
			
		|||
    end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function room.calc_align(width, height)
 | 
			
		||||
    local sx = (320 - width)/2
 | 
			
		||||
    local sy = (240 - height)/2
 | 
			
		||||
 | 
			
		||||
    return sx, sy
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
return room
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -11,22 +11,22 @@ local m = room:from{
 | 
			
		|||
 | 
			
		||||
local oldfrom = m.from
 | 
			
		||||
function m:from(t)
 | 
			
		||||
    local kp = oldfrom(m, t)
 | 
			
		||||
    local kp = oldfrom(self, t)
 | 
			
		||||
 | 
			
		||||
    local ks = self.key_size
 | 
			
		||||
    local kg = self.key_gap
 | 
			
		||||
    local ks = kp.key_size
 | 
			
		||||
    local kg = kp.key_gap
 | 
			
		||||
 | 
			
		||||
    local kd = ks + kg
 | 
			
		||||
    local kw = kd*3 - kg
 | 
			
		||||
    local kh = kd*5 - kg
 | 
			
		||||
    local sx = (320 - kw)/2
 | 
			
		||||
    local sy = (240 - kh)/2
 | 
			
		||||
    local sx, sy = room.calc_align(kw, kh)
 | 
			
		||||
 | 
			
		||||
    kp.number = 0
 | 
			
		||||
    kp.keys = {}
 | 
			
		||||
    kp.padx = sx
 | 
			
		||||
    kp.pady = sy
 | 
			
		||||
    kp.indicator_width = kw
 | 
			
		||||
    kp.pad_width = kw
 | 
			
		||||
    kp.pad_height = kh
 | 
			
		||||
    kp.pad_x = sx
 | 
			
		||||
    kp.pad_y = sy
 | 
			
		||||
    kp.state = 'normal'
 | 
			
		||||
    kp.timer = 0
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -47,13 +47,13 @@ function m:from(t)
 | 
			
		|||
 | 
			
		||||
    kp.keys[0] = button.new('0',
 | 
			
		||||
        function () kp:input(0) end,
 | 
			
		||||
        sx+kd, br, ks, ks)
 | 
			
		||||
        sx + kd, br, ks, ks)
 | 
			
		||||
 | 
			
		||||
    kp.keys['A'] = button.new('A',
 | 
			
		||||
        function () kp:accept() end,
 | 
			
		||||
        sx+kd*2, br, ks, ks)
 | 
			
		||||
        sx + kd*2, br, ks, ks)
 | 
			
		||||
 | 
			
		||||
    edge:set(m, "down", kp.room)
 | 
			
		||||
    edge:set(kp, "down", kp.room)
 | 
			
		||||
 | 
			
		||||
    register(kp)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -70,12 +70,21 @@ function m:input(n)
 | 
			
		|||
    end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function m:oncorrect()
 | 
			
		||||
    log:log("Correct!")
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function m:onwrong()
 | 
			
		||||
    log:log("it seeems that the number does not match.")
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function m:accept()
 | 
			
		||||
    if self.number == self.code then
 | 
			
		||||
        log:log("correct!")
 | 
			
		||||
        self.state = 'correct'
 | 
			
		||||
        self:oncorrect()
 | 
			
		||||
    else
 | 
			
		||||
        self.state = 'wrong'
 | 
			
		||||
        log:log("it seeems that the number does not match.")
 | 
			
		||||
        self:onwrong()
 | 
			
		||||
    end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -94,13 +103,21 @@ end
 | 
			
		|||
m.draw = window.draw(function (self)
 | 
			
		||||
    love.graphics.setLineWidth(3)
 | 
			
		||||
 | 
			
		||||
    -- start drawing keypad
 | 
			
		||||
 | 
			
		||||
    -- draw indicator box
 | 
			
		||||
    love.graphics.rectangle('line', self.padx, self.pady, self.indicator_width, self.key_size)
 | 
			
		||||
    love.graphics.rectangle('line', self.pad_x, self.pad_y, self.pad_width, self.key_size)
 | 
			
		||||
 | 
			
		||||
    local function display(str)
 | 
			
		||||
        love.graphics.printf(str, self.pad_x + 5, self.pad_y + 5, self.pad_width - 8, 'right')
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    if self.state == 'normal' then
 | 
			
		||||
        love.graphics.printf(tostring(self.number), self.padx + 5, self.pady + 5, self.indicator_width - 8, 'right')
 | 
			
		||||
        display(tostring(self.number))
 | 
			
		||||
    elseif self.state == 'correct' then
 | 
			
		||||
        display("correct")
 | 
			
		||||
    elseif self.state == 'wrong' then
 | 
			
		||||
        love.graphics.printf("wrong", self.padx + 5, self.pady + 5, self.indicator_width - 8, 'right')
 | 
			
		||||
        display("wrong")
 | 
			
		||||
    else
 | 
			
		||||
        error("keypad is not in a defined state")
 | 
			
		||||
    end
 | 
			
		||||
| 
						 | 
				
			
			@ -115,9 +132,11 @@ end)
 | 
			
		|||
m.mousepressed = function (self, x, y, button)
 | 
			
		||||
    local x, y = x - self.x, y - self.y
 | 
			
		||||
 | 
			
		||||
    for i, v in pairs(self.keys) do
 | 
			
		||||
        if v:mousepressed(x, y, button) then
 | 
			
		||||
            return
 | 
			
		||||
    if self.state == 'normal' then
 | 
			
		||||
        for i, v in pairs(self.keys) do
 | 
			
		||||
            if v:mousepressed(x, y, button) then
 | 
			
		||||
                return true
 | 
			
		||||
            end
 | 
			
		||||
        end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue