/// <summary>
        /// Returns a copy of this node.
        /// <param name = "newOwner">The owner of the new node.</param>
        /// <returns>The copy of the node.</returns>
        /// </summary>
        public override ActionNode Copy(INodeOwner newOwner)
        {
            // Copy node
            var copy       = base.Copy(newOwner);
            var branchCopy = copy as BranchNode;

            // The copy is a valid branch?
            if (branchCopy != null)
            {
                // Copy children
                var copiedChildren = new List <ActionNode>();
                for (int i = 0; i < this.children.Length; i++)
                {
                    var copiedChild = this.children[i].Copy(newOwner);
                    if (copiedChild != null)
                    {
                        copiedChildren.Add(copiedChild);
                    }
                }

                branchCopy.SetChildren(copiedChildren.ToArray());
            }

            return(copy);
        }
Exemple #2
0
 public void SetOwner(INodeOwner owner)
 {
     Owner = owner;
     foreach (var node in GenNodes)
     {
         node.SetOwner(this);
     }
 }
Exemple #3
0
        public void AddChildToStart(ConditionalDialogueNode child, string child_id, INodeOwner owner)
        {
            if (child.ParentNode == null)
            {
                child.ParentNode = Nodes[0];
            }
            child.NodeID = child_id;
            child.SetNodeOwner(owner);

            Nodes.Add(child);
            Nodes[0].Add(child);
        }
Exemple #4
0
        /// <summary>
        /// Creates and returns a node of the supplied type.
        /// <param name="type">The node type to be created.</param>
        /// <param name="gameObject">The game object that owns the node.</param>
        /// <param name="nodeOwner">The node owner (usually a InternalStateBehaviour).</param>
        /// <returns>Returns new created node.</returns>
        /// </summary>
        public static ActionNode CreateInstance(Type type, GameObject gameObject, INodeOwner nodeOwner)
        {
            var newNode = Activator.CreateInstance(type) as ActionNode;

            if (newNode != null)
            {
                newNode.name = type.Name;
                newNode.Init(gameObject, nodeOwner);
                if (Application.isEditor)
                {
                    newNode.Reset();
                }
            }
            return(newNode);
        }
Exemple #5
0
        /// <summary>
        /// Returns a copy of this node.
        /// <param name = "newOwner">The owner of the new node.</param>
        /// <returns>The copy of the node.</returns>
        /// </summary>
        public virtual ActionNode Copy(INodeOwner newOwner)
        {
            // Copy node
            var copy = newOwner.AddNode(GetType());

            // Copy SerializedFields
            var fields = NodeSerialization.GetSerializedFields(copy.GetType());

            for (int i = 0; i < fields.Length; i++)
            {
                // Do not copy the instanceID field
                if (fields[i].Name != "instanceID")
                {
                    fields[i].SetValue(copy, fields[i].GetValue(this));
                }
            }

            return(copy);
        }
