From 22d1469b0f53fcad0e19a34d463f65bc5ff1bb00 Mon Sep 17 00:00:00 2001 From: morrigan Date: Thu, 26 Jun 2025 22:19:33 +0200 Subject: [PATCH] movement and input handlin (delta time based distance still needed though) --- main.odin | 23 ++++++++++++----------- movement.odin | 20 ++++++++++++++++++++ unit.odin | 3 +-- 3 files changed, 33 insertions(+), 13 deletions(-) create mode 100644 movement.odin diff --git a/main.odin b/main.odin index 0ca38e8..56742b8 100644 --- a/main.odin +++ b/main.odin @@ -8,9 +8,10 @@ WIDTH :: 1600 HEIGHT :: 900 Game :: struct { - render: ^sdl3.Renderer, - units: [dynamic]Unit, - player: Unit, + render: ^sdl3.Renderer, + units: [dynamic]Unit, + player: Unit, + input_state: map[sdl3.Scancode]bool, } main :: proc() { @@ -28,11 +29,12 @@ main :: proc() { units := make([dynamic]Unit) append(&units, Unit{faction = Faction.Allied, position = Position{x = 500, y = 500}, hp = 100}) append(&units, Unit{faction = Faction.Enemy, position = Position{x = 700, y = 700}, hp = 100}) - + input_state := make(map[sdl3.Scancode]bool) game := Game { render = render, units = units, player = Unit{faction = Faction.Player, position = Position{x = 255, y = 255}, hp = 100}, + input_state = input_state, } for { @@ -41,18 +43,18 @@ main :: proc() { #partial switch event.type { case .QUIT: return + case .KEY_UP: + HandleInput(&game, event) case .KEY_DOWN: - { - fmt.println(event.key) - } + HandleInput(&game, event) } } - - RenderGame(game) + MovePlayer(&game) + RenderGame(&game) } } -RenderGame :: proc(game: Game) { +RenderGame :: proc(game: ^Game) { sdl3.SetRenderDrawColor(game.render, 0, 0, 0, 0) sdl3.RenderClear(game.render) @@ -63,5 +65,4 @@ RenderGame :: proc(game: Game) { RenderUnit(game.player, game) sdl3.RenderPresent(game.render) - } diff --git a/movement.odin b/movement.odin new file mode 100644 index 0000000..66d7388 --- /dev/null +++ b/movement.odin @@ -0,0 +1,20 @@ +package main + +import "core:fmt" +import "vendor:sdl3" + +HandleInput :: proc(game: ^Game, event: sdl3.Event) { + effect := event.type == sdl3.EventType.KEY_DOWN ? true : false + game.input_state[event.key.scancode] = effect +} + + +MovePlayer :: proc(game: ^Game) { + x: f32 = game.input_state[sdl3.Scancode.W] ? 1 : 0 + x = game.input_state[sdl3.Scancode.S] ? -1 : 0 + y: f32 = game.input_state[sdl3.Scancode.A] ? 1 : 0 + y = game.input_state[sdl3.Scancode.D] ? -1 : 0 + fmt.println(x, y) + game.player.position.y += y + game.player.position.x += x +} diff --git a/unit.odin b/unit.odin index 5cad71e..1f8ca94 100644 --- a/unit.odin +++ b/unit.odin @@ -14,7 +14,7 @@ Unit :: struct { hp: u32, } -RenderUnit :: proc(unit: Unit, game: Game) { +RenderUnit :: proc(unit: Unit, game: ^Game) { switch unit.faction { case Faction.Player: sdl3.SetRenderDrawColor(game.render, 0, 255, 0, 0) @@ -29,7 +29,6 @@ RenderUnit :: proc(unit: Unit, game: Game) { ) } - Projectile :: struct { faction: Faction, position: Position,