Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends CharacterBody2D
- @onready var timer: Timer = $Timer
- @onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
- var is_alive = true
- var facing_right = true
- func _ready() -> void:
- add_to_group("Player")
- const SPEED = 100.0
- const JUMP_VELOCITY = -275.0
- func _physics_process(delta: float) -> void:
- # Add the gravity.
- if not is_on_floor():
- velocity += get_gravity() * delta
- # Handle jump.
- if Input.is_action_just_pressed("jump") and is_on_floor() and is_alive:
- velocity.y = JUMP_VELOCITY
- # Get the input direction (-1, 0, 1)
- var direction := Input.get_axis("move_left", "move_right")
- # Flip the sprite
- if direction > 0:
- $AnimatedSprite2D.flip_h = false
- facing_right = true
- if direction < 0:
- $AnimatedSprite2D.flip_h = true
- facing_right = false
- # Animate the sprite
- if is_alive:
- if is_on_floor():
- if direction == 0:
- $AnimationPlayer.play("idle_right")
- if direction > 0:
- $AnimationPlayer.play("run_right")
- facing_right = true
- if direction < 0:
- $AnimationPlayer.play("run_right")
- facing_right = false
- else:
- $AnimationPlayer.play("jump")
- if !is_alive and is_on_floor():
- timer.start()
- if not facing_right:
- $AnimatedSprite2D.flip_h = true
- $AnimationPlayer.play("die_right")
- else:
- $AnimationPlayer.play("die_right")
- # Apply movement
- if is_alive:
- if direction:
- velocity.x = direction * SPEED
- else:
- velocity.x = move_toward(velocity.x, 0, SPEED)
- move_and_slide()
- func die():
- is_alive = false
- print("You Died!")
- Engine.time_scale = 0.5
- func _on_timer_timeout() -> void:
- Engine.time_scale = 1.0
- get_tree().reload_current_scene()
Advertisement
Add Comment
Please, Sign In to add comment