Align patterns better around what UI components are. Move script related controllers to scripts folder
This commit is contained in:
5
scripts/controllers/game/modes/8_ball.gd
Normal file
5
scripts/controllers/game/modes/8_ball.gd
Normal file
@@ -0,0 +1,5 @@
|
||||
class_name Ball8 extends LastBallMode
|
||||
|
||||
|
||||
func _init() -> void:
|
||||
final_ball_name = "08_ball"
|
||||
1
scripts/controllers/game/modes/8_ball.gd.uid
Normal file
1
scripts/controllers/game/modes/8_ball.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bruauynfkrqvv
|
||||
6
scripts/controllers/game/modes/8_ball.tscn
Normal file
6
scripts/controllers/game/modes/8_ball.tscn
Normal 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")
|
||||
5
scripts/controllers/game/modes/9_ball.gd
Normal file
5
scripts/controllers/game/modes/9_ball.gd
Normal file
@@ -0,0 +1,5 @@
|
||||
class_name Ball9 extends LastBallMode
|
||||
|
||||
|
||||
func _init() -> void:
|
||||
final_ball_name = "09_ball"
|
||||
1
scripts/controllers/game/modes/9_ball.gd.uid
Normal file
1
scripts/controllers/game/modes/9_ball.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://da6061q2oytcw
|
||||
6
scripts/controllers/game/modes/9_ball.tscn
Normal file
6
scripts/controllers/game/modes/9_ball.tscn
Normal 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")
|
||||
176
scripts/controllers/game/modes/base_mode.gd
Normal file
176
scripts/controllers/game/modes/base_mode.gd
Normal 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
|
||||
1
scripts/controllers/game/modes/base_mode.gd.uid
Normal file
1
scripts/controllers/game/modes/base_mode.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://m2mab84reiuq
|
||||
13
scripts/controllers/game/modes/free_ball.gd
Normal file
13
scripts/controllers/game/modes/free_ball.gd
Normal 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
|
||||
1
scripts/controllers/game/modes/free_ball.gd.uid
Normal file
1
scripts/controllers/game/modes/free_ball.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://d3lmdgym2didk
|
||||
6
scripts/controllers/game/modes/free_ball.tscn
Normal file
6
scripts/controllers/game/modes/free_ball.tscn
Normal 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")
|
||||
29
scripts/controllers/game/modes/game_manager.gd
Normal file
29
scripts/controllers/game/modes/game_manager.gd
Normal 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()))
|
||||
1
scripts/controllers/game/modes/game_manager.gd.uid
Normal file
1
scripts/controllers/game/modes/game_manager.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://csrolkxqgy5cj
|
||||
6
scripts/controllers/game/modes/game_manager.tscn
Normal file
6
scripts/controllers/game/modes/game_manager.tscn
Normal 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")
|
||||
25
scripts/controllers/game/modes/game_manager_base.gd
Normal file
25
scripts/controllers/game/modes/game_manager_base.gd
Normal 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()
|
||||
1
scripts/controllers/game/modes/game_manager_base.gd.uid
Normal file
1
scripts/controllers/game/modes/game_manager_base.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://cigjgffk40ppg
|
||||
17
scripts/controllers/game/modes/game_manager_multiplayer.gd
Normal file
17
scripts/controllers/game/modes/game_manager_multiplayer.gd
Normal 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()
|
||||
@@ -0,0 +1 @@
|
||||
uid://mq80ifv83gcx
|
||||
18
scripts/controllers/game/modes/last_ball_mode.gd
Normal file
18
scripts/controllers/game/modes/last_ball_mode.gd
Normal 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
|
||||
1
scripts/controllers/game/modes/last_ball_mode.gd.uid
Normal file
1
scripts/controllers/game/modes/last_ball_mode.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://rkwfbkek6ypo
|
||||
44
scripts/controllers/game/modes/snooker.gd
Normal file
44
scripts/controllers/game/modes/snooker.gd
Normal 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()
|
||||
1
scripts/controllers/game/modes/snooker.gd.uid
Normal file
1
scripts/controllers/game/modes/snooker.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://cnjaupry2q5aj
|
||||
6
scripts/controllers/game/modes/snooker.tscn
Normal file
6
scripts/controllers/game/modes/snooker.tscn
Normal 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")
|
||||
Reference in New Issue
Block a user