55 lines
1.4 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
2024-01-29 22:16:06 +00:00
func handle_packet(data: PackedByteArray):
2024-01-26 16:14:20 +00:00
var buffer := StreamPeerBuffer.new()
buffer.data_array = data
2024-02-15 17:06:44 +00:00
var player_id_length := buffer.get_size() - 4 - 4 - 4 - 4
2024-01-26 16:14:20 +00:00
var player_id := buffer.get_string(player_id_length)
var x := buffer.get_float()
var z := buffer.get_float()
2024-02-15 17:06:44 +00:00
var direction_x := buffer.get_float()
var direction_z := buffer.get_float()
2024-01-26 16:14:20 +00:00
2024-01-28 18:18:16 +00:00
var player := Global.players.get_player(player_id)
2024-01-28 21:08:08 +00:00
if !player:
return
2024-01-26 16:14:20 +00:00
var controller := player.controller as ProxyController
2024-01-28 23:55:49 +00:00
if !controller:
return
2024-02-15 17:06:44 +00:00
2024-01-26 16:14:20 +00:00
controller.server_position.x = x
controller.server_position.z = z
2024-02-15 17:06:44 +00:00
controller.direction_changed.emit(Vector3(direction_x, 0, direction_z))
2024-01-26 16:14:20 +00:00
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.z)
2024-02-15 17:06:44 +00:00
buffer.put_float(Global.player.movement.direction.x)
buffer.put_float(Global.player.movement.direction.z)
2024-01-26 18:00:59 +00:00
%Client.send(buffer.data_array)