Advertisement
Guest User

Untitled

a guest
Nov 1st, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. extends CharacterBody2D
  2.  
  3. @onready var animation_sprite = $AnimatedSprite2D
  4. @onready var last_facing_direction = 1 # Default direction to right
  5. @onready var roll_timer = $roll_timer # Timer for roll duration
  6. @onready var roll_cooldown_timer = $roll_cooldown_timer # Timer for roll cooldown (set `wait_time` to 2.0 seconds)
  7. @onready var death_timer: Timer = $death_timer
  8.  
  9. const speed = 125 # Normal speed
  10. const roll_speed = 250 # Roll speed
  11. const gravity = 10
  12. const jump_power = -225
  13. var dir
  14. var roll_direction = 1 # Initialize roll direction
  15.  
  16. var main_sm: LimboHSM
  17.  
  18. func _ready():
  19. add_to_group("Player")
  20. initiate_state_machine()
  21.  
  22. func _physics_process(_delta: float) -> void:
  23. if main_sm.get_active_state().name == "die":
  24. return # Skip physics processing if in die state
  25.  
  26. dir = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
  27.  
  28. if main_sm.get_active_state().name != "roll": # Prevent movement while rolling as well
  29. if dir:
  30. velocity.x = dir * speed
  31. last_facing_direction = dir # Update last facing direction when moving
  32. else:
  33. velocity.x = move_toward(velocity.x, 0, speed)
  34. velocity.y += gravity
  35.  
  36. flip_sprite(dir)
  37. move_and_slide()
  38.  
  39. @warning_ignore("shadowed_variable")
  40. func flip_sprite(dir):
  41. if dir == 1:
  42. animation_sprite.flip_h = false
  43. elif dir == -1:
  44. animation_sprite.flip_h = true
  45.  
  46. func _unhandled_input(event):
  47. if event.is_action_pressed("jump"):
  48. main_sm.dispatch(&"to_jump")
  49. elif event.is_action_pressed("roll") and roll_cooldown_timer.time_left == 0: # Allow roll only if cooldown has ended
  50. main_sm.dispatch(&"to_roll")
  51.  
  52. func initiate_state_machine():
  53. main_sm = LimboHSM.new()
  54. add_child(main_sm)
  55.  
  56. var idle_state = LimboState.new().named("idle").call_on_enter(idle_start).call_on_update(idle_update)
  57. var run_state = LimboState.new().named("run").call_on_enter(run_start).call_on_update(run_update)
  58. var die_state = LimboState.new().named("die").call_on_enter(die_start).call_on_update(die_update)
  59. var jump_state = LimboState.new().named("jump").call_on_enter(jump_start).call_on_update(jump_update)
  60. var roll_state = LimboState.new().named("roll").call_on_enter(roll_start).call_on_update(roll_update)
  61.  
  62. main_sm.add_child(idle_state)
  63. main_sm.add_child(run_state)
  64. main_sm.add_child(die_state)
  65. main_sm.add_child(jump_state)
  66. main_sm.add_child(roll_state)
  67.  
  68. main_sm.initial_state = idle_state
  69.  
  70. main_sm.add_transition(idle_state, run_state, &"to_run")
  71. main_sm.add_transition(main_sm.ANYSTATE, idle_state, &"state_ended")
  72. main_sm.add_transition(idle_state, jump_state, &"to_jump")
  73. main_sm.add_transition(run_state, jump_state, &"to_jump")
  74. main_sm.add_transition(roll_state, jump_state, &"to_jump")
  75. main_sm.add_transition(main_sm.ANYSTATE, die_state, &"player_die")
  76. main_sm.add_transition(idle_state, roll_state, &"to_roll")
  77. main_sm.add_transition(run_state, roll_state, &"to_roll")
  78.  
  79. main_sm.initialize(self)
  80. main_sm.set_active(true)
  81.  
  82. func idle_start():
  83. animation_sprite.play("idle")
  84. func idle_update(_delta: float):
  85. if velocity.x != 0:
  86. main_sm.dispatch(&"to_run")
  87.  
  88. func run_start():
  89. animation_sprite.play("run")
  90. func run_update(_delta: float):
  91. if velocity.x == 0:
  92. main_sm.dispatch(&"state_ended")
  93.  
  94. func jump_start():
  95. if is_on_floor():
  96. animation_sprite.play("jump")
  97. velocity.y = jump_power
  98. else:
  99. main_sm.dispatch(&"state_ended")
  100. func jump_update(_delta: float):
  101. if is_on_floor():
  102. main_sm.dispatch(&"state_ended")
  103.  
  104. # Roll State - Handles roll animation, movement, and timing
  105. func roll_start():
  106. if roll_cooldown_timer.time_left == 0: # Start roll if cooldown has ended
  107. roll_direction = dir if dir != 0 else last_facing_direction
  108. velocity.x = roll_direction * roll_speed # Apply roll speed in current facing direction
  109. roll_timer.start() # Start timer for roll duration
  110. roll_cooldown_timer.start() # Start cooldown timer for roll
  111. animation_sprite.play("roll") # Play roll animation
  112.  
  113. func roll_update(_delta: float):
  114. # Check if the player is still on the floor
  115. if is_on_floor():
  116. # Keep rolling in the last facing direction
  117. velocity.x = roll_direction * roll_speed
  118. else:
  119. # If player rolls off the edge, stop the roll and let gravity take effect
  120. main_sm.dispatch(&"state_ended")
  121.  
  122. # Called when roll timer ends
  123. func _on_roll_timer_timeout() -> void:
  124. main_sm.dispatch(&"state_ended") # Transition back to idle or other state
  125.  
  126. # Die function to handle death when colliding with Killzone
  127. func die():
  128. main_sm.dispatch(&"player_die")
  129.  
  130. # Die state - Handles death animation, time scale, and scene reload
  131. func die_start():
  132. animation_sprite.play("die")
  133. animation_sprite.flip_h = (last_facing_direction == -1) # Flip animation based on last direction
  134. Engine.time_scale = 0.5 # Slow down time
  135. death_timer.start(1.0) # Start death timer for 1 second
  136. velocity = Vector2.ZERO
  137.  
  138. func die_update(_delta: float) -> void:
  139. pass # Your existing logic here, if any
  140.  
  141. # Called when death_timer ends
  142. func _on_death_timer_timeout() -> void:
  143. Engine.time_scale = 1 # Reset time scale to normal
  144. get_tree().reload_current_scene() # Reload the current scene
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement