コード例 #1
0
    // Conversion Methods

    /// <summary>
    /// Creates and returns the <see cref="FSM"/> corresponding to this <see cref="XMLElement"/>
    /// </summary>
    /// <param name="parent"></param>
    /// <param name="selectedNode"></param>
    /// <returns></returns>
    public FSM ToFSM(ClickableElement parent, BaseNode selectedNode = null)
    {
        FSM fsm = ScriptableObject.CreateInstance <FSM>();

        fsm.InitFSMFromXML(parent, this.windowPosX, this.windowPosY, this.Id, this.name);

        foreach (XMLElement node in this.nodes)
        {
            switch (node.elemType)
            {
            case nameof(FSM):
                node.ToFSM(fsm, null);
                break;

            case nameof(BehaviourTree):
                node.ToBehaviourTree(fsm, null);
                break;

            case nameof(UtilitySystem):
                node.ToUtilitySystem(fsm, null);
                break;

            case nameof(StateNode):
                StateNode state = node.ToStateNode(fsm);

                if (node.secondType.Equals(stateType.Entry.ToString()))
                {
                    fsm.AddEntryState(state);
                }
                else
                {
                    fsm.nodes.Add(state);
                }
                break;

            default:
                Debug.LogError("Wrong content in saved data");
                break;
            }
        }

        foreach (XMLElement trans in this.transitions)
        {
            BaseNode node1 = fsm.nodes.Where(n => n.identificator == trans.fromId || n.subElem?.identificator == trans.fromId).FirstOrDefault();
            BaseNode node2 = fsm.nodes.Where(n => n.identificator == trans.toId || n.subElem?.identificator == trans.toId).FirstOrDefault();
            if (node1 != null && node2 != null)
            {
                fsm.AddTransition(trans.ToTransitionGUI(fsm, node1, node2));
            }
        }

        if (parent)
        {
            switch (parent.GetType().ToString())
            {
            case nameof(FSM):
                StateNode state = ScriptableObject.CreateInstance <StateNode>();
                state.InitStateNodeFromXML(parent, stateType.Unconnected, fsm.windowRect.position.x, fsm.windowRect.position.y, this.Id, this.name, fsm);

                if (this.secondType.Equals(stateType.Entry.ToString()))
                {
                    ((FSM)parent).AddEntryState(state);
                }
                else
                {
                    parent.nodes.Add(state);
                }
                break;

            case nameof(BehaviourTree):
                BehaviourNode node = ScriptableObject.CreateInstance <BehaviourNode>();
                node.InitBehaviourNode(parent, behaviourType.Leaf, fsm.windowRect.x, fsm.windowRect.y, fsm);

                parent.nodes.Add(node);

                if (selectedNode != null)
                {
                    TransitionGUI transition = ScriptableObject.CreateInstance <TransitionGUI>();
                    transition.InitTransitionGUI(parent, selectedNode, node);

                    parent.transitions.Add(transition);

                    selectedNode = node;
                }
                break;

            case nameof(UtilitySystem):
                UtilityNode utilNode = ScriptableObject.CreateInstance <UtilityNode>();
                utilNode.InitUtilityNode(parent, utilityType.Action, fsm.windowRect.position.x, fsm.windowRect.position.y, fsm);

                parent.nodes.Add(utilNode);
                break;
            }
        }

        return(fsm);
    }