Skip to content

Commit 7d406f0

Browse files
author
psilo
committed
Minor fixes.
Made result of Get(Rectangle) only containing unique Ts.
1 parent e90c550 commit 7d406f0

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

CollisionGrid/CollisionGrid.Point.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private void AddToGrid(T item, Point cell)
9898
private void AddToItems(T item, Point cell)
9999
{
100100
List<Point> pl;
101-
Items.TryGetValue(item, out pl);
101+
ItemDictionary.TryGetValue(item, out pl);
102102
if (pl == null)
103103
{
104104
if (ListOfPointQueue.Count > 0)
@@ -110,7 +110,7 @@ private void AddToItems(T item, Point cell)
110110
pl = new List<Point>();
111111
}
112112
pl.Add(cell);
113-
Items.Add(item, pl);
113+
ItemDictionary.Add(item, pl);
114114
}
115115
else
116116
{
@@ -139,14 +139,14 @@ public void Remove(Point cell)
139139
foreach (T i in l)
140140
{
141141
List<Point> pl;
142-
Items.TryGetValue(i, out pl);
142+
ItemDictionary.TryGetValue(i, out pl);
143143
if (pl != null)
144144
{
145145
pl.Remove(c);
146146
if (pl.Count == 0)
147147
{
148148
ListOfPointQueue.Enqueue(pl);
149-
Items.Remove(i);
149+
ItemDictionary.Remove(i);
150150
}
151151
}
152152
}

CollisionGrid/CollisionGrid.Rectangle.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@ public T[] Get(Rectangle aabb)
4343
result.Clear();
4444
foreach (Point p in lop)
4545
{
46-
result.AddRange(Get(p));
46+
foreach (T i in Get(p))
47+
{
48+
if (!result.Contains(i))
49+
{
50+
result.Add(i);
51+
}
52+
}
4753
}
4854
return result.ToArray();
4955
}

CollisionGrid/CollisionGrid.cs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public partial class CollisionGrid<T>
3333
public int NumberOfCellsY { get; private set; }
3434

3535
private Dictionary<Point, List<T>> Grid { get; set; }
36-
private Dictionary<T, List<Point>> Items { get; set; }
36+
private Dictionary<T, List<Point>> ItemDictionary { get; set; }
3737
private Queue<List<Point>> ListOfPointQueue { get; set; }
3838
private Queue<List<T>> ListOfItemQueue { get; set; }
3939

@@ -46,7 +46,7 @@ public CollisionGrid(float width, float height, int numberOfCellsX, int numberOf
4646
CellWidth = Width/NumberOfCellsX;
4747
CellHeight = Height/NumberOfCellsY;
4848

49-
Items = new Dictionary<T, List<Point>>();
49+
ItemDictionary = new Dictionary<T, List<Point>>();
5050
Grid = new Dictionary<Point, List<T>>();
5151

5252
ListOfPointQueue = new Queue<List<Point>>();
@@ -58,7 +58,7 @@ public Point[] Get(T item)
5858
lock (lockObject)
5959
{
6060
List<Point> pl;
61-
Items.TryGetValue(item, out pl);
61+
ItemDictionary.TryGetValue(item, out pl);
6262
if (pl == null)
6363
{
6464
return new Point[0];
@@ -76,7 +76,7 @@ public void Remove(T item)
7676
lock (lockObject)
7777
{
7878
List<Point> pl;
79-
Items.TryGetValue(item, out pl);
79+
ItemDictionary.TryGetValue(item, out pl);
8080
if (pl == null)
8181
{
8282
return;
@@ -89,7 +89,7 @@ public void Remove(T item)
8989

9090
pl.Clear();
9191
ListOfPointQueue.Enqueue(pl);
92-
Items.Remove(item);
92+
ItemDictionary.Remove(item);
9393
}
9494
}
9595

@@ -102,25 +102,31 @@ private void RemoveFromGrid(T item, Point cell)
102102
tl.Remove(item);
103103
if (tl.Count == 0)
104104
{
105-
if (ListOfItemQueue.Count == 0)
106-
{
107-
Console.Out.WriteLine("Yippie!");
108-
}
109105
ListOfItemQueue.Enqueue(tl);
110106
Grid.Remove(cell);
111107
}
112108
}
113109
}
114110

115-
public IEnumerable<T> AllItems()
111+
public IEnumerable<T> Items
116112
{
117-
return Items.Keys;
113+
get { return ItemDictionary.Keys; }
118114
}
119115

120-
public IEnumerable<Point> AllOccupiedCells()
116+
public IEnumerable<Point> OccupiedCells
121117
{
122-
return Grid.Keys;
123-
}
118+
get { return Grid.Keys; }
119+
}
120+
121+
public int OccupiedCellCount
122+
{
123+
get { return ItemDictionary.Keys.Count; }
124+
}
125+
126+
public int ItemCount
127+
{
128+
get { return Grid.Keys.Count; }
129+
}
124130

125131
private Rectangle Rectangle(Rect rect)
126132
{
@@ -177,8 +183,8 @@ public void Dispose()
177183
ListOfItemQueue = null;
178184
Grid.Clear();
179185
Grid = null;
180-
Items.Clear();
181-
Items = null;
186+
ItemDictionary.Clear();
187+
ItemDictionary = null;
182188
}
183189
}
184190
}

0 commit comments

Comments
 (0)