44 lines
1.2 KiB
GDScript
44 lines
1.2 KiB
GDScript
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()
|
|
|
|
var player := Global.players.get_player(player_id)
|
|
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)
|
|
%Client.send(buffer.data_array)
|