Guest User

Untitled

a guest
Nov 1st, 2024
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. extends CharacterBody2D
  2.  
  3. @onready var animation_sprite = $AnimatedSprite2D
  4. @onready var last_facing_direction = $".".last_facing_direction
  5.  
  6. const speed = 125
  7. const gravity = 10
  8. const jump_power = -225
  9. const roll_speed = 225
  10. var dir
  11.  
  12. var main_sm: LimboHSM
  13.  
  14. func _ready():
  15. initiate_state_machine()
  16.  
  17. func _physics_process(_delta: float) -> void:
  18. dir = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
  19. if dir:
  20. velocity.x = dir * speed
  21. else:
  22. velocity.x = move_toward(velocity.x, 0, speed)
  23. velocity.y += gravity
  24.  
  25. flip_sprite(dir)
  26. move_and_slide()
  27.  
  28. func flip_sprite(dir):
  29. if dir == 1:
  30. animation_sprite.flip_h = false
  31. elif dir == -1:
  32. animation_sprite.flip_h = true
  33.  
  34. func _unhandled_input(event):
  35. if event.is_action_pressed("jump"):
  36. main_sm.dispatch(&"to_jump")
  37. elif event.is_action_pressed("roll"):
  38. main_sm.dispatch(&"to_roll")
  39.  
  40. func initiate_state_machine():
  41. main_sm = LimboHSM.new()
  42. add_child(main_sm)
  43.  
  44. var idle_state = LimboState.new().named("idle").call_on_enter(idle_start).call_on_update(idle_update)
  45. var run_state = LimboState.new().named("run").call_on_enter(run_start).call_on_update(run_update)
  46. var die_state = LimboState.new().named("die").call_on_enter(die_start).call_on_update(die_update)
  47. var jump_state = LimboState.new().named("jump").call_on_enter(jump_start).call_on_update(jump_update)
  48. var roll_state = LimboState.new().named("roll").call_on_enter(roll_start).call_on_update(roll_update)
  49.  
  50. main_sm.add_child(idle_state)
  51. main_sm.add_child(run_state)
  52. main_sm.add_child(die_state)
  53. main_sm.add_child(jump_state)
  54. main_sm.add_child(roll_state)
  55.  
  56. main_sm.initial_state = idle_state
  57.  
  58. main_sm.add_transition(idle_state, run_state, &"to_run")
  59. main_sm.add_transition(main_sm.ANYSTATE, idle_state, &"state_ended")
  60. main_sm.add_transition(idle_state, jump_state, &"to_jump")
  61. main_sm.add_transition(run_state, jump_state, &"to_jump")
  62. main_sm.add_transition(roll_state, jump_state, &"to_jump")
  63. main_sm.add_transition(main_sm.ANYSTATE, die_state, &"player_die")
  64. main_sm.add_transition(idle_state, roll_state, &"to_roll")
  65. main_sm.add_transition(run_state, roll_state, &"to_roll")
  66.  
  67. main_sm.initialize(self)
  68. main_sm.set_active(true)
  69.  
  70. func idle_start():
  71. animation_sprite.play("idle")
  72. func idle_update(_delta: float):
  73. if velocity.x != 0:
  74. main_sm.dispatch(&"to_run")
  75.  
  76. func run_start():
  77. animation_sprite.play("run")
  78. func run_update(_delta: float):
  79. if velocity.x == 0:
  80. main_sm.dispatch(&"state_ended")
  81.  
  82. func die_start():
  83. pass
  84. func die_update(_delta: float):
  85. pass
  86.  
  87. func jump_start():
  88. animation_sprite.play("jump")
  89. velocity.y = jump_power
  90. func jump_update(_delta: float):
  91. if is_on_floor():
  92. main_sm.dispatch(&"state_ended")
  93.  
  94. func roll_start():
  95. $roll_timer.start()
  96. animation_sprite.play("roll")
  97. if dir == 0:
  98. dir = last_facing_direction
  99. velocity.x = dir * roll_speed
  100. !move_and_slide()
  101. func roll_update(_delta: float):
  102. pass
  103.  
  104. func _on_roll_timer_timeout() -> void:
  105. main_sm.dispatch(&"state_ended")
Advertisement
Add Comment
Please, Sign In to add comment