2024-11-01 18:33:00 +01:00
|
|
|
extends CharacterBody2D
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@export var speed = 300.0
|
|
|
|
|
@export var jump_velocity = 400.0
|
|
|
|
|
|
2024-11-01 23:28:19 +01:00
|
|
|
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
|
|
|
|
|
|
2024-11-01 18:33:00 +01:00
|
|
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
|
|
|
# Add the gravity.
|
|
|
|
|
if not is_on_floor():
|
|
|
|
|
velocity += get_gravity() * delta
|
|
|
|
|
|
|
|
|
|
# Handle jump.
|
|
|
|
|
if Input.is_action_just_pressed("jump") and is_on_floor():
|
|
|
|
|
velocity.y = -jump_velocity
|
|
|
|
|
|
|
|
|
|
# Get the input direction and handle the movement/deceleration.
|
|
|
|
|
var direction := Input.get_axis("move_left", "move_right")
|
|
|
|
|
velocity.x = direction * speed
|
|
|
|
|
|
|
|
|
|
move_and_slide()
|
2024-11-01 23:28:19 +01:00
|
|
|
play_animations(direction)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func play_animations(direction: float):
|
|
|
|
|
if direction != 0.0:
|
|
|
|
|
animated_sprite_2d.flip_h = direction == -1.0
|
|
|
|
|
|
|
|
|
|
if is_on_floor():
|
|
|
|
|
if direction == 0.0:
|
|
|
|
|
animated_sprite_2d.play("idle")
|
|
|
|
|
else:
|
|
|
|
|
animated_sprite_2d.play("run")
|
|
|
|
|
else:
|
|
|
|
|
if velocity.y < 0:
|
|
|
|
|
animated_sprite_2d.play("jump")
|
|
|
|
|
else:
|
|
|
|
|
animated_sprite_2d.play("fall")
|
|
|
|
|
pass
|