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")
|
||||
42
scripts/controllers/networking/game_client.gd
Normal file
42
scripts/controllers/networking/game_client.gd
Normal 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()
|
||||
1
scripts/controllers/networking/game_client.gd.uid
Normal file
1
scripts/controllers/networking/game_client.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://cjtu1d1j35r32
|
||||
6
scripts/controllers/networking/game_client.tscn
Normal file
6
scripts/controllers/networking/game_client.tscn
Normal 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")
|
||||
31
scripts/controllers/networking/game_server.gd
Normal file
31
scripts/controllers/networking/game_server.gd
Normal 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()
|
||||
1
scripts/controllers/networking/game_server.gd.uid
Normal file
1
scripts/controllers/networking/game_server.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bndylf4rwtlkn
|
||||
6
scripts/controllers/networking/game_server.tscn
Normal file
6
scripts/controllers/networking/game_server.tscn
Normal 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")
|
||||
20
scripts/data/data_bridge.tscn
Normal file
20
scripts/data/data_bridge.tscn
Normal file
@@ -0,0 +1,20 @@
|
||||
[gd_scene load_steps=5 format=3 uid="uid://dy51wny4x53f4"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://52eithlmedm3" path="res://scripts/data/game_data.gd" id="1_hulqm"]
|
||||
[ext_resource type="Script" uid="uid://dvw46rvml3ec" path="res://scripts/data/game_state.gd" id="2_6ash1"]
|
||||
[ext_resource type="Script" uid="uid://6sfwe1hwgf0g" path="res://scripts/data/lobby_data.gd" id="3_80q4n"]
|
||||
[ext_resource type="Script" uid="uid://bs4vblwa3u5jq" path="res://scripts/data/multiplayer_data.gd" id="4_setx2"]
|
||||
|
||||
[node name="data_bridge" type="Node"]
|
||||
|
||||
[node name="game_data" type="Node" parent="."]
|
||||
script = ExtResource("1_hulqm")
|
||||
|
||||
[node name="game_state" type="Node" parent="."]
|
||||
script = ExtResource("2_6ash1")
|
||||
|
||||
[node name="lobby_data" type="Node" parent="."]
|
||||
script = ExtResource("3_80q4n")
|
||||
|
||||
[node name="multiplayer_data" type="Node" parent="."]
|
||||
script = ExtResource("4_setx2")
|
||||
70
scripts/data/game_data.gd
Normal file
70
scripts/data/game_data.gd
Normal file
@@ -0,0 +1,70 @@
|
||||
class_name GameData extends Node
|
||||
|
||||
|
||||
var client1: int
|
||||
var client2: int
|
||||
var player_id: String = ""
|
||||
|
||||
var game_address: String = "0.0.0.0"
|
||||
var game_port: int = 8080
|
||||
var game_mode: String = "freeball"
|
||||
|
||||
enum PlayerType {
|
||||
Player1,
|
||||
Player2
|
||||
}
|
||||
|
||||
enum GameModeType {
|
||||
BALL_8,
|
||||
BALL_9,
|
||||
SNOOKER,
|
||||
FREE_BALL
|
||||
}
|
||||
|
||||
const MATCH_TYPES: Dictionary = {
|
||||
"8ball": GameModeType.BALL_8,
|
||||
"9ball": GameModeType.BALL_9,
|
||||
"snooker": GameModeType.SNOOKER,
|
||||
"freeball": GameModeType.FREE_BALL
|
||||
}
|
||||
|
||||
var GameMode: Dictionary = {
|
||||
GameModeType.BALL_8: Ball8,
|
||||
GameModeType.BALL_9: Ball9,
|
||||
GameModeType.SNOOKER: Snooker,
|
||||
GameModeType.FREE_BALL: FreeBall
|
||||
}
|
||||
|
||||
var SelectedGameMode: GameModeType = GameModeType.FREE_BALL
|
||||
|
||||
|
||||
func _init() -> void:
|
||||
Globals.game_data = self
|
||||
|
||||
func get_mode_pretty() -> String:
|
||||
match game_mode:
|
||||
"8ball":
|
||||
return "8-Ball"
|
||||
"9ball":
|
||||
return "9-Ball"
|
||||
"snooker":
|
||||
return "Snooker"
|
||||
"freeball":
|
||||
return "Free Ball"
|
||||
_:
|
||||
return ""
|
||||
|
||||
@rpc("authority", "reliable")
|
||||
func set_game_address(address: String) -> void:
|
||||
game_address = address
|
||||
|
||||
@rpc("authority", "reliable")
|
||||
func set_game_port(port: String) -> void:
|
||||
game_port = int(port)
|
||||
|
||||
|
||||
func set_game_mode(mode: String) -> void:
|
||||
SelectedGameMode = MATCH_TYPES[mode]
|
||||
|
||||
func set_player_id(is_player_1: bool) -> void:
|
||||
player_id = "Player 1" if is_player_1 else "Player 2"
|
||||
1
scripts/data/game_data.gd.uid
Normal file
1
scripts/data/game_data.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://52eithlmedm3
|
||||
57
scripts/data/game_state.gd
Normal file
57
scripts/data/game_state.gd
Normal file
@@ -0,0 +1,57 @@
|
||||
class_name GameState extends Node
|
||||
|
||||
|
||||
@warning_ignore_start("unused_signal")
|
||||
signal update_cue_cam_position
|
||||
|
||||
signal balls_stopped_moving
|
||||
signal white_ball_sunk
|
||||
signal white_ball_needs_hard_reset
|
||||
signal reload_scene
|
||||
signal process_ball
|
||||
signal reap_ball
|
||||
@warning_ignore_restore("unused_signal")
|
||||
|
||||
|
||||
func _init() -> void:
|
||||
Globals.game_state = self
|
||||
|
||||
func load_game_scene(scene_str: String) -> void:
|
||||
var scene = load(scene_str).instantiate()
|
||||
var target = get_tree().root.get_node("billiards/scene")
|
||||
|
||||
target.add_child(scene)
|
||||
|
||||
func set_game_scene(scene_str: String) -> void:
|
||||
var scene = load(scene_str).instantiate()
|
||||
var target = get_tree().root.get_node("billiards/scene")
|
||||
|
||||
if "game" in scene_str:
|
||||
scene.name = "game"
|
||||
elif "lobby" in scene_str:
|
||||
scene.name = "lobby_screen"
|
||||
elif "start" in scene_str:
|
||||
scene.name = "start_screen"
|
||||
|
||||
clear_game_scene(target)
|
||||
|
||||
target.add_child(scene)
|
||||
|
||||
func clear_game_scene(container: Node) -> void:
|
||||
for child in container.get_children():
|
||||
child.queue_free()
|
||||
|
||||
await get_tree().process_frame
|
||||
|
||||
func switch_active_player() -> void:
|
||||
# TODO: Move away from using client ID or mask it with a map ID
|
||||
if Globals.multiplayer_data.current_turn_player_id == Globals.game_data.client1:
|
||||
Globals.multiplayer_data.rpc(
|
||||
"set_active_player_id",
|
||||
Globals.game_data.client2
|
||||
)
|
||||
elif Globals.multiplayer_data.current_turn_player_id == Globals.game_data.client2:
|
||||
Globals.multiplayer_data.rpc(
|
||||
"set_active_player_id",
|
||||
Globals.game_data.client1
|
||||
)
|
||||
1
scripts/data/game_state.gd.uid
Normal file
1
scripts/data/game_state.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://dvw46rvml3ec
|
||||
31
scripts/data/lobby_data.gd
Normal file
31
scripts/data/lobby_data.gd
Normal file
@@ -0,0 +1,31 @@
|
||||
class_name LobbyData extends Node
|
||||
|
||||
|
||||
func _init() -> void:
|
||||
Globals.lobby_data = self
|
||||
|
||||
|
||||
@rpc("authority", "reliable")
|
||||
func connect_to_game() -> void:
|
||||
# NOTE: Leave the lobby
|
||||
MessageBus.emit_local("lobby_close_connection", null)
|
||||
|
||||
# NOTE: Join target game server
|
||||
MessageBus.emit_local(
|
||||
"game_start_client",
|
||||
[
|
||||
Globals.game_data.game_address,
|
||||
Globals.game_data.game_port
|
||||
],
|
||||
true
|
||||
)
|
||||
|
||||
@rpc("authority", "reliable")
|
||||
func go_back_to_match_screen() -> void:
|
||||
if multiplayer.is_server(): return
|
||||
|
||||
push_warning("Client peer disconnected...")
|
||||
|
||||
Globals.game_state.set_game_scene(
|
||||
"res://scenes/screens/lobby_screen/lobby.tscn"
|
||||
)
|
||||
1
scripts/data/lobby_data.gd.uid
Normal file
1
scripts/data/lobby_data.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://6sfwe1hwgf0g
|
||||
139
scripts/data/multiplayer_data.gd
Normal file
139
scripts/data/multiplayer_data.gd
Normal file
@@ -0,0 +1,139 @@
|
||||
class_name MultiplayerData extends Node
|
||||
|
||||
|
||||
var is_multiplayer_active: bool = false
|
||||
var multiplayer_synchronizer: MultiplayerSynchronizer = MultiplayerSynchronizer.new()
|
||||
var synchronizer_config: SceneReplicationConfig = SceneReplicationConfig.new()
|
||||
var current_turn_player_id: int = -1
|
||||
|
||||
|
||||
func _init() -> void:
|
||||
Globals.multiplayer_data = self
|
||||
multiplayer_synchronizer.name = "multiplayer_synchronizer"
|
||||
multiplayer_synchronizer.replication_config = synchronizer_config
|
||||
multiplayer_synchronizer.root_path = "/root/billiards"
|
||||
|
||||
@rpc("authority", "call_local", "reliable")
|
||||
func set_multiplayer_active() -> void:
|
||||
is_multiplayer_active = true
|
||||
|
||||
@rpc("authority", "call_local", "reliable")
|
||||
func set_active_player_id(id: int) -> void:
|
||||
push_warning("Server assigned active player: ", id)
|
||||
|
||||
current_turn_player_id = id
|
||||
MessageBus.emit_local("update_active_lbl", null)
|
||||
|
||||
|
||||
func reset() -> void:
|
||||
multiplayer_synchronizer = MultiplayerSynchronizer.new()
|
||||
synchronizer_config = SceneReplicationConfig.new()
|
||||
current_turn_player_id = -1
|
||||
|
||||
is_multiplayer_active = true
|
||||
multiplayer_synchronizer.name = "multiplayer_synchronizer"
|
||||
multiplayer_synchronizer.replication_config = synchronizer_config
|
||||
multiplayer_synchronizer.root_path = "/root/billiards"
|
||||
|
||||
func init_match() -> void:
|
||||
var match_entry = Dictionary()
|
||||
match_entry.set("type", Globals.game_data.game_mode)
|
||||
|
||||
rpc("set_active_player_id", Globals.game_data.client1)
|
||||
|
||||
MessageBus.emit_propagation_clients_only.rpc_id(
|
||||
Globals.game_data.client1,
|
||||
"match_list_entry_load_match",
|
||||
[match_entry, true],
|
||||
true
|
||||
)
|
||||
|
||||
MessageBus.emit_propagation_clients_only.rpc_id(
|
||||
Globals.game_data.client2,
|
||||
"match_list_entry_load_match",
|
||||
[match_entry, false],
|
||||
true
|
||||
)
|
||||
|
||||
|
||||
func set_multiplayer_inactive() -> void:
|
||||
is_multiplayer_active = false
|
||||
|
||||
func can_do_request_server_only() -> bool:
|
||||
return can_do_request(true)
|
||||
|
||||
# NOTE: Clients could be modified to pass checks; but, because we are server authority based
|
||||
# it wouldn't matter because server should/will override any state of balls/scores and
|
||||
# checks which player is active when processing input. This is mostly patterned
|
||||
# to reduce client side overhead.
|
||||
func can_do_request(server_only: bool = false) -> bool:
|
||||
# NOTE: If request is local and not multiplayer
|
||||
if not is_multiplayer_active:
|
||||
return true
|
||||
|
||||
# NOTE: If request is server only; checkable by client and server side
|
||||
# server_only check must return so CANNOT do if 'server_only and multiplayer.is_server()' ...
|
||||
if server_only:
|
||||
return multiplayer.is_server()
|
||||
|
||||
# NOTE: If request is local or server side
|
||||
if current_turn_player_id == multiplayer.get_unique_id():
|
||||
return true
|
||||
|
||||
# NOTE: If request is on server and checking active player
|
||||
if current_turn_player_id == multiplayer.get_remote_sender_id():
|
||||
return true
|
||||
|
||||
return false
|
||||
|
||||
func set_tracking_of_cue(path: String) -> void:
|
||||
path = str(path).trim_prefix(str(multiplayer_synchronizer.root_path) + "/")
|
||||
|
||||
var position_pth = NodePath( path + ":position" )
|
||||
var rotation_pth = NodePath( path + ":rotation" )
|
||||
|
||||
synchronizer_config.add_property(position_pth)
|
||||
synchronizer_config.add_property(rotation_pth)
|
||||
|
||||
synchronizer_config.property_set_replication_mode(
|
||||
position_pth, SceneReplicationConfig.REPLICATION_MODE_ON_CHANGE
|
||||
)
|
||||
synchronizer_config.property_set_replication_mode(
|
||||
rotation_pth, SceneReplicationConfig.REPLICATION_MODE_ON_CHANGE
|
||||
)
|
||||
|
||||
|
||||
func set_tracking_of_ball(path: String) -> void:
|
||||
path = str(path).trim_prefix(str(multiplayer_synchronizer.root_path) + "/")
|
||||
|
||||
var position_pth = NodePath( path + ":position" )
|
||||
var rotation_pth = NodePath( path + ":rotation" )
|
||||
var linear_velocity_pth = NodePath( path + ":linear_velocity" )
|
||||
|
||||
synchronizer_config.add_property(position_pth)
|
||||
synchronizer_config.add_property(rotation_pth)
|
||||
synchronizer_config.add_property(linear_velocity_pth)
|
||||
|
||||
synchronizer_config.property_set_replication_mode(
|
||||
position_pth, SceneReplicationConfig.REPLICATION_MODE_ON_CHANGE
|
||||
)
|
||||
synchronizer_config.property_set_replication_mode(
|
||||
rotation_pth, SceneReplicationConfig.REPLICATION_MODE_ON_CHANGE
|
||||
)
|
||||
synchronizer_config.property_set_replication_mode(
|
||||
linear_velocity_pth, SceneReplicationConfig.REPLICATION_MODE_ON_CHANGE
|
||||
)
|
||||
|
||||
func remove_tracking_of_ball(path: String) -> void:
|
||||
path = str(path).trim_prefix(str(multiplayer_synchronizer.root_path) + "/")
|
||||
|
||||
var position_pth = NodePath( path + ":position" )
|
||||
var rotation_pth = NodePath( path + ":rotation" )
|
||||
var linear_velocity_pth = NodePath( path + ":linear_velocity" )
|
||||
|
||||
if synchronizer_config.has_property(position_pth):
|
||||
synchronizer_config.remove_property(position_pth)
|
||||
if synchronizer_config.has_property(rotation_pth):
|
||||
synchronizer_config.remove_property(rotation_pth)
|
||||
if synchronizer_config.has_property(linear_velocity_pth):
|
||||
synchronizer_config.remove_property(linear_velocity_pth)
|
||||
1
scripts/data/multiplayer_data.gd.uid
Normal file
1
scripts/data/multiplayer_data.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bs4vblwa3u5jq
|
||||
Reference in New Issue
Block a user