From 7fa37897ca06e81a1dd89d489f3a9627281944c4 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Fri, 19 Jul 2024 14:20:40 +0200 Subject: add character sprites and animations --- Player.gd | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'Player.gd') diff --git a/Player.gd b/Player.gd index 9528586..6e2bfd9 100644 --- a/Player.gd +++ b/Player.gd @@ -1,13 +1,17 @@ extends CharacterBody2D -const SPEED = 300.0 +const SPEED = 60 -const JUMP_VELOCITY = -400.0 +const JUMP_VELOCITY = -400 # Get the gravity from the project settings to be synced with RigidBody nodes. var gravity = ProjectSettings.get_setting("physics/2d/default_gravity") +var is_jumping = false +var was_jumping = false +var last_velocity = Vector2(0,0) + func _physics_process(delta): # Get the input direction and handle the movement/deceleration. @@ -15,9 +19,22 @@ func _physics_process(delta): var direction = Input.get_axis("ui_left", "ui_right") if direction: velocity.x = direction * SPEED + if is_on_floor(): + $AnimationTree.active = true + $AnimationTree.get("parameters/playback").travel("Walk") + if velocity.x > 0: + $AnimatedSprite2D.flip_h = false + else: + $AnimatedSprite2D.flip_h = true else: velocity.x = move_toward(velocity.x, 0, SPEED) + if velocity == Vector2(0,0): + $AnimationTree.active = false + if $AnimatedSprite2D.animation == "jump_land" or $AnimatedSprite2D.animation == "walk_end": + await $AnimatedSprite2D.animation_finished + $AnimatedSprite2D.play("idle") + # Add the gravity. if not is_on_floor(): velocity.y += gravity * delta @@ -25,12 +42,29 @@ func _physics_process(delta): # Handle jump. if Input.is_action_just_pressed("jump") and is_on_floor(): velocity.y = JUMP_VELOCITY + $AnimationTree.active = true + $AnimationTree.get("parameters/playback").travel("Jump") + is_jumping = true if Input.is_action_just_released("jump"): + $AnimatedSprite2D.play("jump_fall") + $AnimationTree.active = false if velocity.y < 0: velocity.y = 0 + if not is_on_floor() and velocity.y > 0: + $AnimatedSprite2D.play("jump_fall") + $AnimationTree.active = false + if is_jumping and is_on_floor(): + was_jumping = true + is_jumping = false + #print(last_velocity.y > 0 and velocity.y == 0) + if was_jumping or (last_velocity.y > 0 and velocity.y <= 0): # TODO: state machine + was_jumping = false + $AnimatedSprite2D.play("jump_land") + # Wall jump if is_on_wall() and not is_on_floor() and direction and Input.is_action_just_pressed("jump"): velocity.y = JUMP_VELOCITY velocity.x += abs((SPEED*2) / (JUMP_VELOCITY/gravity)) * -direction move_and_slide() + last_velocity = velocity -- cgit v1.2.3