42 lines
762 B
GDScript
42 lines
762 B
GDScript
extends CharacterBody2D
|
|
@onready var mysprite = $AnimatedSprite2D
|
|
|
|
const SPEED = 100.0
|
|
const JUMP_VELOCITY = -250.0
|
|
|
|
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") :
|
|
if is_on_floor():
|
|
velocity.y = JUMP_VELOCITY
|
|
|
|
# gets dir -1,0,1
|
|
var direction := Input.get_axis("move_left", "move_right")
|
|
|
|
#flip sprite
|
|
var flippy = direction<0
|
|
mysprite.flip_h = flippy
|
|
|
|
if is_on_floor():
|
|
if direction==0:
|
|
mysprite.play("default")
|
|
else:
|
|
mysprite.play("run")
|
|
else:
|
|
mysprite.play("jump")
|
|
|
|
|
|
|
|
|
|
|
|
if direction:
|
|
velocity.x = direction * SPEED
|
|
else:
|
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
|
|
|
move_and_slide()
|