-
Notifications
You must be signed in to change notification settings - Fork 0
/
shield.lua
89 lines (60 loc) · 1.8 KB
/
shield.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
Shield = class("Shield", Entity)
---
-- Shield:initialize
-- Creates a shield to protect an entity.
--
-- @param body Physics body to attach the shield to
-- @param size Radius of the shield
-- @param max Maximum energy of the shield
--
function Shield:initialize(body, size, max)
self.max = max
self.level = max
self.size = size
self.body = body
end
---
-- Shield:update
-- Updates the shield
--
function Shield:update(dt)
self.level = math.min(self.max, self.level + dt / 5)
end
---
-- Shield:draw
-- Draws an outlined circle, you should override this in a subclass.
--
function Shield:draw()
love.graphics.setColor(200, 200, 200, 150)
love.graphics.circle("line", self.body:getX(), self.body:getY(), self.size - 2, 30)
love.graphics.setColor(150, 200, 255, 250)
-- love.graphics.arc("line", self.body:getX(), self.body:getY(), self.size + 2, 0, self.level / self.max * math.pi * 2, 30) -- shield ~= pacman! :<
local vertices = {}
local x, y = self.body:getX(), self.body:getY()
for i = 0, math.ceil(self.level / self.max * 36) do
local theta = math.min(i * 10 / 180 * math.pi, self.level / self.max * math.pi * 2)
table.insert(vertices, x + math.cos(theta) * self.size)
table.insert(vertices, y + math.sin(theta) * self.size)
end
if #vertices >= 4 then
love.graphics.line(unpack(vertices))
end
end
---
-- Shield:takeDamage
-- Subtract hitpoints.
--
-- @param hp Amount of damage to subtract
--
function Shield:takeDamage(hp)
if not hp then return end
self.level = math.max(0, self.level - hp)
end
---
-- Shield:takeDamage
-- Subtract hitpoints.
--
-- @returns bool True if the shield's level is less than 1, false otherwise
function Shield:isEmpty()
return self.level < 1
end