﻿public class Selector<T> : Composite<T> 
{
    private Behavior<T> m_currentChild;
    private int m_currentIndex;
 
    public Selector() : base()
    {

    }

    public override void OnEnter(T blackboard)
    {
 	    base.OnEnter(blackboard);
        m_currentIndex = 0;
    }

    public override void OnExit(Status status, T blackboard)
    {
        base.OnExit(status, blackboard);
        foreach(Behavior<T> child in m_children)
        {
            child.Status = Status.INVALID;
        }
    }

    public override Status Update(float dt, T blackboard)
    {
        m_currentChild = m_children[m_currentIndex];
        Status s = m_currentChild.Tick(dt, blackboard);

        //  If the child succeeds or is still running, early return.
        if (s != Status.FAILURE)
        {
            return s;
        }
        m_currentIndex++;
        //  If the end of the children is reached, that means the whole thing fails.
        if (m_currentIndex == m_children.Count)
        {
            m_currentIndex = 0;
            return Status.FAILURE;
        }
        return Status.RUNNING;
    }
}
