Created
June 19, 2024 21:43
-
-
Save p1xelHer0/008955694c563d8442445deffaf13c80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Pos :: [2]int | |
Component :: enum u16 { | |
PLAYER, | |
COLLISION, | |
PUSH, | |
SPRITE, | |
} | |
Entity :: struct { | |
id: u16, | |
components: bit_set[Component;u16], | |
position: Pos, | |
position_prev: Pos, | |
position_next: Maybe(Pos), | |
} | |
game_loop :: proc() { | |
for &ent in game.entities { | |
pos_next, is_moving := ent.position_next.? | |
if is_moving { | |
ent_resolve_movement( | |
from = ent.position, | |
to = pos_next, | |
entities = &game.entities, | |
entity = &ent, | |
) | |
} | |
ent.position_next = nil | |
} | |
} | |
ent_resolve_movement :: proc( | |
from, to: Pos, | |
entities: ^[dynamic]Entity, | |
entity: ^Entity, | |
) -> bool { | |
move := to - entity.position | |
colliding_ent, occupied := ent_get_at_pos(entities, to) | |
if occupied && .PUSH in colliding_ent.components { | |
pushed := ent_resolve_movement( | |
from = to, | |
to = to + move, | |
entities = &game.entities, | |
entity = colliding_ent, | |
) | |
if pushed { | |
entity.position_prev = entity.position | |
entity.position = to | |
return true | |
} | |
} else if occupied && .COLLISION in colliding_ent.components { | |
vfx_play_sound( | |
sound_bytes = assets.SOUND[.ATTACK_SWORD_1].bytes, | |
audio = &audio, | |
) | |
} else { | |
entity.position_prev = entity.position | |
entity.position = to | |
return true | |
} | |
return false | |
} | |
ent_get_at_pos :: proc( | |
entities: ^[dynamic]Entity, | |
position: Pos, | |
) -> ( | |
^Entity, | |
bool, | |
) { | |
for &ent in entities { | |
if ent.position == position { | |
return &ent, true | |
} | |
} | |
return {}, false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment