Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make player movement more smooth #322

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/assets/main/players.gd
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ puppet func update_positions(positions_dict: Dictionary, last_received_input: in
players[id].velocity = positions_dict[id][2]
emit_signal("positions_updated", last_received_input)

func _on_main_player_moved(movement: Vector2, velocity: Vector2, last_input: int):
func _on_main_player_moved(movement: Vector2, last_input: int):
if not get_tree().is_network_server():
rpc_id(1, "player_moved", movement, velocity, last_input)
rpc_id(1, "player_moved", movement, last_input)

# Keep the clients' player positions updated
func _physics_process(_delta: float) -> void:
Expand All @@ -107,7 +107,7 @@ func get_network_id_from_player_node_name(node_name: String) -> int:
return -1

# Called from client side to tell the server about the player's actions
remote func player_moved(new_movement: Vector2, velocity: Vector2, last_input: int) -> void:
remote func player_moved(new_movement: Vector2, last_input: int) -> void:
# Should only be run on the server
if !get_tree().is_network_server():
return
Expand Down
22 changes: 17 additions & 5 deletions src/assets/player/player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ onready var anim_fsm: AnimationNodeStateMachinePlayback = animation_tree.get("pa
onready var item_transform: RemoteTransform2D = skeleton.get_node("Skeleton/Spine/RightUpperArm/RightLowerArm/RightHand/ItemTransform")
onready var sprites_viewport: Viewport = $SpritesViewport

signal main_player_moved(position, velocity, input_number)
signal main_player_moved(movement, input_number)

export (int) var speed = 150

Expand All @@ -22,6 +22,8 @@ var _movement_disabled: bool setget set_movement_disabled, is_movement_disabled
var id: int
var ourname: String
var myRole: String
# Where the server thinks this player is. Only used in the client.
var server_position: Vector2
var velocity = Vector2(0,0)
# Contains the current intended movement direction and magnitude in range 0 to 1
var movement = Vector2(0,0)
Expand Down Expand Up @@ -159,8 +161,9 @@ func run_physics(motion):
velocity.x = lerp(prev_velocity.x, 0, 0.17)
if velocity.y == 0:
velocity.y = lerp(prev_velocity.y, 0, 0.17)
# TODO: provide a delta value to this function and use it here
velocity = move_and_slide(velocity)
var collision = move_and_collide(velocity / 60)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the reason for this being divided by 60?

if collision:
velocity = velocity.slide(collision.normal)

func customizePlayer(customize_id):
#------------
Expand All @@ -180,7 +183,7 @@ func _physics_process(delta):
get_input()
input_number += 1
input_queue.push_back([movement, velocity])
emit_signal("main_player_moved", movement, velocity, input_number)
emit_signal("main_player_moved", movement, input_number)
# Remove this if check to get bad movement extrapolation for all players
if main_player or get_tree().is_network_server():
run_physics(movement)
Expand Down Expand Up @@ -233,12 +236,21 @@ func _on_positions_updated(new_last_received_input: int):
if input_queue.size() >= 1:
velocity = input_queue[0][1]
# Run the physics model for the unreceived inputs
var old_position = position
position = server_position
for i in input_queue:
run_physics(i[0])
var target_position = position
position = old_position
$Tween.interpolate_property(self, "position", null, target_position, 0.3)
$Tween.start()

func move_to(new_pos, new_movement):
position = new_pos
server_position = new_pos
movement = new_movement
if not main_player:
$Tween.interpolate_property(self, "position", null, server_position, 0.2)
$Tween.start()

func get_is_alive() -> bool:
return not death_handler.is_dead
1 change: 1 addition & 0 deletions src/assets/player/player.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ script = ExtResource( 28 )
[node name="PickUp" type="Timer" parent="Timers"]
one_shot = true

[node name="Tween" type="Tween" parent="."]
[connection signal="body_entered" from="interactarea" to="interactarea" method="_on_interactarea_body_entered"]
[connection signal="body_exited" from="interactarea" to="interactarea" method="_on_interactarea_body_exited"]
[connection signal="body_entered" from="ItemPickupRange" to="ItemHandler" method="_on_ItemPickupRange_body_entered"]
Expand Down