Guest User

Untitled

a guest
Oct 30th, 2024
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. extends CharacterBody2D
  2.  
  3. @onready var death_timer: Timer = $death_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. $AnimationPlayer.play("jump")
  19.  
  20. # Handle roll.
  21. if Input.is_action_just_pressed("roll") and is_on_floor() and is_alive:
  22. $AnimationPlayer.play("roll_right")
  23.  
  24. # Handle jump.
  25. if Input.is_action_just_pressed("jump") and is_on_floor() and is_alive:
  26. velocity.y = JUMP_VELOCITY
  27.  
  28. # Get the input direction (-1, 0, 1)
  29. var direction := Input.get_axis("move_left", "move_right")
  30.  
  31. # Flip the sprite
  32. if direction > 0:
  33. $AnimatedSprite2D.flip_h = false
  34. facing_right = true
  35. if direction < 0:
  36. $AnimatedSprite2D.flip_h = true
  37. facing_right = false
  38.  
  39. # Animate the sprite
  40. if is_on_floor():
  41. if is_alive:
  42. if direction == 0:
  43. $AnimationPlayer.play("idle_right")
  44. if direction > 0:
  45. $AnimationPlayer.play("run_right")
  46. facing_right = true
  47. if direction < 0:
  48. $AnimationPlayer.play("run_right")
  49. facing_right = false
  50.  
  51. if direction:
  52. velocity.x = direction * SPEED
  53. else:
  54. velocity.x = move_toward(velocity.x, 0, SPEED)
  55. else:
  56. death_timer.start()
  57.  
  58. if not facing_right:
  59. $AnimatedSprite2D.flip_h = true
  60. $AnimationPlayer.play("die_right")
  61. else:
  62. $AnimationPlayer.play("die_right")
  63. move_and_slide()
  64.  
  65. func die():
  66. is_alive = false
  67. print("You Died!")
  68. Engine.time_scale = 0.5
  69. death_timer.start()
  70. if not facing_right:
  71. $AnimatedSprite2D.flip_h = true
  72. $AnimationPlayer.play("die_right")
  73. if facing_right:
  74. $AnimationPlayer.play("die_right")
  75.  
  76. func _on_death_timer_timeout() -> void:
  77. Engine.time_scale = 1.0
  78. get_tree().reload_current_scene()
  79.  
Advertisement
Add Comment
Please, Sign In to add comment