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": if ball.name == "08_ball":
# TODO: 8 ball must not be be destroyed. Handle rule... # TODO: 8 ball must not be be destroyed. Handle rule...
ball_state[ball.name]["valid_end"] = false ball_states[ball.name]["is_foul"] = true
pass
func process_ball(ball: Node3D) -> void: func process_ball(ball: Node3D) -> void:
super(ball) super(ball)
if ball.name == "08_ball": if ball.name == "08_ball":
# TODO: 8 ball must be sunk last. Handle rule... # TODO: 8 ball must be sunk last. Handle rule...
ball_state[ball.name]["valid_end"] = false ball_states[ball.name]["is_foul"] = true
pass
func handle_switch_user() -> void: func handle_switch_user() -> void:
push_warning("Balls reaped: ", balls_reaped.size() ) push_warning("Balls reaped: ", balls_reaped.size() )
push_warning("Balls sunk: ", balls_sunk.size() ) push_warning("Balls sunk: ", balls_sunk.size() )
var switch_player: bool = true
if not balls_reaped.is_empty(): 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() balls_reaped.clear()
if balls_sunk.is_empty(): if not balls_sunk.is_empty():
Globals.game_state.switch_active_player() # TODO: Handle points, etc, insure 8-ball not sunk unless last
turn_count += 1 # 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 return
# TODO: Handle points, etc if switch_player:
# Globals.multiplayer_data.current_turn_player_id Globals.game_state.switch_active_player()
balls_sunk.clear()
turn_count += 1 turn_count += 1
else:
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": if ball.name == "09_ball":
# TODO: 9 ball must not be be destroyed. Handle rule... # TODO: 9 ball must not be be destroyed. Handle rule...
ball_state[ball.name]["valid_end"] = false ball_states[ball.name]["is_foul"] = true
pass
func process_ball(ball: Node3D) -> void: func process_ball(ball: Node3D) -> void:
super(ball) super(ball)
if ball.name == "09_ball": if ball.name == "09_ball":
# TODO: 9 ball must be sunk last. Handle rule... # TODO: 9 ball must be sunk last. Handle rule...
ball_state[ball.name]["valid_end"] = false ball_states[ball.name]["is_foul"] = true
pass
func handle_switch_user() -> void: func handle_switch_user() -> void:
push_warning("Balls reaped: ", balls_reaped.size() ) push_warning("Balls reaped: ", balls_reaped.size() )
push_warning("Balls sunk: ", balls_sunk.size() ) push_warning("Balls sunk: ", balls_sunk.size() )
var switch_player: bool = true
if not balls_reaped.is_empty(): 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() balls_reaped.clear()
if balls_sunk.is_empty(): if not balls_sunk.is_empty():
Globals.game_state.switch_active_player() # TODO: Handle points, etc, insure 9-ball not sunk unless last.
turn_count += 1 # 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 return
# TODO: Handle points, etc if switch_player:
# Globals.multiplayer_data.current_turn_player_id Globals.game_state.switch_active_player()
balls_sunk.clear()
turn_count += 1 turn_count += 1
else:
turn_count += 1
func process_win_condition() -> void:
pass

View File

