Improved movement interpolation
This commit is contained in:
@ -1,9 +1,15 @@
|
||||
class_name PlayerMove
|
||||
extends PacketHandler
|
||||
|
||||
@export var delay := 50
|
||||
const updates_per_second := 20.0
|
||||
|
||||
var last_sent := Time.get_ticks_msec()
|
||||
var delay: float
|
||||
var last_sent := Time.get_unix_time_from_system()
|
||||
var last_sent_position := Vector3.ZERO
|
||||
var last_sent_direction := Vector3.ZERO
|
||||
|
||||
func _ready():
|
||||
delay = 1.0 / updates_per_second
|
||||
|
||||
func handle_packet(data: PackedByteArray):
|
||||
var buffer := StreamPeerBuffer.new()
|
||||
@ -30,21 +36,22 @@ func handle_packet(data: PackedByteArray):
|
||||
controller.server_position.z = z
|
||||
controller.direction_changed.emit(Vector3(direction_x, 0, direction_z))
|
||||
|
||||
func _physics_process(_delta):
|
||||
func _process(_delta):
|
||||
if !Global.player:
|
||||
return
|
||||
|
||||
if Global.player.position == last_sent_position:
|
||||
if Global.player.position == last_sent_position && Global.player.movement.direction == last_sent_direction:
|
||||
return
|
||||
|
||||
if Time.get_ticks_msec() < last_sent + delay:
|
||||
if Time.get_unix_time_from_system() < last_sent + delay:
|
||||
return
|
||||
|
||||
send_position()
|
||||
last_sent = Time.get_ticks_msec()
|
||||
send_movement()
|
||||
last_sent = Time.get_unix_time_from_system()
|
||||
last_sent_position = Global.player.position
|
||||
last_sent_direction = Global.player.movement.direction
|
||||
|
||||
func send_position():
|
||||
func send_movement():
|
||||
var buffer := StreamPeerBuffer.new()
|
||||
buffer.put_u8(PacketHandler.Packet.PLAYER_MOVE)
|
||||
buffer.put_float(Global.player.position.x)
|
||||
|
Reference in New Issue
Block a user