movement and input handlin (delta time based distance still needed though)
This commit is contained in:
parent
8babb48fd2
commit
22d1469b0f
3 changed files with 33 additions and 13 deletions
23
main.odin
23
main.odin
|
@ -8,9 +8,10 @@ WIDTH :: 1600
|
||||||
HEIGHT :: 900
|
HEIGHT :: 900
|
||||||
|
|
||||||
Game :: struct {
|
Game :: struct {
|
||||||
render: ^sdl3.Renderer,
|
render: ^sdl3.Renderer,
|
||||||
units: [dynamic]Unit,
|
units: [dynamic]Unit,
|
||||||
player: Unit,
|
player: Unit,
|
||||||
|
input_state: map[sdl3.Scancode]bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
main :: proc() {
|
main :: proc() {
|
||||||
|
@ -28,11 +29,12 @@ main :: proc() {
|
||||||
units := make([dynamic]Unit)
|
units := make([dynamic]Unit)
|
||||||
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)
|
||||||
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},
|
||||||
|
input_state = input_state,
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
@ -41,18 +43,18 @@ main :: proc() {
|
||||||
#partial switch event.type {
|
#partial switch event.type {
|
||||||
case .QUIT:
|
case .QUIT:
|
||||||
return
|
return
|
||||||
|
case .KEY_UP:
|
||||||
|
HandleInput(&game, event)
|
||||||
case .KEY_DOWN:
|
case .KEY_DOWN:
|
||||||
{
|
HandleInput(&game, event)
|
||||||
fmt.println(event.key)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
MovePlayer(&game)
|
||||||
RenderGame(game)
|
RenderGame(&game)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
RenderGame :: proc(game: Game) {
|
RenderGame :: proc(game: ^Game) {
|
||||||
sdl3.SetRenderDrawColor(game.render, 0, 0, 0, 0)
|
sdl3.SetRenderDrawColor(game.render, 0, 0, 0, 0)
|
||||||
sdl3.RenderClear(game.render)
|
sdl3.RenderClear(game.render)
|
||||||
|
|
||||||
|
@ -63,5 +65,4 @@ RenderGame :: proc(game: Game) {
|
||||||
RenderUnit(game.player, game)
|
RenderUnit(game.player, game)
|
||||||
|
|
||||||
sdl3.RenderPresent(game.render)
|
sdl3.RenderPresent(game.render)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
20
movement.odin
Normal file
20
movement.odin
Normal 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
|
||||||
|
}
|
|
@ -14,7 +14,7 @@ Unit :: struct {
|
||||||
hp: u32,
|
hp: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
RenderUnit :: proc(unit: Unit, game: Game) {
|
RenderUnit :: proc(unit: Unit, game: ^Game) {
|
||||||
switch unit.faction {
|
switch unit.faction {
|
||||||
case Faction.Player:
|
case Faction.Player:
|
||||||
sdl3.SetRenderDrawColor(game.render, 0, 255, 0, 0)
|
sdl3.SetRenderDrawColor(game.render, 0, 255, 0, 0)
|
||||||
|
@ -29,7 +29,6 @@ RenderUnit :: proc(unit: Unit, game: Game) {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Projectile :: struct {
|
Projectile :: struct {
|
||||||
faction: Faction,
|
faction: Faction,
|
||||||
position: Position,
|
position: Position,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue