-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.lua
103 lines (83 loc) · 2.77 KB
/
utilities.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
local utilities = {}
-- https://stackoverflow.com/questions/41349526/how-to-iterate-lua-table-from-end
local function reversedipairsiter(t, i)
i = i - 1
if i ~= 0 then
return i, t[i]
end
end
function utilities.ripairs(t)
return reversedipairsiter, t, #t + 1
end
-- Test collision between any two GameObjects
function utilities.checkBoxCollision(a,b)
return a:getX() < (b:getX() + b:getWidth()) and
(a:getX() + a:getWidth()) > b:getX() and
a:getY() < (b:getY() + b:getHeight()) and
(a:getY() + a:getHeight()) > b:getY()
end
-- for an object at X location objectX, find whether the nearest enemy (horizontally) is left/right
function utilities.findNearestEnemyX(objectX, enemies)
local enemyDist = nil
local enemyDir = 0
local enemyX = objectX
-- find closest
for ii,enemy in ipairs(enemies) do
ii = ii
local thisEnemyX = enemy:getX() + enemy:getWidth()/2
if (enemyDist == nil or (math.abs(objectX - thisEnemyX) < enemyDist)) then
enemyDist = math.abs(objectX - thisEnemyX)
enemyX = thisEnemyX
end
end
if(enemyDist < 3) then
-- stop oscillation
enemyDir = 0
elseif(objectX > enemyX) then
enemyDir = -1
elseif (objectX < enemyX) then
enemyDir = 1
end
return enemyDir
end
function utilities.gradientMesh(dir, ...)
-- Check for direction
local isHorizontal = true
if dir == "vertical" then
isHorizontal = false
elseif dir ~= "horizontal" then
error("bad argument #1 to 'gradient' (invalid value)", 2)
end
-- Check for colors
local colorLen = select("#", ...)
if colorLen < 2 then
error("color list is less than two", 2)
end
-- Generate mesh
local meshData = {}
if isHorizontal then
for i = 1, colorLen do
local color = select(i, ...)
local x = (i - 1) / (colorLen - 1)
meshData[#meshData + 1] = {x, 1, x, 1, color[1], color[2], color[3], color[4] or 1}
meshData[#meshData + 1] = {x, 0, x, 0, color[1], color[2], color[3], color[4] or 1}
end
else
for i = 1, colorLen do
local color = select(i, ...)
local y = (i - 1) / (colorLen - 1)
meshData[#meshData + 1] = {1, y, 1, y, color[1], color[2], color[3], color[4] or 1}
meshData[#meshData + 1] = {0, y, 0, y, color[1], color[2], color[3], color[4] or 1}
end
end
-- Resulting Mesh has 1x1 image size
return love.graphics.newMesh(meshData, "strip", "static")
end
local showHitbox = false
function utilities.getShowHitbox()
return showHitbox
end
function utilities.toggleHitbox()
showHitbox = not showHitbox
end
return utilities