Add wrong state
This commit is contained in:
parent
49d9ab1d8a
commit
d2610ee690
3 changed files with 44 additions and 5 deletions
19
main.lua
19
main.lua
|
@ -2,6 +2,19 @@
|
||||||
local util = require("util")
|
local util = require("util")
|
||||||
local window = require("window")
|
local window = require("window")
|
||||||
local button = require("button")
|
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")
|
local rooms = require("rooms")
|
||||||
|
|
||||||
current_room = rooms['default']
|
current_room = rooms['default']
|
||||||
|
@ -114,6 +127,9 @@ function love.load()
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.update(dt)
|
function love.update(dt)
|
||||||
|
for i, e in ipairs(active_entities) do
|
||||||
|
e:update(dt)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
mw, mh = 0, 0
|
mw, mh = 0, 0
|
||||||
|
@ -130,13 +146,10 @@ function love.mousepressed(x, y, button)
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.draw()
|
function love.draw()
|
||||||
love.graphics.push()
|
|
||||||
love.graphics.translate(mw, mh)
|
love.graphics.translate(mw, mh)
|
||||||
love.graphics.scale(MULTIPLIER)
|
love.graphics.scale(MULTIPLIER)
|
||||||
|
|
||||||
screen:draw()
|
screen:draw()
|
||||||
inventory:draw()
|
inventory:draw()
|
||||||
log:draw()
|
log:draw()
|
||||||
|
|
||||||
love.graphics.pop()
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -27,6 +27,8 @@ function m:from(t)
|
||||||
kp.padx = sx
|
kp.padx = sx
|
||||||
kp.pady = sy
|
kp.pady = sy
|
||||||
kp.indicator_width = kw
|
kp.indicator_width = kw
|
||||||
|
kp.state = 'normal'
|
||||||
|
kp.timer = 0
|
||||||
|
|
||||||
for i = 1, 9 do
|
for i = 1, 9 do
|
||||||
local x = sx + kd * ((i-1)%3)
|
local x = sx + kd * ((i-1)%3)
|
||||||
|
@ -53,6 +55,8 @@ function m:from(t)
|
||||||
|
|
||||||
edge:set(m, "down", kp.room)
|
edge:set(m, "down", kp.room)
|
||||||
|
|
||||||
|
register(kp)
|
||||||
|
|
||||||
return kp
|
return kp
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -70,14 +74,36 @@ function m:accept()
|
||||||
if self.number == self.code then
|
if self.number == self.code then
|
||||||
log:log("correct!")
|
log:log("correct!")
|
||||||
else
|
else
|
||||||
|
self.state = 'wrong'
|
||||||
log:log("it seeems that the number does not match.")
|
log:log("it seeems that the number does not match.")
|
||||||
end
|
end
|
||||||
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)
|
m.draw = window.draw(function (self)
|
||||||
love.graphics.setLineWidth(3)
|
love.graphics.setLineWidth(3)
|
||||||
|
|
||||||
|
-- draw indicator box
|
||||||
love.graphics.rectangle('line', self.padx, self.pady, self.indicator_width, self.key_size)
|
love.graphics.rectangle('line', self.padx, self.pady, self.indicator_width, self.key_size)
|
||||||
love.graphics.printf(tostring(self.number), self.padx + 5, self.pady + 5, self.indicator_width - 8, 'right')
|
|
||||||
|
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
|
for i, v in pairs(self.keys) do
|
||||||
v:draw()
|
v:draw()
|
||||||
|
|
2
util.lua
2
util.lua
|
@ -27,7 +27,7 @@ function m.add_entity(a, x)
|
||||||
end
|
end
|
||||||
|
|
||||||
function m.del_entity(a, x)
|
function m.del_entity(a, x)
|
||||||
table.remove(a, x)
|
table.remove(a, x.idx)
|
||||||
|
|
||||||
for i, v in ipairs(a) do
|
for i, v in ipairs(a) do
|
||||||
v.idx = i
|
v.idx = i
|
||||||
|
|
Loading…
Add table
Reference in a new issue