Add wrong state

This commit is contained in:
백현웅 2025-01-05 20:45:26 +09:00
parent 49d9ab1d8a
commit d2610ee690
3 changed files with 44 additions and 5 deletions

View file

@ -2,6 +2,19 @@
local util = require("util")
local window = require("window")
local button = require("button")
-- entities that has update method
active_entities = {}
function register(entity)
util.add_entity(active_entities, entity)
end
function unregister(entity)
util.del_entity(active_entities, entity)
end
--load room list
local rooms = require("rooms")
current_room = rooms['default']
@ -114,6 +127,9 @@ function love.load()
end
function love.update(dt)
for i, e in ipairs(active_entities) do
e:update(dt)
end
end
mw, mh = 0, 0
@ -130,13 +146,10 @@ function love.mousepressed(x, y, button)
end
function love.draw()
love.graphics.push()
love.graphics.translate(mw, mh)
love.graphics.scale(MULTIPLIER)
screen:draw()
inventory:draw()
log:draw()
love.graphics.pop()
end

View file

@ -27,6 +27,8 @@ function m:from(t)
kp.padx = sx
kp.pady = sy
kp.indicator_width = kw
kp.state = 'normal'
kp.timer = 0
for i = 1, 9 do
local x = sx + kd * ((i-1)%3)
@ -53,6 +55,8 @@ function m:from(t)
edge:set(m, "down", kp.room)
register(kp)
return kp
end
@ -70,14 +74,36 @@ function m:accept()
if self.number == self.code then
log:log("correct!")
else
self.state = 'wrong'
log:log("it seeems that the number does not match.")
end
end
m.update = function(self, dt)
if self.state == 'wrong' then
self.timer = self.timer + dt
if self.timer > 1 then
self.state = 'normal'
self.timer = 0
self:clear()
end
end
end
m.draw = window.draw(function (self)
love.graphics.setLineWidth(3)
-- draw indicator box
love.graphics.rectangle('line', self.padx, self.pady, self.indicator_width, self.key_size)
if self.state == 'normal' then
love.graphics.printf(tostring(self.number), self.padx + 5, self.pady + 5, self.indicator_width - 8, 'right')
elseif self.state == 'wrong' then
love.graphics.printf("wrong", self.padx + 5, self.pady + 5, self.indicator_width - 8, 'right')
else
error("keypad is not in a defined state")
end
for i, v in pairs(self.keys) do
v:draw()

View file

@ -27,7 +27,7 @@ function m.add_entity(a, x)
end
function m.del_entity(a, x)
table.remove(a, x)
table.remove(a, x.idx)
for i, v in ipairs(a) do
v.idx = i