-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.lua
More file actions
186 lines (153 loc) · 4.68 KB
/
Copy pathutils.lua
File metadata and controls
186 lines (153 loc) · 4.68 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
function lerp(a,b,t) return a + (b-a) * t end
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
return x1 < x2+w2 and
x2 < x1+w1 and
y1 < y2+h2 and
y2 < y1+h1
end
function IsHoveredByMouse(x1,y1,x2,y2)
local mx, my = love.mouse.getPosition()
return x1 <= mx and mx <= x2 and y1 <= my and my <= y2
end
function DrawHitbox(hitbox)
if not ShowHitboxes then return end
love.graphics.setColor(0, 1, 0)
love.graphics.setLineWidth(1)
love.graphics.rectangle("line", unpack(hitbox))
end
function DrawSpeed(x, y, sx, sy)
if not ShowHitboxes then return end
love.graphics.setColor(0, 1, 0)
love.graphics.setLineWidth(2)
love.graphics.line(x, y, (x + sx), (y + sy))
end
function IsOnTheEdge(x, y, size)
local rightEdge = x + size/2 >= love.graphics.getWidth() + Camera.x
local leftEdge = x - size/2 <= 0 + Camera.x
local bottomEdge = y + size/2 >= love.graphics.getHeight() + Camera.y
local topEdge = y - size/2 <= 0 + Camera.y
return rightEdge or leftEdge or topEdge or bottomEdge
end
function DestroyObjectByIndex(space, pos)
table.remove(space, pos)
end
function IndexOf(array, value)
for i, v in ipairs(array) do
if v == value then
return i
end
end
return nil
end
function DeleteRedundantObjects()
local BulletsToDelete = {}
local EnemiesToDelete = {}
local PowerupsToDelete = {}
for i=1, #Bullets do
if not Bullets[i].isAlive then
table.insert(BulletsToDelete, Bullets[i])
end
end
for i=1, #Enemies do
if not Enemies[i].isAlive then
table.insert(EnemiesToDelete, Enemies[i])
end
end
for i=1, #Powerups do
if not Powerups[i].isAlive then
table.insert(PowerupsToDelete, Powerups[i])
end
end
for i=1, #BulletsToDelete do
DestroyObjectByIndex(Bullets, IndexOf(Bullets, BulletsToDelete[i]))
end
for i=1, #EnemiesToDelete do
DestroyObjectByIndex(Enemies, IndexOf(Enemies, EnemiesToDelete[i]))
end
for i=1, #PowerupsToDelete do
DestroyObjectByIndex(Powerups, IndexOf(Powerups, PowerupsToDelete[i]))
end
end
function MergeTables(first_table, second_table)
for k,v in pairs(second_table) do first_table[k] = v end
return first_table
end
function RandomChoice(table, c)
-- print(Dump(table))
-- print(Dump(c))
if type(c) == "nil" then
local n = math.random(#table)
return table[n]
end
local chances = {}
for key, value in ipairs(c) do
chances[key] = value
end
local chancesSum = 0
for key, value in ipairs(chances) do
local chance = value
chances[key] = chances[key] + chancesSum
chancesSum = chancesSum + chance
end
local n = math.random(chancesSum)
print(Dump(table))
print(Dump(chances))
print(n)
for key, value in ipairs(chances) do
if value >= n then
return table[key]
end
end
end
function GetRandomSpotOnScreenPerimeter()
local side = math.random(1,4)
local x,y
if side == 1 then
x, y = 0, math.random(0, love.graphics.getHeight())
elseif side == 2 then
x, y = math.random(0, love.graphics.getWidth()), 0
elseif side == 3 then
x, y = love.graphics.getWidth(), math.random(0, love.graphics.getHeight())
elseif side == 4 then
x, y = math.random(0, love.graphics.getWidth()), love.graphics.getHeight()
end
x = x + Camera.x
y = y + Camera.y
return x, y
end
function Dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. Dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
function love.graphics.setAlpha(alpha)
local r, g, b, a = love.graphics.getColor()
love.graphics.setColor(r, g, b, alpha)
end
function DrawFunctionToImage(width, height, drawFunction)
local lineWidth = love.graphics.getLineWidth()
local canvas = love.graphics.newCanvas(width + lineWidth, height + lineWidth)
love.graphics.setCanvas(canvas)
drawFunction()
love.graphics.setCanvas()
local image = love.graphics.newImage(canvas:newImageData())
return image
-- return canvas
end
function DrawFunctionToImageData(width, height, drawFunction)
local lineWidth = love.graphics.getLineWidth()
local canvas = love.graphics.newCanvas(width + lineWidth, height + lineWidth)
love.graphics.setCanvas(canvas)
drawFunction()
love.graphics.setCanvas()
local imageData = canvas:newImageData()
return imageData
-- return canvas
end