43 lines
1.0 KiB
GDScript
43 lines
1.0 KiB
GDScript
extends Node3D
|
|
|
|
const statsTemplate := "FPS: %d\nPing: %d ms"
|
|
|
|
var runner_scene := preload("res://scenes/runner.tscn")
|
|
var _ping := 0.0
|
|
@onready var _runners_node: Node3D = %Runners
|
|
|
|
func spawn_player(player_id) -> void:
|
|
if multiplayer.is_server():
|
|
var runner = runner_scene.instantiate()
|
|
runner.player_id = player_id
|
|
runner.name = str(player_id)
|
|
|
|
_runners_node.add_child(runner)
|
|
|
|
func _process(_delta: float) -> void:
|
|
if DisplayServer.get_name() == "headless":
|
|
return
|
|
|
|
%StatsLabel.text = statsTemplate % [
|
|
Engine.get_frames_per_second(),
|
|
_ping,
|
|
]
|
|
|
|
func _get_timestamp() -> int:
|
|
return floor(Time.get_unix_time_from_system() * 1000)
|
|
|
|
func _on_ping_timer_timeout() -> void:
|
|
if multiplayer.is_server():
|
|
%PingTimer.stop()
|
|
return
|
|
|
|
_ping_call.rpc_id(1, _get_timestamp())
|
|
|
|
@rpc("any_peer", "call_remote", "unreliable", 99)
|
|
func _ping_call(timestamp: int) -> void:
|
|
if multiplayer.is_server():
|
|
_ping_call.rpc_id(multiplayer.get_remote_sender_id(), timestamp)
|
|
return
|
|
|
|
_ping = _get_timestamp() - timestamp
|