Initial simple event manager.
This commit is contained in:
@@ -2,7 +2,6 @@ package game
|
||||
|
||||
import "core:math/rand"
|
||||
import rl "vendor:raylib"
|
||||
import "core:log"
|
||||
|
||||
init_ball :: proc() -> Entity {
|
||||
return Entity{
|
||||
@@ -57,13 +56,13 @@ update_ball :: proc(ball: ^Entity, level: ^Level, dt: f32) {
|
||||
if ball.pos.y <= field_bounds.y + ball.size.x { ball.vel.y = -ball.vel.y }
|
||||
|
||||
if ball.pos.x >= (field_bounds.x + field_bounds.width) - ball.size.x {
|
||||
level.score_player += 1
|
||||
increment_score(&level.score_player, level.score_win, true)
|
||||
reset_ball(ball)
|
||||
level.serve_timer = level.wait_time
|
||||
}
|
||||
|
||||
if ball.pos.x <= field_bounds.x + ball.size.x {
|
||||
level.score_enemy += 1
|
||||
increment_score(&level.score_enemy, level.score_win, false)
|
||||
reset_ball(ball)
|
||||
level.serve_timer = level.wait_time
|
||||
}
|
||||
@@ -79,6 +78,12 @@ check_ball_paddle_collision :: proc(ball: Entity, paddle: Entity) -> bool {
|
||||
return rl.CheckCollisionCircleRec(ball.pos, ball.size.x, paddle_rec)
|
||||
}
|
||||
|
||||
increment_score :: proc(score_side: ^int, max_score: int, player: bool) {
|
||||
score_side^ += 1
|
||||
|
||||
if score_side^ == max_score do emit_event(Event{ .Game_Over, player })
|
||||
}
|
||||
|
||||
render_ball :: proc(ball: Entity, field_bounds: rl.Rectangle) {
|
||||
rl.DrawCircleV(ball.pos, ball.size.x, rl.MAROON)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package game
|
||||
|
||||
// all possible game events
|
||||
Event_Type :: enum {
|
||||
Game_Over,
|
||||
Score_Change,
|
||||
Ball_Collision,
|
||||
}
|
||||
|
||||
// optional details attached to an event
|
||||
Event :: struct {
|
||||
type: Event_Type,
|
||||
winner_is_player: bool,
|
||||
}
|
||||
|
||||
// global event storage
|
||||
GLOBAL_EVENTS: [dynamic]Event
|
||||
|
||||
emit_event :: proc(event: Event) {
|
||||
append(&GLOBAL_EVENTS, event)
|
||||
}
|
||||
|
||||
flush_events :: proc() {
|
||||
clear(&GLOBAL_EVENTS)
|
||||
}
|
||||
@@ -38,6 +38,9 @@ game_update :: proc(game: ^Game) {
|
||||
} else {
|
||||
scene_update(&game.scene, game, input, dt)
|
||||
}
|
||||
|
||||
// flush any events triggered in this loop
|
||||
flush_events()
|
||||
}
|
||||
|
||||
game_render :: proc(game: ^Game) {
|
||||
|
||||
Reference in New Issue
Block a user