-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslimeBalls.lua
101 lines (90 loc) · 3.03 KB
/
slimeBalls.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
-- todo: generalize projectiles and put code there
slimeBalls = {
container = {}
}
slimeBalls.slimeType2color = {
slime1 = {0/255, 101/255, 234/255, 174/255},
slime2 = {0/255, 234/255, 101/255, 174/255}
}
slimeBalls.speed = 1.5e2
function slimeBalls.spawn(t)
local defaults = {
slimeType = 'slime1',
x = 0, y = 0,
angle = 0,
speed = slimeBalls.speed,
life = 1,
pierce = 0,
damage = 5
}
for k, v in pairs(defaults) do
if t[k] == nil then t[k] = v end
end
t.id = lume.uuid()
t.type = 'slimeBall'
t.color = slimeBalls.slimeType2color[t.slimeType]
t.spawnTime = gameTime
t.body = love.physics.newBody(clientRealm.physics.world, t.x, t.y, 'dynamic')
t.shape = love.physics.newCircleShape(5)
t.fixture = love.physics.newFixture(t.body, t.shape, 1)
t.fixture:setUserData(t)
t.fixture:setCategory(3)
t.body:setAngle(-t.angle)
t.body:setFixedRotation(true)
t.body:setLinearVelocity(math.cos(t.angle)*t.speed, -math.sin(t.angle)*t.speed)
slimeBalls.container[t.id] = t
end
function slimeBalls.destroy(id)
local v = slimeBalls.container[id]
if v then
if not v.fixture:isDestroyed() then v.fixture:destroy() end
if not v.body:isDestroyed() then v.body:destroy() end
slimeBalls.container[id] = nil
end
end
function slimeBalls.reset()
for k, v in pairs(slimeBalls.container) do
slimeBalls.destroy(k)
end
end
function slimeBalls.update(dt)
for k, v in pairs(slimeBalls.container) do
v.x, v.y = v.body:getPosition()
local d = math.sqrt(v.x^2 + v.y^2)
if d < 8*15 then
local f = math.min((8*15 - d)*50, slimeBalls.speed)
local fx = v.x/d*f
local fy = v.y/d*f
v.body:applyForce(fx, fy)
local lvx, lvy = v.body:getLinearVelocity()
local dv = math.sqrt(lvx^2 + lvy^2)
if dv > slimeBalls.speed then
v.body:setLinearVelocity(lvx/dv*slimeBalls.speed, lvy/dv*slimeBalls.speed)
end
end
if gameTime - v.spawnTime > v.life then
slimeBalls.destroy(k)
end
end
end
function slimeBalls.draw()
for k, v in pairs(slimeBalls.container) do
scene.add{
draw = function()
local _shader = love.graphics.getShader()
love.graphics.setShader(shaders.outline)
shaders.outline:send('stepSize', {
1/gfx.slimeBall:getWidth(),
1/gfx.slimeBall:getHeight()
})
shaders.outline:send('outlineColor', {0, 0, 0, 1})
love.graphics.setColor(v.color)
local x, y = v.body:getPosition()
love.graphics.draw(gfx.slimeBall, lume.round(x), lume.round(y), 0, 1, 1,
lume.round(gfx.slimeBall:getWidth()/2), lume.round(gfx.slimeBall:getHeight()/2))
love.graphics.setShader(_shader)
end,
y = v.body:getY()
}
end
end