Exemple #6
0
        /// <summary>
        /// Returns a copy of this node.
        /// <param name = "newOwner">The owner of the new node.</param>
        /// <returns>The copy of the node.</returns>
        /// </summary>
        public override ActionNode Copy (INodeOwner newOwner) {
            // Copy node
            var copy = base.Copy(newOwner);
            var branchCopy = copy as BranchNode;

            // The copy is a valid branch?
            if (branchCopy != null) {
                // Copy children
                var copiedChildren = new List<ActionNode>();
                for (int i = 0; i < this.children.Length; i++) {
                    var copiedChild = this.children[i].Copy(newOwner);
                    if (copiedChild != null)
                        copiedChildren.Add(copiedChild);
                }

                branchCopy.SetChildren(copiedChildren.ToArray());
            }

            return copy;
        }
        /// <summary>
        /// Marks the state dirty flag; if the state is a prefab instance then disconnects the m_NodeSerialization property.
        /// <param name="owner">The target owner to mark the dirty flag.</param>
        /// </summary>
        public static void SetDirty(INodeOwner owner)
        {
            var unityObject = owner as UnityEngine.Object;

            if (!Application.isPlaying && unityObject != null)
            {
                // its a prefab Instance?
                var prefabType = UnityEditor.PrefabUtility.GetPrefabType(unityObject);
                if (prefabType != UnityEditor.PrefabType.None && prefabType != UnityEditor.PrefabType.Prefab && prefabType != UnityEditor.PrefabType.ModelPrefab)
                {
                    PropertyUtility.DisconnectPropertyFromPrefab(unityObject, "m_NodeSerialization");

                    // Workaround to disconnect ArraySize properties
                    owner.Clear();
                    PrefabUtility.RecordPrefabInstancePropertyModifications(unityObject);
                }
            }

            owner.SaveNodes();
            EditorUtility.SetDirty(unityObject);
        }
        /// <summary>
        /// Paste the state in StateUtility.stateToPaste in the supplied fsm.
        /// <param name="gameObject">The target gameObject.</param>
        /// <param name="originalStates">The original states.</param>
        /// <param name="parent">Optionally parent for the cloned states.</param>
        /// </summary>
        public static void CloneStates(GameObject gameObject, InternalStateBehaviour[] originalStates, ParentBehaviour parent)
        {
            if (gameObject != null && originalStates != null && originalStates.Length > 0)
            {
                var orginalClone = new Dictionary <InternalStateBehaviour, InternalStateBehaviour>();
                var originalFsm = parent != null ? originalStates[0].parent as InternalStateMachine : null;
                var newFsm = parent as InternalStateMachine;
                InternalStateBehaviour startState = null, concurrentState = null;
                InternalAnyState       anyState = null;

                // Copy blackboard data?
                var newBlackboard = gameObject.GetComponent <InternalBlackboard>();
                if (newBlackboard == null)
                {
                    // Get the original blackboard
                    InternalBlackboard originalBlackboard = originalStates[0].GetComponent <InternalBlackboard>();

                    #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2
                    Undo.RegisterSceneUndo("Paste State");
                    // Create the new blacbkoard
                    newBlackboard = gameObject.AddComponent(originalBlackboard.GetType()) as InternalBlackboard;
                    #else
                    // Create the new blacbkoard
                    newBlackboard = gameObject.AddComponent(originalBlackboard.GetType()) as InternalBlackboard;
                    if (newBlackboard != null)
                    {
                        Undo.RegisterCreatedObjectUndo(newBlackboard, "Paste State");
                    }
                    #endif

                    // Copy serialized values
                    EditorUtility.CopySerialized(originalBlackboard, newBlackboard);
                }

                foreach (InternalStateBehaviour state in originalStates)
                {
                    // Don't clone AnyState in StateMachines
                    if (state != null && (newFsm == null || !(state is InternalAnyState) || newFsm.anyState == null))
                    {
                        #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2
                        Undo.RegisterSceneUndo("Paste State");
                        // Create a new state
                        var newState = gameObject.AddComponent(state.GetType()) as InternalStateBehaviour;
                        #else
                        // Create a new state
                        var newState = gameObject.AddComponent(state.GetType()) as InternalStateBehaviour;
                        if (newState != null)
                        {
                            Undo.RegisterCreatedObjectUndo(newState, "Paste State");
                        }
                        #endif

                        if (newState != null)
                        {
                            // Store state
                            orginalClone.Add(state, newState);

                            // Copy serialized values
                            EditorUtility.CopySerialized(state, newState);

                            // Update blackboard
                            if (state.gameObject != newState.gameObject)
                            {
                                var serialObj = new SerializedObject(newState);
                                serialObj.FindProperty("m_Blackboard").objectReferenceValue = newBlackboard;
                                serialObj.ApplyModifiedProperties();
                                serialObj.Dispose();
                            }

                            // Update the AnyState, StartState and ConcurrentState
                            if (newState is InternalStateMachine)
                            {
                                var fsm = newState as InternalStateMachine;
                                fsm.startState      = null;
                                fsm.concurrentState = null;
                                fsm.anyState        = null;
                            }

                            EditorUtility.SetDirty(newState);

                            // Set new parent
                            if (parent != null)
                            {
                                newState.parent = parent;

                                // Update position
                                if (parent == state.parent)
                                {
                                    newState.position += new Vector2(20f, 20f);
                                }
                            }
                            else
                            {
                                newState.parent = null;
                            }

                            // Saves state and sets dirty flag
                            INodeOwner nodeOwner = newState as INodeOwner;
                            if (nodeOwner != null)
                            {
                                nodeOwner.LoadNodes();
                                StateUtility.SetDirty(nodeOwner);
                            }
                            else
                            {
                                EditorUtility.SetDirty(newState);
                            }

                            // Try to get the StartState, AnyState and ConcurrentState
                            if (originalFsm != null)
                            {
                                if (originalFsm.startState == state)
                                {
                                    startState = newState;
                                }
                                if (anyState == null)
                                {
                                    anyState = newState as InternalAnyState;
                                }
                                if (originalFsm.concurrentState == state)
                                {
                                    concurrentState = newState;
                                }
                            }
                        }
                    }
                }

                // Set StartState, AnyState and ConcurrentState
                if (newFsm != null)
                {
                    if (newFsm.startState == null)
                    {
                        newFsm.startState = startState;
                    }
                    if (newFsm.anyState == null)
                    {
                        newFsm.anyState = anyState;
                    }
                    if (newFsm.concurrentState == null)
                    {
                        newFsm.concurrentState = concurrentState;
                    }
                    EditorUtility.SetDirty(newFsm);
                }

                // Try to update the transitions' destination
                foreach (KeyValuePair <InternalStateBehaviour, InternalStateBehaviour> pair in orginalClone)
                {
                    InternalStateBehaviour state    = pair.Key;
                    InternalStateBehaviour newState = pair.Value;

                    // Update the newState transition
                    for (int i = 0; i < newState.transitions.Length && i < state.transitions.Length; i++)
                    {
                        // The original destination is valid?
                        if (state.transitions[i].destination != null && orginalClone.ContainsKey(state.transitions[i].destination))
                        {
                            newState.transitions[i].destination = orginalClone[state.transitions[i].destination];
                        }
                    }

                    if (newState is ParentBehaviour)
                    {
                        var stateAsParent = state as ParentBehaviour;

                        // Removes the newState from the children state to avoid an infinite loop
                        List <InternalStateBehaviour> children = stateAsParent.states;
                        if (children.Contains(newState))
                        {
                            children.Remove(newState);
                        }

                        StateUtility.CloneStates(newState.gameObject, children.ToArray(), newState as ParentBehaviour);
                    }

                    EditorUtility.SetDirty(newState);
                }

                EditorUtility.SetDirty(gameObject);
            }
        }
