Code improvements

This commit is contained in:
백현웅 2025-01-06 20:21:23 +09:00
parent d2610ee690
commit 82c0976e3c
5 changed files with 87 additions and 42 deletions

View file

@ -11,15 +11,13 @@ local m = {
m.__index = m m.__index = m
function m:new() function m:from(t)
local t = {}
t.__index = t t.__index = t
return setmetatable(t, self) return setmetatable(t, self)
end end
function m:from(t) function m:new()
t.__index = t return self:from({})
return setmetatable(t, self)
end end
function m:setpos(x, y) function m:setpos(x, y)
@ -50,7 +48,7 @@ end
function m:draw_lines(lines) function m:draw_lines(lines)
love.graphics.push('all') love.graphics.push('all')
love.graphics.translate(self.x, self.y) love.graphics.translate(self.x, self.y)
love.graphics.setLineWidth(3) love.graphics.setLineWidth(2)
if type(lines[1]) == 'string' then if type(lines[1]) == 'string' then
draw_line(lines) draw_line(lines)

View file

@ -51,7 +51,15 @@ inventory = {
items = {}, 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) inventory.draw = window.draw(function (self)
for i, item in pairs(self.items) do 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) window.draw_border(self.width, self.height)
end) end)
function inventory:pickup(item) function inventory:set_pos(idx, item)
util.add_entity(self.items, item) local x = (idx - 1) * 60 + (60 - item.width) / 2
local x = (#self.items - 1) * 60 + (60 - item.width) / 2
local y = (60 - item.height) / 2 local y = (60 - item.height) / 2
item:setpos(x, y) item:setpos(x, y)
end 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 = { log = {
x = inventory.width , y = 0, x = inventory.width , y = 0,
width = WIDTH - screen.width, width = WIDTH - screen.width,

View file

@ -33,6 +33,8 @@ m.mousepressed = window.mousepressed(function (self)
if i and i.code then if i and i.code then
if i.code == self.code then if i.code == self.code then
self.locked = false self.locked = false
inventory:use()
log:log("Door opened.") log:log("Door opened.")
else else
log:log("This seems not a matching key.") log:log("This seems not a matching key.")

View file

@ -1,3 +1,4 @@
local util = require('util')
local window = require('window') local window = require('window')
local entity = require('entity') local entity = require('entity')
@ -7,13 +8,12 @@ local entity = require('entity')
-- - edges -- - edges
local room = entity:from{ local room = entity:from{
background = love.graphics.newImage("res/defaultbg.png"), background = love.graphics.newImage("res/defaultbg.png"),
edges = {},
objects = {},
} }
local oldnew = room.new local from = room.from
function room:new() function room:from(t)
local r = oldnew(room) local r = from(self, t)
r.edges = {} r.edges = {}
r.objects = {} r.objects = {}
@ -21,16 +21,11 @@ function room:new()
end end
function room:insert(obj) function room:insert(obj)
table.insert(self.objects, obj) util.add_entity(self.objects, obj)
obj.idx = #self.objects
end end
function room:remove(obj) function room:remove(obj)
table.remove(self.objects, obj.idx) util.del_entity(self.objects, obj)
for i, v in ipairs(self.objects) do
v.idx = i
end
end end
room.mousepressed = window.propagate{'edges', 'objects'} room.mousepressed = window.propagate{'edges', 'objects'}
@ -47,4 +42,11 @@ function room:draw()
end end
end end
function room.calc_align(width, height)
local sx = (320 - width)/2
local sy = (240 - height)/2
return sx, sy
end
return room return room

View file

@ -11,22 +11,22 @@ local m = room:from{
local oldfrom = m.from local oldfrom = m.from
function m:from(t) function m:from(t)
local kp = oldfrom(m, t) local kp = oldfrom(self, t)
local ks = self.key_size local ks = kp.key_size
local kg = self.key_gap local kg = kp.key_gap
local kd = ks + kg local kd = ks + kg
local kw = kd*3 - kg local kw = kd*3 - kg
local kh = kd*5 - kg local kh = kd*5 - kg
local sx = (320 - kw)/2 local sx, sy = room.calc_align(kw, kh)
local sy = (240 - kh)/2
kp.number = 0 kp.number = 0
kp.keys = {} kp.keys = {}
kp.padx = sx kp.pad_width = kw
kp.pady = sy kp.pad_height = kh
kp.indicator_width = kw kp.pad_x = sx
kp.pad_y = sy
kp.state = 'normal' kp.state = 'normal'
kp.timer = 0 kp.timer = 0
@ -47,13 +47,13 @@ function m:from(t)
kp.keys[0] = button.new('0', kp.keys[0] = button.new('0',
function () kp:input(0) end, function () kp:input(0) end,
sx+kd, br, ks, ks) sx + kd, br, ks, ks)
kp.keys['A'] = button.new('A', kp.keys['A'] = button.new('A',
function () kp:accept() end, 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) register(kp)
@ -70,12 +70,21 @@ function m:input(n)
end end
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() function m:accept()
if self.number == self.code then if self.number == self.code then
log:log("correct!") self.state = 'correct'
self:oncorrect()
else else
self.state = 'wrong' self.state = 'wrong'
log:log("it seeems that the number does not match.") self:onwrong()
end end
end end
@ -94,13 +103,21 @@ end
m.draw = window.draw(function (self) m.draw = window.draw(function (self)
love.graphics.setLineWidth(3) love.graphics.setLineWidth(3)
-- start drawing keypad
-- draw indicator box -- 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 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 elseif self.state == 'wrong' then
love.graphics.printf("wrong", self.padx + 5, self.pady + 5, self.indicator_width - 8, 'right') display("wrong")
else else
error("keypad is not in a defined state") error("keypad is not in a defined state")
end end
@ -115,9 +132,11 @@ end)
m.mousepressed = function (self, x, y, button) m.mousepressed = function (self, x, y, button)
local x, y = x - self.x, y - self.y local x, y = x - self.x, y - self.y
for i, v in pairs(self.keys) do if self.state == 'normal' then
if v:mousepressed(x, y, button) then for i, v in pairs(self.keys) do
return if v:mousepressed(x, y, button) then
return true
end
end end
end end