-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemy.lua
More file actions
30 lines (25 loc) · 849 Bytes
/
enemy.lua
File metadata and controls
30 lines (25 loc) · 849 Bytes
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
enemies = {}
function spawnEnemy(x, y)
local enemy = world:newRectangleCollider(x, y, 75, 81, {collision_class = "Danger"})
enemy.direction = 1
enemy.speed = 200
enemy.animation = animations.enemy
table.insert(enemies, enemy)
end
function updateEnemies(dt)
for i,e in ipairs(enemies) do
e.animation:update(dt)
local ex, ey = e:getPosition()
local colliders = world:queryRectangleArea(ex + (75 * e.direction), ey + 40, 10, 10, {'Platform'})
if #colliders == 0 then
e.direction = e.direction * -1
end
e:setX(ex + e.speed * dt * e.direction)
end
end
function drawEnemies()
for i,e in ipairs(enemies) do
local ex, ey = e:getPosition()
e.animation:draw(sprites.dogSprite, ex, ey, nil, e.direction, 1, 75, 100)
end
end