44 lines
1.2 KiB
GDScript3
Raw Normal View History

2024-01-26 16:14:20 +00:00
extends PacketHandler
@export var delay := 50
var last_sent := Time.get_ticks_msec()
var last_sent_position := Vector3.ZERO
func handle_packet(data: PackedByteArray, _peer: PacketPeer):
var buffer := StreamPeerBuffer.new()
buffer.data_array = data
var player_id_length := buffer.get_size() - 8
var player_id := buffer.get_string(player_id_length)
var x := buffer.get_float()
# var y := buffer.get_float()
var z := buffer.get_float()
2024-01-28 18:18:16 +00:00
var player := Global.players.get_player(player_id)
2024-01-26 16:14:20 +00:00
var controller := player.controller as ProxyController
controller.server_position.x = x
controller.server_position.z = z
func _physics_process(_delta):
if Global.player == null:
return
if Global.player.position == last_sent_position:
return
if Time.get_ticks_msec() < last_sent + delay:
return
send_position()
last_sent = Time.get_ticks_msec()
last_sent_position = Global.player.position
func send_position():
var buffer := StreamPeerBuffer.new()
buffer.put_8(PacketHandler.Packet.PLAYER_MOVE)
buffer.put_float(Global.player.position.x)
# buffer.put_float(Global.player.position.y)
buffer.put_float(Global.player.position.z)
2024-01-26 18:00:59 +00:00
%Client.send(buffer.data_array)