55 lines
1.4 KiB
GDScript
55 lines
1.4 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):
|
|
var buffer := StreamPeerBuffer.new()
|
|
buffer.data_array = data
|
|
|
|
var player_id_length := buffer.get_size() - 4 - 4 - 4 - 4
|
|
var player_id := buffer.get_string(player_id_length)
|
|
var x := buffer.get_float()
|
|
var z := buffer.get_float()
|
|
var direction_x := buffer.get_float()
|
|
var direction_z := buffer.get_float()
|
|
|
|
var player := Global.players.get_player(player_id)
|
|
|
|
if !player:
|
|
return
|
|
|
|
var controller := player.controller as ProxyController
|
|
|
|
if !controller:
|
|
return
|
|
|
|
controller.server_position.x = x
|
|
controller.server_position.z = z
|
|
controller.direction_changed.emit(Vector3(direction_x, 0, direction_z))
|
|
|
|
func _physics_process(_delta):
|
|
if !Global.player:
|
|
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.z)
|
|
buffer.put_float(Global.player.movement.direction.x)
|
|
buffer.put_float(Global.player.movement.direction.z)
|
|
%Client.send(buffer.data_array)
|