martian-mike/scripts/player.gd
2024-11-02 09:50:32 +03:00

46 lines
1 KiB
GDScript

extends CharacterBody2D
class_name Player
@export var speed = 300.0
@export var jump_force = 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():
jump(jump_force)
# 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
func jump(jump_force: float):
velocity.y = -jump_force