diff --git a/src/game_entity.odin b/src/game_entity.odin index 99a134f..243b2a2 100644 --- a/src/game_entity.odin +++ b/src/game_entity.odin @@ -2,10 +2,10 @@ package game import rl "vendor:raylib" -/* -Entites are generic structures shared between enemies, environment items, player etc -Anything here is intended to be used with any entity some optional and some required -*/ +// ------------------------------------------------------------------------------ +// Generic base structure shared by all world objects. +// Fields are populated selectively based on entity type. +// ------------------------------------------------------------------------------ Entity_Type :: enum { Player, @@ -14,15 +14,23 @@ Entity_Type :: enum { } Shape_Type :: enum { - Rectangle, - Circle + Rectangle, + Circle, } Entity :: struct { - type: Entity_Type, - active: bool, - pos: rl.Vector2, - vel: rl.Vector2, - shape: Shape_Type, + type: Entity_Type, + active: bool, + pos: rl.Vector2, + shape: Shape_Type, shape_size: rl.Vector2, -} \ No newline at end of file + + // Fixed input speed (paddles) + speed: f32, + // 2D momentum & direction (ball / physics) + vel: rl.Vector2, +} + +// ------------------------------------------------------------------------------ +// TODO: Entity Dispatchers +// ------------------------------------------------------------------------------ diff --git a/src/game_entity_ball.odin b/src/game_entity_ball.odin new file mode 100644 index 0000000..f93c53c --- /dev/null +++ b/src/game_entity_ball.odin @@ -0,0 +1,20 @@ +package game + +import rl "vendor:raylib" + +init_ball :: proc() -> Entity { + ball_radius: f32 = 25 + + return Entity{ + type = .Ball, + active = true, + pos = rl.Vector2{(WINDOW_WIDTH / 2), (WINDOW_HEIGHT / 2) - (ball_radius / 2)}, + speed = 900, + shape = .Circle, + shape_size = rl.Vector2{25, 0} + } +} + +render_ball :: proc(ball: Entity, field_bounds: rl.Rectangle) { + rl.DrawCircleV(ball.pos, ball.shape_size.x, rl.MAROON) +} diff --git a/src/game_entity_enemy.odin b/src/game_entity_enemy.odin new file mode 100644 index 0000000..65a8006 --- /dev/null +++ b/src/game_entity_enemy.odin @@ -0,0 +1,18 @@ +package game + +import rl "vendor:raylib" + +init_enemy :: proc() -> Entity { + return Entity{ + type = .Enemy, + active = true, + pos = rl.Vector2{0,0}, + speed = 900, + shape = .Rectangle, + shape_size = rl.Vector2{50, 250} + } +} + +render_enemy :: proc(enemy: Entity, field_bounds: rl.Rectangle) { + rl.DrawRectangleV(enemy.pos, enemy.shape_size, rl.BLACK) +} diff --git a/src/game_entity_player.odin b/src/game_entity_player.odin index 86747a2..850cbc2 100644 --- a/src/game_entity_player.odin +++ b/src/game_entity_player.odin @@ -1,5 +1,6 @@ package game +import "core:math" import rl "vendor:raylib" init_player :: proc() -> Entity { @@ -7,24 +8,34 @@ init_player :: proc() -> Entity { type = .Player, active = true, pos = rl.Vector2{0,0}, - vel = rl.Vector2{450,0}, + speed = 900, shape = .Rectangle, shape_size = rl.Vector2{50, 250} } } -update_player :: proc(dt: f32, player: ^Entity) { - // Input - if rl.IsKeyDown(.W) do player.pos.y -= player.vel.x * dt - if rl.IsKeyDown(.S) do player.pos.y += player.vel.x * dt +update_player :: proc(dt: f32, player: ^Entity, level: ^Level) { + bounds : rl.Rectangle = level.field_bounds + + // 1. Gather Directional Input + move_dir: f32 = 0 + if rl.IsKeyDown(.W) || rl.IsKeyDown(.UP) { + move_dir -= 1.0 + } + if rl.IsKeyDown(.S) || rl.IsKeyDown(.DOWN) { + move_dir += 1.0 + } + + // 2. Apply Movement (Frame-rate independent) + player.pos.y += move_dir * player.speed * dt + + // 3. Clamp Position within Field Bounds + min_y := bounds.y + 3 // Padding offset inside border + max_y := bounds.y + bounds.height - player.shape_size.y - 3 + + player.pos.y = math.clamp(player.pos.y, min_y, max_y) } -render_player :: proc(player: Entity) { - rl.DrawRectangle( - i32(player.pos.x), - i32(player.pos.y), - i32(player.shape_size.x), - i32(player.shape_size.y), - rl.BLACK - ) +render_player :: proc(player: Entity, field_bounds: rl.Rectangle) { + rl.DrawRectangleV(player.pos, player.shape_size, rl.BLACK) } \ No newline at end of file