Add basic functions
This commit is contained in:
		
							parent
							
								
									bc8308723b
								
							
						
					
					
						commit
						40fdba2588
					
				
					 9 changed files with 148 additions and 51 deletions
				
			
		
							
								
								
									
										1
									
								
								.gitignore
									
										
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
										
									
									
										vendored
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1 @@
 | 
				
			||||||
 | 
					*.love
 | 
				
			||||||
							
								
								
									
										23
									
								
								TODO.md
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								TODO.md
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,23 @@
 | 
				
			||||||
 | 
					ui system
 | 
				
			||||||
 | 
					game system
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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
 | 
				
			||||||
| 
						 | 
					@ -1,19 +0,0 @@
 | 
				
			||||||
local window = require("window")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
local w = {}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function w.new(inner, x, y, w, h)
 | 
					 | 
				
			||||||
    local mt = { __index = inner }
 | 
					 | 
				
			||||||
    local a = {}
 | 
					 | 
				
			||||||
    setmetatable(a, mt)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    function a:draw()
 | 
					 | 
				
			||||||
        inner:draw()
 | 
					 | 
				
			||||||
        love.graphics.rectangle("line", 0, 0, w, h)
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    local r = window.new(a, x, y, h, w)
 | 
					 | 
				
			||||||
    return r
 | 
					 | 
				
			||||||
end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
return w
 | 
					 | 
				
			||||||
							
								
								
									
										32
									
								
								button.lua
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								button.lua
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,32 @@
 | 
				
			||||||
 | 
					local window = require('window')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					local font = love.graphics.getFont()
 | 
				
			||||||
 | 
					local m = {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function m.new(text, callback, x, y, width, height)
 | 
				
			||||||
 | 
					    local tw = font:getWidth(text)
 | 
				
			||||||
 | 
					    local th = font:getHeight(text)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    local btn = {
 | 
				
			||||||
 | 
					        text = text,
 | 
				
			||||||
 | 
					        x = x,
 | 
				
			||||||
 | 
					        y = y,
 | 
				
			||||||
 | 
					        width = width,
 | 
				
			||||||
 | 
					        height = height,
 | 
				
			||||||
 | 
					        tx = (width - tw) / 2,
 | 
				
			||||||
 | 
					        ty = (height - th) / 2
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    btn.mousepressed = window.mousepressed(function(self, x, y, button)
 | 
				
			||||||
 | 
					        callback(x, y, button)
 | 
				
			||||||
 | 
					    end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    btn.draw = window.draw(function(self)
 | 
				
			||||||
 | 
					        love.graphics.printf(self.text, self.tx, self.ty, self.width)
 | 
				
			||||||
 | 
					        window.draw_border(self.width, self.height)
 | 
				
			||||||
 | 
					    end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return btn
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					return m
 | 
				
			||||||
							
								
								
									
										8
									
								
								conf.lua
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								conf.lua
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,8 @@
 | 
				
			||||||
 | 
					WIDTH = 320
 | 
				
			||||||
 | 
					HEIGHT = 240
 | 
				
			||||||
 | 
					MULTIPLIER = 1.5
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function love.conf(t)
 | 
				
			||||||
 | 
					    t.window.width = MULTIPLIER * WIDTH
 | 
				
			||||||
 | 
					    t.window.height = MULTIPLIER * HEIGHT
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
							
								
								
									
										5
									
								
								export.sh
									
										
									
									
									
										Executable file
									
								
							
							
						
						
									
										5
									
								
								export.sh
									
										
									
									
									
										Executable file
									
								
							| 
						 | 
					@ -0,0 +1,5 @@
 | 
				
			||||||
 | 
					#!/bin/sh
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					rm garden.love
 | 
				
			||||||
 | 
					zip garden.love *.lua
 | 
				
			||||||
 | 
					cp garden.love ~/storage/downloads/
 | 
				
			||||||
							
								
								
									
										56
									
								
								main.lua
									
										
									
									
									
								
							
							
						
						
									
										56
									
								
								main.lua
									
										
									
									
									
								
							| 
						 | 
					@ -1,20 +1,60 @@
 | 
				
			||||||
local window = require("window")
 | 
					local window = require("window")
 | 
				
			||||||
local bwindow = require("bordered_window")
 | 
					local button = require("button")
 | 
				
			||||||
--local button = require("button")
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
function love.load()
 | 
					function love.load()
 | 
				
			||||||
    b = {}
 | 
					    timer = 0
 | 
				
			||||||
    function b.draw()
 | 
					    bx, by = 0, 0
 | 
				
			||||||
        love.graphics.print("A", 10, 10)
 | 
					    cx, cy = 0, 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    main = {
 | 
				
			||||||
 | 
					        x = 50,
 | 
				
			||||||
 | 
					        y = 50,
 | 
				
			||||||
 | 
					    rx = 50, ry = 50, width = 300, height = 200
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    function main:moverect()
 | 
				
			||||||
 | 
					        self.rx = self.rx + 50
 | 
				
			||||||
 | 
					        self.ry = self.ry + 50
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ba = bwindow.new(b, 200, 200, 50, 50)
 | 
					    local m = main
 | 
				
			||||||
    -- canvas = love.graphics.newCanvas()
 | 
					    cback = function (x, y)
 | 
				
			||||||
 | 
					        cx, cy = x, y
 | 
				
			||||||
 | 
					        m:moverect()
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    btn = button.new('Move', cback, 10, 10, 50, 50)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    main.draw = window.draw(function(self)
 | 
				
			||||||
 | 
					        love.graphics.rectangle('fill', self.rx, self.ry, 50, 50)
 | 
				
			||||||
 | 
					        love.graphics.print(string.format("x %d", self.rx), 50, 100)
 | 
				
			||||||
 | 
					        btn:draw()
 | 
				
			||||||
 | 
					    end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    main.mousepressed = window.mousepressed(function(self, x, y, button)
 | 
				
			||||||
 | 
					        btn:mousepressed(x, y, button)
 | 
				
			||||||
 | 
					    end)
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function love.mousepressed(x, y, button)
 | 
				
			||||||
 | 
					    local x, y = x / MULTIPLIER, y / MULTIPLIER
 | 
				
			||||||
 | 
					    bx, by = x, y
 | 
				
			||||||
 | 
					    main:mousepressed(x, y, button)
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function love.update(dt)
 | 
					function love.update(dt)
 | 
				
			||||||
 | 
					    timer = timer + dt
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function love.draw()
 | 
					function love.draw()
 | 
				
			||||||
    ba:draw()
 | 
					    love.graphics.push()
 | 
				
			||||||
 | 
					    love.graphics.scale(MULTIPLIER)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    love.graphics.print(string.format("t: %d", timer), 0, 0)
 | 
				
			||||||
 | 
					    love.graphics.print(string.format("%d %d", bx, by), 0, 20)
 | 
				
			||||||
 | 
					    love.graphics.print(string.format("%d %d", cx, cy), 0, 40)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    main:draw()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    love.graphics.pop()
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										10
									
								
								util.lua
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								util.lua
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,10 @@
 | 
				
			||||||
 | 
					local m = {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function m.between(x, a, b)
 | 
				
			||||||
 | 
					    return a <= x and x <= b
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function m.inside(px, py, x, y, w, h)
 | 
				
			||||||
 | 
					    return m.between(px, x, x+w) and
 | 
				
			||||||
 | 
					    m.between(py, y, y+h)
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
							
								
								
									
										45
									
								
								window.lua
									
										
									
									
									
								
							
							
						
						
									
										45
									
								
								window.lua
									
										
									
									
									
								
							| 
						 | 
					@ -1,33 +1,30 @@
 | 
				
			||||||
 | 
					-- primitive of drawing  objects
 | 
				
			||||||
local w = {}
 | 
					local w = {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function w.new(inner, x, y, w, h)
 | 
					function w.draw(f)
 | 
				
			||||||
    local r = {
 | 
					    return function(self)
 | 
				
			||||||
        transform = love.math.newTransform(x, y),
 | 
					 | 
				
			||||||
        width = w,
 | 
					 | 
				
			||||||
        height = h
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    function r:transformPoint(x, y)
 | 
					 | 
				
			||||||
        local x, y = transform:transformPoint(x, y)
 | 
					 | 
				
			||||||
        if 0 <= x and x <= width and 0 <= y and y <= height then
 | 
					 | 
				
			||||||
            return x, y
 | 
					 | 
				
			||||||
        else
 | 
					 | 
				
			||||||
            return nil
 | 
					 | 
				
			||||||
        end
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    function r:mousepressed(m, x, y)
 | 
					 | 
				
			||||||
        return inner:mousepressed(m, x, y)
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    function r:draw()
 | 
					 | 
				
			||||||
        love.graphics.push()
 | 
					        love.graphics.push()
 | 
				
			||||||
        love.graphics.applyTransform(self.transform)
 | 
					        love.graphics.translate(self.x, self.y)
 | 
				
			||||||
        inner:draw()
 | 
					        f(self)
 | 
				
			||||||
        love.graphics.pop()
 | 
					        love.graphics.pop()
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return r
 | 
					function w.mousepressed(f)
 | 
				
			||||||
 | 
					    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
 | 
				
			||||||
 | 
					            f(self, x, y, button)
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function w.draw_border(width, height)
 | 
				
			||||||
 | 
					    love.graphics.push('all')
 | 
				
			||||||
 | 
					    love.graphics.setLineWidth(5)
 | 
				
			||||||
 | 
					    love.graphics.rectangle('line', 0, 0, width, height, 5)
 | 
				
			||||||
 | 
					    love.graphics.pop()
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
return w
 | 
					return w
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue