diff --git a/assets/core/enviroment/custom-skies/sky-asset/custom_skies.gd b/assets/core/enviroment/custom-skies/sky-asset/custom_skies.gd index 37a072a..09a1f95 100644 --- a/assets/core/enviroment/custom-skies/sky-asset/custom_skies.gd +++ b/assets/core/enviroment/custom-skies/sky-asset/custom_skies.gd @@ -30,6 +30,18 @@ var moonPosition : float = 0.0 var star_alpha var start_time : float = 0.0 +var network_uuid: int = 0 + +var time_of_day_cache: float = 0.0 + + +func _ready() -> void: + NetworkManager.register_node(self) + network_uuid = NetworkManager.get_network_uuid(self) + + NetworkManager.property_update_received.connect(_on_property_update) + + func _physics_process(delta: float) -> void: _updateRotation() _updateSky() @@ -37,11 +49,19 @@ func _physics_process(delta: float) -> void: _simulateTime(delta) _updateFog() _updateStars(delta) + + +func _process(delta: float) -> void: + if !NetworkManager.has_network_authority: return + if time_of_day_cache != timeOfDay: + NetworkManager.sync_property(network_uuid, "timeOfDay", timeOfDay) func _simulateTime(delta): + if !NetworkManager.has_network_authority: return if simulatingTime: timeOfDay += (rateOfTime / 36) * delta timeOfDay = wrapf(timeOfDay, 0.0, 2400.0) + time_of_day_cache = timeOfDay func _updateLight(): sunPosition = sun_directional_light_3d.global_position.y + 0.5 @@ -84,3 +104,11 @@ func _updateSky(): # Match ground to gradient color skyMaterial.ground_bottom_color = sky_base_color_gradient.gradient.sample(sunPosition) # sample gradient and set ground top color to match based on the time of day skyMaterial.ground_horizon_color = sky_sun_set_rise_color_gradient.gradient.sample(sunPosition) # sample gradient and set ground horizon color to match based on the time of day + + +func _on_property_update(uuid: int, property: String, value: Variant) -> void: + if NetworkManager.node_map.has(uuid): + var node = NetworkManager.node_map[uuid] + node.set(property, value) + else: + printerr("Received property update but node_id is wrong? Expected " + str(network_uuid) + " but got " + str(uuid)) \ No newline at end of file diff --git a/assets/core/networking/scripts/NetworkManager.gd b/assets/core/networking/scripts/NetworkManager.gd index 312e073..e451888 100644 --- a/assets/core/networking/scripts/NetworkManager.gd +++ b/assets/core/networking/scripts/NetworkManager.gd @@ -4,6 +4,8 @@ const STEAM_APP_ID: int = 480 const PACKET_READ_LIMIT: int = 32 +var has_network_authority: bool = false + var level_scene: PackedScene var player_scene: PackedScene @@ -314,15 +316,15 @@ func read_p2p_packet() -> void: #print("Received property update packet for node %d: %s = %s" % [readable_data["node_id"], readable_data["property_name"], readable_data["value"]]) -func register_node(node: Node) -> String: +func register_node(node: Node) -> int: var uuid = generate_uuid() node_map[uuid] = node return uuid -func generate_uuid() -> String: +func generate_uuid() -> int: uuid_counter += 1 - return str(uuid_counter) + return uuid_counter func request_player_avatar(user_id: int, size: int = 128) -> void: @@ -374,14 +376,16 @@ func _on_game_started(): player.username = Steam.getFriendPersonaName(member["steam_id"]) if member["steam_id"] == steam_id: + has_network_authority = true player.is_network_authority = true else: + has_network_authority = false player.is_network_authority = false level.add_child(player) -func sync_property(node_id: String, property_name: String, value: Variant): +func sync_property(node_id: int, property_name: String, value: Variant): var packet_data = {"message":"property_update", "node_id": node_id, "property_name":property_name, "value":value} send_p2p_packet(0, packet_data) diff --git a/assets/core/player-controller/scripts/player.gd b/assets/core/player-controller/scripts/player.gd index a1aed63..47d30f3 100644 --- a/assets/core/player-controller/scripts/player.gd +++ b/assets/core/player-controller/scripts/player.gd @@ -4,8 +4,7 @@ var player_tag: Label3D var username: String = "" var is_piloting: bool = false -var is_network_authority: bool = false -var network_uuid: String = "" +var network_uuid: int var steam_id: int = 0 @export_category("Player") @@ -34,17 +33,17 @@ var previous_global_rotation: Vector3 func _ready() -> void: NetworkManager.property_update_received.connect(_on_property_update) - network_uuid = NetworkManager.register_node(self) + network_uuid = NetworkManager.get_network_uuid(self) player_tag = get_node("PlayerTag") player_tag.text = username - if is_network_authority: + if NetworkManager.has_network_authority: camera.make_current() func _unhandled_input(event: InputEvent) -> void: - if !is_network_authority: return + if !NetworkManager.has_network_authority: return if Input.mouse_mode == Input.MOUSE_MODE_CAPTURED: if event is InputEventMouseMotion: is_using_joystick = false @@ -57,7 +56,7 @@ func _unhandled_input(event: InputEvent) -> void: func _input(event): - if !is_network_authority: return + if !NetworkManager.has_network_authority: return if event.is_action_pressed("esc") and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED: release_mouse() elif event.is_action_pressed("esc") and not Input.mouse_mode == Input.MOUSE_MODE_CAPTURED: @@ -65,7 +64,7 @@ func _input(event): func _physics_process(delta: float) -> void: - if !is_network_authority: return + if !NetworkManager.has_network_authority: return if current_ship != null: if is_piloting: #if we are piloting a ship global_transform.basis = current_ship.global_transform.basis @@ -84,7 +83,7 @@ func _physics_process(delta: float) -> void: func _process(delta: float) -> void: - if !is_network_authority: return + if !NetworkManager.has_network_authority: return # If the global_position has changed, notify the NetworkManager if previous_global_position != global_position: @@ -96,17 +95,17 @@ func _process(delta: float) -> void: func capture_mouse() -> void: - if !is_network_authority: return + if !NetworkManager.has_network_authority: return Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) func release_mouse() -> void: - if !is_network_authority: return + if !NetworkManager.has_network_authority: return Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) func _rotate_camera(sens_mod: float = 1.0) -> void: - if !is_network_authority: return + if !NetworkManager.has_network_authority: return var camera_sens_final = camera_sens if is_using_joystick: camera_sens_final = camera_sens * joystick_camera_sens_multiplier @@ -147,21 +146,21 @@ func _jump(delta: float) -> Vector3: func player_entered_ship(ship_global_position: Vector3, ship: Ship): - if !is_network_authority: return + if !NetworkManager.has_network_authority: return current_ship = ship print(ship.ship_id) print(global_position) func player_exited_ship(ship_global_position: Vector3, ship: Ship): - if !is_network_authority: return + if !NetworkManager.has_network_authority: return current_ship = null print(ship.ship_id) print(global_position) func set_is_piloting(state: bool): - if !is_network_authority: return + if !NetworkManager.has_network_authority: return is_piloting = state GameConsole.log_debug("player pilot state" + str(is_piloting))