-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.lua
More file actions
56 lines (46 loc) · 1.71 KB
/
component.lua
File metadata and controls
56 lines (46 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
-- @param ... any number of constructors, or a string or a function that returns a string
return function (x, y, ...)
local offset = { x = x, y = y }
local text, func, components = nil, nil, {}
local image
-- by default the component draws its components,
-- but leaf components will draw something else
local draw_logic = function (x, y)
for key, value in pairs(components) do
value.draw(x, y)
end
end
-- iterate over the args, until we hit a string
-- or a function
-- a component either: shows a string, or shows the result of a function
-- call, and shows all of its components
-- We call these: static, dynamic, and composite components
local initialize = function (...)
for i, arg in ipairs({...}) do
if type(arg) == "string" then
draw_logic = function (x, y)
love.graphics.print(arg, x, y)
end
return
-- later we should maybe replace func with a free standing
-- draw function, rather than a function that returns a string.
-- That way we will be able to have coin and flower pictures
elseif type(arg) == "function" then
-- close around out offsets
draw_logic = arg
return
elseif type(arg) == "table" then
table.insert(components, arg)
end
end
end
-- @params frame_x,_y the position of the parent component
local draw = function (frame_x, frame_y)
draw_logic(frame_x + offset.x, frame_y + offset.y)
end
initialize(unpack({...}))
return {
draw = draw,
offset = offset
}
end