Add keypad
This commit is contained in:
		
							parent
							
								
									340ff8f519
								
							
						
					
					
						commit
						49d9ab1d8a
					
				
					 10 changed files with 156 additions and 32 deletions
				
			
		
							
								
								
									
										24
									
								
								TODO.md
									
										
									
									
									
								
							
							
						
						
									
										24
									
								
								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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										5
									
								
								conf.lua
									
										
									
									
									
								
							
							
						
						
									
										5
									
								
								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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										10
									
								
								edge.lua
									
										
									
									
									
								
							
							
						
						
									
										10
									
								
								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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										4
									
								
								main.lua
									
										
									
									
									
								
							
							
						
						
									
										4
									
								
								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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										26
									
								
								obj/keypad.lua
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								obj/keypad.lua
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										101
									
								
								room/keypad.lua
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										101
									
								
								room/keypad.lua
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue