Align patterns better around what UI components are. Move script related controllers to scripts folder

This commit is contained in:
2026-08-15 14:33:53 -05:00
parent 27411ea5fa
commit 2b196455b2
57 changed files with 128 additions and 60 deletions

View File

@@ -0,0 +1,5 @@
class_name Ball8 extends LastBallMode
func _init() -> void:
final_ball_name = "08_ball"

View File

@@ -0,0 +1 @@
uid://bruauynfkrqvv

View File

@@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://b6hjxh7luqvj8"]
[ext_resource type="Script" uid="uid://bruauynfkrqvv" path="res://scripts/controllers/game/modes/8_ball.gd" id="1_6i6et"]
[node name="8-ball" type="Node"]
script = ExtResource("1_6i6et")

View File

@@ -0,0 +1,5 @@
class_name Ball9 extends LastBallMode
func _init() -> void:
final_ball_name = "09_ball"

View File

@@ -0,0 +1 @@
uid://da6061q2oytcw

View File

@@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://cej6o2ix55hu6"]
[ext_resource type="Script" uid="uid://da6061q2oytcw" path="res://scripts/controllers/game/modes/9_ball.gd" id="1_4m6hi"]
[node name="9-ball" type="Node"]
script = ExtResource("1_4m6hi")

View File

@@ -0,0 +1,176 @@
class_name BaseMode extends Node
# NOTE: The ball's queue_free is called higher up and after game mode handles
# the ball. Never call it in a given game mode. A game mode should handle
# 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
# NOTE: Kinda a super tracker of balls. Not all entries
# generated are used depending on the game type.
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 = []
# 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,
"is_foul": false,
"points": 0,
}
# NOTE: Override when a mode doesn't award points for a sunk balls.
func score_sunk_balls() -> bool:
return true
# NOTE: Override when a mode shouldn't keep the player's turn after a legal sink.
func keep_turn_after_sink() -> bool:
return true
func get_player() -> String:
return (
"Player 1"
if Globals.game_data.client1 ==
Globals.multiplayer_data.current_turn_player_id
else
"Player 2"
)
# NOTE: Handled before 'handle_switch_user' for early bail out if invalid reap.
func reap_ball(ball: Node3D) -> void:
_record_ball(ball, "reaped", balls_reaped)
on_ball_reaped(ball)
# NOTE: Override when a mode has extra processing such as
# checking if solid or striped ball. Or 8-ball not sunk last.
func on_ball_reaped(_ball: Node3D) -> void:
pass
# NOTE: Handled in 'handle_switch_user'
func _process_reaped_balls(player: String) -> void:
for ball_name in balls_reaped:
_add_points(player, -ball_states[ball_name]["points"])
balls_reaped.clear()
# NOTE: Handled before 'handle_switch_user' for early bail out if invalid sink.
func process_ball(ball: Node3D) -> void:
_record_ball(ball, "sunk", balls_sunk)
on_ball_sunk(ball)
# NOTE: Override when a mode has extra processing such as
# checking if solid or striped ball. Or 8-ball not sunk last.
func on_ball_sunk(_ball: Node3D) -> void:
pass
# NOTE: Handled in 'handle_switch_user'
func _process_sunk_balls(player: String) -> void:
for ball_name in balls_sunk:
_add_points(player, ball_states[ball_name]["points"])
balls_sunk.clear()
func _add_points(player: String, amount: int) -> void:
if player == "Player 1":
player_1_points += amount
else:
player_2_points += amount
func _record_ball(
ball: Node3D,
state_key: String,
tracker: Array
) -> void:
tracker.append(ball.name)
ball_states[ball.name]["turn"] = turn_count
ball_states[ball.name]["player"] = get_player()
ball_states[ball.name][state_key] = true
func generate_ball_states() -> Dictionary:
var state: Dictionary = {}
for i: int in range(1, 16):
var ball_name = "%02d_ball" % i
state[ball_name] = BALL_STATE.duplicate_deep()
state[ball_name]["points"] = i
return state
# NOTE: For testing end state. Keep below commented out.
#func generate_ball_states() -> Dictionary:
#var state: Dictionary = {}
#
#for i: int in range(1, 16):
#var ball_name = "%02d_ball" % i
#state[ball_name] = BALL_STATE.duplicate_deep()
#state[ball_name]["turn"] = i
#state[ball_name]["player"] = "Player " + str( 1 if randi() % 2 else 2 )
#state[ball_name]["sunk"] = true
#state[ball_name]["is_foul"] = (randi() % 10 == 0)
#state[ball_name]["points"] = i
#
#return state
# End for testing end state. Keep above commented out.
func handle_switch_user() -> void:
push_warning("Balls reaped: ", balls_reaped.size() )
push_warning("Balls sunk: ", balls_sunk.size() )
var player: String = get_player()
var no_sunk_balls: bool = balls_sunk.is_empty()
_process_reaped_balls(player)
if score_sunk_balls():
_process_sunk_balls(player)
if ball_states_finalized():
process_win_condition()
return
if no_sunk_balls or not keep_turn_after_sink():
Globals.game_state.switch_active_player()
turn_count += 1
func ball_states_finalized() -> bool:
for ball_state in ball_states.values():
if ball_state["reaped"] or ball_state["sunk"]:
continue
return false
return true
func process_win_condition() -> String:
var sub_message: String = \
(
"Player "
+ str( 1 if player_1_points > player_2_points else 2 )
+ " Won!"
)
var message: String = \
(
"=================================================="
+ "\n" + Globals.game_data.get_mode_pretty() + " Game Over\n"
+ sub_message +
"\n=================================================="
)
push_warning(message)
MessageBus.emit_propagation_clients_only.rpc("declare_win_condition", message)
return message

