Bläddra i källkod

Merge pull request #1524 from AtomicGameEngine/JME-ATOMIC-SCENEDESTROY

[C#] Adding explicit Scene/Node/Component Destroy methods
JoshEngebretson 8 år sedan
förälder
incheckning
982da971fd

+ 1 - 1
Script/AtomicNET/AtomicNET/Player/Player.cs

@@ -29,7 +29,7 @@ namespace AtomicPlayer
                 if (loadedScenes.Contains(e.Scene))
                     loadedScenes.Remove(e.Scene);
 
-                e.Scene.Dispose();
+                e.Scene.Destroy();
             });
 
         }

+ 20 - 0
Script/AtomicNET/AtomicNET/Scene/Component.cs

@@ -4,6 +4,26 @@ namespace AtomicEngine
 
     public partial class Component : Animatable
     {
+        /// <summary>
+        /// Whether the component has been explicitly destroyed
+        /// </summary>
+        public bool Destroyed { get; private set; }
+
+        /// <summary>
+        /// Explicitly removes component and disposes of it
+        /// </summary>       
+        public virtual void Destroy()
+        {
+            if (Destroyed)
+                return;
+
+            Destroyed = true;
+
+            Remove();
+
+            Dispose();
+
+        }
 
         public T GetComponent<T>(bool recursive = false) where T : Component
         {

+ 62 - 0
Script/AtomicNET/AtomicNET/Scene/Node.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Collections.Generic;
 using static System.Reflection.IntrospectionExtensions;
 
 namespace AtomicEngine
@@ -7,6 +8,67 @@ namespace AtomicEngine
     public partial class Node : Animatable
     {
 
+        /// <summary>
+        /// Whether the node has been explicitly destroyed
+        /// </summary>
+        public bool Destroyed { get; private set; }
+
+        /// <summary>
+        /// Explicitly removes node from hierarchy and disposes of children and components
+        /// </summary>       
+        public virtual void Destroy()
+        {
+            // Destroying node hierarchy is flattened and not recursive
+            // for performance considerations
+
+            if (Destroyed)
+                return;
+
+            Destroyed = true;
+
+            UnsubscribeFromAllEvents();
+
+            // Remove from hierarchy
+            Remove();
+
+            // list of nodes/components to dispose
+            var disposeList = new List<RefCounted>();
+
+            // IMPORTANT: Care must be taken to clear these vectors
+            // otherwise, references will be held until the Vector is GC'd
+            // and the child nodes/components/resources will not be immediately disposed
+
+            var nodes = new Vector<Node>();
+            var components = new Vector<Component>();
+
+            // Get node components and add to dispose list
+            GetComponents(components);
+            disposeList.AddRange(components);
+            components.Clear();
+
+            // get all children of node and add their components to the dispose list
+            GetChildren(nodes, true);
+            foreach (var node in nodes)
+            {
+                node.Destroyed = true;
+                node.GetComponents(components);
+                disposeList.AddRange(components);
+                components.Clear();
+            }
+
+            // add nodes to the back of the list
+            disposeList.AddRange(nodes);
+
+            nodes.Clear();
+
+            // Add ourself to the dispose list
+            disposeList.Add(this);
+
+            // dispose of list
+            RefCountedCache.Dispose(disposeList);
+
+        }
+
         public void RemoveComponent<T>() where T : Component
         {
             var type = typeof(T);

+ 10 - 38
Script/AtomicNET/AtomicNET/Scene/Scene.cs

@@ -8,47 +8,19 @@ namespace AtomicEngine
 {
     public partial class Scene : Node
     {
-        override protected void Dispose(bool disposing)
-        {
-            UnsubscribeFromAllEvents();
-
-            if (disposing)
-            {
-                // list of nodes/components to dispose
-                var disposeList = new List<RefCounted>();
-
-                // IMPORTANT: Care must be taken to clear these vectors
-                // otherwise, references will be held until the Vector is GC'd
-                // and the child nodes/components/resources will not be immediately disposed
-                var components = new Vector<Component>();
-                var nodes = new Vector<Node>();
-
-                // Get scene components and add to dispose list
-                GetComponents(components);
-                disposeList.AddRange(components);
-                components.Clear();
-
-                // get all children of scene and add their components to the dispose list
-                GetChildren(nodes, true);                
-                foreach (var node in nodes)
-                {
-                    node.GetComponents(components);
-                    disposeList.AddRange(components);
-                    components.Clear();
-                }
 
-                // add nodes to the back of the list
-                disposeList.AddRange(nodes);
-
-                nodes.Clear();
+        /// <summary>
+        /// Explicitly destroys scene and disposes of children and components
+        /// </summary>       
+        public override void Destroy()
+        {
+            if (Destroyed)
+                return;
 
-                // dispose of list
-                RefCountedCache.Dispose(disposeList);                
-            }
-                            
-            // dispose ourself
-            base.Dispose(disposing);            
+            cscomponents.Clear();
+            cscomponentStart.Clear();
 
+            base.Destroy();
         }
 
         internal override void PostNativeUpdate()