You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.2 KiB
66 lines
2.2 KiB
extends CharacterBody2D
|
|
|
|
@export var speed = 300.0
|
|
@export var jump_velocity = -400.0
|
|
@export var wall_jump_velocity = 600.0
|
|
@export var ungrounded_x_lerp = 10.0
|
|
@export var grounded_drag_lerp = 5.0
|
|
@export var ungrounded_drag_lerp = 5.0
|
|
@export var fox_time = 5
|
|
@export var limp_gravity_modifier = 2.0
|
|
|
|
var x_velocity_offset = 0
|
|
var x_movement = 0
|
|
var desired_x_movement = 0
|
|
var last_grounded = 0
|
|
|
|
# Get the gravity from the project settings to be synced with RigidBody nodes.
|
|
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
|
|
|
|
func _ready():
|
|
$MultiplayerSynchronizer.set_multiplayer_authority(name.to_int())
|
|
var username = GameManager.players[name.to_int()].name
|
|
$Label.text = username
|
|
var username_hash = 0
|
|
for i in username.length():
|
|
username_hash += username.unicode_at(i)
|
|
$Sprite2D.modulate = Color.from_hsv((username_hash % 255) / 255.0, 0.5, 0.8)
|
|
|
|
func _physics_process(delta):
|
|
if $MultiplayerSynchronizer.get_multiplayer_authority() != multiplayer.get_unique_id():
|
|
return
|
|
|
|
# Add the gravity.
|
|
if is_on_floor():
|
|
last_grounded = Time.get_ticks_msec()
|
|
else:
|
|
velocity.y += gravity * delta * (limp_gravity_modifier if Input.is_action_pressed("limp") else 1)
|
|
|
|
var grounded = Time.get_ticks_msec() - last_grounded < fox_time
|
|
|
|
x_velocity_offset = lerpf(x_velocity_offset, 0, (grounded_drag_lerp if is_on_floor() else ungrounded_drag_lerp) * delta)
|
|
|
|
# Handle Jump.
|
|
if Input.is_action_just_pressed("jump"):
|
|
if grounded:
|
|
velocity.y = jump_velocity
|
|
elif len($WallDetect.get_overlapping_bodies()) > 0:
|
|
velocity.y = jump_velocity
|
|
if $WallDetect.get_overlapping_bodies()[0].position.x > position.x:
|
|
x_velocity_offset = -wall_jump_velocity
|
|
else:
|
|
x_velocity_offset = wall_jump_velocity
|
|
|
|
# Get the input direction and handle the movement/deceleration.
|
|
# As good practice, you should replace UI actions with custom gameplay actions.
|
|
var direction = Input.get_axis("move_left", "move_right")
|
|
velocity.x = x_velocity_offset
|
|
if direction:
|
|
desired_x_movement = direction * speed;
|
|
x_movement = lerpf(x_movement, desired_x_movement, 1 if grounded else ungrounded_x_lerp * delta)
|
|
velocity.x += x_movement
|
|
else:
|
|
velocity.x = move_toward(velocity.x, 0, speed)
|
|
|
|
move_and_slide()
|