Added state component

This commit is contained in:
2024-02-15 13:45:17 +01:00
parent 17a507d07c
commit 37ed8890ec
19 changed files with 172 additions and 86 deletions

View File

@ -0,0 +1,48 @@
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)