Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends CharacterBody2D
- @onready var animation_sprite = $AnimatedSprite2D
- @onready var last_facing_direction = $".".last_facing_direction
- const speed = 125
- const gravity = 10
- const jump_power = -225
- const roll_speed = 225
- var dir
- var main_sm: LimboHSM
- func _ready():
- initiate_state_machine()
- func _physics_process(_delta: float) -> void:
- dir = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
- if dir:
- velocity.x = dir * speed
- else:
- velocity.x = move_toward(velocity.x, 0, speed)
- velocity.y += gravity
- flip_sprite(dir)
- move_and_slide()
- func flip_sprite(dir):
- if dir == 1:
- animation_sprite.flip_h = false
- elif dir == -1:
- animation_sprite.flip_h = true
- func _unhandled_input(event):
- if event.is_action_pressed("jump"):
- main_sm.dispatch(&"to_jump")
- elif event.is_action_pressed("roll"):
- main_sm.dispatch(&"to_roll")
- func initiate_state_machine():
- main_sm = LimboHSM.new()
- add_child(main_sm)
- var idle_state = LimboState.new().named("idle").call_on_enter(idle_start).call_on_update(idle_update)
- var run_state = LimboState.new().named("run").call_on_enter(run_start).call_on_update(run_update)
- var die_state = LimboState.new().named("die").call_on_enter(die_start).call_on_update(die_update)
- var jump_state = LimboState.new().named("jump").call_on_enter(jump_start).call_on_update(jump_update)
- var roll_state = LimboState.new().named("roll").call_on_enter(roll_start).call_on_update(roll_update)
- main_sm.add_child(idle_state)
- main_sm.add_child(run_state)
- main_sm.add_child(die_state)
- main_sm.add_child(jump_state)
- main_sm.add_child(roll_state)
- main_sm.initial_state = idle_state
- main_sm.add_transition(idle_state, run_state, &"to_run")
- main_sm.add_transition(main_sm.ANYSTATE, idle_state, &"state_ended")
- main_sm.add_transition(idle_state, jump_state, &"to_jump")
- main_sm.add_transition(run_state, jump_state, &"to_jump")
- main_sm.add_transition(roll_state, jump_state, &"to_jump")
- main_sm.add_transition(main_sm.ANYSTATE, die_state, &"player_die")
- main_sm.add_transition(idle_state, roll_state, &"to_roll")
- main_sm.add_transition(run_state, roll_state, &"to_roll")
- main_sm.initialize(self)
- main_sm.set_active(true)
- func idle_start():
- animation_sprite.play("idle")
- func idle_update(_delta: float):
- if velocity.x != 0:
- main_sm.dispatch(&"to_run")
- func run_start():
- animation_sprite.play("run")
- func run_update(_delta: float):
- if velocity.x == 0:
- main_sm.dispatch(&"state_ended")
- func die_start():
- pass
- func die_update(_delta: float):
- pass
- func jump_start():
- animation_sprite.play("jump")
- velocity.y = jump_power
- func jump_update(_delta: float):
- if is_on_floor():
- main_sm.dispatch(&"state_ended")
- func roll_start():
- $roll_timer.start()
- animation_sprite.play("roll")
- if dir == 0:
- dir = last_facing_direction
- velocity.x = dir * roll_speed
- !move_and_slide()
- func roll_update(_delta: float):
- pass
- func _on_roll_timer_timeout() -> void:
- main_sm.dispatch(&"state_ended")
Advertisement
Add Comment
Please, Sign In to add comment