-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.lua
161 lines (139 loc) · 2.54 KB
/
util.lua
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
local M = { }
local lg = love.graphics
local fonts = { }
function M.color(cr, cg, cb, ca)
ca = ca or 255
return { r = cr, g = cg, b = cb, a = ca }
end
function M.split(s, sep)
local sep, fields = sep or ":", {}
local pattern = string.format("([^%s]+)", sep)
s:gsub(pattern, function(c) fields[#fields+1] = c end)
return fields
end
function M.get_font(size)
if fonts[size] ~= nil then
return fonts[size]
else
local f = lg.newFont(size, "Tuffy.ttf") -- http://tulrich.com/fonts/ Thatcher Ulrich
fonts[size] = f
return f
end
end
function M.remove_object(t, o)
for i,v in ipairs(t) do
if v == o then
table.remove(t, i)
end
end
end
function M.contains(t, o)
if t[o] ~= nil then
return true
end
for k,v in pairs(t) do
if v == o then
return true
end
end
return false;
end
function M.scramble(t)
local t2 = {}
local pos
while #t > 0 do
pos = math.random(1,#t)
table.insert(t2, t[pos]);
table.remove(t, pos)
end
return t2
end
function M.distance_to(x1, y1, x2, y2)
local dist = math.sqrt( math.pow(x2 - x1, 2) + math.pow(y2 - y1, 2) )
return dist
end
function M.point_within(px, py, ax, ay, w, h)
if px >= ax and px <= ax+w and py >= ay and py <= ay+h then
return true
else
return false
end
end
function M.indent(s, ind)
local ret = ""
for i=0,ind do
ret = ret.."\t"
end
return ret..s
end
local function serialize_indent(o, i)
if type(o) == "number" then
return o
elseif type(o) == "string" then
return string.format("%q", o)
elseif type(o) == "boolean" then
if o then
return "true"
else
return "false"
end
elseif type(o) == "table" then
local has_table = false
for k,v in pairs(o) do
if type(v) == "table" then
has_table = true
break
end
end
local tnl, ind
if has_table then
tnl = "\n"
ind = M.indent("", i + 1)
else
tnl = " "
ind = ""
end
local ret = "{"..tnl
for k,v in pairs(o) do
local k2 = k
if type(k) == "string" then
k2 = "[\""..k2.."\"]"
end
ret = ret..ind..k2.." = "..serialize_indent(v, i + 1)..","..tnl
end
if has_table then
ret = ret..M.indent("}", i)
else
ret = ret.."}"
end
return ret
else
error("cannot serialize a " .. type(o))
end
end
function M.serialize(o)
return serialize_indent(o, 0)
end
function M.print_table(t)
if t ~= nil then
local i = 0
for k,v in pairs(t) do
print(k, "=>", v)
i = i + 1
end
if i == 0 then
print("empty")
end
else
print("nil")
end
end
function M.num_keys(t)
local i = 0
for k,v in pairs(t) do
i = i + 1
--print(k, v)
end
return i
end
return M