Engine 0.0.6
Game engine in lua
Loading...
Searching...
No Matches
poly.lua
Go to the documentation of this file.
1local function decorator_poo(object, func)
2 if not object or not func then return func end
3 return function(a, b, c, d)
4 return func(object, a, b, c, d)
5 end
6end
7
8local function decorator_line(func_draw_line)
9 return function(mode, verts)
10 local index = 4
11 while index <= #verts do
12 func_draw_line(verts[index - 3], verts[index - 2], verts[index - 1], verts[index])
13 index = index + 2
14 end
15 end
16end
18local function decorator_triangle(func_draw_poly, std, func_draw_triangle)
19 if not func_draw_triangle then
20 return func_draw_poly
21 end
22
23 local point = function(x, y, px, py, scale, angle, ox, oy)
24 local xx = x + ((ox - px) * -scale * std.math.cos(angle)) - ((ox - py) * -scale * std.math.sin(angle))
25 local yy = y + ((oy - px) * -scale * std.math.sin(angle)) + ((oy - py) * -scale * std.math.cos(angle))
26 return xx, yy
27 end
28
29 return function(engine_mode, verts, x, y, scale, angle, ox, oy)
30 if #verts ~= 6 then
31 return func_draw_poly(engine_mode, verts, x, y, scale, angle, ox, oy)
32 end
33
34 ox = ox or 0
35 oy = oy or ox or 0
36 x, y = x or 0, y or 0
37
38 local x1, y1 = point(x, y, verts[1], verts[2], scale, angle, ox, oy)
39 local x2, y2 = point(x, y, verts[3], verts[4], scale, angle, ox, oy)
40 local x3, y3 = point(x, y, verts[5], verts[6], scale, angle, ox, oy)
41
42 return func_draw_triangle(engine_mode, x1, y1, x2, y2, x3, y3)
43 end
44end
45
46local function decorator_poly(func_draw_poly, std, modes, repeats)
47 return function (engine_mode, verts, x, y, scale, angle, ox, oy)
48 if #verts < 6 or #verts % 2 ~= 0 then return end
49 local mode = modes and modes[engine_mode + 1] or engine_mode
50 local rotated = std.math.cos and angle and angle ~= 0
51 ox = ox or 0
52 oy = oy or ox or 0
53
54 if repeats and repeats[engine_mode + 1] then
55 verts[#verts + 1] = verts[1]
56 verts[#verts + 1] = verts[2]
57 end
58
59 if x and y and not rotated then
60 local index = 1
61 local verts2 = {}
62 scale = scale or 1
63
64 while index <= #verts do
65 if index % 2 ~= 0 then
66 verts2[index] = x + (verts[index] * scale)
67 else
68 verts2[index] = y + (verts[index] * scale)
69 end
70 index = index + 1
71 end
72 func_draw_poly(mode, verts2)
73 elseif x and y then
74 local index = 1
75 local verts2 = {}
76 while index < #verts do
77 local px = verts[index]
78 local py = verts[index + 1]
79 local xx = x + ((ox - px) * -scale * std.math.cos(angle)) - ((ox - py) * -scale * std.math.sin(angle))
80 local yy = y + ((oy - px) * -scale * std.math.sin(angle)) + ((oy - py) * -scale * std.math.cos(angle))
81 verts2[index] = xx
82 verts2[index + 1] = yy
83 index = index + 2
84 end
85 func_draw_poly(mode, verts2)
86 else
87 func_draw_poly(mode, verts)
88 end
89 end
90end
91
92local function install(std, game, application, config)
93 local draw_line = decorator_poo(config.object, config.line)
94 local draw_poly = decorator_poo(config.object, config.poly) or decorator_line(draw_line)
95 local draw_poly2 = config.poly2 or decorator_poly(draw_poly, std, config.modes, config.repeats)
96 local draw_verts = decorator_triangle(draw_poly2, std, config.triangle)
97
98 std = std or {}
99 std.draw = std.draw or {}
100 std.draw.poly = draw_verts
101
102 return {poly=std.draw.poly}
103end
104
105local P = {
107}
108
109return P
local function decorator_poly(func_draw_poly, std, modes, repeats)
local function decorator_poo(object, func)
local function decorator_triangle(func_draw_poly, std, func_draw_triangle)
local function install(std, game, application, config)
local function decorator_line(func_draw_line)
local application
Definition main.lua:16
local game
Definition main.lua:17
local std
Definition main.lua:18