using Godot;
using Godot.Collections;

namespace Cirno.Scripts.Components.FSM;

public partial class ActorStateMachine : CharacterBody2D
{
    public Dictionary<int, State> States { get; private set; } = new();

    private int _currentStateIndex = 0;

    public State CurrentState => States[_currentStateIndex];

    public override void _Ready()
    {
        var children = GetChildren();

        foreach (var child in children)
        {
            if (child is State state)
            {
                States.Add(state.StateId, state);
                state.Init(this);
            }
        }
    }

    public void SetState(int stateId)
    {
        CurrentState.ExitState();
        _currentStateIndex = stateId;
        CurrentState.EnterState();
    }

    public override void _Process(double delta)
    {
        CurrentState.ProcessState(delta);
    }

    public override void _PhysicsProcess(double delta)
    {
        CurrentState.PhysicsProcessState(delta);
    }
}