feat(game): refactor ball state and turn handling

* Replace `valid_end` with `is_foul` for invalid ball sinks
* Rename and centralize ball state tracking
* Add ball finalization and win-condition hooks
* Update turn switching to retain the player after valid sinks
* Apply negative points to reaped balls
* Add active-player indicator to the camera UI
* Emit active-player updates when the turn changes
This commit is contained in:
2026-08-12 22:14:03 -05:00
parent 09369d3dd4
commit c95cc511e6
9 changed files with 166 additions and 77 deletions

View File

@@ -9,32 +9,43 @@ func reap_ball(ball: Node3D) -> void:
if ball.name == "08_ball":
# TODO: 8 ball must not be be destroyed. Handle rule...
ball_state[ball.name]["valid_end"] = false
pass
ball_states[ball.name]["is_foul"] = true
func process_ball(ball: Node3D) -> void:
super(ball)
if ball.name == "08_ball":
# TODO: 8 ball must be sunk last. Handle rule...
ball_state[ball.name]["valid_end"] = false
pass
ball_states[ball.name]["is_foul"] = true
func handle_switch_user() -> void:
push_warning("Balls reaped: ", balls_reaped.size() )
push_warning("Balls sunk: ", balls_sunk.size() )
var switch_player: bool = true
if not balls_reaped.is_empty():
# TODO: Handle points, etc
for ball_name in balls_reaped:
ball_states[ball_name]["points"] = -ball_states[ball_name]["points"]
balls_reaped.clear()
if balls_sunk.is_empty():
Globals.game_state.switch_active_player()
turn_count += 1
if not balls_sunk.is_empty():
# TODO: Handle points, etc, insure 8-ball not sunk unless last
# Insure we process stripped vs solid ball and apropriate points accordingly
# ball_states[ball.name]["is_foul"] = true
switch_player = false
balls_sunk.clear()
if ball_states_finalized():
process_win_condition()
return
# TODO: Handle points, etc
# Globals.multiplayer_data.current_turn_player_id
if switch_player:
Globals.game_state.switch_active_player()
turn_count += 1
else:
turn_count += 1
balls_sunk.clear()
turn_count += 1
func process_win_condition() -> void:
pass

View File

@@ -9,32 +9,43 @@ func reap_ball(ball: Node3D) -> void:
if ball.name == "09_ball":
# TODO: 9 ball must not be be destroyed. Handle rule...
ball_state[ball.name]["valid_end"] = false
pass
ball_states[ball.name]["is_foul"] = true
func process_ball(ball: Node3D) -> void:
super(ball)
if ball.name == "09_ball":
# TODO: 9 ball must be sunk last. Handle rule...
ball_state[ball.name]["valid_end"] = false
pass
ball_states[ball.name]["is_foul"] = true
func handle_switch_user() -> void:
push_warning("Balls reaped: ", balls_reaped.size() )
push_warning("Balls sunk: ", balls_sunk.size() )
var switch_player: bool = true
if not balls_reaped.is_empty():
# TODO: Handle points, etc
for ball_name in balls_reaped:
ball_states[ball_name]["points"] = -ball_states[ball_name]["points"]
balls_reaped.clear()
if balls_sunk.is_empty():
Globals.game_state.switch_active_player()
turn_count += 1
if not balls_sunk.is_empty():
# TODO: Handle points, etc, insure 9-ball not sunk unless last.
# Insure we process stripped vs solid ball and apropriate points accordingly.
# ball_states[ball.name]["is_foul"] = true
switch_player = false
balls_sunk.clear()
if ball_states_finalized():
process_win_condition()
return
# TODO: Handle points, etc
# Globals.multiplayer_data.current_turn_player_id
if switch_player:
Globals.game_state.switch_active_player()
turn_count += 1
else:
turn_count += 1
balls_sunk.clear()
turn_count += 1
func process_win_condition() -> void:
pass

View File

