diff --git a/src/animator.odin b/src/animator.odin new file mode 100644 index 0000000..72c83c5 --- /dev/null +++ b/src/animator.odin @@ -0,0 +1,58 @@ +package game + +import rl "vendor:raylib" + +AnimationClip :: struct { + texture: rl.Texture2D, + frame_count: int, + frame_width: i32, + frame_height: i32, + frame_time: f32, + looping: bool, +} + +AnimationState :: struct { + clip: ^AnimationClip, + current_frame: int, + timer: f32, + finished: bool, +} + +animation_play :: proc(state: ^AnimationState, clip: ^AnimationClip) { + // already playing, do not reset + if state.clip == clip do return + state.clip = clip + state.current_frame = 0 + state.timer = 0 + state.finished = false +} + +animation_update :: proc(state: ^AnimationState, dt: f32) { + clip := state.clip + if clip == nil || state.finished do return + + state.timer += dt + if state.timer >= clip.frame_time { + state.timer -= clip.frame_time + state.current_frame += 1 + if state.current_frame >= clip.frame_count { + if clip.looping { + state.current_frame = 0 + } else { + state.current_frame = clip.frame_count - 1 + state.finished = true + } + } + } +} + +animation_frame_rect :: proc(state: ^AnimationState) -> rl.Rectangle { + clip := state.clip + + return rl.Rectangle { + x = f32(state.current_frame) * f32(clip.frame_width), + y = 0, + width = f32(clip.frame_width), + height = f32(clip.frame_height), + } +} diff --git a/src/asset_manager.odin b/src/asset_manager.odin index 83c5687..24716a7 100644 --- a/src/asset_manager.odin +++ b/src/asset_manager.odin @@ -6,22 +6,34 @@ // ----------------------------------------------------------------------------- package game -// import rl "vendor:raylib" +import rl "vendor:raylib" + +Player_Assets :: struct { + clips: [Player_State]AnimationClip, +} Asset_Manager :: struct { - // Empty for now. - //player: rl.Texture2D, + player: Player_Assets, } asset_manager_init :: proc() -> Asset_Manager { assets := Asset_Manager{} - // assets.player = rl.LoadTexture("assets/sprites/player.png") + idle_tex := rl.LoadTexture("assets/textures/player/player_idle.png") + walk_tex := rl.LoadTexture("assets/textures/player/player_walk.png") + + assets.player.clips[.IDLE] = AnimationClip { + texture = idle_tex, frame_count = 6, frame_width = 32, frame_height = 32, frame_time = 0.14, looping = true, + } + assets.player.clips[.WALK] = AnimationClip { + texture = walk_tex, frame_count = 8, frame_width = 32, frame_height = 32, frame_time = 0.14, looping = true, + } return assets } asset_manager_destroy :: proc(assets: ^Asset_Manager) { - // unload textures here later. - //rl.UnloadTexture(assets.player) + for clip in assets.player.clips { + rl.UnloadTexture(clip.texture) + } } \ No newline at end of file