using Quadtree.Items;
using System.Collections.Generic;
using UnityEngine;
namespace Quadtree
{
///
/// Main class of the Quadtree structure - it represents the root of the tree.
///
public abstract class QuadtreeMonoRoot : MonoBehaviour, IQuadtreeRoot
where TItem : IItem
where TNode : INode, new()
{
//==========================================================================dd==
// MonoBehaviour METHODS
//==========================================================================dd==
protected void Start()
{
Init();
}
protected void OnEnable()
{
Init();
}
protected void OnDrawGizmos()
{
DrawBounds();
}
//==========================================================================dd==
// CORE ROOT NODE METHODS
//==========================================================================dd==
///
/// Root node containing all items and sub-nodes.
///
protected QuadtreeRoot TreeRoot = null;
public bool Initialized => TreeRoot != null && TreeRoot.Initialized;
public TNode CurrentRootNode => TreeRoot.CurrentRootNode;
[SerializeField]
protected Vector3 DefaultRootNodeSize = new Vector3(64f, 0f, 64f);
///
[SerializeField]
protected float MinimumPossibleNodeSize = 1f;
float IQuadtreeRoot.MinimumPossibleNodeSize => MinimumPossibleNodeSize;
///
[SerializeField]
private bool DisplayNumberOfItemsInGizmos = false;
bool IQuadtreeRoot.DisplayNumberOfItemsInGizmos => DisplayNumberOfItemsInGizmos;
///
/// Initializes Quadtree - creates initial root node and builds the tree (if allowed).
///
///
///
protected void Init()
{
if (TreeRoot == null)
{
TreeRoot = new QuadtreeRoot(transform.position, DefaultRootNodeSize);
}
else
{
// root node has already been initialized, clear the tree
TreeRoot.Clear();
}
// send a message to children that the tree root has been initialized
BroadcastMessage("QuadTree_Root_Initialized", this, SendMessageOptions.DontRequireReceiver);
}
///
/// Displays Quadtree node boundaries.
///
///
///
protected void DrawBounds()
{
TreeRoot?.CurrentRootNode.DrawBounds(DisplayNumberOfItemsInGizmos);
}
public void Insert(TItem item) => TreeRoot.Insert(item);
public void Expand() => TreeRoot.Expand();
public List Find(Bounds bounds) => TreeRoot.Find(bounds);
public void Remove(TItem item) => TreeRoot.Remove(item);
public void Clear() => TreeRoot.Clear();
}
}