コード例 #1
0
        // PRIVATE / PROTECTED METHODS //////////////////////

        // When saving a UFlow state, number flows of the same type.
        private string GetAddFlowID(Dictionary <string, List <UMachine> > flows, UMachine flow)
        {
            string flowID = flow?.FlowID;

            if (string.IsNullOrEmpty(flowID) || !_flows.ContainsKey(flowID))
            {
                return(null);
            }
            if (!flows.ContainsKey(flowID))
            {
                flows.Add(flowID, new List <UMachine>()
                {
                    flow
                });
                return(flowID + "#0");
            }
            int index = flows[flowID].IndexOf(flow);

            if (index < 0)
            {
                index = flows[flowID].Count;
                flows[flowID].Add(flow);
            }
            return(flowID + "#" + index);
        }
コード例 #2
0
        public UMachine Invoke(string flowID, UMachine flow = null)
        {
            UMachine result = Instantiate(flowID, flow);

            result?.OnEnter();
            return(result);
        }
コード例 #3
0
ファイル: UController.cs プロジェクト: rasmus-z/DeWinter
 void OnDestroy()
 {
     if (_machine != null)
     {
         _machine.Dispose();
     }
     _machine = null;
 }
コード例 #4
0
        internal UController GetController(UMachine machine)
        {
            UController result;

            while (machine._machine != null)
            {
                machine = machine._machine;
            }
            return(_controllers.TryGetValue(machine, out result) ? result : null);
        }
コード例 #5
0
        internal void RemoveMachine(UMachine machine)
        {
            UController controller;

            if (_controllers.TryGetValue(machine, out controller))
            {
                controller._machine = null;
                _controllers.Remove(machine);
            }
            _active.Remove(machine);
        }
コード例 #6
0
        private UMachine RestoreFlow(string[] state, Dictionary <string, UMachine> machines)
        {
            // Corrupted state; early out
            int len = state?.Length ?? 0;

            if (len < 1)
            {
                return(null);
            }
            DirectedGraph <UStateNode> graph;

            string[] stateStr = state[0].Split('#');

            // Couldn't find graph; early out
            if (stateStr.Length < 1 || !_flows.TryGetValue(stateStr[0], out graph))
            {
                return(null);
            }

            string machineID = stateStr[0];

            int[] init = new int[len - 1];
            Dictionary <int, UMachine> subflows = new Dictionary <int, UMachine>();
            UMachine machine;
            UMachine result = Instantiate(machineID);

            for (int i = 0; i < len - 1; ++i)
            {
                stateStr = state[i + 1].Split(':');
                init[i]  = int.Parse(stateStr[0]);
                if (stateStr.Length > 1)
                {
                    // subflows are states that contain other machines
                    // subflows need to be rebuilt before flows that depend on them
                    if (machines.TryGetValue(stateStr[1], out machine))
                    {
                        machine._Flow     = result;
                        machine.ID        = graph.Nodes[init[i]].ID;
                        subflows[init[i]] = machine;
                    }
                    else
                    {
                        return(null); // Not all machines instantiated; early out
                    }
                }
            }

            if (result.RestoreStates(state, subflows))
            {
                return(result);
            }
            result.Dispose();
            return(null);
        }
コード例 #7
0
        public UMachine InvokeMachine(string machineID)
        {
            UStateNode <UMachine, string> node = new UStateNode <UMachine, string>();

            node.Data = machineID;
            UMachine mac = BuildMachine(node);

            if (mac != null)
            {
                mac.OnEnterState();
            }
            return(mac);
        }
コード例 #8
0
        public UMachine Instantiate(string flowID, UMachine flow = null)
        {
            if (!_flows.ContainsKey(flowID))
            {
                return(null);
            }
            UMachine result = new UMachine(this)
            {
                _Flow = flow
            };

            result.Initialize(flowID);
            _active.Add(result);
            return(result);
        }
コード例 #9
0
        internal UMachine BuildMachine(UStateNode <UMachine, string> node)
        {
            UMachineGraph graph;

            if (!_machines.TryGetValue(node.Data, out graph))
            {
                return(null);
            }
            UMachine mac = new UMachine();

            mac.SetData(node.Data);
            mac._graph = graph;
            mac._uflow = this;
            mac._node  = node;
            _active.Add(mac);
            return(mac);
        }
コード例 #10
0
 // Remove a machine from the active machines list.
 // Note that this does not fully dispose of the removed machine.
 internal void DectivateMachine(UMachine machine) => _active.Remove(machine);
コード例 #11
0
 public bool Exit(UMachine flow)
 {
     flow?.Exit();
     return(flow != null);
 }
コード例 #12
0
 internal virtual UState Instantiate(UMachine flow) => new UState()
 {
     ID = this.ID, _Flow = flow
 };