-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemy.lua
More file actions
61 lines (56 loc) · 1.59 KB
/
enemy.lua
File metadata and controls
61 lines (56 loc) · 1.59 KB
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 constants = require("constants")
local utils = require("utils")
local enemy = {
enemies = {}
}
function enemy.load(positions)
enemy.enemies = {}
for _, pos in ipairs(positions) do
table.insert(enemy.enemies, {
x = pos.x,
y = pos.y,
width = constants.PLAYER_SIZE,
height = constants.PLAYER_SIZE,
speed = constants.ENEMY_SPEED,
direction = 1
})
end
end
function enemy.update(dt, platforms)
local enemies_to_remove = {}
for i, e in ipairs(enemy.enemies) do
local old_x = e.x
local old_direction = e.direction
local next_x = e.x + e.speed * e.direction * dt
local has_platform = false
local hits_wall = false
for _, platform in ipairs(platforms) do
if utils.checkCollision({
x = e.direction == 1 and next_x + e.width or next_x,
y = e.y + e.height,
width = 1,
height = 1
}, platform) then
has_platform = true
end
if utils.checkCollision({
x = e.direction == 1 and next_x + e.width or next_x,
y = e.y,
width = 1,
height = e.height
}, platform) then
hits_wall = true
end
end
if not has_platform or hits_wall then
e.direction = -e.direction
next_x = e.x
end
e.x = next_x
end
return enemies_to_remove
end
function enemy.getEnemies()
return enemy.enemies
end
return enemy