forked from jcmoyer/LD26
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemy.lua
47 lines (41 loc) · 1.05 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
local enemy = {}
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, { __index = enemy })
end
function enemy:update(dt)
if not self.patrol 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
end
function enemy:draw()
local g = love.graphics
local y = self.owner:y(self.x)
local w, h = calculatePixelSize(self)
local hw = w / 2
g.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
return enemy