mirror of
https://github.com/BlackMATov/unity-iso-tools.git
synced 2025-12-13 15:52:03 +07:00
quadtree with id for his item
This commit is contained in:
@@ -9,9 +9,6 @@
|
|||||||
|
|
||||||
public IsoPool(int capacity) {
|
public IsoPool(int capacity) {
|
||||||
_items = new IsoList<T>(capacity);
|
_items = new IsoList<T>(capacity);
|
||||||
for ( var i = 0; i < capacity; ++i ) {
|
|
||||||
_items.Add(CreateItem());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public T Take() {
|
public T Take() {
|
||||||
|
|||||||
@@ -196,10 +196,15 @@
|
|||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
class Item : IItem {
|
class Item : IItem {
|
||||||
|
public int QTId = 0;
|
||||||
public Node Owner = null;
|
public Node Owner = null;
|
||||||
public IsoRect Bounds = IsoRect.zero;
|
public IsoRect Bounds = IsoRect.zero;
|
||||||
public T Content = default(T);
|
public T Content = default(T);
|
||||||
|
|
||||||
|
public Item(int qtId) {
|
||||||
|
QTId = qtId;
|
||||||
|
}
|
||||||
|
|
||||||
public Item Init(Node owner, IsoRect bounds, T content) {
|
public Item Init(Node owner, IsoRect bounds, T content) {
|
||||||
Owner = owner;
|
Owner = owner;
|
||||||
Bounds = bounds;
|
Bounds = bounds;
|
||||||
@@ -228,11 +233,14 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
class ItemPool : IsoPool<Item> {
|
class ItemPool : IsoPool<Item> {
|
||||||
public ItemPool(int capacity) : base(capacity) {
|
int _qtId = 0;
|
||||||
|
|
||||||
|
public ItemPool(int qtId, int capacity) : base(capacity) {
|
||||||
|
_qtId = qtId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Item CreateItem() {
|
public override Item CreateItem() {
|
||||||
return new Item();
|
return new Item(_qtId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -242,6 +250,9 @@
|
|||||||
//
|
//
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
int _qtId = 0;
|
||||||
|
static int _genQTId = 0;
|
||||||
|
|
||||||
Node _rootNode = null;
|
Node _rootNode = null;
|
||||||
IsoIPool<Node> _nodePool = null;
|
IsoIPool<Node> _nodePool = null;
|
||||||
IsoIPool<Item> _itemPool = null;
|
IsoIPool<Item> _itemPool = null;
|
||||||
@@ -253,9 +264,10 @@
|
|||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
public IsoQuadTree(int capacity) {
|
public IsoQuadTree(int capacity) {
|
||||||
|
_qtId = ++_genQTId;
|
||||||
_rootNode = null;
|
_rootNode = null;
|
||||||
_nodePool = new NodePool(capacity);
|
_nodePool = new NodePool(capacity);
|
||||||
_itemPool = new ItemPool(capacity);
|
_itemPool = new ItemPool(_qtId, capacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IItem AddItem(IsoRect bounds, T content) {
|
public IItem AddItem(IsoRect bounds, T content) {
|
||||||
@@ -281,10 +293,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveItem(IItem iitem) {
|
public void RemoveItem(IItem iitem) {
|
||||||
if ( iitem == null ) {
|
var item = GetItemWithCast(iitem);
|
||||||
throw new System.ArgumentNullException("iitem");
|
|
||||||
}
|
|
||||||
var item = iitem as Item;
|
|
||||||
var item_node = item.Owner;
|
var item_node = item.Owner;
|
||||||
if ( item_node != null ) {
|
if ( item_node != null ) {
|
||||||
item_node.RemoveItem(item, _itemPool);
|
item_node.RemoveItem(item, _itemPool);
|
||||||
@@ -297,10 +306,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
public IItem MoveItem(IsoRect bounds, IItem iitem) {
|
public IItem MoveItem(IsoRect bounds, IItem iitem) {
|
||||||
if ( iitem == null ) {
|
var item = GetItemWithCast(iitem);
|
||||||
throw new System.ArgumentNullException("iitem");
|
|
||||||
}
|
|
||||||
var item = iitem as Item;
|
|
||||||
var item_node = item.Owner;
|
var item_node = item.Owner;
|
||||||
if ( item_node != null ) {
|
if ( item_node != null ) {
|
||||||
if ( item_node.SelfBounds.Contains(bounds) && item_node.Items.Count <= MinChildCountPerNode ) {
|
if ( item_node.SelfBounds.Contains(bounds) && item_node.Items.Count <= MinChildCountPerNode ) {
|
||||||
@@ -348,15 +354,12 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void VisitItemsByItem(IItem iitem, IContentLookUpper look_upper) {
|
public void VisitItemsByItem(IItem iitem, IContentLookUpper look_upper) {
|
||||||
if ( iitem == null ) {
|
var item = GetItemWithCast(iitem);
|
||||||
throw new System.ArgumentNullException("iitem");
|
var item_node = item.Owner;
|
||||||
|
if ( item_node != null ) {
|
||||||
|
item_node.VisitItemsByBounds(item.Bounds, look_upper);
|
||||||
|
BackwardVisitNodes(item_node.Parent, item.Bounds, look_upper);
|
||||||
}
|
}
|
||||||
if ( look_upper == null ) {
|
|
||||||
throw new System.ArgumentNullException("look_upper");
|
|
||||||
}
|
|
||||||
var item = iitem as Item;
|
|
||||||
item.Owner.VisitItemsByBounds(item.Bounds, look_upper);
|
|
||||||
BackwardVisitNodes(item.Owner.Parent, item.Bounds, look_upper);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void VisitItemsByBounds(IsoRect bounds, IContentLookUpper look_upper) {
|
public void VisitItemsByBounds(IsoRect bounds, IContentLookUpper look_upper) {
|
||||||
@@ -404,6 +407,17 @@
|
|||||||
_rootNode = new_root;
|
_rootNode = new_root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Item GetItemWithCast(IItem iitem) {
|
||||||
|
if ( iitem == null ) {
|
||||||
|
throw new System.ArgumentNullException("iitem");
|
||||||
|
}
|
||||||
|
var item = iitem as Item;
|
||||||
|
if ( item == null || item.QTId != _qtId ) {
|
||||||
|
throw new System.ArgumentException("item from another IsoQuadTree", "iitem");
|
||||||
|
}
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
Node BackwardNodeCleanUp(Node node) {
|
Node BackwardNodeCleanUp(Node node) {
|
||||||
while ( node != null && node.CleanUpNodes(_nodePool, _itemPool) ) {
|
while ( node != null && node.CleanUpNodes(_nodePool, _itemPool) ) {
|
||||||
node = node.Parent;
|
node = node.Parent;
|
||||||
|
|||||||
Reference in New Issue
Block a user