Пример #1
0
    /// <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 UtilitySystem ToUtilitySystem(ClickableElement parent, BaseNode selectedNode = null)
    {
        UtilitySystem utilSystem = ScriptableObject.CreateInstance <UtilitySystem>();

        utilSystem.InitUtilitySystemFromXML(parent, this.windowPosX, this.windowPosY, this.Id, this.name);

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

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

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

            case nameof(UtilityNode):
                UtilityNode state = node.ToUtilityNode(utilSystem);

                utilSystem.nodes.Add(state);
                break;

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

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

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

                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, utilSystem.windowRect.x, utilSystem.windowRect.y, utilSystem);

                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, utilSystem.windowRect.position.x, utilSystem.windowRect.position.y, utilSystem);

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

        return(utilSystem);
    }