49 lines
830 B
GDScript3
Raw Normal View History

2024-02-15 12:45:17 +00:00
class_name StateComponent
extends Node
enum State {
None,
Idle,
Run,
Jump,
Fall,
Skill,
}
signal transitioned(from: State, to: State)
var movement: MovementComponent
var _current: State
func _ready():
current = State.Idle
movement = owner.find_child("Movement")
func _process(_delta):
if current == State.Skill:
return
current = next_state()
func next_state() -> State:
if movement.body.velocity.y > 0:
return State.Jump
elif movement.body.velocity.y < 0:
return State.Fall
elif movement.direction != Vector3.ZERO:
return State.Run
else:
return State.Idle
## Current character state getter and setter.
var current: State:
get:
return _current
set(new_state):
if _current == new_state:
return
var old_state := _current
_current = new_state
transitioned.emit(old_state, new_state)