delta time!
This commit is contained in:
parent
22d1469b0f
commit
266adb051e
2 changed files with 22 additions and 8 deletions
13
main.odin
13
main.odin
|
@ -11,6 +11,8 @@ Game :: struct {
|
||||||
render: ^sdl3.Renderer,
|
render: ^sdl3.Renderer,
|
||||||
units: [dynamic]Unit,
|
units: [dynamic]Unit,
|
||||||
player: Unit,
|
player: Unit,
|
||||||
|
time: u64,
|
||||||
|
delta: f32,
|
||||||
input_state: map[sdl3.Scancode]bool,
|
input_state: map[sdl3.Scancode]bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,10 +32,16 @@ main :: proc() {
|
||||||
append(&units, Unit{faction = Faction.Allied, position = Position{x = 500, y = 500}, hp = 100})
|
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})
|
append(&units, Unit{faction = Faction.Enemy, position = Position{x = 700, y = 700}, hp = 100})
|
||||||
input_state := make(map[sdl3.Scancode]bool)
|
input_state := make(map[sdl3.Scancode]bool)
|
||||||
|
|
||||||
|
time := sdl3.GetTicksNS()
|
||||||
|
|
||||||
|
|
||||||
game := Game {
|
game := Game {
|
||||||
render = render,
|
render = render,
|
||||||
units = units,
|
units = units,
|
||||||
player = Unit{faction = Faction.Player, position = Position{x = 255, y = 255}, hp = 100},
|
player = Unit{faction = Faction.Player, position = Position{x = 255, y = 255}, hp = 100},
|
||||||
|
time = time,
|
||||||
|
delta = 0.0,
|
||||||
input_state = input_state,
|
input_state = input_state,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,4 +73,9 @@ RenderGame :: proc(game: ^Game) {
|
||||||
RenderUnit(game.player, game)
|
RenderUnit(game.player, game)
|
||||||
|
|
||||||
sdl3.RenderPresent(game.render)
|
sdl3.RenderPresent(game.render)
|
||||||
|
|
||||||
|
new_time := sdl3.GetTicksNS()
|
||||||
|
delta_ns := new_time - game.time
|
||||||
|
game.time = new_time
|
||||||
|
game.delta = f32(delta_ns) / f32(sdl3.NS_PER_SECOND)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,18 +3,19 @@ package main
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
import "vendor:sdl3"
|
import "vendor:sdl3"
|
||||||
|
|
||||||
|
SPEED: f32 : 200
|
||||||
|
|
||||||
HandleInput :: proc(game: ^Game, event: sdl3.Event) {
|
HandleInput :: proc(game: ^Game, event: sdl3.Event) {
|
||||||
effect := event.type == sdl3.EventType.KEY_DOWN ? true : false
|
effect := event.type == sdl3.EventType.KEY_DOWN ? true : false
|
||||||
game.input_state[event.key.scancode] = effect
|
game.input_state[event.key.scancode] = effect
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
MovePlayer :: proc(game: ^Game) {
|
MovePlayer :: proc(game: ^Game) {
|
||||||
x: f32 = game.input_state[sdl3.Scancode.W] ? 1 : 0
|
y: f32 = game.input_state[sdl3.Scancode.W] ? -SPEED : 0
|
||||||
x = game.input_state[sdl3.Scancode.S] ? -1 : 0
|
y += game.input_state[sdl3.Scancode.S] ? SPEED : 0
|
||||||
y: f32 = game.input_state[sdl3.Scancode.A] ? 1 : 0
|
x: f32 = game.input_state[sdl3.Scancode.A] ? -SPEED : 0
|
||||||
y = game.input_state[sdl3.Scancode.D] ? -1 : 0
|
x += game.input_state[sdl3.Scancode.D] ? SPEED : 0
|
||||||
fmt.println(x, y)
|
|
||||||
game.player.position.y += y
|
game.player.position.y += y * game.delta
|
||||||
game.player.position.x += x
|
game.player.position.x += x * game.delta
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue