public override bool Evaluate()
    {
        //this method is called to do a potential state transition
        foreach (FiniteStateMachineTransition t in currentNode.Transitions)
        {
            //if the condition is satisfied, we move to that transitions end node
            if (t.Evaluate())
            {
                currentNode = t.End;
                break;
            }
        }

        //then execute the action at the current node
        try
        {
            currentNode.Execute();
        }

        catch
        {
            return(false);
        }

        return(true);
    }
 public FiniteStateMachineTransition(string name, FiniteStateMachineNode start, FiniteStateMachineNode end, Func <bool> condition, int priority)
 {
     this.name      = name;
     this.start     = start;
     this.end       = end;
     this.condition = condition;
     this.priority  = priority;
 }
    private void SetFiniteStateMachine()
    {
        FiniteStateMachineNode wander     = new FiniteStateMachineNode("wander", SetWander);
        FiniteStateMachineNode turnAround = new FiniteStateMachineNode("turn around", SetTurnAround);
        FiniteStateMachineNode runAway    = new FiniteStateMachineNode("run away", SetRunAway);
        FiniteStateMachineNode stayPut    = new FiniteStateMachineNode("stay put", SetStayPut);

        wander.AddTransition("Player in line of sight", PlayerInLineOfSight, 2, runAway);
        wander.AddTransition("At Dead End", AtDeadEnd, 1, turnAround);

        runAway.AddTransition("Player not in line of sight", PlayerNotInLineOfSight, 1, wander);

        turnAround.AddTransition("Not at dead end", NotAtDeadEnd, 1, wander);
        turnAround.AddTransition("Player in line of sight", PlayerInLineOfSight, 2, stayPut);

        stayPut.AddTransition("Player not in line of sight", PlayerNotInLineOfSight, 1, wander);

        finiteStateMachine = new FiniteStateMachine(wander);
    }
 public FiniteStateMachine(FiniteStateMachineNode start_node)
 {
     currentNode = start_node;
 }
 public void AddTransition(string name, Func <bool> condition, int priority, FiniteStateMachineNode end)
 {
     transitions.Add(new FiniteStateMachineTransition(name, this, end, condition, priority));
     SortTransitions();
 }