Пример #1
0
        public void Foreach()
        {
            FSMachine fsm = GetExprFSM();

            fsm.Logger = new LogMessageTrace();

            int[] intArray = new int[] { 1, 2, 3 };
            fsm.GlobalContext.AddParameter(intArray.GetType(), "array");
            fsm.GlobalContext.SetParameter("array", intArray);

            fsm.LoadXml(TestUtils.LoadText("xml.actions.foreach.xml"));
            fsm.Start();

            Assert.AreEqual("index:0, value:1_index:1, value:2_index:2, value:3", fsm.Logger.ToString());

            fsm        = GetExprFSM();
            fsm.Logger = new LogMessageTrace();

            List <int> listArray = new List <int>(new int[] { 1, 2, 3 });

            fsm.GlobalContext.AddParameter(listArray.GetType(), "array");
            fsm.GlobalContext.SetParameter("array", listArray);
            fsm.LoadXml(TestUtils.LoadText("xml.actions.foreach.xml"));
            fsm.Start();

            Assert.AreEqual("index:0, value:1_index:1, value:2_index:2, value:3", fsm.Logger.ToString());
            //System.Func<A, B> aa=null;
            //System.Func<int, object> cc = null;
            //System.Func<B, A> bb = null;
            //bb = aa;
        }
Пример #2
0
        public void Transition_Event_Cond()
        {
            State root = new State("root");

            root.Initial = "s1";
            State s1 = new State("s1");

            s1.AddParamerter(new Parameter(typeof(bool), "isTrue"));
            s1.AddTransition(new Transition("s2", "to.s2", Variable <int>("isTrue")));
            root.AddChild(s1);

            State s2 = new State("s2");

            root.AddChild(s2);

            FSMachine fsm = GetFSM();

            fsm.SetRoot(root);

            fsm.Start();
            Assert.AreEqual("s1", fsm.Current.Name);

            fsm.SendEvent("to.s2");
            fsm.Update();
            Assert.AreEqual("s1", fsm.Current.Name);

            fsm.GetState("s1").SetParameter("isTrue", true);
            fsm.SendEvent("to.s2");
            fsm.Update();

            Assert.AreEqual("s2", fsm.Current.Name);
        }
Пример #3
0
 /// <summary>
 /// method called when the enemy FSM is created,
 /// Add the enemy FSM to the list an start the FSM
 /// </summary>
 /// <param name="machine"></param>
 private void AddNewEnemyFSM(FSMachine machine)
 {
     // add the new FSM to the list
     _stateMachines.Add(machine);
     // call to init the state machine
     machine.Create();
 }
Пример #4
0
        public void Parameter_Child()
        {
            State root = new State("root");

            root.Initial = "s1";
            root.AddParamerter <int>("a");
            State s1 = new State("s1");

            s1.AddParamerter <int>("b");
            root.AddChild(s1);
            root.AddParamerter <int>("result");

            FSMachine fsm = GetFSM();

            fsm.SetRoot(root);
            //Assert.IsFalse(fsm.Root.ContainsParameter("a"));
            fsm.Start();
            Assert.IsTrue(fsm.Root.ContainsParameter("a"));
            Assert.IsFalse(fsm.Root.ContainsParameter("b"));

            fsm["a"] = 1;
            fsm.GetState("s1")["b"] = 2;
            Assert.IsTrue(fsm.GetState("s1").ContainsParameter("b"));
            fsm.GetState("s1").GetContext().EvalExpression(Assign(Variable <int>("result"), Add(Variable <int>("a"), Variable <int>("b"))));
            Assert.AreEqual(1, fsm["a"]);
            Assert.AreEqual(2, fsm.GetState("s1")["b"]);
            Assert.AreEqual(3, fsm.GetState("s1")["result"]);
        }
Пример #5
0
        public void Transition_Multi()
        {
            State root = new State("root");

            root.Initial = "s1";

            State s1 = new State("s1");

            s1.AddTransition(new Transition("s2"));
            root.AddChild(s1);

            State s2 = new State("s2");

            s2.AddTransition(new Transition("s3"));
            root.AddChild(s2);

            State s3 = new State("s3");

            s3.AddTransition(new Transition("s4"));
            root.AddChild(s3);

            State s4 = new State("s4");

            root.AddChild(s4);

            FSMachine fsm = new FSMachine();

            fsm.SetRoot(root);
            fsm.Start();

            Assert.AreEqual("s4", fsm.Current.Name);
        }
