Guest User

Untitled

a guest
Oct 30th, 2024
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 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. if !is_alive and is_on_floor():
  49. timer.start()
  50.  
  51. if not facing_right:
  52. $AnimatedSprite2D.flip_h = true
  53. $AnimationPlayer.play("die_right")
  54. else:
  55. $AnimationPlayer.play("die_right")
  56.  
  57.  
  58. # Apply movement
  59. if is_alive:
  60. if direction:
  61. velocity.x = direction * SPEED
  62. else:
  63. velocity.x = move_toward(velocity.x, 0, SPEED)
  64. move_and_slide()
  65. func die():
  66. is_alive = false
  67. print("You Died!")
  68. Engine.time_scale = 0.5
  69.  
  70. func _on_timer_timeout() -> void:
  71. Engine.time_scale = 1.0
  72. get_tree().reload_current_scene()
  73.  
Advertisement
Add Comment
Please, Sign In to add comment