From 6f212af9dd2586317a128626e7524c51edcd545d Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Mon, 30 Sep 2024 23:57:13 +0200 Subject: initial commit --- Player/player.gd | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Player/player.gd (limited to 'Player/player.gd') diff --git a/Player/player.gd b/Player/player.gd new file mode 100644 index 0000000..fb1219f --- /dev/null +++ b/Player/player.gd @@ -0,0 +1,55 @@ +extends CharacterBody2D + + +const SPEED = 60.0 +const JUMP_VELOCITY = -300.0 + +var is_in_air = false +var is_crouching = false + + +func _physics_process(delta: float) -> void: + # Add the gravity. + if not is_on_floor(): + velocity += get_gravity() * delta + $AnimatedSprite2D.play("default_jump") + is_in_air = true + + # Handle jump. + if (Input.is_action_just_pressed("ui_accept") or Input.is_action_just_pressed("ui_up")) and is_on_floor(): + velocity.y = JUMP_VELOCITY + $AnimatedSprite2D.play("default_jump") + $Audio/Jump.play() + + if Input.is_action_pressed("ui_down") and is_on_floor(): + is_crouching = true + elif Input.is_action_just_released("ui_down"): + is_crouching = false + + # 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: + if is_crouching: + velocity.x = direction * (SPEED / 2) + else: + velocity.x = direction * SPEED + if is_on_floor(): + if is_crouching: + $AnimatedSprite2D.play("default_crouch") + else: + $AnimatedSprite2D.play("default_walk") + $AnimatedSprite2D.flip_h = direction < 0 + else: + velocity.x = move_toward(velocity.x, 0, SPEED) + if is_on_floor(): + if is_crouching: + $AnimatedSprite2D.play("default_crouch_idle") + else: + $AnimatedSprite2D.play("default_idle") + + if is_on_floor() and is_in_air: + is_in_air = false + $Audio/Land.play() + + move_and_slide() -- cgit v1.2.3