Пример #6
0
        public void Transition_NotTransitionParent()
        {
            State root = new State("root");

            root.Initial = "s1";

            State s1 = new State("s1");

            s1.Initial = "s11";

            State s11 = new State("s11");

            s11.AddTransition(new Transition("s2"));
            s1.AddChild(s11);

            root.AddChild(s1);

            State s2 = new State("s2");

            root.AddChild(s2);

            FSMachine fsm = new FSMachine();

            fsm.SetRoot(root);
            fsm.Start();

            Assert.AreEqual("s1", fsm.Current.Name);
        }
Пример #7
0
        public void Parameter()
        {
            State root = new State();

            root.Initial = "s1";
            root.AddParamerter <int>("a");
            root.AddParamerter <int>("b");

            FSMachine fsm = new FSMachine();

            fsm.SetRoot(root);
            //Assert.IsFalse(fsm.Root.ContainsParameter("a"));
            fsm.Start();
            Assert.IsTrue(fsm.Root.ContainsParameter("a"));

            fsm["a"] = 1;
            fsm["b"] = 2;
            Assert.AreEqual(1, fsm["a"]);
            Assert.AreEqual(2, fsm["b"]);
            try
            {
                fsm.Root.GetParameter("1");
                Assert.Fail();
            }
            catch (FSMParameterException ex)
            {
            }
        }
Пример #8
0
    /// <summary>
    /// Constructor for the state, will be call when the state machine is being constructed
    /// </summary>
    /// <param name="stateMachine">state machine owner of this state</param>
    public FSMState(FSMachine stateMachine)
    {
        // init the transition list
        _transitions = new List <FSMTransition>();

        // state machine owner of this state
        StateMachine = stateMachine;
    }
Пример #9
0
        public void Cancel()
        {
            FSMachine fsm = new FSMachine();

            fsm.LoadXml(TestUtils.LoadText("xml.actions.cancel.xml"));
            fsm.Start();
            Assert.AreEqual("s1", fsm.Current.Name);
        }
Пример #10
0
        public void Raise()
        {
            FSMachine fsm = new FSMachine();

            fsm.LoadXml(TestUtils.LoadText("xml.actions.raise.xml"));

            fsm.Start();
            Assert.AreEqual("s3", fsm.Current.Name);
        }
Пример #11
0
        public static FSMachine GetFSM()
        {
            FSMContext context = new FSMContext();

            context.ExpressionProvider = new FSMExpressionProvider();
            FSMachine fsm = new FSMachine(context);

            return(fsm);
        }
Пример #12
0
        public void Assign()
        {
            FSMachine fsm = GetExprFSM();

            fsm.LoadXml(TestUtils.LoadText("xml.actions.assign.xml"));
            fsm.Start();
            Assert.AreEqual(1, fsm["int32ByValue"]);
            Assert.AreEqual("Hello World", fsm["stringByExpr"]);
        }
Пример #13
0
        public void Raise_Data()
        {
            FSMachine fsm = GetExprFSM();

            fsm.LoadXml(TestUtils.LoadText("xml.actions.raise_data.xml"));
            fsm.Logger = new LogMessageTrace();
            fsm.Start();
            Assert.AreEqual("s2", fsm.Current.Name);
            Assert.AreEqual("Hello World", fsm.Logger.ToString());
        }
Пример #14
0
    public override void _Ready()
    {
        _fs = (FSMachine)Owner.GetNodeOrNull("FSMachine");

        if (_fs == null)
        {
            GD.Print($"{Owner.Name} does not have a state machine!");
            QueueFree();
        }

        _owner = (MonsterCharacter)Owner;
    }
Пример #15
0
        public void Log()
        {
            FSMachine fsm = GetExprFSM();

            fsm.LoadXml(TestUtils.LoadText("xml.actions.log.xml"));
            StringTraceLog log = new StringTraceLog();

            fsm.Logger = log;
            fsm.Start();

            Assert.AreEqual(":root.msg_string:root.type.string_format:root.format_:s1.onEntry_expr:s1.type.expr_format:1+2=3",
                            log.ToString());
        }
Пример #16
0
        public void Empty()
        {
            State root = new State();

            FSMachine fsm = new FSMachine();

            fsm.SetRoot(root);
            Assert.IsFalse(fsm.Root.IsActive);
            Assert.IsNull(fsm.Current);

            fsm.Start();
            Assert.IsNull(fsm.Current);
            Assert.IsTrue(fsm.Root.IsActive);
        }
