forked from newt/gmtk-2024
52 lines
1.2 KiB
GDScript3
52 lines
1.2 KiB
GDScript3
|
extends CharacterBody2D
|
||
|
|
||
|
@export var SPEED = 300
|
||
|
@export var GRAVITY = 30
|
||
|
@export var JUMP_FORCE = 500
|
||
|
|
||
|
@onready var animated_sprite = $AnimatedSprite2D;
|
||
|
|
||
|
var jumping = false;
|
||
|
var facing_right = true;
|
||
|
|
||
|
func _ready():
|
||
|
animated_sprite.play("idle")
|
||
|
|
||
|
func _physics_process(delta):
|
||
|
if is_on_floor():
|
||
|
# allow the player to jump
|
||
|
if jumping:
|
||
|
jumping = false
|
||
|
|
||
|
if Input.is_action_just_pressed("jump"):
|
||
|
velocity.y = -JUMP_FORCE
|
||
|
jumping = true
|
||
|
animated_sprite.play("jump")
|
||
|
else:
|
||
|
# apply gravity
|
||
|
velocity.y += GRAVITY
|
||
|
if velocity.y > 1000:
|
||
|
velocity.y = 1000
|
||
|
|
||
|
# perform horizontal movement
|
||
|
var horizontal_direction = Input.get_axis("move_left", "move_right")
|
||
|
velocity.x = SPEED * horizontal_direction
|
||
|
move_and_slide()
|
||
|
|
||
|
# ensure the player faces the correct direction
|
||
|
if horizontal_direction > 0 && !facing_right:
|
||
|
animated_sprite.flip_h = false
|
||
|
elif horizontal_direction < 0 && facing_right:
|
||
|
animated_sprite.flip_h = true
|
||
|
|
||
|
if horizontal_direction != 0:
|
||
|
facing_right = horizontal_direction > 0
|
||
|
|
||
|
# figure out which animation should be played
|
||
|
# respecting the jump animation above all
|
||
|
if !jumping:
|
||
|
if is_on_floor() && horizontal_direction != 0:
|
||
|
animated_sprite.play("walk")
|
||
|
else:
|
||
|
animated_sprite.play("idle")
|