summaryrefslogtreecommitdiff
path: root/bloodParticle.lua
blob: 12aa2af37da46f35ab02e4795a5d85ca6612a494 (plain)
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
local BloodParticle = {}
BloodParticle.__index = BloodParticle

local SHRINK_RATE = 6
local GRAVITY = 300
local PARTICLE_COUNT = 32
local BURST_SPEED_MIN = 40
local BURST_SPEED_MAX = 160

local BLOOD_COLORS = {
    {0.22, 0.38, 0.22, 1},
    {0.18, 0.30, 0.18, 1},
    {0.28, 0.44, 0.28, 1},
    {0.14, 0.22, 0.14, 1},
}

local GLOW_COLOR = {0.05, 0.7, 0.05, 0.0} -- meh

function BloodParticle.new(x, y, radius, color, vx, vy)
    local self = setmetatable({}, BloodParticle)
    self.x = x
    self.y = y
    self.radius = radius
    self.initialRadius = radius
    self.color = color
    self.vx = vx or 0
    self.vy = vy or 0
    self.stuck = false
    self.kill = false
    return self
end

function BloodParticle:update(dt, isSolidAt)
    if self.stuck or self.kill then return end

    self.x = self.x + self.vx * dt
    self.y = self.y + self.vy * dt
    self.vy = self.vy + GRAVITY * dt

    self.radius = self.radius - SHRINK_RATE * dt

    local hitWall = isSolidAt and isSolidAt(self.x, self.y)
    if hitWall or self.radius <= self.initialRadius * 0.5 then
        self.stuck = true
        self.radius = math.max(self.radius, self.initialRadius * 0.35)
        self.vx, self.vy = 0, 0
        return
    end

    if self.radius <= 0 then
        self.kill = true
    end
end

function BloodParticle:draw()
    if self.kill then return end

    love.graphics.setBlendMode("add")
    love.graphics.setColor(GLOW_COLOR)
    love.graphics.circle("fill", self.x, self.y, self.radius * 2.2)
    love.graphics.setBlendMode("alpha")

    love.graphics.setColor(self.color)
    love.graphics.circle("fill", self.x, self.y, self.radius)
    love.graphics.setColor(1, 1, 1, 1)
end

function BloodParticle.burst(x, y)
    local particles = {}
    for _ = 1, PARTICLE_COUNT do
        local angle = math.random() * math.pi * 2
        local speed = BURST_SPEED_MIN + math.random() * (BURST_SPEED_MAX - BURST_SPEED_MIN)
        local vx = math.cos(angle) * speed
        local vy = math.sin(angle) * speed - 40
        local radius = 1.5 + math.random() * 8.0
        local color = BLOOD_COLORS[math.random(#BLOOD_COLORS)]
        table.insert(particles, BloodParticle.new(x, y, radius, color, vx, vy))
    end
    return particles
end

return BloodParticle