-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.lua
More file actions
117 lines (100 loc) · 2.43 KB
/
utils.lua
File metadata and controls
117 lines (100 loc) · 2.43 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
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
module(..., package.seeall)
-- --
-- String Addons
-- --
--
-- Split a string based on splitchar
--
function string.split(word, splitchar)
local ret = {}
word = word..splitchar
word:gsub('([^'..splitchar..']*)'..splitchar, function(x) table.insert(ret, x) end)
return ret
end
-- --
-- Math Addons
-- --
--
--
--
function math.sign(number)
return number > 0 and 1 or number < 0 and -1 or 0
end
--
-- Convert an RRGGBB format to a table of colors {r=0,g=0,b=0}
--
function RGBToTable(colors)
assert(#colors == 6, "Invalid color for conversion (RRGGBB)")
local a = {r=1,g=3,b=5}
for v,i in pairs(a) do
a[v] = tonumber('0x'..colors:sub(i,i+1))
end
return a
end
--
-- Check to see if the string is a number
--
function isNum(s) return type(s) == 'string' and s:find('^%d+$') end
--
-- Check to see if the string has numbers in it
--
function hasNum(s) return type(s) == 'string' and s:find('%d') end
--
-- Copy elements from the xargs table to tbl
--
function CopyXargs(tbl, xargs)
for i, arg in pairs(xargs) do
tbl[i] = isNum(arg) and tonumber(arg) or arg
end
end
--
-- Load an image and add a transparency key to it (255,0,255)
--
function loadImage(filename, transkey)
if not love.filesystem.exists(filename) then filename = 'gfx/'..filename end
local imageData = love.image.newImageData(filename)
transkey = transkey or {255,0,255}
imageData:mapPixel( function(x, y, r, g, b, a)
if r == transkey[1] and g == transkey[2] and b == transkey[3] then
return 0, 0, 0, 0
else
return r, g, b, a
end
end )
return love.graphics.newImage(imageData)
end
--
-- Wrap a number from 0 to max
--
function wrap(number, max)
return number > max and number - max or number < 0 and number + max or number
end
function wrapAng(number)
return wrap(number, 360)
end
--
-- Restrict a number between a min and max
--
function clamp(min, number, max)
return math.max(math.min(max, number), min)
end
--
-- Wrap a vector to the screen
--
function wrapScreen(vector)
local width = love.graphics.getWidth()
local height = love.graphics.getHeight()
vector.x = vector.x < 0 and vector.x + width or vector.x > width and vector.x - width or vector.x
vector.y = vector.y < 0 and vector.y + height or vector.y > height and vector.y - height or vector.y
end
--
-- Check if something is in a table
--
function isIn(table, elem)
for _,v in ipairs(table) do
if v == elem then
return true
end
end
return false
end