107 lines
3.7 KiB
Odin
107 lines
3.7 KiB
Odin
package game
|
|
|
|
import "core:fmt"
|
|
import rl "vendor:raylib"
|
|
|
|
// ------------------------------------------------------------------------------
|
|
// LEVEL specifics
|
|
// ------------------------------------------------------------------------------
|
|
FIELD_BORDER :: 3
|
|
SERVE_WAIT_TIME :: 1.0
|
|
|
|
Level :: struct {
|
|
field_bounds: rl.Rectangle,
|
|
player: Entity,
|
|
opponent: Entity,
|
|
ball: Entity,
|
|
serve_timer: f32,
|
|
wait_time: f32,
|
|
score_player: int,
|
|
score_enemy: int,
|
|
score_win: int,
|
|
}
|
|
|
|
init_level :: proc() -> Level {
|
|
// positions work off viewport resolution,
|
|
// independent of target display size
|
|
// 10% padding around the field
|
|
pad_x := f32(VIEWPORT_W) * 0.10
|
|
pad_y := f32(VIEWPORT_H) * 0.10
|
|
|
|
bounds := rl.Rectangle{
|
|
x = pad_x,
|
|
y = pad_y,
|
|
width = f32(VIEWPORT_W) - (pad_x * 2),
|
|
height = f32(VIEWPORT_H) - (pad_y * 2),
|
|
}
|
|
|
|
player_ent := init_player()
|
|
player_ent.pos.x = bounds.x + FIELD_BORDER
|
|
player_ent.pos.y = bounds.y + (bounds.height / 2) - (player_ent.size.y / 2)
|
|
|
|
opponent_ent := init_opponent()
|
|
opponent_ent.pos.x = (bounds.x + bounds.width) - (opponent_ent.size.x) - FIELD_BORDER
|
|
opponent_ent.pos.y = bounds.y + (bounds.height / 2) - (opponent_ent.size.y / 2)
|
|
|
|
return Level{
|
|
field_bounds = bounds,
|
|
player = player_ent,
|
|
opponent = opponent_ent,
|
|
ball = init_ball(),
|
|
serve_timer = SERVE_WAIT_TIME,
|
|
wait_time = SERVE_WAIT_TIME,
|
|
score_win = 1,
|
|
score_player = 0,
|
|
score_enemy = 0,
|
|
}
|
|
}
|
|
|
|
update_level :: proc(level: ^Level, input: Input_Event, dt: f32) {
|
|
update_ball(&level.ball, level, dt)
|
|
update_player(&level.player, input, level.field_bounds, dt)
|
|
update_opponent(&level.opponent, level.ball, level.field_bounds, dt)
|
|
}
|
|
|
|
render_level :: proc(level: Level) {
|
|
rl.DrawRectangleRec(level.field_bounds, rl.DARKGRAY)
|
|
rl.DrawRectangleLinesEx(level.field_bounds, FIELD_BORDER, rl.RAYWHITE)
|
|
|
|
center_x := f32(VIEWPORT_W) / 2.0
|
|
start_pos := rl.Vector2{center_x, level.field_bounds.y + FIELD_BORDER}
|
|
end_pos := rl.Vector2{center_x, level.field_bounds.y + level.field_bounds.height - FIELD_BORDER}
|
|
rl.DrawLineEx(start_pos, end_pos, FIELD_BORDER, rl.WHITE)
|
|
|
|
render_player(level.player, level.field_bounds)
|
|
render_opponent(level.opponent, level.field_bounds)
|
|
render_ball(level.ball, level.field_bounds)
|
|
}
|
|
|
|
render_hud :: proc(level: Level) {
|
|
// 2 spacing, 20 font size, 5 padding
|
|
font := rl.GetFontDefault()
|
|
|
|
// player one id
|
|
p1_size := rl.MeasureTextEx(font, "PLAYER 1", 20, 2)
|
|
p1_box := rl.Rectangle{5, 5, p1_size.x, p1_size.y}
|
|
p1_pos := rl.Vector2{p1_box.x, p1_box.y}
|
|
rl.DrawTextEx(font, "PLAYER 1", p1_pos, 20, 2, rl.WHITE)
|
|
// rl.DrawRectangleLinesEx(p1_box, 1.0, rl.RED)
|
|
|
|
// opponent id
|
|
opp_size := rl.MeasureTextEx(font, "COMPUTER", 20, 2)
|
|
opp_box := rl.Rectangle{(VIEWPORT_W - 5 - opp_size.x), 5, (opp_size.x), (opp_size.y)}
|
|
opp_pos := rl.Vector2{opp_box.x, opp_box.y}
|
|
rl.DrawTextEx(font, "COMPUTER", opp_pos, 20, 2, rl.WHITE)
|
|
|
|
// score box
|
|
// Format the score dynamically into a C-string allocated on the temp allocator
|
|
score_text := fmt.ctprintf("%d : %d", level.score_player, level.score_enemy)
|
|
score_size := rl.MeasureTextEx(font, score_text, 20, 2)
|
|
score_box := rl.Rectangle{(VIEWPORT_W / 2 - score_size.x / 2), 5, (score_size.x), (score_size.y)}
|
|
score_pos := rl.Vector2{score_box.x, score_box.y}
|
|
rl.DrawTextEx(font, score_text, score_pos, 20, 2, rl.WHITE)
|
|
|
|
// game name
|
|
// rl.DrawRectangleLines(VIEWPORT_W / 2 - 50, VIEWPORT_H - 50, 100, 50, rl.RED)
|
|
}
|