29 lines
579 B
GDScript
29 lines
579 B
GDScript
extends PacketHandler
|
|
|
|
signal changed(ping: float)
|
|
|
|
const HISTORY_SIZE = 8
|
|
|
|
var count := 0
|
|
var history: Array[float] = []
|
|
|
|
func _init():
|
|
history.resize(HISTORY_SIZE)
|
|
|
|
func handle_packet(data: PackedByteArray, _peer: PacketPeer):
|
|
var id := data[0]
|
|
var ping := get_time() - history[id]
|
|
changed.emit(ping)
|
|
|
|
func send_ping():
|
|
var buffer := StreamPeerBuffer.new()
|
|
buffer.put_8(Packet.PING)
|
|
buffer.put_8(count)
|
|
%Client.send(buffer.data_array)
|
|
history[count] = get_time()
|
|
count = (count + 1) % HISTORY_SIZE
|
|
|
|
func get_time() -> float:
|
|
return Time.get_unix_time_from_system()
|
|
|