Better naming. opponent > enemy
This commit is contained in:
+4
-2
@@ -9,7 +9,7 @@ import rl "vendor:raylib"
|
|||||||
|
|
||||||
Entity_Type :: enum {
|
Entity_Type :: enum {
|
||||||
Player,
|
Player,
|
||||||
Enemy,
|
Opponent,
|
||||||
Ball,
|
Ball,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,8 +29,10 @@ Entity :: struct {
|
|||||||
speed: f32,
|
speed: f32,
|
||||||
// 2D momentum & direction (ball / physics)
|
// 2D momentum & direction (ball / physics)
|
||||||
vel: rl.Vector2,
|
vel: rl.Vector2,
|
||||||
|
target_y: f32,
|
||||||
|
reaction_timer: f32
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------
|
||||||
// TODO: Entity Dispatchers
|
// @todo: entity dispatchers
|
||||||
// ------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package game
|
package game
|
||||||
|
|
||||||
import "core:math/rand"
|
import "core:math/rand"
|
||||||
import "core:log"
|
|
||||||
import rl "vendor:raylib"
|
import rl "vendor:raylib"
|
||||||
|
import "core:log"
|
||||||
|
|
||||||
init_ball :: proc() -> Entity {
|
init_ball :: proc() -> Entity {
|
||||||
return Entity{
|
return Entity{
|
||||||
@@ -35,12 +35,9 @@ update_ball :: proc(ball: ^Entity, level: ^Level, dt: f32) {
|
|||||||
if level.serve_timer <= 0 {
|
if level.serve_timer <= 0 {
|
||||||
serve_ball(ball)
|
serve_ball(ball)
|
||||||
}
|
}
|
||||||
|
|
||||||
// return early if timer still ticking
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// serve_ball(ball)
|
|
||||||
ball.pos += ball.vel * dt
|
ball.pos += ball.vel * dt
|
||||||
radius := ball.size.x
|
radius := ball.size.x
|
||||||
field_bounds := level.field_bounds
|
field_bounds := level.field_bounds
|
||||||
@@ -50,9 +47,9 @@ update_ball :: proc(ball: ^Entity, level: ^Level, dt: f32) {
|
|||||||
ball.vel.x = abs(ball.vel.x)
|
ball.vel.x = abs(ball.vel.x)
|
||||||
// stp stickiness
|
// stp stickiness
|
||||||
ball.pos.x = level.player.pos.x + level.player.size.x + radius
|
ball.pos.x = level.player.pos.x + level.player.size.x + radius
|
||||||
} else if check_ball_paddle_collision(ball^, level.enemy) {
|
} else if check_ball_paddle_collision(ball^, level.opponent) {
|
||||||
ball.vel.x = -abs(ball.vel.x)
|
ball.vel.x = -abs(ball.vel.x)
|
||||||
ball.pos.x = level.enemy.pos.x - radius
|
ball.pos.x = level.opponent.pos.x - radius
|
||||||
}
|
}
|
||||||
|
|
||||||
// bounds collision detection
|
// bounds collision detection
|
||||||
|
|||||||
+42
-9
@@ -1,5 +1,6 @@
|
|||||||
package game
|
package game
|
||||||
|
|
||||||
|
import "core:fmt"
|
||||||
import rl "vendor:raylib"
|
import rl "vendor:raylib"
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------
|
||||||
@@ -8,7 +9,7 @@ import rl "vendor:raylib"
|
|||||||
Level :: struct {
|
Level :: struct {
|
||||||
field_bounds: rl.Rectangle,
|
field_bounds: rl.Rectangle,
|
||||||
player: Entity,
|
player: Entity,
|
||||||
enemy: Entity,
|
opponent: Entity,
|
||||||
ball: Entity,
|
ball: Entity,
|
||||||
serve_timer: f32,
|
serve_timer: f32,
|
||||||
wait_time: f32,
|
wait_time: f32,
|
||||||
@@ -17,7 +18,7 @@ Level :: struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
FIELD_BORDER :: 3
|
FIELD_BORDER :: 3
|
||||||
SERVE_WAIT_TIME :: 0.5
|
SERVE_WAIT_TIME :: 1.0
|
||||||
|
|
||||||
init_level :: proc() -> Level {
|
init_level :: proc() -> Level {
|
||||||
// positions work off viewport resolution, independent of target display size
|
// positions work off viewport resolution, independent of target display size
|
||||||
@@ -36,14 +37,14 @@ init_level :: proc() -> Level {
|
|||||||
player_ent.pos.x = bounds.x + FIELD_BORDER
|
player_ent.pos.x = bounds.x + FIELD_BORDER
|
||||||
player_ent.pos.y = bounds.y + (bounds.height / 2) - (player_ent.size.y / 2)
|
player_ent.pos.y = bounds.y + (bounds.height / 2) - (player_ent.size.y / 2)
|
||||||
|
|
||||||
enemy_ent := init_enemy()
|
opponent_ent := init_opponent()
|
||||||
enemy_ent.pos.x = (bounds.x + bounds.width) - (enemy_ent.size.x) - FIELD_BORDER
|
opponent_ent.pos.x = (bounds.x + bounds.width) - (opponent_ent.size.x) - FIELD_BORDER
|
||||||
enemy_ent.pos.y = bounds.y + (bounds.height / 2) - (enemy_ent.size.y / 2)
|
opponent_ent.pos.y = bounds.y + (bounds.height / 2) - (opponent_ent.size.y / 2)
|
||||||
|
|
||||||
return Level{
|
return Level{
|
||||||
field_bounds = bounds,
|
field_bounds = bounds,
|
||||||
player = player_ent,
|
player = player_ent,
|
||||||
enemy = enemy_ent,
|
opponent = opponent_ent,
|
||||||
ball = init_ball(),
|
ball = init_ball(),
|
||||||
serve_timer = SERVE_WAIT_TIME,
|
serve_timer = SERVE_WAIT_TIME,
|
||||||
wait_time = SERVE_WAIT_TIME,
|
wait_time = SERVE_WAIT_TIME,
|
||||||
@@ -53,9 +54,9 @@ init_level :: proc() -> Level {
|
|||||||
}
|
}
|
||||||
|
|
||||||
update_level :: proc(level: ^Level, input: Input_Event, dt: f32) {
|
update_level :: proc(level: ^Level, input: Input_Event, dt: f32) {
|
||||||
update_player(&level.player, input, level.field_bounds, dt)
|
|
||||||
// update_enemy(&level.enemy, level.ball, level.field_bounds, dt)
|
|
||||||
update_ball(&level.ball, level, dt)
|
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) {
|
render_level :: proc(level: Level) {
|
||||||
@@ -68,6 +69,38 @@ render_level :: proc(level: Level) {
|
|||||||
rl.DrawLineEx(start_pos, end_pos, FIELD_BORDER, rl.WHITE)
|
rl.DrawLineEx(start_pos, end_pos, FIELD_BORDER, rl.WHITE)
|
||||||
|
|
||||||
render_player(level.player, level.field_bounds)
|
render_player(level.player, level.field_bounds)
|
||||||
render_enemy(level.enemy, level.field_bounds)
|
render_opponent(level.opponent, level.field_bounds)
|
||||||
render_ball(level.ball, 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)
|
||||||
|
// rl.DrawRectangleLinesEx(opp_box, 1.0, rl.RED)
|
||||||
|
|
||||||
|
// score box
|
||||||
|
// DYNAMIC 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)
|
||||||
|
// rl.DrawRectangleLinesEx(score_box, 1.0, rl.RED)
|
||||||
|
|
||||||
|
// game name
|
||||||
|
// rl.DrawRectangleLines(VIEWPORT_W / 2 - 50, VIEWPORT_H - 50, 100, 50, rl.RED)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user