View File

@@ -0,0 +1 @@
uid://m2mab84reiuq

View File

@@ -0,0 +1,13 @@
class_name FreeBall extends BaseMode
func _init() -> void:
pass
func process_win_condition() -> String:
return super()
#for ball_state in ball_states.values():
#if ball_state["reaped"] or ball_state["sunk"]:
#continue

View File

@@ -0,0 +1 @@
uid://d3lmdgym2didk

View File

@@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://c5jdawgemyqu8"]
[ext_resource type="Script" uid="uid://cnjaupry2q5aj" path="res://scripts/controllers/game/modes/snooker.gd" id="1_3fw5q"]
[node name="free_ball" type="Node"]
script = ExtResource("1_3fw5q")

View File

@@ -0,0 +1,29 @@
class_name GameManager extends GamManagerMultiplayer
func _ready() -> void:
setup_signals()
setup_multiplayer()
func setup_signals() -> void:
Globals.game_state.balls_stopped_moving.connect(_balls_stopped_moving)
Globals.game_state.reap_ball.connect(_reap_ball)
Globals.game_state.process_ball.connect(_process_ball)
func _reap_ball(ball: Node3D) -> void:
if ball.name == "white_ball":
Globals.game_state.white_ball_needs_hard_reset.emit()
return
mode.reap_ball(ball)
rpc("reap_ball", str(ball.get_path()))
func _process_ball(ball: Node3D) -> void:
if ball.name == "white_ball":
# NOTE: Signal to reset 'white_ball' to 'white_ball_marker' global_pos
Globals.game_state.white_ball_sunk.emit()
return
mode.process_ball(ball)
rpc("reap_ball", str(ball.get_path()))

View File

@@ -0,0 +1 @@
uid://csrolkxqgy5cj

View File

@@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://bw7f8kunlxd2"]
[ext_resource type="Script" uid="uid://csrolkxqgy5cj" path="res://scripts/controllers/game/modes/game_manager.gd" id="1_pwtrd"]
[node name="game_manager" type="Node"]
script = ExtResource("1_pwtrd")

View File

@@ -0,0 +1,25 @@
class_name GamManagerBase extends Node
@export var mode: BaseMode
@onready var pool_table_manager: PoolTableManager = $"../pool_table_manager"
func _balls_stopped_moving() -> void:
if pool_table_manager.white_ball_sunk or pool_table_manager.white_ball_hard_sunk:
pool_table_manager.initial_white_ball_start()
if pool_table_manager.white_ball_hard_sunk:
pool_table_manager.balls.white_ball.set_visible(true)
mode.handle_switch_user()
return
pool_table_manager.align_to_white_ball()
mode.handle_switch_user()
func set_mode(game_mode) -> void:
mode = Globals.game_data.GameMode.get(game_mode).new()
mode.ball_states = mode.generate_ball_states()

View File

@@ -0,0 +1 @@
uid://cigjgffk40ppg

View File

@@ -0,0 +1,17 @@
class_name GamManagerMultiplayer extends GamManagerBase
func setup_multiplayer() -> void:
if not Globals.multiplayer_data.is_multiplayer_active: return
if multiplayer.is_server(): return
Globals.game_state.balls_stopped_moving.disconnect(_balls_stopped_moving)
@rpc("authority", "call_local", "reliable")
func reap_ball(ball_pth: String) -> void:
var ball = get_node(ball_pth)
push_warning("Ball sunk: ", ball_pth)
Globals.multiplayer_data.remove_tracking_of_ball( ball_pth )
ball.queue_free()

View File

@@ -0,0 +1 @@
uid://mq80ifv83gcx

View File

@@ -0,0 +1,18 @@
class_name LastBallMode extends BaseMode
@export var final_ball_name: String = ""
func on_ball_reaped(ball: Node3D) -> void:
if ball.name == final_ball_name:
ball_states[ball.name]["is_foul"] = true
func on_ball_sunk(ball: Node3D) -> void:
if ball.name == final_ball_name:
ball_states[ball.name]["is_foul"] = true
func process_win_condition() -> String:
var message: String = final_ball_name + " Win!"
push_warning(message)
return message

View File

@@ -0,0 +1 @@
uid://rkwfbkek6ypo

View File

@@ -0,0 +1,44 @@
class_name Snooker extends BaseMode
# NOTE: The game is sometimes played with fewer red balls- commonly 6 or 10.
# Normally, 15 red balls (1 point) and 6 balls of different colors:
# yellow (2 points), green (3), brown (4), blue (5), pink (6), black (7).
const COLORED_BALLS := {
"yellow_ball": 2, "green_ball": 3, "brown_ball": 4,
"blue_ball": 5, "pink_ball": 6, "black_ball": 7,
}
func _init() -> void:
pass
func generate_ball_states() -> Dictionary:
var state: Dictionary = {}
for ball_name in COLORED_BALLS:
state[ball_name] = BALL_STATE.duplicate_deep()
state[ball_name]["points"] = COLORED_BALLS[ball_name]
for i: int in range(1, 16):
var ball_name := "%02d_ball" % i
state[ball_name] = BALL_STATE.duplicate_deep()
state[ball_name]["points"] = 1
return state
func on_ball_reaped(ball: Node3D) -> void:
if ball.name in COLORED_BALLS:
ball_states[ball.name]["is_foul"] = true
func on_ball_sunk(ball: Node3D) -> void:
if ball.name in COLORED_BALLS:
# TODO: Check whether a red ball has been sunk first.
ball_states[ball.name]["is_foul"] = true
func score_sunk_balls() -> bool:
return true
func process_win_condition() -> String:
return super()

View File

@@ -0,0 +1 @@
uid://cnjaupry2q5aj

View File

@@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://bh05rdwspf87c"]
[ext_resource type="Script" uid="uid://cnjaupry2q5aj" path="res://scripts/controllers/game/modes/snooker.gd" id="1_yq5ii"]
[node name="snooker" type="Node"]
script = ExtResource("1_yq5ii")

View File

@@ -0,0 +1,42 @@
class_name GameClient extends ClientNetworking
func _ready() -> void:
setup_signals()
func setup_signals() -> void:
MessageBus.subscribe("game_start_client", _start_client)
MessageBus.subscribe("game_wait_for_connection", _wait_for_connection)
MessageBus.subscribe("game_close_connection", _wait_close_connection)
MessageBus.subscribe("game_peer_disconnected", _on_game_peer_disconnected)
func _start_client(address: String = "127.0.0.1", port: int = 8080) -> void:
if peer: return
start_client(address, port)
func _on_game_peer_disconnected() -> void:
close_connection()
Globals.multiplayer_data.reset()
Globals.lobby_data.go_back_to_match_screen()
MessageBus.clear_voided_listeners()
func client_connected_to_server() -> void:
push_warning("Connected to server!")
func close_connection() -> void:
if not peer: return
push_warning("Disconnected from, server...")
unset_client()
func client_disconnected_from_server() -> void:
push_warning("Disconnected from, server...")
func client_connection_failed_to_server() -> void:
push_warning("Connection failed!")
unset_client()

View File

@@ -0,0 +1 @@
uid://cjtu1d1j35r32

View File

@@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://cbvv2msu13vr2"]
[ext_resource type="Script" uid="uid://cjtu1d1j35r32" path="res://scripts/controllers/networking/game_client.gd" id="1_tea1p"]
[node name="client_net" type="Node"]
script = ExtResource("1_tea1p")

View File

@@ -0,0 +1,31 @@
class_name GameServer extends ServerNetworking
func _ready() -> void:
setup_signals()
func setup_signals() -> void:
pass
func server_client_connected(id: int) -> void:
push_warning("Client Connected... ID: ", id)
if not Globals.game_data.client1:
Globals.game_data.client1 = id
elif not Globals.game_data.client2:
Globals.game_data.client2 = id
if Globals.game_data.client1 and Globals.game_data.client2:
Globals.multiplayer_data.init_match()
# TODO: Add watch match feature...
func server_client_disconnected(id: int) -> void:
push_warning("Client Disconnected... ID: ", id)
MessageBus.emit_propagation_clients_only.rpc("game_peer_disconnected", null)
await get_tree().create_timer(5.0).timeout
# NOTE: Quits game server instance.
get_tree().quit()

View File

@@ -0,0 +1 @@
uid://bndylf4rwtlkn

View File

@@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://bss61m8hfvab3"]
[ext_resource type="Script" uid="uid://bndylf4rwtlkn" path="res://scripts/controllers/networking/game_server.gd" id="1_3xapr"]
[node name="server_net" type="Node"]
script = ExtResource("1_3xapr")