Exemple #9
0
 public void SetNodeOwner(INodeOwner owner)
 {
     NodeOwner = owner;
 }
        /// <summary>
        /// Call this to store the nodes.
        /// <param name="nodes">The list of nodes to be saved.</param>
        /// <param name="gameObject">The target game object.</param>
        /// <param name="nodeOwner">The node owner.</param>
        /// </summary>
        public void SaveNodes (ActionNode[] nodes, GameObject gameObject, INodeOwner nodeOwner) {
            m_ResaveNodes = false;

            // Validate parameters
            if (nodes == null || gameObject == null || nodeOwner == null)
                return;

            // Clear data
            Clear();

            // Update game object, tree and blackboard
            m_GameObject = gameObject;
            m_NodeOwner = nodeOwner;
            m_Blackboard = gameObject.GetComponent<InternalBlackboard>();

            // Save nodes
            for (int i = 0; i < nodes.Length; i++)
                Save (nodes[i]);

            m_NodeFieldStartIndex.Add(m_NextFieldIndex);
        }
        /// <summary>
        /// Call this to load nodes.
        /// <param name="gameObject">The target game object.</param>
        /// <param name="nodeOwner">The node owner.</param>
        /// </summary>
        public ActionNode[] LoadNodes (GameObject gameObject, INodeOwner nodeOwner) {

            var nodes = new List<ActionNode>();
            m_BranchChildren = new Dictionary<int, List<ActionNode>>();

            // Validate parameters
            if (gameObject == null || nodeOwner == null )
                return nodes.ToArray();

            m_GameObject = gameObject;
            m_NodeOwner = nodeOwner;
            m_Blackboard = gameObject.GetComponent<InternalBlackboard>();

            // Goes through all node types and creates an instance
            for (int i = 0; i < m_NodeTypes.Count; i++) {
                ActionNode node = Load(nodes, i);

                // It's a valid node?
                if (node != null)
                    nodes.Add(node);
            }

            // Add children to branch
            foreach (var pair in m_BranchChildren) {
                var branch = nodes[pair.Key] as BranchNode;

                if (branch == null || !branch.SetChildren(m_BranchChildren[pair.Key].ToArray()))
                    nodeOwner.HierarchyChanged();
            }

            m_BranchChildren = null;

            return nodes.ToArray();
        }
Exemple #12
0
 /// <summary>
 /// Creates and returns a node of the supplied type.
 /// <param name="type">The node type to be created.</param>
 /// <param name="gameObject">The game object that owns the node.</param>
 /// <param name="nodeOwner">The node owner (usually a InternalStateBehaviour).</param>
 /// <returns>Returns new created node.</returns>
 /// </summary>
 public static ActionNode CreateInstance (Type type, GameObject gameObject, INodeOwner nodeOwner) {
     var newNode = Activator.CreateInstance(type) as ActionNode;
     if (newNode != null) {
         newNode.name = type.Name;
         newNode.Init(gameObject, nodeOwner);
         if (Application.isEditor)
             newNode.Reset();
     }
     return newNode;
 }
