movement and input handlin (delta time based distance still needed though)

This commit is contained in:
Morrígan 2025-06-26 22:19:33 +02:00
parent 8babb48fd2
commit 22d1469b0f
Signed by: morrigan
GPG key ID: CACB010F463A77D0
3 changed files with 33 additions and 13 deletions

View file

@ -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)
}

20
movement.odin Normal file
View file

@ -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
}

View file

@ -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,