GodotTutorial/player.gd

45 lines
1.2 KiB
GDScript

extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -300.0
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
#own variables see tutorial 2
var is_jumping = false
@onready var animated_sprite_2d = $AnimatedSprite2D
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
else:
is_jumping = false
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
is_jumping = true
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("ui_left", "ui_right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
update_animation(direction)
move_and_slide()
func update_animation(direction):
if is_jumping:
animated_sprite_2d.play("jump")
elif direction != 0:
animated_sprite_2d.flip_h = (direction < 0)
animated_sprite_2d.play("run")
else:
animated_sprite_2d.play("idle")