Initial movement added

This commit is contained in:
2025-02-17 08:09:14 -05:00
parent 8332b30d4f
commit 94795e4c77
4 changed files with 288 additions and 33 deletions

View File

@ -1,33 +1,46 @@
extends CharacterBody2D
@export var max_speed: float = 200.0
@export var acceleration: float = 50.0
@export var jump_force: float = -300.0
@export var base_weight: float = 1
@export var min_weight: float = 0.5
@export var max_weight: float = 2.0
@export var speed: float = 200.0
@export var jump_velocity: float = -150.0
@export var double_jump_velocity: float = -150.0
var has_double_jumped: bool = false
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
var animation_locked: bool = false
var direction: Vector2 = Vector2.ZERO
var was_in_air: bool = false
var current_speed: float = 0.0
var current_weight: float = base_weight
var is_jumping: bool = false
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
else:
has_double_jumped = false
# Handle jump.
if Input.is_action_just_pressed("jump"):
if is_on_floor():
velocity.y = jump_velocity
elif not has_double_jumped:
velocity.y = double_jump_velocity;
has_double_jumped = true
func update_animation():
if not animation_locked:
if direction.x != 0:
animated_sprite.play("run")
else:
animated_sprite.play("idle")
func update_facing_direction():
if direction.x > 0:
animated_sprite.flip_h = false
elif direction.x < 0:
animated_sprite.flip_h = 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("left", "right")
if direction:
velocity.x = direction * speed
else:
velocity.x = move_toward(velocity.x, 0, speed)
func jump():
velocity.y = jump_force
animated_sprite.play("jump_start")
animation_locked = true
move_and_slide()
func land():
animated_sprite.play("jump_end")
animation_locked = true
func _on_animated_sprite_2d_animation_finished() -> void:
if animated_sprite.animation == "jump_end":
animation_locked = false