From 99c32198ec6c8b64e4ed65962ec789612657e327 Mon Sep 17 00:00:00 2001 From: Jason Hilder Date: Thu, 16 Jul 2026 07:49:47 +0200 Subject: [PATCH] Cleanup, rename player func and add map interaction. --- src/main.odin | 5 ++--- src/player.odin | 56 ++++++++++++++++++++++++++++++------------------- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/main.odin b/src/main.odin index 60919c1..4c4d372 100644 --- a/src/main.odin +++ b/src/main.odin @@ -3,7 +3,7 @@ package game import "core:log" import "core:mem" -// Configuration Variables +// Global Config Variables // ----------------------------------------------------------------------------- DEBUG_MODE :: true @@ -15,6 +15,7 @@ GAME_HEIGHT :: 270 WINDOW_TITLE :: "Graveyard" PLAYER_SPEED :: 180 TILE_SIZE :: 32 +// ----------------------------------------------------------------------------- main :: proc() { context.logger = log.create_console_logger() @@ -51,5 +52,3 @@ main :: proc() { } } } - - diff --git a/src/player.odin b/src/player.odin index 32a222b..6807c37 100644 --- a/src/player.odin +++ b/src/player.odin @@ -1,5 +1,6 @@ package game +import "core:fmt" import rl "vendor:raylib" Direction :: enum { LEFT, RIGHT } @@ -8,58 +9,69 @@ Player_State :: enum { IDLE, WALK, ATTACK } Player :: struct { pos: rl.Vector2, direction: Direction, - state: Player_State, + state: Player_State, ani_state: AnimationState, } player_init :: proc(assets: ^Player_Assets) -> Player { player := Player{} - // center for now not really needed - player.pos = {(WINDOW_WIDTH / 2), (WINDOW_HEIGHT / 2)} + player.pos = {320, 320} player.state = .IDLE animation_play(&player.ani_state, &assets.clips[.IDLE]) return player } -update_player :: proc(player: ^Player, assets: ^Player_Assets) { +update_player :: proc(player: ^Player, assets: ^Player_Assets, camera: rl.Camera2D) { moving := false dt := rl.GetFrameTime() if rl.IsKeyDown(.W) { player.pos.y -= PLAYER_SPEED * dt; moving = true } if rl.IsKeyDown(.S) { player.pos.y += PLAYER_SPEED * dt; moving = true } - if rl.IsKeyDown(.A) { - player.pos.x -= PLAYER_SPEED * dt + if rl.IsKeyDown(.A) { + player.pos.x -= PLAYER_SPEED * dt player.direction = .LEFT - moving = true + moving = true } - if rl.IsKeyDown(.D) { + if rl.IsKeyDown(.D) { player.pos.x += PLAYER_SPEED * dt player.direction = .RIGHT - moving = true + moving = true } + if rl.IsMouseButtonPressed(.LEFT) { + pixel_pos := rl.GetMousePosition() + //convert to render size + pixel_pos.x = pixel_pos.x / (WINDOW_WIDTH / GAME_WIDTH) + pixel_pos.y = pixel_pos.y / (WINDOW_HEIGHT / GAME_HEIGHT) + + grid_space := world_to_grid(pixel_pos, camera) + fmt.printf("GRID SPACE: %v\n", grid_space) + world_space := grid_to_world(grid_space[0], grid_space[1]) + fmt.printf("WORLD SPACE: %v\n", world_space) + } + player.state = moving ? .WALK : .IDLE animation_play(&player.ani_state, &assets.clips[player.state]) animation_update(&player.ani_state, dt) } -renderer_draw_player :: proc(player: ^Player) { - clip := player.ani_state.clip - src_rect := animation_frame_rect(&player.ani_state) +draw_player :: proc(player: ^Player) { + clip := player.ani_state.clip + src_rect := animation_frame_rect(&player.ani_state) - if player.direction == .LEFT { - src_rect.width = -src_rect.width - } + if player.direction == .LEFT { + src_rect.width = -src_rect.width + } - dst_rect := rl.Rectangle{ - x = player.pos.x - f32(clip.frame_width) / 2, - y = player.pos.y - f32(clip.frame_height) / 2, - width = f32(clip.frame_width), - height = f32(clip.frame_height), - } + dst_rect := rl.Rectangle{ + x = player.pos.x - f32(clip.frame_width) / 2, + y = player.pos.y - f32(clip.frame_height) / 2, + width = f32(clip.frame_width), + height = f32(clip.frame_height), + } - rl.DrawTexturePro(clip.texture, src_rect, dst_rect, rl.Vector2{}, 0, rl.WHITE) + rl.DrawTexturePro(clip.texture, src_rect, dst_rect, rl.Vector2{}, 0, rl.WHITE) }