diff --git a/client/Main.tscn b/client/Main.tscn index d572336..a3dc047 100644 --- a/client/Main.tscn +++ b/client/Main.tscn @@ -84,7 +84,6 @@ packet_type = 11 [node name="PlayerMove" type="Node" parent="Client"] script = ExtResource("8_ke1yy") -delay = 100 packet_type = 12 [node name="PlayerJump" type="Node" parent="Client"] diff --git a/client/network/PlayerMove.gd b/client/network/PlayerMove.gd index 710fcc0..fceea72 100644 --- a/client/network/PlayerMove.gd +++ b/client/network/PlayerMove.gd @@ -9,11 +9,12 @@ func handle_packet(data: PackedByteArray): var buffer := StreamPeerBuffer.new() buffer.data_array = data - var player_id_length := buffer.get_size() - 8 + 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 y := 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) @@ -24,9 +25,10 @@ func handle_packet(data: PackedByteArray): 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 == null: @@ -46,6 +48,7 @@ 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) + buffer.put_float(Global.player.movement.direction.x) + buffer.put_float(Global.player.movement.direction.z) %Client.send(buffer.data_array) diff --git a/client/player/Player.gd b/client/player/Player.gd index fb0b38a..2353cbc 100644 --- a/client/player/Player.gd +++ b/client/player/Player.gd @@ -4,6 +4,10 @@ extends Character signal name_changed(new_name: String) var id: String +var movement: MovementComponent + +func _enter_tree(): + movement = get_node("Movement") func set_player_name(new_name: String): name = new_name diff --git a/client/player/controller/ProxyController.gd b/client/player/controller/ProxyController.gd index c1989f1..88e2925 100644 --- a/client/player/controller/ProxyController.gd +++ b/client/player/controller/ProxyController.gd @@ -1,10 +1,9 @@ class_name ProxyController extends Controller -## The character that we're controlling. -var player: Player +@export var interpolation_speed: float = 10.0 -## The authoritative position on the server. +var player: Player var server_position: Vector3 func _init(new_player: Player): @@ -14,13 +13,7 @@ func _init(new_player: Player): func _ready(): server_position = player.position -func _process(_delta): - var move := server_position - player.position - move.y = 0.0 - - if move.length_squared() < 0.02: - direction_changed.emit(Vector3.ZERO) - return - - var direction := Vector3(move.x, 0, move.z).normalized() - direction_changed.emit(direction) \ No newline at end of file +func _process(delta: float): + var time := interpolation_speed * delta + player.position.x = lerpf(player.position.x, server_position.x, time) + player.position.z = lerpf(player.position.z, server_position.z, time) \ No newline at end of file diff --git a/server/game/Move.go b/server/game/Move.go index 0b829a9..b302fdf 100644 --- a/server/game/Move.go +++ b/server/game/Move.go @@ -21,10 +21,14 @@ func (game *Game) Move(data []byte, address *net.UDPAddr) error { player.Position.X = math.Float32frombits(binary.LittleEndian.Uint32(data)) player.Position.Z = math.Float32frombits(binary.LittleEndian.Uint32(data[4:])) + player.direction.X = math.Float32frombits(binary.LittleEndian.Uint32(data[8:])) + player.direction.Z = math.Float32frombits(binary.LittleEndian.Uint32(data[12:])) update := []byte(player.ID) update = AppendFloat(update, player.Position.X) update = AppendFloat(update, player.Position.Z) + update = AppendFloat(update, player.direction.X) + update = AppendFloat(update, player.direction.Z) game.BroadcastOthers(PlayerMove, update, player) return nil diff --git a/server/game/Player.go b/server/game/Player.go index 47a9713..5ade4d5 100644 --- a/server/game/Player.go +++ b/server/game/Player.go @@ -12,6 +12,7 @@ type Player struct { game *Game authToken string address *net.UDPAddr + direction Vector3 lastPacket time.Time }