Compare commits

..

No commits in common. "9fb75de9be0db9a78f568e73f2f374673f2ed52e" and "60d71ae1c050d92d1398bcb7b31f595729dd2017" have entirely different histories.

3 changed files with 47 additions and 48 deletions

View file

@ -30,19 +30,40 @@ main :: proc() {
assert(render != nil) assert(render != nil)
units := make([dynamic]Unit) units := make([dynamic]Unit)
append(&units, new_unit(Faction.Allied, util.Position{500, 500})) append(
append(&units, new_unit(Faction.Enemy, util.Position{700, 700})) &units,
Unit {
faction = Faction.Allied,
position = util.Position{x = 500, y = 500},
velocity = DEFAULT_VELOCITY,
hp = 100,
},
)
append(
&units,
Unit {
faction = Faction.Enemy,
position = util.Position{x = 700, y = 700},
velocity = DEFAULT_VELOCITY,
hp = 100,
},
)
input_state := make(map[sdl3.Scancode]bool) input_state := make(map[sdl3.Scancode]bool)
time := sdl3.GetTicksNS() time := sdl3.GetTicksNS()
game := Game { game := Game {
render = render, render = render,
units = units, units = units,
player = new_unit(Faction.Player, util.Position{100, 100}, 200), player = Unit {
time = time, faction = Faction.Player,
delta = 0.0, position = util.Position{x = 255, y = 255},
velocity = DEFAULT_VELOCITY,
hp = 100,
},
time = time,
delta = 0.0,
input_state = input_state, input_state = input_state,
} }
@ -53,33 +74,33 @@ main :: proc() {
case .QUIT: case .QUIT:
return return
case .KEY_UP: case .KEY_UP:
handle_input(&game, event) HandleInput(&game, event)
case .KEY_DOWN: case .KEY_DOWN:
handle_input(&game, event) HandleInput(&game, event)
} }
} }
move_player(&game) MovePlayer(&game)
render_game(&game) RenderGame(&game)
update_time(&game) UpdateTime(&game)
} }
} }
update_time :: proc(game: ^Game) { UpdateTime :: proc(game: ^Game) {
new_time := sdl3.GetTicksNS() new_time := sdl3.GetTicksNS()
delta_ns := new_time - game.time delta_ns := new_time - game.time
game.time = new_time game.time = new_time
game.delta = f32(delta_ns) / f32(sdl3.NS_PER_SECOND) game.delta = f32(delta_ns) / f32(sdl3.NS_PER_SECOND)
} }
render_game :: 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)
for unit in game.units { for unit in game.units {
render_unit(unit, game) RenderUnit(unit, game)
} }
render_unit(game.player, game) RenderUnit(game.player, game)
sdl3.RenderPresent(game.render) sdl3.RenderPresent(game.render)
} }

View file

@ -4,18 +4,19 @@ import "core:fmt"
import "util" import "util"
import "vendor:sdl3" import "vendor:sdl3"
handle_input :: proc(game: ^Game, event: sdl3.Event) { DEFAULT_VELOCITY: util.Velocity : util.Velocity{x = 0, y = 0}
SPEED: f32 : 200
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
} }
move_player :: proc(game: ^Game) { MovePlayer :: proc(game: ^Game) {
speed := game.player.speed y: f32 = game.input_state[sdl3.Scancode.W] ? -SPEED : 0
y += game.input_state[sdl3.Scancode.S] ? SPEED : 0
y: f32 = game.input_state[sdl3.Scancode.W] ? -speed : 0 x: f32 = game.input_state[sdl3.Scancode.A] ? -SPEED : 0
y += game.input_state[sdl3.Scancode.S] ? speed : 0 x += game.input_state[sdl3.Scancode.D] ? SPEED : 0
x: f32 = game.input_state[sdl3.Scancode.A] ? -speed : 0
x += game.input_state[sdl3.Scancode.D] ? speed : 0
game.player.velocity = util.Velocity{x, y} game.player.velocity = util.Velocity{x, y}
game.player.position.y += y * game.delta game.player.position.y += y * game.delta

View file

@ -3,10 +3,6 @@ package main
import "util" import "util"
import "vendor:sdl3" import "vendor:sdl3"
DEFAULT_HP :: 100
DEFAULT_VELOCITY: util.Velocity : util.Velocity{x = 0, y = 0}
DEFAULT_SPEED: f32 : 200
Faction :: enum { Faction :: enum {
Player, Player,
Allied, Allied,
@ -17,11 +13,10 @@ Unit :: struct {
faction: Faction, faction: Faction,
position: util.Position, position: util.Position,
velocity: util.Velocity, velocity: util.Velocity,
speed: f32,
hp: u32, hp: u32,
} }
render_unit :: 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)
@ -36,24 +31,6 @@ render_unit :: proc(unit: Unit, game: ^Game) {
) )
} }
new_unit :: proc {
new_unit_all,
new_unit_base,
new_unit_hp,
}
new_unit_base :: proc(faction: Faction, position: util.Position) -> Unit {
return Unit{faction, position, DEFAULT_VELOCITY, DEFAULT_SPEED, DEFAULT_HP}
}
new_unit_hp :: proc(faction: Faction, position: util.Position, hp: u32) -> Unit {
return Unit{faction, position, DEFAULT_VELOCITY, DEFAULT_SPEED, hp}
}
new_unit_all :: proc(faction: Faction, position: util.Position, speed: f32, hp: u32) -> Unit {
return Unit{faction, position, DEFAULT_VELOCITY, speed, hp}
}
Projectile :: struct { Projectile :: struct {
faction: Faction, faction: Faction,
position: util.Position, position: util.Position,