From 647e2e1812c00e29337a61560644f7aa91d0eb0f Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Sat, 12 Oct 2024 13:59:11 +0200 Subject: next commit --- Game/Client.gd | 14 +++++++------- Game/Network.gd | 46 +++++++++++++++++++++++++++++++++++----------- Game/Network.tscn | 4 ---- Game/Player.gd | 16 +++------------- Game/Player.tscn | 20 +------------------- 5 files changed, 46 insertions(+), 54 deletions(-) (limited to 'Game') diff --git a/Game/Client.gd b/Game/Client.gd index dc18765..5051dc6 100644 --- a/Game/Client.gd +++ b/Game/Client.gd @@ -10,15 +10,13 @@ var state: State : var stage: Stage -var _player: Player # workaround for MultiplayerSpawner var player: Player: get(): - if _player: return _player - else: return Network.get_player(multiplayer.get_unique_id()) + return Network.get_player(multiplayer.get_unique_id()) func _ready(): - _player = preload("res://Game/Player.tscn").instantiate() + pass func initialize_stage(current_stage: Stage): @@ -36,13 +34,15 @@ func place_tower(tower: Tower, position: Vector2): #Network.place_tower.rpc(Network.to_rpc_object(tower), position) player.towers[position] = tower - #player.score += 1 + player.score += 1 + Network.update_player.rpc({"score": player.score}) func remove_tower(tower: Tower): if tower.owner_id == multiplayer.get_unique_id(): destroy_tower(tower) - #player.score -= 1 + player.score -= 1 + Network.update_player.rpc({"score": player.score}) func destroy_tower(tower: Tower): @@ -74,7 +74,7 @@ func spawn_unit(unit: Unit, spawn: Spawn): unit.name = "Unit@" + str(network_id) + "@" + str(Time.get_ticks_usec()) unit.global_position = spawn.spawn_position - unit.target = spawn.next_node.path_position + unit.target = spawn.next_node unit.hp = randi_range(50, 150) #20000b unit.speed = randi_range(100, 150) diff --git a/Game/Network.gd b/Game/Network.gd index 5f8be5c..1fabeaf 100644 --- a/Game/Network.gd +++ b/Game/Network.gd @@ -3,11 +3,12 @@ extends Node signal players_changed -var players = {} - func _ready(): + reset_players() + multiplayer.connected_to_server.connect(_on_connected_to_server) + multiplayer.server_disconnected.connect(_on_disconneted_from_server) multiplayer.peer_connected.connect(_on_peer_connected) multiplayer.peer_disconnected.connect(_on_peer_disconnected) @@ -21,7 +22,6 @@ func host_game(port): multiplayer.multiplayer_peer = peer Client.player.id = multiplayer.get_unique_id() - %Players.add_child(Client.player) func join_game(ip, port): var peer = ENetMultiplayerPeer.new() @@ -35,32 +35,45 @@ func join_game(ip, port): func _on_connected_to_server(): print(multiplayer.get_unique_id(), ": connected to server") +func _on_disconneted_from_server(): + print("disconnected from server") + + multiplayer.multiplayer_peer = OfflineMultiplayerPeer.new() + reset_players() + func _on_peer_connected(id): print(multiplayer.get_unique_id(), ": peer connected: ", id) - if id == 1: # tell host to add player - add_to_players.rpc_id(1, inst_to_dict(Client.player)) - Client._player = null + add_to_players.rpc_id(id, inst_to_dict(Client.player)) # TODO: add existing towers to new peers func _on_peer_disconnected(id): print(multiplayer.get_unique_id(), ": peer disconnected: ", id) + + %Players.remove_child(get_player(id)) + # TODO: move towers owned by peer to host if id == 1: # if host disconnected go back to Lobby - get_tree().change_scene_to_file("res://UI/Lobby.tscn") + get_tree().change_scene_to_file("res://UI/Start.tscn") + +func reset_players(): + for node in %Players.get_children(): + %Players.remove_child(node) + + %Players.add_child(preload("res://Game/Player.tscn").instantiate()) func get_player(id: int) -> Player: + if %Players.get_child_count() == 1: + return %Players.get_child(0) + return %Players.get_node(str(id)) -func get_ordered_player_ids(): # TODO: return type needed for players_list.gd "find" method? +func get_ordered_player_ids() -> Array: var keys = %Players.get_children().map(func(item: Player): return item.id ) - keys = keys.filter(func(item: int): # workaround for MultiplayerSpawner + Synchronizer - return item != 0 - ) keys.sort_custom(func(a: int, b: int): return int(str(a).substr(0, 8)) < int(str(b).substr(0, 8)) ) @@ -81,9 +94,20 @@ func add_to_players(remote_data: Dictionary): players_changed.emit() +@rpc("any_peer") +func update_player(remote_data: Dictionary): + var id = multiplayer.get_remote_sender_id() + var player = get_player(id) + + player.score = remote_data.score + + players_changed.emit() + + @rpc("any_peer") #func place_tower(remote_tower: Dictionary, position: Vector2): func place_tower(remote_data: Dictionary): + print(remote_data) var data: Tower.NetworkData = dict_to_inst(remote_data) var remote_player_id = multiplayer.get_remote_sender_id() diff --git a/Game/Network.tscn b/Game/Network.tscn index 5e7c131..4b6159b 100644 --- a/Game/Network.tscn +++ b/Game/Network.tscn @@ -7,7 +7,3 @@ script = ExtResource("1_p1jvi") [node name="Players" type="Node" parent="."] unique_name_in_owner = true - -[node name="MultiplayerSpawner" type="MultiplayerSpawner" parent="."] -_spawnable_scenes = PackedStringArray("res://Game/Player.tscn") -spawn_path = NodePath("../Players") diff --git a/Game/Player.gd b/Game/Player.gd index 57eb73b..dc81e0a 100644 --- a/Game/Player.gd +++ b/Game/Player.gd @@ -4,14 +4,14 @@ extends Node signal score_changed -@export var username: String +var username: String -@export var score: int : +var score: int : set(value): score = value score_changed.emit() -@export var id: int: +var id: int: set(value): id = value name = str(value) @@ -31,13 +31,3 @@ func get_color(): @warning_ignore("integer_division") return Color(rng.randf(), rng.randf(), rng.randf()) - - -func _on_multiplayer_synchronizer_synchronized() -> void: - Network.players_changed.emit() - -func _on_multiplayer_synchronizer_delta_synchronized() -> void: - Network.players_changed.emit() - -func _on_score_changed() -> void: - Network.players_changed.emit() diff --git a/Game/Player.tscn b/Game/Player.tscn index 38e1d48..23faa6e 100644 --- a/Game/Player.tscn +++ b/Game/Player.tscn @@ -1,24 +1,6 @@ -[gd_scene load_steps=3 format=3 uid="uid://fvspuiqj0osm"] +[gd_scene load_steps=2 format=3 uid="uid://fvspuiqj0osm"] [ext_resource type="Script" path="res://Game/Player.gd" id="1_37njm"] -[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_6h1lm"] -properties/0/path = NodePath(".:score") -properties/0/spawn = true -properties/0/replication_mode = 2 -properties/1/path = NodePath(".:username") -properties/1/spawn = true -properties/1/replication_mode = 2 -properties/2/path = NodePath(".:id") -properties/2/spawn = true -properties/2/replication_mode = 0 - [node name="Player" type="Node"] script = ExtResource("1_37njm") - -[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."] -replication_config = SubResource("SceneReplicationConfig_6h1lm") - -[connection signal="score_changed" from="." to="." method="_on_score_changed"] -[connection signal="delta_synchronized" from="MultiplayerSynchronizer" to="." method="_on_multiplayer_synchronizer_delta_synchronized"] -[connection signal="synchronized" from="MultiplayerSynchronizer" to="." method="_on_multiplayer_synchronizer_synchronized"] -- cgit v1.2.3