-
Notifications
You must be signed in to change notification settings - Fork 1
/
enemy.lua
61 lines (53 loc) · 1.42 KB
/
enemy.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
local enemy = {}
local mt = { __index = enemy }
local setmetatable = setmetatable
local graphics = love.graphics
local polygon = graphics.polygon
local abs = math.abs
local mathex = require('hug.extensions.math')
local clamp = mathex.clamp
local function calculatePixelSize(e)
return e.size * 32, e.size * 32
end
function enemy.new(owner, x, patrol, size, speed)
local instance = {
owner = owner,
x = x,
size = size or 1,
speed = speed or 50,
patrol = patrol,
direction = 'right'
}
return setmetatable(instance, mt)
end
function enemy:update(dt)
if self.patrol == nil then return end
if self.direction == 'right' then
self.x = self.x + dt * self.speed
if self.x >= self.patrol.right then
self.direction = 'left'
end
elseif self.direction == 'left' then
self.x = self.x - dt * self.speed
if self.x <= self.patrol.left then
self.direction = 'right'
end
end
self.x = clamp(self.x, self.patrol.left, self.patrol.right)
end
function enemy:draw()
local y = self.owner:y(self.x)
local w, h = calculatePixelSize(self)
local hw = w / 2
polygon('fill', self.x, y, self.x - hw, y - h, self.x + hw, y - h)
end
function enemy:contains(x)
local w, _ = calculatePixelSize(self)
return x >= self.x - w / 2 and x <= self.x + w / 2
end
function enemy:overlaps(x, r)
local w, _ = calculatePixelSize(self)
local d = abs(self.x - x)
return d < r + w / 2
end
return enemy