Пример #17
0
    /// <summary>
    /// method called when a enemy should be spawn,
    /// -create the enemy with the enemy factory
    /// -call the method "CreateEnemyBehaviorFSM" for create the enemy state machine with the spawn info data
    /// -set the FSM to the enemy
    /// -add the FSM to the list
    /// -remove the spawn info from the level spawn info
    /// </summary>
    private void processEnemyInfo()
    {
        // create the enemy
        EnemyEntity enemyBeingSpawn = FactoryEnemies.CreateEnemy(_enemiesSpawnInfo[0].EnemyPrefab);
        // create the FSM for the enemy with the spawn data
        FSMachine machine = CreateEnemyBehaviorFSM(enemyBeingSpawn, _enemiesSpawnInfo[0]);

        // set the behavior var for the enemy
        enemyBeingSpawn.GetComponentInChildren <EnemyBehavior>().Behavior = machine;
        // adding the enemy to the FSM
        AddNewEnemyFSM(machine);
        // remove for the info
        _enemiesSpawnInfo.RemoveAt(0);
    }
Пример #18
0
        public void Custom()
        {
            FSMachine fsm    = new FSMachine();
            var       reader = new Model.Xml.FSMXmlReader();

            reader.AddActionReader("myAction", "urn:test", typeof(MyAction), (Model.Xml.FSMXmlReader r) =>
            {
                MyAction action = new MyAction();
                action.Text     = r.ReadAttributeValue <string>("text", null);

                return(action);
            });
            fsm.SetRoot(reader.Read(TestUtils.LoadText("xml.actions.custom.xml")));
            fsm.Logger = new LogMessageTrace();
            fsm.Start();
            Assert.AreEqual("hello world", fsm.Logger.ToString());
        }
Пример #19
0
        public void If()
        {
            FSMachine fsm = GetExprFSM();

            fsm.LoadXml(TestUtils.LoadText("xml.actions.if.xml"));
            fsm.Logger = new LogMessageTrace();
            fsm.Start();
            fsm.SendEvent("to.s2");
            fsm.Update();
            Assert.AreEqual("s2", fsm.Current.Name);
            Assert.AreEqual("s2:0", fsm.Logger.ToString());

            fsm = GetExprFSM();
            fsm.LoadXml(TestUtils.LoadText("xml.actions.if.xml"));
            fsm.Logger = new LogMessageTrace();
            fsm.Start();
            fsm["n"] = 1;
            fsm.SendEvent("to.s2");
            fsm.Update();
            Assert.AreEqual("s2", fsm.Current.Name);
            Assert.AreEqual("s2:1", fsm.Logger.ToString());

            fsm = GetExprFSM();
            fsm.LoadXml(TestUtils.LoadText("xml.actions.if.xml"));
            fsm.Logger = new LogMessageTrace();
            fsm.Start();
            fsm["n"] = 2;
            fsm.SendEvent("to.s2");
            fsm.Update();
            Assert.AreEqual("s2", fsm.Current.Name);
            Assert.AreEqual("s2:2", fsm.Logger.ToString());

            fsm = GetExprFSM();
            fsm.LoadXml(TestUtils.LoadText("xml.actions.if.xml"));
            fsm.Logger = new LogMessageTrace();
            fsm.Start();
            fsm["n"] = 3;
            fsm.SendEvent("to.s2");
            fsm.Update();

            Assert.AreEqual("s2", fsm.Current.Name);
            Assert.AreEqual("s2:3", fsm.Logger.ToString());
        }
Пример #20
0
        public void Transition_Event()
        {
            State root = new State("root");

            root.Initial = "s1";

            State s1 = new State("s1");

            s1.AddTransition(new Transition("s2", "to.s2"));
            root.AddChild(s1);

            State s2 = new State("s2");

            root.AddChild(s2);

            StringBuilder sb = new StringBuilder();

            FSMachine fsm = new FSMachine();

            fsm.SetRoot(root);
            fsm.StateTransition += (object o, TransitionEventArgs e) =>
            {
                sb.Append(e.FromState == null ? "null" : e.FromState.Name)
                .Append("=>")
                .Append(e.ToState.Name);
            };


            fsm.Start();
            var current = fsm.Current;

            Assert.IsNotNull(current);
            Assert.AreEqual("s1", current.Name);

            fsm.SendEvent("to.s2");
            fsm.Update();

            current = fsm.Current;
            Assert.IsNotNull(current);
            Assert.AreEqual("s2", current.Name);
            Console.WriteLine(sb.ToString());
        }
