65 lines
1.7 KiB
GDScript3
Raw Normal View History

2024-02-23 19:13:46 +00:00
class_name PlayerMove
2024-01-26 16:14:20 +00:00
extends PacketHandler
2024-02-23 19:13:46 +00:00
const updates_per_second := 20.0
2024-01-26 16:14:20 +00:00
2024-02-23 19:13:46 +00:00
var delay: float
var last_sent := Time.get_unix_time_from_system()
2024-01-26 16:14:20 +00:00
var last_sent_position := Vector3.ZERO
2024-02-23 19:13:46 +00:00
var last_sent_direction := Vector3.ZERO
func _ready():
delay = 1.0 / updates_per_second
2024-01-26 16:14:20 +00:00
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-28 12:23:17 +00:00
emit.call_deferred(controller, Vector3(direction_x, 0, direction_z))
func emit(controller: Controller, direction: Vector3):
controller.direction_changed.emit(direction)
2024-01-26 16:14:20 +00:00
2024-02-27 22:21:44 +00:00
func _process(_delta: float):
2024-02-16 13:45:49 +00:00
if !Global.player:
2024-01-26 16:14:20 +00:00
return
2024-02-23 19:13:46 +00:00
if Global.player.position == last_sent_position && Global.player.movement.direction == last_sent_direction:
2024-01-26 16:14:20 +00:00
return
2024-02-23 19:13:46 +00:00
if Time.get_unix_time_from_system() < last_sent + delay:
2024-01-26 16:14:20 +00:00
return
2024-02-23 19:13:46 +00:00
send_movement()
last_sent = Time.get_unix_time_from_system()
2024-01-26 16:14:20 +00:00
last_sent_position = Global.player.position
2024-02-23 19:13:46 +00:00
last_sent_direction = Global.player.movement.direction
2024-01-26 16:14:20 +00:00
2024-02-23 19:13:46 +00:00
func send_movement():
2024-01-26 16:14:20 +00:00
var buffer := StreamPeerBuffer.new()
2024-02-16 14:51:23 +00:00
buffer.put_u8(PacketHandler.Packet.PLAYER_MOVE)
2024-01-26 16:14:20 +00:00
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)