2024-01-15 23:22:14 +00:00
|
|
|
extends Node
|
|
|
|
|
|
|
|
signal changed(ping: float)
|
|
|
|
|
|
|
|
const HISTORY_SIZE = 8
|
|
|
|
|
|
|
|
var count := 0
|
|
|
|
var history: Array[float] = []
|
|
|
|
|
|
|
|
func _init():
|
|
|
|
history.resize(HISTORY_SIZE)
|
|
|
|
|
|
|
|
func _ready():
|
2024-01-16 22:01:16 +00:00
|
|
|
%Network.add_handler(Packet.PING, self)
|
|
|
|
|
|
|
|
func send_ping():
|
2024-01-15 23:22:14 +00:00
|
|
|
var buffer := StreamPeerBuffer.new()
|
2024-01-16 22:01:16 +00:00
|
|
|
buffer.put_8(Packet.PING)
|
2024-01-15 23:22:14 +00:00
|
|
|
buffer.put_8(count)
|
|
|
|
%Network.udp.put_packet(buffer.data_array)
|
|
|
|
|
|
|
|
history[count] = Time.get_unix_time_from_system()
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
if count >= HISTORY_SIZE:
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
func handle_packet(data: PackedByteArray):
|
2024-01-16 22:01:16 +00:00
|
|
|
var id := data.decode_u8(0)
|
2024-01-15 23:22:14 +00:00
|
|
|
var ping := Time.get_unix_time_from_system() - history[id]
|
|
|
|
changed.emit(ping)
|
|
|
|
|