Push project up
This commit is contained in:
67
scripts/client_networking.gd
Normal file
67
scripts/client_networking.gd
Normal file
@@ -0,0 +1,67 @@
|
||||
class_name ClientNetworking extends Node
|
||||
|
||||
|
||||
var peer: ENetMultiplayerPeer
|
||||
|
||||
|
||||
func start_client(address: String = "127.0.0.1", port: int = 8080) -> void:
|
||||
if peer: return
|
||||
|
||||
push_warning("Client Starting... Address: ", address, " Port: ", port)
|
||||
# TODO: Add throbber or other indication of connection attempt.
|
||||
|
||||
peer = ENetMultiplayerPeer.new()
|
||||
peer.create_client(address, port)
|
||||
|
||||
multiplayer.multiplayer_peer = peer
|
||||
multiplayer.connected_to_server.connect(client_connected_to_server)
|
||||
multiplayer.server_disconnected.connect(client_disconnected_from_server)
|
||||
multiplayer.connection_failed.connect(client_connection_failed_to_server)
|
||||
|
||||
|
||||
func _wait_for_connection() -> void:
|
||||
while not peer:
|
||||
await get_tree().process_frame
|
||||
|
||||
while peer.get_connection_status() != 2:
|
||||
await get_tree().process_frame
|
||||
|
||||
func _wait_close_connection():
|
||||
close_connection()
|
||||
|
||||
while peer:
|
||||
await get_tree().process_frame
|
||||
|
||||
|
||||
func client_connected_to_server() -> void:
|
||||
assert(false, "This method needs to be overridden...")
|
||||
|
||||
func client_disconnected_from_server() -> void:
|
||||
assert(false, "This method needs to be overridden...")
|
||||
|
||||
|
||||
func client_connection_failed_to_server() -> void:
|
||||
assert(false, "This method needs to be overridden...")
|
||||
|
||||
|
||||
func close_connection() -> void:
|
||||
if not peer: return
|
||||
|
||||
push_warning("Disconnected from, server...")
|
||||
|
||||
unset_client()
|
||||
|
||||
|
||||
func unset_client() -> void:
|
||||
if not peer: return
|
||||
if not multiplayer:
|
||||
peer.close()
|
||||
peer = null
|
||||
return
|
||||
|
||||
multiplayer.connected_to_server.disconnect(client_connected_to_server)
|
||||
multiplayer.connection_failed.disconnect(client_connection_failed_to_server)
|
||||
multiplayer.server_disconnected.disconnect(client_disconnected_from_server)
|
||||
|
||||
peer.close()
|
||||
peer = null
|
||||
1
scripts/client_networking.gd.uid
Normal file
1
scripts/client_networking.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://ci57tiuqsx1jp
|
||||
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://b808va021l1w7" path="res://scripts/data/game_data.gd" id="1_hulqm"]
|
||||
[ext_resource type="Script" uid="uid://cai8lpy7e00d6" path="res://scripts/data/game_state.gd" id="2_6ash1"]
|
||||
[ext_resource type="Script" uid="uid://cihlohg5ouy6q" path="res://scripts/data/lobby_data.gd" id="3_80q4n"]
|
||||
[ext_resource type="Script" uid="uid://dof720ujunrw8" 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")
|
||||
5
scripts/data/game_data.gd
Normal file
5
scripts/data/game_data.gd
Normal file
@@ -0,0 +1,5 @@
|
||||
class_name GameData extends Node
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
Globals.game_data = self
|
||||
1
scripts/data/game_data.gd.uid
Normal file
1
scripts/data/game_data.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://b808va021l1w7
|
||||
32
scripts/data/game_state.gd
Normal file
32
scripts/data/game_state.gd
Normal file
@@ -0,0 +1,32 @@
|
||||
class_name GameState extends Node
|
||||
|
||||
|
||||
func _ready() -> 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("shell/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("shell/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
|
||||
1
scripts/data/game_state.gd.uid
Normal file
1
scripts/data/game_state.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://cai8lpy7e00d6
|
||||
5
scripts/data/lobby_data.gd
Normal file
5
scripts/data/lobby_data.gd
Normal file
@@ -0,0 +1,5 @@
|
||||
class_name LobbyData extends Node
|
||||
|
||||
|
||||
func _init() -> void:
|
||||
Globals.lobby_data = self
|
||||
1
scripts/data/lobby_data.gd.uid
Normal file
1
scripts/data/lobby_data.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://cihlohg5ouy6q
|
||||
13
scripts/data/multiplayer_data.gd
Normal file
13
scripts/data/multiplayer_data.gd
Normal file
@@ -0,0 +1,13 @@
|
||||
class_name MultiplayerData extends Node
|
||||
|
||||
|
||||
var is_multiplayer_active: bool = false
|
||||
var multiplayer_synchronizer: MultiplayerSynchronizer = MultiplayerSynchronizer.new()
|
||||
var synchronizer_config: SceneReplicationConfig = SceneReplicationConfig.new()
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
Globals.multiplayer_data = self
|
||||
multiplayer_synchronizer.name = "multiplayer_synchronizer"
|
||||
multiplayer_synchronizer.replication_config = synchronizer_config
|
||||
multiplayer_synchronizer.root_path = "/root/shell"
|
||||
1
scripts/data/multiplayer_data.gd.uid
Normal file
1
scripts/data/multiplayer_data.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://dof720ujunrw8
|
||||
47
scripts/server_networking.gd
Normal file
47
scripts/server_networking.gd
Normal file
@@ -0,0 +1,47 @@
|
||||
class_name ServerNetworking extends Node
|
||||
|
||||
|
||||
var peer: ENetMultiplayerPeer
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
pass
|
||||
|
||||
|
||||
func start_server(address: String = "0.0.0.0", port: int = 8080) -> void:
|
||||
if peer: return
|
||||
|
||||
push_warning("Server Starting... Address: ", address, " Port: ", port)
|
||||
|
||||
peer = ENetMultiplayerPeer.new()
|
||||
peer.set_bind_ip(address)
|
||||
|
||||
var state = peer.create_server(port)
|
||||
match state:
|
||||
OK:
|
||||
push_warning("Server Started: ", state)
|
||||
|
||||
multiplayer.multiplayer_peer = peer
|
||||
multiplayer.peer_connected.connect(server_client_connected)
|
||||
multiplayer.peer_disconnected.connect(server_client_disconnected)
|
||||
ERR_ALREADY_IN_USE:
|
||||
peer.close()
|
||||
peer = null
|
||||
start_server(address, port)
|
||||
ERR_CANT_CREATE:
|
||||
push_warning("Couldn't create server...")
|
||||
|
||||
func close_connection() -> void:
|
||||
push_warning("Server Closed...")
|
||||
|
||||
multiplayer.peer_connected.disconnect(server_client_connected)
|
||||
multiplayer.peer_disconnected.disconnect(server_client_disconnected)
|
||||
|
||||
peer.close()
|
||||
peer = null
|
||||
|
||||
func server_client_connected(_id: int) -> void:
|
||||
assert(false, "This method needs to be overridden...")
|
||||
|
||||
func server_client_disconnected(_id: int) -> void:
|
||||
assert(false, "This method needs to be overridden...")
|
||||
1
scripts/server_networking.gd.uid
Normal file
1
scripts/server_networking.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bgnr36m8qvbx3
|
||||
Reference in New Issue
Block a user