Пример #21
0
        public void SendEvent()
        {
            State root = new State();
            State s1   = new State();

            s1.AddTransition(new Transition("s2", "s1.to.s2"));

            State s2 = new State();

            s2.AddTransition(new Transition("s1", "s2.to.s1"));


            FSMachine fsm = new FSMachine();

            fsm.SetRoot(root);
            Assert.IsFalse(fsm.Root.IsActive);
            Assert.IsNull(fsm.Current);

            fsm.Start();
            Assert.IsNull(fsm.Current);
            Assert.IsTrue(fsm.Root.IsActive);
        }
Пример #22
0
        public void Base()
        {
            State root = new State();

            root.Initial = "s1";

            State s1 = new State();

            s1.Name = "s1";

            root.AddChild(s1);

            FSMachine fsm = new FSMachine();

            fsm.SetRoot(root);
            Assert.IsNull(fsm.Current);
            fsm.Start();
            var current = fsm.Current;

            Assert.IsNotNull(current);
            Assert.AreEqual("s1", current.Name);
        }
Пример #23
0
        public void Parameter_ExprEval()
        {
            State root = new State();

            root.Initial = "s1";
            root.AddParamerter <int>("a");
            root.AddParamerter <int>("b");
            root.AddParamerter <int>("result");

            //FSMachine fsm = GetFSM();
            FSMachine fsm = new FSMachine();

            fsm.SetRoot(root);
            fsm.Start();
            Assert.IsTrue(fsm.Root.ContainsParameter("a"), "not contains param a");

            //fsm["a"] = 1;
            //fsm["b"] = 2;
            //fsm.Root.EvalExpression(Assign(Variable("result"), Add(Variable("a"), Variable("b"))));
            //Assert.AreEqual(1, fsm["a"]);
            //Assert.AreEqual(2, fsm["b"]);
            //Assert.AreEqual(3, fsm["result"]);
        }
Пример #24
0
        public void Transition_Entry()
        {
            State      root  = new State("root");
            EntryState entry = new EntryState();

            entry.AddTransition(new Transition("s2"));
            root.EntryState = entry;

            State s1 = new State("s1");

            root.AddChild(s1);

            State s2 = new State("s2");

            root.AddChild(s2);

            FSMachine fsm = new FSMachine();

            fsm.SetRoot(root);
            fsm.Start();

            Assert.AreEqual("s2", fsm.Current.Name);
        }
Пример #25
0
    public override void _Ready()
    {
        LocalCharacterManager.RegisterPresent(this);
        RegularAttack = GetNodeOrNull <RegularAttack>("RegularAttack");
        Animator      = GetNodeOrNull <CharacterAnimator>("Animator");


        //Check for an overrideStatblock -- this lets us set custom stats for pre-placed encounters
        Stats = GetNodeOrNull <Statblock>("OverrideStatblock");

        if (Stats == null)
        {
            Stats = GetNode <Statblock>("Statblock");            //Use default statblock if no custom one found.
        }
        else
        {
            GetNode <Statblock>("Statblock").QueueFree();            //Remove the regular one, we'll use the override!
        }
        _fsm    = ((FSMachine)GetNode("FSMachine"));
        onDeath = (EventCreateObject)GetNodeOrNull("OnDeath");

        _fsm.Activate();         //Setup once stats set
    }
Пример #26
0
 /// <summary>
 /// contrusctor for the transition, the time will be setted here
 /// </summary>
 /// <param name="stateMachine"></param>
 /// <param name="time"></param>
 public FSMTransitionTime(FSMachine stateMachine, float time) : base(stateMachine)
 {
     _time = time;
 }
Пример #27
0
 /// <summary>
 /// Constructor for the transition, will be call when the state machine is being constructed
 /// </summary>
 /// <param name="stateMachine">state machine owner of this transition</param>
 public FSMTransition(FSMachine stateMachine)
 {
     _nextState    = null;
     _stateMachine = stateMachine;
 }
Пример #28
0
 public FSMComponent(FSMachine fsm, bool isTarget)
 {
     m_IsTarget = isTarget;
     m_FSM      = fsm;
 }
Пример #29
0
 // Use this for initialization
 void Start()
 {
     _pathFinder   = GetComponent <PathFinder>();
     _stateMachine = new FSMachine();
 }
Пример #30
0
 /// <summary>
 /// empty constructor
 /// </summary>
 /// <param name="stateMachine">owner of this state</param>
 public FSMStateShot(FSMachine stateMachine) : base(stateMachine)
 {
 }