diff options
Diffstat (limited to 'Game')
-rw-r--r-- | Game/Client.gd | 21 | ||||
-rw-r--r-- | Game/Network.gd | 44 | ||||
-rw-r--r-- | Game/Selection/selection_rectangle.gd | 2 |
3 files changed, 51 insertions, 16 deletions
diff --git a/Game/Client.gd b/Game/Client.gd index 7ca26ec..13fae56 100644 --- a/Game/Client.gd +++ b/Game/Client.gd @@ -6,7 +6,7 @@ signal stage_state_changed(state: State) @warning_ignore("unused_signal") signal placed_tower(tower: Tower) -signal multi_select_finished +signal multi_select_finished(nodes) var state: State : @@ -24,10 +24,11 @@ var player: Player: func initialize_stage(stage: Stage): + current_stage = stage + +func ready_stage(_stage: Stage): selection = preload("res://Game/Selection/SelectionManager.tscn").instantiate() add_child(selection) - - current_stage = stage func place_tower(tower: Tower): @@ -72,7 +73,7 @@ func multi_select(layer: int): selection_area.select.connect(func(nodes): for node in nodes: Client.select_tower(node) - multi_select_finished.emit() + multi_select_finished.emit(nodes) ) get_tree().current_scene.add_child(selection_area) @@ -91,8 +92,8 @@ func spawn_unit(unit: Unit, spawn: Spawn, overwrite_target: PathNode = null): unit.global_position = spawn.spawn_position unit.target = spawn.next_node - unit.hp = randi_range(5, 10) #20000b - unit.base_speed = randi_range(50, 150) + #unit.hp = randi_range(5, 10) #20000b + #unit.base_speed = randi_range(50, 150) if overwrite_target: unit.target = overwrite_target @@ -105,6 +106,14 @@ func update_unit(path: NodePath, data: Unit.NetworkData): Network.update_unit.rpc(path, inst_to_dict(data)) +func change_scene_to_instance(scene: Node): + (func(): + get_tree().current_scene.queue_free() + get_tree().root.add_child(scene) + get_tree().current_scene = scene + ).call_deferred() + + func array_intersect(first, second): var compare = {} for value in first: diff --git a/Game/Network.gd b/Game/Network.gd index 355f7a4..5dcb074 100644 --- a/Game/Network.gd +++ b/Game/Network.gd @@ -9,9 +9,9 @@ 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) + multiplayer.server_disconnected.connect(_on_disconnected_from_server) + #multiplayer.peer_connected.connect(_on_peer_connected) + #multiplayer.peer_disconnected.connect(_on_peer_disconnected) multiplayer.allow_object_decoding = true @@ -36,26 +36,33 @@ func join_game(ip, port): func _on_connected_to_server(): print(multiplayer.get_unique_id(), ": connected to server") -func _on_disconneted_from_server(): +func _on_disconnected_from_server(): print("disconnected from server") multiplayer.multiplayer_peer = OfflineMultiplayerPeer.new() reset_players() -func _on_peer_connected(id): +func _on_peer_connected(id: int): print(multiplayer.get_unique_id(), ": peer connected: ", id) - add_to_players.rpc_id(id, inst_to_dict(Client.player)) + ask_game_running.rpc_id(id) + is_game_running.connect(func(is_running: bool): + if not is_running: + add_to_players.rpc_id(id, inst_to_dict(Client.player)) + elif is_running: + get_tree().change_scene_to_file("res://UI/Start.tscn") + ) + # TODO: add existing towers to new peers -func _on_peer_disconnected(id): +func _on_peer_disconnected(id: int): print(multiplayer.get_unique_id(), ": peer disconnected: ", id) - %Players.remove_child(get_player(id)) + remove_player(id) # TODO: move towers owned by peer to host - if id == 1: # if host disconnected go back to Lobby + if id == 1: # if host disconnected go back to Start get_tree().change_scene_to_file("res://UI/Start.tscn") @@ -84,6 +91,25 @@ func get_ordered_player_ids(players = get_players()) -> Array: return keys +func remove_player(id: int): + var player = get_player(id) + + if player: + %Players.remove_child(player) + players_changed.emit() + + +signal is_game_running(is_running: bool) + +@rpc("any_peer") +func ask_game_running(): + var is_running = get_tree().current_scene is Stage + receive_game_running.rpc_id(multiplayer.get_remote_sender_id(), is_running) + +@rpc("any_peer") +func receive_game_running(is_running: bool): + is_game_running.emit(is_running) + @rpc("any_peer") func add_to_players(remote_data: Dictionary): diff --git a/Game/Selection/selection_rectangle.gd b/Game/Selection/selection_rectangle.gd index 40619ba..b643636 100644 --- a/Game/Selection/selection_rectangle.gd +++ b/Game/Selection/selection_rectangle.gd @@ -37,7 +37,7 @@ func _process(_delta: float) -> void: area.get_parent().is_selected = true if not areas.is_empty(): - Client.multi_select_finished.emit() + Client.multi_select_finished.emit(areas) size = Vector2.ZERO queue_redraw() |