-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemy.lua
More file actions
212 lines (189 loc) · 6.35 KB
/
Copy pathenemy.lua
File metadata and controls
212 lines (189 loc) · 6.35 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
local recovery_frames = 30
local attack_delay_frames = 5
local enemy_states = {
neutral = 0,
follow_player_x = 1,
follow_player_y = 2,
attack1 = 3,
recovery1 = 4,
}
local enemy_animations = {}
local enemy_still_animations = {}
local enemies = {}
local function on_sword_collision(enemy)
set_sticky_friction(10)
enemy.actor.enabled = false
end
local function on_update(enemy)
enemy.state_actions[enemy.state](enemy)
end
local function on_cant_move(enemy)
enemy.actor.dx *= -1
enemy.actor.dy *= -1
end
local function can_see_player_x(enemy, px, py)
local dist_x = enemy.actor.x - px
if (-1 < dist_x and dist_x < 1) then
if (enemy.actor.y > py) then
for y = py, enemy.actor.y do
if is_tile(sprite_flags.wall, px, y) then return false end
end
return true
else --(enemy.actor.y < py)
for y = enemy.actor.y, py do
if is_tile(sprite_flags.wall, px, y) then return false end
end
return true
end
end
return false
end
local function can_see_player_y(enemy, px, py)
local dist_y = enemy.actor.y - py
if (-1 < dist_y and dist_y < 1) then
if (enemy.actor.x > px) then
for x = px, enemy.actor.x do
if is_tile(sprite_flags.wall, x, py) then return false end
end
return true
else -- (enemy.actor.x < px)
for x = enemy.actor.x, px do
if is_tile(sprite_flags.wall, px, y) then return false end
end
return true
end
end
return false
end
local function update_direction_neutral(enemy)
if (enemy.actor.dx < 0) then
enemy.direction = directions.left
enemy.actor.animations = enemy_animations[directions.left]
enemy.actor.flip_x = true
elseif (enemy.actor.dx > 0) then
enemy.direction = directions.right
enemy.actor.animations = enemy_animations[directions.right]
enemy.actor.flip_x = false
elseif (enemy.actor.dy < 0) then
enemy.direction = directions.up
enemy.actor.animations = enemy_animations[directions.up]
elseif (enemy.actor.dy > 0) then
enemy.direction = directions.down
enemy.actor.animations = enemy_animations[directions.down]
elseif (enemy.actor.dx == 0 and enemy.actor.dy == 0) then
enemy.actor.animations = { enemy_still_animations, enemy.actor.animations[2] }
end
end
local function neutral_update(enemy)
local px,py = get_player_pos()
if (can_see_player_x(enemy, px, py)) then
enemy.state = enemy_states.follow_player_x
elseif (can_see_player_y(enemy, px, py)) then
enemy.state = enemy_states.follow_player_y
else
update_direction_neutral(enemy)
end
end
local function update_direction_follow(enemy)
local px, py = get_player_pos()
if (enemy.state == enemy_states.follow_player_x) then
if (enemy.actor.y > py) then
enemy.direction = directions.up
elseif (enemy.actor.y < py) then
enemy.direction = directions.down
end
elseif (enemy.state == enemy_states.follow_player_y) then
if (enemy.actor.x > px) then
enemy.direction = directions.left
enemy.actor.flip_x = true
elseif (enemy.actor.x < px) then
enemy.direction = direction.right
enemy.actor.flip_x = false
end
end
end
local function follow_player_update(enemy)
enemy.actor.dx = 0; enemy.actor.dy = 0
local px, py = get_player_pos()
local dist_x = enemy.actor.x - px
local dist_y = enemy.actor.y - py
if (-2 < dist_x and dist_x < 2 and -2 < dist_y and dist_y < 2) then
enemy.sword.direction = enemy.direction
enemy.sword.frame = 1
enemy.state = enemy_states.attack1
enemy.frame_count = attack_delay_frames
return
end
if (enemy.state == enemy_states.follow_player_x) then
if (dist_x < -0.125) then enemy.actor.dx = 1
elseif (dist_x > 0.125) then enemy.actor.dx = -1 end
else
if (dist_y < -0.125) then enemy.actor.dy = 1
elseif (dist_y > 0.125) then enemy.actor.dy = -1 end
end
update_direction_neutral(enemy) -- update the direction for animation
update_direction_follow(enemy) -- override the direction for attacking
end
local function attack_update(enemy)
if (enemy.frame_count > 0) then
enemy.frame_count -= 1
return
end
check_sword_collisions(enemy.sword, enemy.actor)
enemy.sword.frame += 1
if (enemy.sword.frame == num_sword_frames()) then
enemy.frame_count = recovery_frames
enemy.state = enemy_states.recovery1
end
end
local function recovery_update(enemy)
if (enemy.frame_count > 0) then
enemy.frame_count -= 1
return
end
local px, py = get_player_pos()
local dist_x = enemy.actor.x - px
local dist_y = enemy.actor.y - py
if (-2 < dist_x and dist_x < 2 and -2 < dist_y and dist_y < 2) then
enemy.sword.direction = enemy.direction
enemy.sword.frame = 1
enemy.state = enemy_states.attack1
else
enemy.state = enemy_states.neutral
end
end
local function post_draw(enemy)
if (enemy.state == enemy_states.attack1 or enemy.state == enemy_states.recovery1) then
draw_sword(enemy.sword, enemy.actor)
end
end
function create_enemy(x, y)
local new_enemy = {
state = enemy_states.neutral,
state_actions = {
[enemy_states.neutral] = neutral_update,
[enemy_states.follow_player_x] = follow_player_update,
[enemy_states.follow_player_y] = follow_player_update,
[enemy_states.attack1] = attack_update,
[enemy_states.recovery1] = recovery_update,
},
sword = create_sword(),
frame_count = 0,
direction = directions.right
}
new_enemy.actor = create_actor{
x = x, y = y,
speed = 1/16,
animations = enemy_animations[directions.left],
parent = new_enemy,
on_update = on_update,
on_sword_collision = on_sword_collision,
on_cant_move = on_cant_move,
post_draw = post_draw
}
new_enemy.actor.dx = 1
add(enemies, new_enemy)
end
function init_enemy_data()
enemy_animations, enemy_still_animations = create_character_animation()
end