From 1a24af72307a095d5fb18176be58654c8249c755 Mon Sep 17 00:00:00 2001 From: morrigan Date: Sat, 28 Jun 2025 15:21:52 +0200 Subject: [PATCH] improve Unit and utilities --- main.odin | 35 +++++++---------------------------- movement.odin | 13 ++++++------- unit.odin | 23 +++++++++++++++++++++++ 3 files changed, 36 insertions(+), 35 deletions(-) diff --git a/main.odin b/main.odin index f19726e..6232f3c 100644 --- a/main.odin +++ b/main.odin @@ -30,40 +30,19 @@ main :: proc() { assert(render != nil) units := make([dynamic]Unit) - append( - &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, - }, - ) + append(&units, NewUnit(Faction.Allied, util.Position{500, 500})) + append(&units, NewUnit(Faction.Enemy, util.Position{700, 700})) input_state := make(map[sdl3.Scancode]bool) time := sdl3.GetTicksNS() game := Game { - render = render, - units = units, - player = Unit { - faction = Faction.Player, - position = util.Position{x = 255, y = 255}, - velocity = DEFAULT_VELOCITY, - hp = 100, - }, - time = time, - delta = 0.0, + render = render, + units = units, + player = NewUnit(Faction.Player, util.Position{100, 100}, 200), + time = time, + delta = 0.0, input_state = input_state, } diff --git a/movement.odin b/movement.odin index 2e75864..83ac987 100644 --- a/movement.odin +++ b/movement.odin @@ -4,19 +4,18 @@ import "core:fmt" import "util" import "vendor:sdl3" -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 game.input_state[event.key.scancode] = effect } MovePlayer :: proc(game: ^Game) { - y: f32 = game.input_state[sdl3.Scancode.W] ? -SPEED : 0 - y += game.input_state[sdl3.Scancode.S] ? SPEED : 0 - x: f32 = game.input_state[sdl3.Scancode.A] ? -SPEED : 0 - x += game.input_state[sdl3.Scancode.D] ? SPEED : 0 + speed := game.player.speed + + y: f32 = game.input_state[sdl3.Scancode.W] ? -speed : 0 + y += game.input_state[sdl3.Scancode.S] ? 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.position.y += y * game.delta diff --git a/unit.odin b/unit.odin index 3bc6d29..e4f4c03 100644 --- a/unit.odin +++ b/unit.odin @@ -3,6 +3,10 @@ package main import "util" import "vendor:sdl3" +DEFAULT_HP :: 100 +DEFAULT_VELOCITY: util.Velocity : util.Velocity{x = 0, y = 0} +DEFAULT_SPEED: f32 : 200 + Faction :: enum { Player, Allied, @@ -13,6 +17,7 @@ Unit :: struct { faction: Faction, position: util.Position, velocity: util.Velocity, + speed: f32, hp: u32, } @@ -31,6 +36,24 @@ RenderUnit :: proc(unit: Unit, game: ^Game) { ) } +NewUnit :: proc { + NewUnitBase, + NewUnitHp, + NewUnitAll +} + +NewUnitBase :: proc(faction: Faction, position: util.Position) -> Unit { + return Unit{faction, position, DEFAULT_VELOCITY, DEFAULT_SPEED, DEFAULT_HP} +} + +NewUnitHp :: proc(faction: Faction, position: util.Position, hp: u32) -> Unit { + return Unit{faction, position, DEFAULT_VELOCITY, DEFAULT_SPEED, hp} +} + +NewUnitAll :: proc(faction: Faction, position: util.Position, speed: f32, hp: u32) -> Unit { + return Unit{faction, position, DEFAULT_VELOCITY, speed, hp} +} + Projectile :: struct { faction: Faction, position: util.Position,