@@ -10,7 +10,7 @@ var player_2_points: int = 0
# NOTE: Kinda a super tracker of balls. Not all entries # NOTE: Kinda a super tracker of balls. Not all entries
# generated are used depending on the game type. # 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. # NOTE: Used to track handoff logic and is cleared after eah switch accordingly.
var balls_sunk: Array = [] var balls_sunk: Array = []
@@ -23,7 +23,7 @@ var BALL_STATE: Dictionary = {
"player": "", "player": "",
"sunk": false, "sunk": false,
"reaped": false, "reaped": false,
"valid_end": true, "is_foul": false,
"points": 0, "points": 0,
} }
@@ -31,7 +31,7 @@ var BALL_STATE: Dictionary = {
func _init() -> void: func _init() -> void:
pass pass
func generate_ball_state() -> Dictionary: func generate_ball_states() -> Dictionary:
var state := {} var state := {}
for i: int in range(1, 16): for i: int in range(1, 16):
@@ -41,8 +41,19 @@ func generate_ball_state() -> Dictionary:
return state 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 \ return \
"Player 1" \ "Player 1" \
if \ if \
@@ -53,16 +64,16 @@ func which_player() -> String:
func reap_ball(ball: Node3D) -> void: func reap_ball(ball: Node3D) -> void:
balls_reaped.append(ball.name) balls_reaped.append(ball.name)
ball_state[ball.name]["turn"] = turn_count ball_states[ball.name]["turn"] = turn_count
ball_state[ball.name]["player"] = which_player() ball_states[ball.name]["player"] = get_player()
ball_state[ball.name]["reaped"] = not ball_state[ball.name]["reaped"] ball_states[ball.name]["reaped"] = true
func process_ball(ball: Node3D) -> void: func process_ball(ball: Node3D) -> void:
balls_sunk.append(ball.name) balls_sunk.append(ball.name)
ball_state[ball.name]["turn"] = turn_count ball_states[ball.name]["turn"] = turn_count
ball_state[ball.name]["player"] = which_player() ball_states[ball.name]["player"] = get_player()
ball_state[ball.name]["sunk"] = not ball_state[ball.name]["sunk"] ball_states[ball.name]["sunk"] = true
func handle_switch_user() -> void: func handle_switch_user() -> void:
assert(false, "This method needs to be overridden...") 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: func handle_switch_user() -> void:
push_warning("Balls reaped: ", balls_reaped.size() ) push_warning("Balls reaped: ", balls_reaped.size() )
push_warning("Balls sunk: ", balls_sunk.size() ) push_warning("Balls sunk: ", balls_sunk.size() )
var switch_player: bool = true
if not balls_reaped.is_empty(): 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() balls_reaped.clear()
if balls_sunk.is_empty(): if not balls_sunk.is_empty():
Globals.game_state.switch_active_player() # TODO: Handle points, etc
turn_count += 1
switch_player = false
balls_sunk.clear()
if ball_states_finalized():
process_win_condition()
return return
# TODO: Handle points, etc if switch_player:
# Globals.multiplayer_data.current_turn_player_id Globals.game_state.switch_active_player()
balls_sunk.clear()
turn_count += 1 turn_count += 1
else:
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: func set_mode(game_mode) -> void:
mode = Globals.game_data.GameMode.get(game_mode).new() 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: func _init() -> void:
pass pass
func generate_ball_state() -> Dictionary: func generate_ball_states() -> Dictionary:
var state: Dictionary = {} var state: Dictionary = {}
for i in COLORED_BALLS.size(): for i in COLORED_BALLS.size():
var ball_name = COLORED_BALLS[i] var ball_name = COLORED_BALLS[i]
var points = COLORED_BALLS_POINTS[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 state[ball_name]["points"] = points
# NOTE: Red balls # NOTE: Red balls
@@ -34,7 +34,7 @@ func generate_ball_state() -> Dictionary:
state[ball_name] = BALL_STATE.duplicate_deep() state[ball_name] = BALL_STATE.duplicate_deep()
state[ball_name]["points"] = 1 state[ball_name]["points"] = 1
ball_state = state ball_states = state
return state return state
func is_colored_ball(ball_name: String) -> bool: func is_colored_ball(ball_name: String) -> bool:
@@ -45,8 +45,7 @@ func reap_ball(ball: Node3D) -> void:
if is_colored_ball(ball.name): if is_colored_ball(ball.name):
# TODO: Colored ball must not be be destroyed. Handle rule... # TODO: Colored ball must not be be destroyed. Handle rule...
ball_state[ball.name]["valid_end"] = false ball_states[ball.name]["is_foul"] = true
pass
func process_ball(ball: Node3D) -> void: func process_ball(ball: Node3D) -> void:
super(ball) super(ball)
@@ -56,24 +55,34 @@ func process_ball(ball: Node3D) -> void:
# Handle rule... # Handle rule...
var red_ball_sunk = true var red_ball_sunk = true
if not red_ball_sunk: if not red_ball_sunk:
pass ball_states[ball.name]["is_foul"] = true
pass
func handle_switch_user() -> void: func handle_switch_user() -> void:
push_warning("Balls reaped: ", balls_reaped.size() ) push_warning("Balls reaped: ", balls_reaped.size() )
push_warning("Balls sunk: ", balls_sunk.size() ) push_warning("Balls sunk: ", balls_sunk.size() )
var switch_player: bool = true
if not balls_reaped.is_empty(): 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() balls_reaped.clear()
if balls_sunk.is_empty(): if not balls_sunk.is_empty():
Globals.game_state.switch_active_player() # TODO: Handle points, etc, insure color ball not sunk before red ball
turn_count += 1
switch_player = false
balls_sunk.clear()
if ball_states_finalized():
process_win_condition()
return return
# TODO: Handle points, etc if switch_player:
# Globals.multiplayer_data.current_turn_player_id Globals.game_state.switch_active_player()
balls_sunk.clear()
turn_count += 1 turn_count += 1
else:
turn_count += 1
func process_win_condition() -> void:
pass

View File

@@ -22,6 +22,8 @@ func set_active_player_id(id: int) -> void:
push_warning("Server assigned active player: ", id) push_warning("Server assigned active player: ", id)
current_turn_player_id = id current_turn_player_id = id
MessageBus.emit_local("update_active_lbl", null)
func reset() -> void: func reset() -> void:
multiplayer_synchronizer = MultiplayerSynchronizer.new() multiplayer_synchronizer = MultiplayerSynchronizer.new()

View File

@@ -4,20 +4,19 @@ class_name CameraController extends Control
@onready var player_lbl: Label = $margin_container/vbox/hbox/hbox/player_lbl @onready var player_lbl: Label = $margin_container/vbox/hbox/hbox/player_lbl
@onready var prev_bttn: Button = $margin_container/vbox/hbox/hbox2/prev_bttn @onready var prev_bttn: Button = $margin_container/vbox/hbox/hbox2/prev_bttn
@onready var next_bttn: Button = $margin_container/vbox/hbox/hbox2/next_bttn @onready var next_bttn: Button = $margin_container/vbox/hbox/hbox2/next_bttn
@onready var reset_bttn: Button = $margin_container/vbox/reset_bttn @onready var active_lbl: Label = $margin_container/vbox/hbox2/hbox/active_lbl
@onready var reset_bttn: Button = $margin_container/vbox/hbox2/hbox2/reset_bttn
@export var cameras: Array[Camera3D] @export var cameras: Array[Camera3D]
var cue_cam: Camera3D var cue_cam: Camera3D
var current_cam: Camera3D var current_cam: Camera3D
var current_cam_index: int = 0 var current_cam_index: int = 0
func _ready() -> void: func _ready() -> void:
Globals.game_state.update_cue_cam_position.connect( setup_signals()
_on_update_cue_cam_position
)
setup_multiplayer() setup_multiplayer()
for camera in get_children(): for camera in get_children():
@@ -27,6 +26,14 @@ func _ready() -> void:
cue_cam = camera cue_cam = camera
current_cam = cameras.get(current_cam_index) current_cam = cameras.get(current_cam_index)
MessageBus.emit_local("update_active_lbl", null)
func setup_signals() -> void:
Globals.game_state.update_cue_cam_position.connect(
_on_update_cue_cam_position
)
MessageBus.subscribe("update_active_lbl", _on_update_active_lbl)
func setup_multiplayer() -> void: func setup_multiplayer() -> void:
player_lbl.text = Globals.game_data.player_id player_lbl.text = Globals.game_data.player_id
@@ -36,6 +43,10 @@ func setup_multiplayer() -> void:
reset_bttn.pressed.disconnect(_on_reset_bttn_pressed) reset_bttn.pressed.disconnect(_on_reset_bttn_pressed)
reset_bttn.visible = false reset_bttn.visible = false
func _on_update_active_lbl() -> void:
active_lbl.visible = \
Globals.multiplayer_data.current_turn_player_id == multiplayer.get_unique_id()
func _on_update_cue_cam_position(marker_rotation: Vector3, marker_position: Vector3) -> void: func _on_update_cue_cam_position(marker_rotation: Vector3, marker_position: Vector3) -> void:
cue_cam.global_rotation = marker_rotation cue_cam.global_rotation = marker_rotation
cue_cam.global_position = marker_position cue_cam.global_position = marker_position

View File

@@ -1,10 +1,13 @@
[gd_scene load_steps=3 format=3 uid="uid://w3vh1gj6t2k2"] [gd_scene load_steps=4 format=3 uid="uid://w3vh1gj6t2k2"]
[ext_resource type="Script" uid="uid://bpf107mmjiswh" path="res://scenes/menus/camera/camera_controller.gd" id="1_mvpa7"] [ext_resource type="Script" uid="uid://bpf107mmjiswh" path="res://scenes/menus/camera/camera_controller.gd" id="1_mvpa7"]
[sub_resource type="LabelSettings" id="LabelSettings_mvpa7"] [sub_resource type="LabelSettings" id="LabelSettings_mvpa7"]
font_size = 24 font_size = 24
[sub_resource type="LabelSettings" id="LabelSettings_duxs1"]
font_color = Color(0, 1, 0.14117648, 1)
[node name="camera_controller" type="Control"] [node name="camera_controller" type="Control"]
layout_mode = 3 layout_mode = 3
anchors_preset = 15 anchors_preset = 15
@@ -66,7 +69,27 @@ mouse_default_cursor_shape = 2
text = "<Next>" text = "<Next>"
flat = true flat = true
[node name="reset_bttn" type="Button" parent="margin_container/vbox"] [node name="hbox2" type="HBoxContainer" parent="margin_container/vbox"]
layout_mode = 2
size_flags_vertical = 3
[node name="hbox" type="HBoxContainer" parent="margin_container/vbox/hbox2"]
layout_mode = 2
size_flags_horizontal = 3
[node name="active_lbl" type="Label" parent="margin_container/vbox/hbox2/hbox"]
visible = false
layout_mode = 2
text = "Active"
label_settings = SubResource("LabelSettings_duxs1")
uppercase = true
[node name="hbox2" type="HBoxContainer" parent="margin_container/vbox/hbox2"]
clip_contents = true
custom_minimum_size = Vector2(149, 0)
layout_mode = 2
[node name="reset_bttn" type="Button" parent="margin_container/vbox/hbox2/hbox2"]
layout_mode = 2 layout_mode = 2
focus_mode = 0 focus_mode = 0
mouse_default_cursor_shape = 2 mouse_default_cursor_shape = 2
@@ -76,4 +99,4 @@ alignment = 0
[connection signal="pressed" from="margin_container/vbox/hbox/hbox2/prev_bttn" to="." method="_on_prev_bttn_pressed"] [connection signal="pressed" from="margin_container/vbox/hbox/hbox2/prev_bttn" to="." method="_on_prev_bttn_pressed"]
[connection signal="pressed" from="margin_container/vbox/hbox/hbox2/next_bttn" to="." method="_on_next_bttn_pressed"] [connection signal="pressed" from="margin_container/vbox/hbox/hbox2/next_bttn" to="." method="_on_next_bttn_pressed"]
[connection signal="pressed" from="margin_container/vbox/reset_bttn" to="." method="_on_reset_bttn_pressed"] [connection signal="pressed" from="margin_container/vbox/hbox2/hbox2/reset_bttn" to="." method="_on_reset_bttn_pressed"]