diff options
| author | Jan Tuomi <jan@jantuomi.fi> | 2025-11-13 23:17:21 +0200 |
|---|---|---|
| committer | Jan Tuomi <jan@jantuomi.fi> | 2025-11-13 23:17:21 +0200 |
| commit | c03fe1e0422984b37050043fcea22bee845969a0 (patch) | |
| tree | 75db94fe772585d33e48ee90cee7673e5cb904e0 /player.gd | |
Initial commit
Diffstat (limited to 'player.gd')
| -rw-r--r-- | player.gd | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/player.gd b/player.gd new file mode 100644 index 0000000..cdd55d1 --- /dev/null +++ b/player.gd @@ -0,0 +1,33 @@ +extends CharacterBody2D + +@export var speed: float +@export var jump_strength: float + +var gravity = ProjectSettings.get_setting("physics/2d/default_gravity") + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + pass # Replace with function body. + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(_delta: float) -> void: + $AnimatedSprite2D.flip_h = (velocity.x < 0) + + if not is_on_floor(): + $AnimatedSprite2D.set_animation("jump") + elif abs(velocity.x) > 0.0: + $AnimatedSprite2D.set_animation("walk") + else: + $AnimatedSprite2D.set_animation("idle") + +func _physics_process(delta: float): + velocity.y += gravity * delta + + if Input.is_action_just_pressed("jump") and is_on_floor(): + velocity.y = -jump_strength + + var direction = Input.get_axis("move_left", "move_right") + velocity.x = direction * speed + + # Move the character + move_and_slide() |
