-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemies.odin
280 lines (231 loc) · 6.11 KB
/
enemies.odin
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import "core:fmt"
import "core:math"
import "core:slice"
import rl "vendor:raylib"
EnemyState :: enum {
IDLE,
WALKING_NORTH,
WALKING_SOUTH,
WALKING_EAST,
WALKING_WEST,
ATTACKING,
COOLDOWN,
DEAD,
}
Aggro :: struct {
position: rl.Vector2,
radius: f32,
}
Enemy :: struct {
position: rl.Vector2,
aggro: Aggro,
sprite_sheet: rl.Texture2D,
state: EnemyState,
target: rl.Vector2,
speed: f32,
direction: Direction,
size: rl.Vector2,
current_frame: i32,
frame_counter: i32,
sprite_cols: i32,
sprite_rows: i32,
hp: i32,
is_dying: bool, // Add this to track death animation
flash_timer: f32, // Add this for the red flash effect
}
ENEMY_SPEED :: f32(100) // Pixels per second
init_enemy :: proc(pos: rl.Vector2) -> Enemy {
aggro := Aggro{pos, 50.0}
enemy_sprite_sheet := rl.LoadTexture("./assets/Enemies/Skeleton.png")
return Enemy {
position = pos,
sprite_sheet = enemy_sprite_sheet,
aggro = aggro,
direction = .SOUTH,
target = pos,
speed = ENEMY_SPEED,
current_frame = 0,
sprite_cols = 6,
sprite_rows = 10,
hp = 1,
size = {32, 32}, // Adjust size as needed
}
}
init_enemy_animations :: proc() -> map[EnemyState]Animation {
animations := make(map[EnemyState]Animation)
// configure each animation state
animations[.IDLE] = Animation {
row = 0,
frames = 6,
frame_speed = 8,
blocks_input = false,
}
animations[.WALKING_NORTH] = Animation {
row = 5,
frames = 6,
frame_speed = 8,
blocks_input = false,
}
animations[.WALKING_SOUTH] = Animation {
row = 3,
frames = 6,
frame_speed = 8,
blocks_input = false,
}
animations[.WALKING_EAST] = Animation {
row = 4,
frames = 6,
frame_speed = 8,
blocks_input = false,
}
animations[.WALKING_WEST] = Animation {
row = 4,
frames = 6,
frame_speed = 8,
blocks_input = false,
}
animations[.DEAD] = Animation {
row = 6,
frames = 4,
frame_speed = 8,
blocks_input = true,
}
return animations
}
update_enemies :: proc(
enemies: ^[dynamic]Enemy,
player: ^Player,
animations: map[EnemyState]Animation,
) {
for &enemy, idx in enemies {
// check if enemy is inside of enemy aggro range
// if so walk at them
// when in range attack
if enemy.flash_timer > 0 {
enemy.flash_timer -= rl.GetFrameTime()
}
if player.current_state == .DEAD {
enemy.state = .IDLE
return
}
// If enemy is dying, only update death animation
if enemy.is_dying {
current_anim := animations[.DEAD]
enemy.frame_counter += 1
if enemy.frame_counter >= current_anim.frame_speed {
enemy.frame_counter = 0
if enemy.current_frame < current_anim.frames - 1 {
enemy.current_frame += 1
} else {
// Death animation complete, remove enemy
ordered_remove(&enemies^, idx)
}
}
continue // Skip normal update for dying enemies
}
if rl.CheckCollisionPointCircle(
player.position,
rl.Vector2{enemy.position.x + 15, enemy.position.y + 15},
200.0,
) {
current_anim := animations[enemy.state]
enemy.frame_counter += 1
if enemy.frame_counter >= current_anim.frame_speed {
enemy.frame_counter = 0
enemy.current_frame = (enemy.current_frame + 1) % current_anim.frames
// If this animation blocks input and has completed, return to idle
if current_anim.blocks_input && enemy.current_frame == 0 {
enemy.state = .IDLE
}
}
// player is in aggro range
if rl.CheckCollisionPointCircle(
player.position,
rl.Vector2{enemy.position.x + 15, enemy.position.y + 15},
200.0,
) {
if int(enemy.position.x) != int(player.position.x) {
// check if player is more to the right or left
if (enemy.position.x < player.position.x) {
// move to the right
enemy.position.x += 1
enemy.state = .WALKING_EAST
enemy.direction = .EAST
} else if (enemy.position.x > player.position.x) {
// move to the left
enemy.position.x -= 1
enemy.state = .WALKING_WEST
enemy.direction = .WEST
}
} else if (enemy.position.y != player.position.y) {
// move up or down
if enemy.position.y < player.position.y {
// move down
enemy.position.y += 1
enemy.state = .WALKING_SOUTH
enemy.direction = .SOUTH
} else if enemy.position.y > player.position.y {
enemy.position.y -= 1
enemy.state = .WALKING_NORTH
enemy.direction = .NORTH
}
}
} else {
enemy.state = .IDLE
}
}
}
}
draw_enemies :: proc(enemies: [dynamic]Enemy, animations: map[EnemyState]Animation) {
for enemy in enemies {
current_anim := animations[enemy.state]
frame_width := enemy.sprite_sheet.width / enemy.sprite_cols
frame_height := enemy.sprite_sheet.height / enemy.sprite_rows
source := rl.Rectangle {
x = f32(enemy.current_frame * frame_width),
y = f32(current_anim.row * frame_height),
height = f32(frame_height),
}
dest := rl.Rectangle {
x = enemy.position.x,
y = enemy.position.y,
width = f32(frame_width),
height = f32(frame_height),
}
switch enemy.direction {
case .EAST:
source.width = f32(frame_width)
case .WEST:
source.width = -f32(frame_width)
case .NORTH, .SOUTH:
source.width = f32(frame_width)
}
// Choose color based on flash timer
color := rl.WHITE
if enemy.flash_timer > 0 {
color = rl.RED
}
rl.DrawTexturePro(enemy.sprite_sheet, source, dest, rl.Vector2{0, 0}, 0, color)
}
}
check_if_enemy_hit :: proc(player: Player, enemies: ^[dynamic]Enemy, player_attack: rl.Rectangle) {
for &enemy in enemies {
hitbox := rl.Rectangle {
enemy.position.x,
enemy.position.y,
f32(enemy.sprite_sheet.width / enemy.sprite_cols),
f32(enemy.sprite_sheet.height / enemy.sprite_rows),
}
if rl.CheckCollisionRecs(hitbox, player_attack) {
enemy.hp -= 1
enemy.flash_timer = 0.2 // Start flash effect
if enemy.hp <= 0 && !enemy.is_dying {
enemy.is_dying = true
enemy.state = .DEAD
enemy.current_frame = 0
enemy.frame_counter = 0
}
}
}
}