Exemple #13
0
        /// <summary>
        /// Returns a copy of this node.
        /// <param name = "newOwner">The owner of the new node.</param>
        /// <returns>The copy of the node.</returns>
        /// </summary>
        public virtual ActionNode Copy (INodeOwner newOwner) {
            // Copy node
            var copy = newOwner.AddNode(GetType());

            // Copy SerializedFields
            var fields = NodeSerialization.GetSerializedFields(copy.GetType());
            for (int i = 0; i < fields.Length; i++) {
                // Do not copy the instanceID field
                if (fields[i].Name != "instanceID")
                    fields[i].SetValue(copy, fields[i].GetValue(this));
            }

            return copy;
        }
Exemple #14
0
 /// <summary>
 /// Node initialization.
 /// Set the game object and the owner of the node.
 /// <param name="gameObject">The game object that owns the node.</param>
 /// <param name="nodeOwner">The state that owns the node.</param>
 /// </summary>
 public void Init (GameObject gameObject, INodeOwner nodeOwner) {
     m_Self = gameObject;
     m_Owner = nodeOwner;
 }
Exemple #15
0
 /// <summary>
 /// Node initialization.
 /// Set the game object and the owner of the node.
 /// <param name="gameObject">The game object that owns the node.</param>
 /// <param name="nodeOwner">The state that owns the node.</param>
 /// </summary>
 public void Init(GameObject gameObject, INodeOwner nodeOwner)
 {
     m_Self  = gameObject;
     m_Owner = nodeOwner;
 }
 /// <summary>
 /// Clears all data in the object.
 /// </summary>
 public void Clear () {
     m_NodeTypes.Clear();
     m_NodeTypeHash.Clear();
     m_NodeFieldStartIndex.Clear();
     m_UnityObjectValue.Clear();
     m_IntValue.Clear();
     m_FloatValue.Clear();
     m_StringValue.Clear();
     m_CurveValue.Clear();
     m_GenericFieldType.Clear();
     m_GenericFieldHash.Clear();
     m_GenericFieldSize.Clear();
     m_FieldIndex.Clear();
     m_FieldType.Clear();
     m_FieldPath.Clear();
     m_BranchIndex.Clear();
     m_NextFieldIndex = 0;
     m_GameObject = null;
     m_NodeOwner = null;
     m_Blackboard = null;
 }
Exemple #17
0
        /// <summary> 
        /// Marks the state dirty flag; if the state is a prefab instance then disconnects the m_NodeSerialization property.
        /// <param name="owner">The target owner to mark the dirty flag.</param>
        /// </summary>
        public static void SetDirty (INodeOwner owner) {
            var unityObject = owner as UnityEngine.Object;
            if (!Application.isPlaying && unityObject != null) {
                // its a prefab Instance?
                var prefabType = UnityEditor.PrefabUtility.GetPrefabType(unityObject);
                if (prefabType != UnityEditor.PrefabType.None && prefabType != UnityEditor.PrefabType.Prefab && prefabType != UnityEditor.PrefabType.ModelPrefab) {
                    PropertyUtility.DisconnectPropertyFromPrefab(unityObject, "m_NodeSerialization");

                    // Workaround to disconnect ArraySize properties
                    owner.Clear();
                    PrefabUtility.RecordPrefabInstancePropertyModifications(unityObject);
                }
            }

            owner.SaveNodes();
            EditorUtility.SetDirty(unityObject);
        }
Exemple #18
0
        public void AddChild(ConditionalDialogueNode parent, ConditionalDialogueNode child, string child_id, INodeOwner owner)
        {
            if (Nodes.Count == 2)
            {
                if (child.ParentNode == null)
                {
                    child.ParentNode = Nodes[0];
                }
                child.NodeID = child_id;
                child.SetNodeOwner(owner);

                Nodes.Add(child);
                Nodes[0].Add(child);
            }
            else
            {
                int index = SearchNodeIndexByValue(parent.Value);
                if (index == -1)
                {
                    throw new Exception("No such parent node!");
                }
                if (child.ParentNode != null)
                {
                    //Multiple Parents
                    child.MultipleParents = true;
                    child.SetNodeOwner(owner);
                    child.AddParent(parent);
                    child.NodeID = child_id;
                    Nodes[index].Add(child);
                }
                else
                {
                    child.NodeID = child_id;
                    child.SetNodeOwner(owner);
                    child.AddParent(parent);
                    Nodes.Add(child);
                    Nodes[index].Add(child);
                    //SingleParent
                }
            }
        }