martian-mike/scripts/player.gd
2024-11-02 01:28:19 +03:00

41 lines
978 B
GDScript

extends CharacterBody2D
@export var speed = 300.0
@export var jump_velocity = 400.0
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
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()
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