-
Notifications
You must be signed in to change notification settings - Fork 0
/
thruster.lua
70 lines (64 loc) · 1.75 KB
/
thruster.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
local SharedState = require 'sharedstate'
local Thruster = {
_particles = {},
}
local _PARTICLE_COUNT = 10
local _THRUSTER_DISTANCE = 96
function Thruster:new(p)
p = p or { _particles = {} }
setmetatable(p, { __index = self })
for i = 1, _PARTICLE_COUNT do
table.insert(
p._particles,
{
x = -18 + math.random() * 36,
y = math.random(0, _THRUSTER_DISTANCE),
vx = 0,
vy = 0,
mass = math.random(),
}
)
end
return p
end
function Thruster:draw()
for index = 1, _PARTICLE_COUNT do
local p = self._particles[index]
if SharedState.boost > 0 then
love.graphics.setColor(0, 1, 1, math.min(1, -p.vy / 400))
else
love.graphics.setColor(1, 1, 1, math.min(1, -p.vy / 400))
end
love.graphics.circle('fill', p.x, p.y, 1.5 + p.mass)
end
end
function Thruster:update(dt, isActive)
for index = 1, _PARTICLE_COUNT do
local p = self._particles[index]
if isActive then
local attractor = { x = 0, y = 0 }
if math.random() < 0.2 then
attractor.y = math.random(-8, 8)
end
local accCoeff = 0.25 - p.mass * 0.21
local acceleration = {
x = (attractor.x - p.x) * accCoeff,
y = -(_THRUSTER_DISTANCE - (attractor.y - p.y)) * accCoeff,
}
p.vx = p.vx + acceleration.x
p.vy = p.vy + acceleration.y
else
p.vx = p.vx * 0.84
p.vy = p.vy * 0.84
end
p.x = p.x + p.vx * dt
p.y = p.y + p.vy * dt
if p.y <= 0 or math.random() < 0.2 * dt then
p.y = _THRUSTER_DISTANCE
p.x = -18 + math.random() * 36
p.vx = 0
p.vy = 0
end
end
end
return Thruster