Guest User

Untitled

a guest
Oct 29th, 2024
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. extends CharacterBody2D
  2.  
  3. @onready var timer: Timer = $Timer
  4. @onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
  5. var is_alive = true
  6. var facing_right = true
  7.  
  8. func _ready() -> void:
  9. add_to_group("Player")
  10.  
  11. const SPEED = 100.0
  12. const JUMP_VELOCITY = -275.0
  13.  
  14. func _physics_process(delta: float) -> void:
  15. # Add the gravity.
  16. if not is_on_floor():
  17. velocity += get_gravity() * delta
  18.  
  19. # Handle jump.
  20. if Input.is_action_just_pressed("jump") and is_on_floor() and is_alive:
  21. velocity.y = JUMP_VELOCITY
  22.  
  23. # Get the input direction (-1, 0, 1)
  24. var direction := Input.get_axis("move_left", "move_right")
  25.  
  26. # Flip the sprite
  27. if direction > 0:
  28. $AnimatedSprite2D.flip_h = false
  29. facing_right = true
  30. if direction < 0:
  31. $AnimatedSprite2D.flip_h = true
  32. facing_right = false
  33.  
  34. # Animate the sprite
  35. if is_alive:
  36. if is_on_floor():
  37. if direction == 0:
  38. $AnimationPlayer.play("idle_right")
  39. if direction > 0:
  40. $AnimationPlayer.play("run_right")
  41. facing_right = true
  42. if direction < 0:
  43. $AnimationPlayer.play("run_right")
  44. facing_right = false
  45. else:
  46. $AnimationPlayer.play("jump")
  47.  
  48. # Apply movement
  49. if is_alive:
  50. if direction:
  51. velocity.x = direction * SPEED
  52. else:
  53. velocity.x = move_toward(velocity.x, 0, SPEED)
  54. move_and_slide()
  55.  
  56. func die():
  57. is_alive = false
  58. print("You Died!")
  59. Engine.time_scale = 0.5
  60. timer.start()
  61. if not facing_right:
  62. $AnimatedSprite2D.flip_h = true
  63. $AnimationPlayer.play("die_right")
  64. if facing_right:
  65. $AnimationPlayer.play("die_right")
  66.  
  67. func _on_timer_timeout() -> void:
  68. Engine.time_scale = 1.0
  69. get_tree().reload_current_scene()
Advertisement
Add Comment
Please, Sign In to add comment