From 49d9ab1d8a0995c0b157c26dc19e9bb62e5fb097 Mon Sep 17 00:00:00 2001 From: Hyeonung Baek Date: Sun, 5 Jan 2025 19:59:49 +0900 Subject: [PATCH] Add keypad --- TODO.md | 24 +---------- button.lua | 2 +- conf.lua | 5 +++ edge.lua | 10 ++++- entity.lua | 5 +-- main.lua | 4 ++ obj/keypad.lua | 26 ++++++++++++ room/doortest.lua | 3 ++ room/keypad.lua | 101 ++++++++++++++++++++++++++++++++++++++++++++++ window.lua | 8 ++-- 10 files changed, 156 insertions(+), 32 deletions(-) create mode 100644 obj/keypad.lua create mode 100644 room/keypad.lua diff --git a/TODO.md b/TODO.md index 36498e0..be7deb7 100644 --- a/TODO.md +++ b/TODO.md @@ -1,23 +1,3 @@ -ui system -game system +# Todo -seperated - -simple, minimalistic solution - -mousepressed = window.mousepressed(self, function(self,x, y, button) -end) - -draw = window.draw(self, function (self) -end) - -update - -- game - + screen - + items - + log - -- node - + x, y, width, height, - + draw, update, mousepressed +- [ ] add more rooms diff --git a/button.lua b/button.lua index be86e7d..e68d543 100644 --- a/button.lua +++ b/button.lua @@ -23,7 +23,7 @@ function m.new(text, callback, x, y, width, height) btn.draw = window.draw(function (self) love.graphics.printf(self.text, self.tx, self.ty, self.width) - window.draw_border(self.width, self.height) + love.graphics.rectangle('line', 0, 0, self.width, self.height, 5) end) return btn diff --git a/conf.lua b/conf.lua index 5f575b4..62ba675 100644 --- a/conf.lua +++ b/conf.lua @@ -2,6 +2,11 @@ WIDTH = 528 HEIGHT = 300 MULTIPLIER = 1.2 +--[[ +SCREEN_WIDTH = 320 +SCREEN_HEIGHT = 240 +--]] + function love.conf(t) --[[ t.window.width = MULTIPLIER * WIDTH diff --git a/edge.lua b/edge.lua index 8cf0772..9436599 100644 --- a/edge.lua +++ b/edge.lua @@ -41,8 +41,14 @@ local edge = entity:from{ destination = "nowhere", mousepressed = window.mousepressed(function (self) - log:log(string.format("You enter %s", self.destination)) - set_room(self.destination) + if type(self.destination) == 'string' then + log:log(string.format("You enter %s", self.destination)) + set_room(self.destination) + elseif type(self.destination) == 'table' then + set_room_raw(self.destination) + else + error("destination is not valid") + end end) } diff --git a/entity.lua b/entity.lua index 0d5c6cc..f8c9fbc 100644 --- a/entity.lua +++ b/entity.lua @@ -68,9 +68,8 @@ function m:draw() end function m:install(room, x, y) - local e = self:new() - e:setpos(x, y) - room:insert(e) + self:setpos(x, y) + room:insert(self) end return m diff --git a/main.lua b/main.lua index 6a1a464..1f43dd8 100644 --- a/main.lua +++ b/main.lua @@ -6,6 +6,10 @@ local rooms = require("rooms") current_room = rooms['default'] +function set_room_raw(r) + current_room = r +end + function set_room(r) current_room = rooms[r] end diff --git a/obj/keypad.lua b/obj/keypad.lua new file mode 100644 index 0000000..84f01f4 --- /dev/null +++ b/obj/keypad.lua @@ -0,0 +1,26 @@ +local window = require('window') +local entity = require('entity') +local keypad = require('room/keypad') + +local m = entity:from{ + width = 50, + height = 50, + lines = {'fill', {0, 0, 0, 50, 50, 50, 50, 0}}, + code = 45100, +} + +local install = m.install +function m:install(room, x, y) + self.keypad = keypad:from{ + code = self.code, + room = room + } + + install(self, room, x, y) +end + +m.mousepressed = window.mousepressed(function (self) + set_room_raw(self.keypad) +end) + +return m diff --git a/room/doortest.lua b/room/doortest.lua index 2187f2c..5cccc3f 100644 --- a/room/doortest.lua +++ b/room/doortest.lua @@ -1,5 +1,6 @@ local room = require('room') local door = require('obj/door') +local keypad = require('obj/keypad') local key = require('item/key') local r = room:new() @@ -14,4 +15,6 @@ door:from{ key:from{ code = 45100 } :install(r, 180, 160) +keypad:from{ code = 45100 }:install(r, 160, 120) + return r diff --git a/room/keypad.lua b/room/keypad.lua new file mode 100644 index 0000000..c02e6b9 --- /dev/null +++ b/room/keypad.lua @@ -0,0 +1,101 @@ +local window = require("window") +local room = require("room") +local edge = require("edge") +local button = require("button") + +local m = room:from{ + code = 45100, + key_size = 25, + key_gap = 10, +} + +local oldfrom = m.from +function m:from(t) + local kp = oldfrom(m, t) + + local ks = self.key_size + local kg = self.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 + + kp.number = 0 + kp.keys = {} + kp.padx = sx + kp.pady = sy + kp.indicator_width = kw + + for i = 1, 9 do + local x = sx + kd * ((i-1)%3) + local y = sy + kd * math.floor(1 + (i-1) / 3) + + kp.keys[i] = button.new(tostring(i), + function () kp:input(i) end, + x, y, ks, ks) + end + + local br = sy + kd*4 + + kp.keys['C'] = button.new('C', + function () kp:clear() end, + sx, br, ks, ks) + + kp.keys[0] = button.new('0', + function () kp:input(0) end, + sx+kd, br, ks, ks) + + kp.keys['A'] = button.new('A', + function () kp:accept() end, + sx+kd*2, br, ks, ks) + + edge:set(m, "down", kp.room) + + return kp +end + +function m:clear() + self.number = 0 +end + +function m:input(n) + if self.number < 100000 then + self.number = self.number * 10 + n + end +end + +function m:accept() + if self.number == self.code then + log:log("correct!") + else + log:log("it seeems that the number does not match.") + end +end + +m.draw = window.draw(function (self) + love.graphics.setLineWidth(3) + 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') + + for i, v in pairs(self.keys) do + v:draw() + end + + self.edges[1]:draw() +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 + end + end + + return self.edges[1]:mousepressed(x, y, button) +end + +return m diff --git a/window.lua b/window.lua index c5a077d..abebc2a 100644 --- a/window.lua +++ b/window.lua @@ -4,8 +4,8 @@ local util = require('util') local w = {} function w.draw(f) - return function(self) - love.graphics.push() + return function (self) + love.graphics.push('all') love.graphics.translate(self.x, self.y) f(self) love.graphics.pop() @@ -13,7 +13,7 @@ function w.draw(f) end function w.mousepressed(f) - return function(self, x, y, button) + return function (self, x, y, button) local x, y = x - self.x, y - self.y if 0 <= x and x <= self.width and 0 <= y and y <= self.height then @@ -26,7 +26,7 @@ function w.mousepressed(f) end function w.propagate(tnames) - return function(self, x, y, button) + return function (self, x, y, button) local x, y = x - self.x, y - self.y for _, tname in ipairs(tnames) do