Example #1
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);
        }
Example #2
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);
        }
Example #3
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);
        }
Example #4
0
    /// <summary>
    /// Fills in decision tree starting from 'rootState' until 'depths' has reached a predefined limit.
    /// </summary>
    /// <param name="rootState"></param>
    /// <param name="type"></param>
    /// <param name="depths"></param>
    public void FillStatesFromRoot(State rootState, State.Type type, int depths)
    {
        if (depths > 5)
            return;

        depths++;

        for (int i = 0; i < GameManager.boardWidth; i++) //there are 'width' states
        {
            State.Type nextType;
            if (type == State.Type.Max)
                nextType = State.Type.Min;
            else
                nextType = State.Type.Max;

            State newState = new State(GameManager.boardHeight, GameManager.boardWidth, nextType);

            if (depths == 5)
            {
                //newState.Score();
                newState.terminal = true;
            }

            rootState.AddChild(newState);

            FillStatesFromRoot(newState, newState.type, depths);
        }
    }
Example #5
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"]);
        }
Example #6
0
 protected void AddChildren(State state, XmlNodeList childNodes)
 {
     foreach (XmlNode childList in childNodes)
     {
         if (childList.Name == "children")
         {
             foreach (XmlNode child in childList.ChildNodes)
             {
                 foreach (XmlAttribute attribute in child.Attributes)
                 {
                     if (attribute.Name.Equals("id"))
                     {
                         int value = StateIds.Index(attribute.Value);
                         if (value != StateIds.NONE)
                         {
                             state.AddChild(value);
                         }
                         break;
                     }
                 }
             }
             break;
         }
     }
 }
Example #7
0
        protected override IState BuildState()
        {
            var root = new State();

            var context = Context.GlobalContext;

            var types = GetType().Assembly.GetTypes()
                        .Where(x => typeof(GameProcedure <TProcedureController, TProcedureIndex>).IsAssignableFrom(x));

            var procedures = new List <GameProcedure <TProcedureController, TProcedureIndex> >();

            foreach (var type in types)
            {
                if (!(context.Create(type) is GameProcedure <TProcedureController, TProcedureIndex> instance))
                {
                    continue;
                }

                instance.SetContext((TProcedureController)this);
                procedures.Add(instance);
            }

            procedures = procedures.OrderBy(x => x.Index).ToList();

            foreach (var procedure in procedures)
            {
                var id = procedure.Index;

                if (Indices.ContainsKey(id))
                {
                    Debug.LogErrorFormat("{0}[{1}] already added.", id, procedure.GetType().Name);
                    continue;
                }

                Indices.Add(id, procedure);
                IndexLookup.Add(procedure, id);
                root.AddChild(id.ToString(CultureInfo.InvariantCulture), procedure);
            }

            Root = root;
            if (procedures.Count <= 0)
            {
                return(Root);
            }

            if (procedures.Any(p => p.Index.Equals(InitState)))
            {
                ChangeState(InitState);
            }
            else
            {
                var first = procedures[0].Index;
                Her.Warn($"Procedure of [{InitState}] is no available, change to {first} instead.");
                ChangeState(first);
            }

            return(Root);
        }
Example #8
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());
        }
Example #9
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);
        }
        public CapturePoint()
        {
            Immovable = true;
            this.AddCollisionGroup("capturePoint");
            this.Collidable = true;
            LoadTexture(Controller.Content.Load <Texture2D>("images/CapturePoint"), 128, 96);
            AddAnimation("uncaptured", new int[2] {
                0, 0
            }, 0);
            AddAnimation("captured", new int[2] {
                1, 1
            }, 0);

            this.BloodParticles = new ParticleEngine(Controller.Content.Load <ParticleEffect>("bloodParticle"));
            state.AddChild(this.BloodParticles);
            this.BloodParticles.Position = this.Position;
            this.BloodParticles.Start();

            this.GlowTexture = Controller.Content.Load <Texture2D>("glow");
            CreateTakingOverRectangle();
            AddAnimation("p1uncaptured", new int[1] {
                0
            }, 0);
            AddAnimation("p1captured", new int[3] {
                1, 2, 3
            }, 0.5f);
            AddAnimation("p2uncaptured", new int[1] {
                0
            }, 0);
            AddAnimation("p2captured", new int[3] {
                4, 5, 6
            }, 0.5f);
            AddAnimation("p3uncaptured", new int[1] {
                0
            }, 0);
            AddAnimation("p3captured", new int[3] {
                7, 8, 9
            }, 0.5f);
            AddAnimation("p4uncaptured", new int[1] {
                0
            }, 0);
            AddAnimation("p4captured", new int[3] {
                10, 11, 12
            }, 0.5f);

            soundTakeOver = Controller.Content.Load <SoundEffect>("sounds/takeOver");
            soundTaken    = Controller.Content.Load <SoundEffect>("sounds/winCapture");
        }
Example #11
0
        public override IState BuildState()
        {
            var root = new State();

            var types = GetType().Assembly.GetTypes()
                        .Where(x => typeof(GameProcedure <TProcedureController, TProcedureIndex>).IsAssignableFrom(x));

            var instances = new List <GameProcedure <TProcedureController, TProcedureIndex> >();

            foreach (var type in types)
            {
                var instance = Activator.CreateInstance(type) as GameProcedure <TProcedureController, TProcedureIndex>;
                if (instance != null)
                {
                    instance.SetContext((TProcedureController)this);
                    instances.Add(instance);
                }
            }

            instances = instances.OrderBy(x => x.Index).ToList();

            for (var i = 0; i < instances.Count; i++)
            {
                var instance = instances[i];
                var id       = instance.Index;

                if (Indices.ContainsKey(id))
                {
                    Debug.LogErrorFormat("{0}[{1}] already added.", id, instance.GetType().Name);
                    continue;
                }

                Indices.Add(id, instance);
                IndexLookup.Add(instance, id);
                root.AddChild(id.ToString(CultureInfo.InvariantCulture), instance);
            }

            Root = root;
            if (instances.Count > 0)
            {
                ChangeState(instances[0].Index);
            }

            return(Root);
        }
Example #12
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);
        }
        private State CreateState(IVertex parent, StateDefinition stateDef, VertexLookup vertexLookup)
        {
            var state = new State(parent, stateDef.ShortName);

            vertexLookup.Add(state);

            foreach (var component in stateDef.Contents)
            {
                switch (component)
                {
                case StateDefinition childState:
                    state.AddChild(CreateState(state, childState, vertexLookup));
                    break;

                default:
                    // Ignore everything else in this pass
                    break;
                }
            }

            return(state);
        }
Example #14
0
 public void AddState(State newState, State parentState)
 {
     parentState.AddChild(newState);
 }
Example #15
0
 public void AddState(StrHash name, State state)
 {
     rootState.AddChild(state, name);
 }