@@ -4,34 +4,34 @@ class_name BaseMode extends Node
# these signals to handle game related rules regarding ball sinks.
var turn_count: int = 1
var player_1_points: int = 0
var player_2_points: int = 0
var turn_count: int = 1
var player_1_points: int = 0
var player_2_points: int = 0
# NOTE: Kinda a super tracker of balls. Not all entries
# generated are used depending on the game type.
var ball_state: Dictionary = {}
var ball_states: Dictionary = {}
# NOTE: Used to track handoff logic and is cleared after eah switch accordingly.
var balls_sunk: Array = []
var balls_reaped: Array = []
var balls_sunk: Array = []
var balls_reaped: Array = []
# NOTE: Kinda a super tracker of the state of a ball.
# Not all entries are used depending on the game type.
var BALL_STATE: Dictionary = {
"turn": 0,
"player": "",
"sunk": false,
"reaped": false,
"valid_end": true,
"points": 0,
var BALL_STATE: Dictionary = {
"turn": 0,
"player": "",
"sunk": false,
"reaped": false,
"is_foul": false,
"points": 0,
}
func _init() -> void:
pass
func generate_ball_state() -> Dictionary:
func generate_ball_states() -> Dictionary:
var state := {}
for i: int in range(1, 16):
@@ -41,8 +41,19 @@ func generate_ball_state() -> Dictionary:
return state
func ball_states_finalized() -> bool:
for ball_state in ball_states.values():
if ball_state["reaped"] or ball_state["sunk"]:
continue
func which_player() -> String:
return false
return true
func process_win_condition() -> void:
assert(false, "This method needs to be overridden...")
func get_player() -> String:
return \
"Player 1" \
if \
@@ -53,16 +64,16 @@ func which_player() -> String:
func reap_ball(ball: Node3D) -> void:
balls_reaped.append(ball.name)
ball_state[ball.name]["turn"] = turn_count
ball_state[ball.name]["player"] = which_player()
ball_state[ball.name]["reaped"] = not ball_state[ball.name]["reaped"]
ball_states[ball.name]["turn"] = turn_count
ball_states[ball.name]["player"] = get_player()
ball_states[ball.name]["reaped"] = true
func process_ball(ball: Node3D) -> void:
balls_sunk.append(ball.name)
ball_state[ball.name]["turn"] = turn_count
ball_state[ball.name]["player"] = which_player()
ball_state[ball.name]["sunk"] = not ball_state[ball.name]["sunk"]
ball_states[ball.name]["turn"] = turn_count
ball_states[ball.name]["player"] = get_player()
ball_states[ball.name]["sunk"] = true
func handle_switch_user() -> void:
assert(false, "This method needs to be overridden...")

View File

@@ -13,18 +13,29 @@ func process_ball(ball: Node3D) -> void:
func handle_switch_user() -> void:
push_warning("Balls reaped: ", balls_reaped.size() )
push_warning("Balls sunk: ", balls_sunk.size() )
var switch_player: bool = true
if not balls_reaped.is_empty():
# TODO: Handle points, etc
for ball_name in balls_reaped:
ball_states[ball_name]["points"] = -ball_states[ball_name]["points"]
balls_reaped.clear()
if balls_sunk.is_empty():
Globals.game_state.switch_active_player()
turn_count += 1
if not balls_sunk.is_empty():
# TODO: Handle points, etc
switch_player = false
balls_sunk.clear()
if ball_states_finalized():
process_win_condition()
return
# TODO: Handle points, etc
# Globals.multiplayer_data.current_turn_player_id
if switch_player:
Globals.game_state.switch_active_player()
turn_count += 1
else:
turn_count += 1
balls_sunk.clear()
turn_count += 1
func process_win_condition() -> void:
pass

View File

@@ -22,4 +22,4 @@ func _balls_stopped_moving() -> void:
func set_mode(game_mode) -> void:
mode = Globals.game_data.GameMode.get(game_mode).new()
mode.ball_state = mode.generate_ball_state()
mode.ball_states = mode.generate_ball_states()

View File

@@ -18,14 +18,14 @@ var COLORED_BALLS_POINTS: Array[int] = [
func _init() -> void:
pass
func generate_ball_state() -> Dictionary:
func generate_ball_states() -> Dictionary:
var state: Dictionary = {}
for i in COLORED_BALLS.size():
var ball_name = COLORED_BALLS[i]
var points = COLORED_BALLS_POINTS[i]
state[ball_name] = BALL_STATE.duplicate_deep()
state[ball_name] = ball_states.duplicate_deep()
state[ball_name]["points"] = points
# NOTE: Red balls
@@ -34,7 +34,7 @@ func generate_ball_state() -> Dictionary:
state[ball_name] = BALL_STATE.duplicate_deep()
state[ball_name]["points"] = 1
ball_state = state
ball_states = state
return state
func is_colored_ball(ball_name: String) -> bool:
@@ -45,8 +45,7 @@ func reap_ball(ball: Node3D) -> void:
if is_colored_ball(ball.name):
# TODO: Colored ball must not be be destroyed. Handle rule...
ball_state[ball.name]["valid_end"] = false
pass
ball_states[ball.name]["is_foul"] = true
func process_ball(ball: Node3D) -> void:
super(ball)
@@ -56,24 +55,34 @@ func process_ball(ball: Node3D) -> void:
# Handle rule...
var red_ball_sunk = true
if not red_ball_sunk:
pass
pass
ball_states[ball.name]["is_foul"] = true
func handle_switch_user() -> void:
push_warning("Balls reaped: ", balls_reaped.size() )
push_warning("Balls sunk: ", balls_sunk.size() )
var switch_player: bool = true
if not balls_reaped.is_empty():
# TODO: Handle points, etc
for ball_name in balls_reaped:
ball_states[ball_name]["points"] = -ball_states[ball_name]["points"]
balls_reaped.clear()
if balls_sunk.is_empty():
Globals.game_state.switch_active_player()
turn_count += 1
if not balls_sunk.is_empty():
# TODO: Handle points, etc, insure color ball not sunk before red ball
switch_player = false
balls_sunk.clear()
if ball_states_finalized():
process_win_condition()
return
# TODO: Handle points, etc
# Globals.multiplayer_data.current_turn_player_id
if switch_player:
Globals.game_state.switch_active_player()
turn_count += 1
else:
turn_count += 1
balls_sunk.clear()
turn_count += 1
func process_win_condition() -> void:
pass