-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.odin
376 lines (319 loc) · 8.79 KB
/
player.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package main
import "core:fmt"
import rl "vendor:raylib"
Player :: struct {
position: rl.Vector2,
sprite_sheet: rl.Texture2D,
direction: Direction,
current_state: Animation_State,
current_frame: i32,
frame_counter: i32,
sprite_cols: i32,
sprite_rows: i32,
hp: int,
hit_cooldown: f32,
can_be_hit: bool,
knockback_velocity: rl.Vector2,
is_knocked_back: bool,
}
PlayerAttack :: struct {
position: rl.Vector2,
is_active: bool,
damage: int,
}
init_animations :: proc() -> map[Animation_State]Animation {
animations := make(map[Animation_State]Animation)
// configure each animation state
animations[.IDLE] = Animation {
row = 0,
frames = 6,
frame_speed = 8,
blocks_input = false,
}
animations[.WALK_NORTH] = Animation {
row = 5,
frames = 6,
frame_speed = 8,
blocks_input = false,
}
animations[.WALK_SOUTH] = Animation {
row = 3,
frames = 6,
frame_speed = 8,
blocks_input = false,
}
animations[.WALK_EAST] = Animation {
row = 4,
frames = 6,
frame_speed = 8,
blocks_input = false,
}
animations[.WALK_WEST] = Animation {
row = 4,
frames = 6,
frame_speed = 8,
blocks_input = false,
}
animations[.ATTACK_NORTH] = Animation {
row = 8,
frames = 4,
frame_speed = 4,
blocks_input = true,
}
animations[.ATTACK_SOUTH] = Animation {
row = 6,
frames = 4,
frame_speed = 4,
blocks_input = true,
}
animations[.ATTACK_EAST] = Animation {
row = 7,
frames = 4,
frame_speed = 4,
blocks_input = true,
}
animations[.ATTACK_WEST] = Animation {
row = 7,
frames = 4,
frame_speed = 4,
blocks_input = true,
}
animations[.DEAD] = Animation {
row = 9,
frames = 4,
frame_speed = 8,
blocks_input = true,
}
return animations
}
init_player :: proc(sprite: rl.Texture2D) -> Player {
player := Player {
position = {800, 800},
sprite_sheet = sprite,
direction = .SOUTH,
current_state = .IDLE,
current_frame = 0,
frame_counter = 0,
sprite_cols = 6,
sprite_rows = 10,
hp = 100,
hit_cooldown = 0,
can_be_hit = true,
is_knocked_back = false,
}
return player
}
update_player :: proc(player: ^Player, animations: map[Animation_State]Animation) {
check_if_hit(player, enemies)
current_anim := animations[player.current_state]
update_hit_cooldown(player)
if player.hp <= 0 && player.current_state != .DEAD {
player.current_state = .DEAD
player.current_frame = 0
player.frame_counter = 0
return // Skip rest of update if just died
}
// If already dead, only update the death animation once
if player.current_state == .DEAD {
current_anim := animations[.DEAD]
player.frame_counter += 1
if player.frame_counter >= current_anim.frame_speed {
player.frame_counter = 0
// Only advance frame if we haven't reached the end
if player.current_frame < current_anim.frames - 1 {
player.current_frame += 1
}
}
return // Skip rest of update if dead
}
if player.is_knocked_back {
// Apply knockback velocity
player.position = player.position + player.knockback_velocity
// Reduce knockback velocity over time (friction)
player.knockback_velocity = player.knockback_velocity * 0.9
// Stop knockback when velocity is very small
if rl.Vector2Length(player.knockback_velocity) < 0.1 {
player.is_knocked_back = false
player.knockback_velocity = rl.Vector2{0, 0}
}
// Skip normal movement input while being knocked back
if player.is_knocked_back do return
}
player.frame_counter += 1
if player.frame_counter >= current_anim.frame_speed {
player.frame_counter = 0
player.current_frame = (player.current_frame + 1) % current_anim.frames
// If this animation blocks input and has completed, return to idle
if current_anim.blocks_input && player.current_frame == 0 {
player.current_state = .IDLE
}
}
// Only process input if current animation allows it
if !current_anim.blocks_input {
// Handle movement
if rl.IsKeyDown(.W) {
player.position.y -= 2
player.current_state = .WALK_NORTH
player.direction = .NORTH
} else if rl.IsKeyDown(.S) {
player.position.y += 2
player.current_state = .WALK_SOUTH
player.direction = .SOUTH
} else if rl.IsKeyDown(.D) {
player.position.x += 2
player.current_state = .WALK_EAST
player.direction = .EAST
} else if rl.IsKeyDown(.A) {
player.position.x -= 2
player.current_state = .WALK_WEST
player.direction = .WEST
} else if player.current_state != .IDLE {
// Return to idle if no movement keys are pressed
player.current_state = .IDLE
player.current_frame = 0
}
// Handle attack input
if rl.IsKeyPressed(.SPACE) {
// Set attack animation based on current direction
switch player.direction {
case .NORTH:
player_attack := PlayerAttack {
{player.position.x, player.position.y - 10},
false,
10,
}
player_attack_rect := rl.Rectangle {
player_attack.position.x,
player_attack.position.y,
40,
40,
}
check_if_enemy_hit(player^, &enemies, player_attack_rect)
player.current_state = .ATTACK_NORTH
case .SOUTH:
player_attack := PlayerAttack {
{player.position.x, player.position.y + 10},
false,
10,
}
player_attack_rect := rl.Rectangle {
player_attack.position.x,
player_attack.position.y,
40,
40,
}
check_if_enemy_hit(player^, &enemies, player_attack_rect)
player.current_state = .ATTACK_SOUTH
case .EAST:
player_attack := PlayerAttack {
{player.position.x + 10, player.position.y},
false,
10,
}
player_attack_rect := rl.Rectangle {
player_attack.position.x,
player_attack.position.y,
40,
40,
}
check_if_enemy_hit(player^, &enemies, player_attack_rect)
player.current_state = .ATTACK_EAST
case .WEST:
player_attack := PlayerAttack {
{player.position.x - 10, player.position.y},
false,
10,
}
player_attack_rect := rl.Rectangle {
player_attack.position.x,
player_attack.position.y,
40,
40,
}
check_if_enemy_hit(player^, &enemies, player_attack_rect)
player.current_state = .ATTACK_WEST
}
player.current_frame = 0
player.frame_counter = 0
}
}
}
draw_player :: proc(player: Player, animations: map[Animation_State]Animation) {
current_anim := animations[player.current_state]
frame_width := player.sprite_sheet.width / player.sprite_cols
frame_height := player.sprite_sheet.height / player.sprite_rows
source := rl.Rectangle {
x = f32(player.current_frame * frame_width),
y = f32(current_anim.row * frame_height),
height = f32(frame_height),
}
dest := rl.Rectangle {
x = player.position.x,
y = player.position.y,
width = f32(frame_width),
height = f32(frame_height),
}
// Adjust the source.width based on the player's direction
switch player.direction {
case .EAST:
source.width = f32(frame_width)
case .WEST:
source.width = -f32(frame_width)
case .NORTH, .SOUTH:
source.width = f32(frame_width)
}
color := player.can_be_hit ? rl.WHITE : rl.ColorAlpha(rl.RED, 0.7)
rl.DrawTexturePro(player.sprite_sheet, source, dest, rl.Vector2{0, 0}, 0, color)
}
check_if_hit :: proc(player: ^Player, enemies: [dynamic]Enemy) {
if !player.can_be_hit do return
hitbox := rl.Rectangle {
player.position.x,
player.position.y,
f32(player.sprite_sheet.width / player.sprite_cols),
f32(player.sprite_sheet.height / player.sprite_rows),
}
KNOCKBACK_FORCE :: f32(6.0)
for enemy in enemies {
if rl.CheckCollisionPointRec(enemy.position, hitbox) {
player.hp -= 1
player.can_be_hit = false
player.hit_cooldown = 1.0
player.is_knocked_back = true
if enemy.direction == .NORTH {
direction := rl.Vector2{0, -10}
direction = rl.Vector2Normalize(direction)
player.knockback_velocity = direction * KNOCKBACK_FORCE
return
}
if enemy.direction == .SOUTH {
direction := rl.Vector2{0, 10}
direction = rl.Vector2Normalize(direction)
player.knockback_velocity = direction * KNOCKBACK_FORCE
return
}
if enemy.direction == .EAST {
direction := rl.Vector2{10, 0}
direction = rl.Vector2Normalize(direction)
player.knockback_velocity = direction * KNOCKBACK_FORCE
return
}
if enemy.direction == .WEST {
direction := rl.Vector2{-10, 0}
direction = rl.Vector2Normalize(direction)
player.knockback_velocity = direction * KNOCKBACK_FORCE
return
}
// Calculate direction from enemy to player
}
}
}
update_hit_cooldown :: proc(player: ^Player) {
if !player.can_be_hit {
player.hit_cooldown -= rl.GetFrameTime()
if player.hit_cooldown <= 0 {
player.can_be_hit = true
player.hit_cooldown = 0
}
}
}