/// <summary> /// Traverse the tree to fetch the node that contains the specified collideable object /// </summary> /// <param name="obj">collideable object</param> /// <returns>The containing node</returns> private Node FetchContaining(ICollides obj) { //guard if (obj == null) { return(null); } //end condition (no more child nodes) if (childNodes == null) { if (ICollidesList.Contains(obj)) { return(this); } else { return(null); } } else { //traverse foreach (Node n in childNodes) { if (n.ContainsPos(obj.RealPos)) { return(n.FetchContaining(obj)); } } return(null); } }
/// <summary> /// Register collidable to the right node /// </summary> public void Register(ICollides obj) { //guards if (obj == null) { return; } if (!SwinGame.RectOnScreen(grid)) { return; } //end condition if (childNodes == null) { ICollidesList.Add(obj); return; } //find the node which contains the entiites position foreach (Node n in childNodes) { //only explore traverse relevant nodes if (n.ContainsPos(obj.RealPos)) { n.Register(obj); } } }
/// <summary> /// clear the tree of all collideable entities /// </summary> public void Clear() { ICollidesList?.Clear(); if (childNodes == null) { return; } foreach (Node n in childNodes) { n.Clear(); } }