public CollisionMap(byte type, int w, int h) { this.type = type; chunks = new CollisionNode <T> [ cW = (w + CHUNK_SIZE - 1) / CHUNK_SIZE, cH = (h + CHUNK_SIZE - 1) / CHUNK_SIZE]; this.w = w; this.h = h; }
public void InsertAfter(CollisionNode <T> node) { if (Next != null) { node.Next = Next; Next.Previous = node; } else { node.Next = null; } node.Previous = this; Next = node; }
public IEnumerable <T> HitTest(double _x, double _y) { if (_x < 0 || _x >= w || _y <= 0 || _y >= h) { yield break; } int x = (int)_x / CHUNK_SIZE; int y = (int)_y / CHUNK_SIZE; CollisionNode <T> node = chunks[x, y]; while (node != null) { yield return((T)node.Parent); node = node.Next; } }
public CollisionNode <T> Remove() { CollisionNode <T> ret = null; if (Previous != null) { ret = Previous; Previous.Next = Next; } if (Next != null) { ret = Next; Next.Previous = Previous; } Previous = null; Next = null; return(ret); }
public IEnumerable <T> HitTest(double _x, double _y, double radius) { int xl = Math.Max(0, (int)(_x - radius) / CHUNK_SIZE); int xh = Math.Min(cW - 1, (int)(_x + radius) / CHUNK_SIZE); int yl = Math.Max(0, (int)(_y - radius) / CHUNK_SIZE); int yh = Math.Min(cH - 1, (int)(_y + radius) / CHUNK_SIZE); for (int y = yl; y <= yh; y++) { for (int x = xl; x <= xh; x++) { CollisionNode <T> node = chunks[x, y]; while (node != null) { yield return((T)node.Parent); node = node.Next; } } } }
public IEnumerable <T> GetActiveChunks(CollisionMap <T> from) { if (from.w != w || from.h != h) { throw new ArgumentException("from"); } HashSet <T> ret = new HashSet <T>(); for (int y = 0; y < cH; y++) { for (int x = 0; x < cW; x++) { if (from.chunks[x, y] != null) { for (int i = -ACTIVE_RADIUS; i <= ACTIVE_RADIUS; i++) { for (int j = -ACTIVE_RADIUS; j <= ACTIVE_RADIUS; j++) { if (x + j < 0 || x + j >= cW || y + i < 0 || y + i >= cH) { continue; } CollisionNode <T> node = chunks[x + j, y + i]; while (node != null) { ret.Add((T)node.Parent); node = node.Next; } } } } } } return(ret); }