Skip to content

Commit 66b8b53

Browse files
author
psilo
committed
Updated license text in headers.
Using some C# 6.0 features.
1 parent 7d406f0 commit 66b8b53

File tree

10 files changed

+1005
-876
lines changed

10 files changed

+1005
-876
lines changed

CollisionGrid.sln.DotSettings

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderText/@EntryValue">*************************************************************************** &#xD;
3+
This is free and unencumbered software released into the public domain.&#xD;
4+
&#xD;
5+
Anyone is free to copy, modify, publish, use, compile, sell, or&#xD;
6+
distribute this software, either in source code form or as a compiled&#xD;
7+
binary, for any purpose, commercial or non-commercial, and by any&#xD;
8+
means.&#xD;
9+
&#xD;
10+
In jurisdictions that recognize copyright laws, the author or authors&#xD;
11+
of this software dedicate any and all copyright interest in the&#xD;
12+
software to the public domain. We make this dedication for the benefit&#xD;
13+
of the public at large and to the detriment of our heirs and&#xD;
14+
successors. We intend this dedication to be an overt act of&#xD;
15+
relinquishment in perpetuity of all present and future rights to this&#xD;
16+
software under copyright law.&#xD;
17+
&#xD;
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,&#xD;
19+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF&#xD;
20+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.&#xD;
21+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR&#xD;
22+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,&#xD;
23+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR&#xD;
24+
OTHER DEALINGS IN THE SOFTWARE.&#xD;
25+
&#xD;
26+
For more information, please refer to &lt;http://unlicense.org&gt;&#xD;
27+
***************************************************************************</s:String></wpf:ResourceDictionary>
Lines changed: 178 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -1,183 +1,195 @@
11
// ***************************************************************************
2-
// Copyright (c) 2016 by Unterrainer Informatik OG.
3-
// This source is licensed to Unterrainer Informatik OG.
4-
// All rights reserved.
5-
//
6-
// In other words:
7-
// YOU MUST NOT COPY, USE, CHANGE OR REDISTRIBUTE ANY ART, MUSIC, CODE OR
8-
// OTHER DATA, CONTAINED WITHIN THESE DIRECTORIES WITHOUT THE EXPRESS
9-
// PERMISSION OF Unterrainer Informatik OG.
10-
// ---------------------------------------------------------------------------
11-
// Programmer: G U,
12-
// Created: 2016-03-30
2+
// This is free and unencumbered software released into the public domain.
3+
//
4+
// Anyone is free to copy, modify, publish, use, compile, sell, or
5+
// distribute this software, either in source code form or as a compiled
6+
// binary, for any purpose, commercial or non-commercial, and by any
7+
// means.
8+
//
9+
// In jurisdictions that recognize copyright laws, the author or authors
10+
// of this software dedicate any and all copyright interest in the
11+
// software to the public domain. We make this dedication for the benefit
12+
// of the public at large and to the detriment of our heirs and
13+
// successors. We intend this dedication to be an overt act of
14+
// relinquishment in perpetuity of all present and future rights to this
15+
// software under copyright law.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20+
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21+
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
//
25+
// For more information, please refer to <http://unlicense.org>
1326
// ***************************************************************************
1427

1528
using System.Collections.Generic;
16-
using System.Linq;
1729
using Microsoft.Xna.Framework;
1830

1931
namespace CollisionGrid
2032
{
21-
public partial class CollisionGrid<T>
22-
{
23-
public T[] Get(Point cell)
24-
{
25-
lock (lockObject)
26-
{
27-
List<T> contents;
28-
Grid.TryGetValue(Clamp(cell), out contents);
29-
if (contents == null)
30-
{
31-
return new T[0];
32-
}
33-
return contents.ToArray();
34-
}
35-
}
33+
public partial class CollisionGrid<T>
34+
{
35+
public T[] Get(Point cell)
36+
{
37+
lock (lockObject)
38+
{
39+
List<T> contents;
40+
Grid.TryGetValue(Clamp(cell), out contents);
41+
if (contents == null)
42+
{
43+
return new T[0];
44+
}
45+
return contents.ToArray();
46+
}
47+
}
3648

37-
/// <summary>
38-
/// Gets the first item encountered on the given cell.
39-
/// </summary>
40-
/// <param name="cell">The cell to search</param>
41-
/// <returns>The item or default(T)</returns>
42-
public T First(Point cell)
43-
{
44-
lock (lockObject)
45-
{
46-
List<T> contents;
47-
Grid.TryGetValue(Clamp(cell), out contents);
48-
if (contents != null && contents.Count > 0)
49-
{
50-
return contents[0];
51-
}
52-
return default(T);
53-
}
54-
}
49+
/// <summary>
50+
/// Gets the first item encountered on the given cell.
51+
/// </summary>
52+
/// <param name="cell">The cell to search</param>
53+
/// <returns>The item or default(T)</returns>
54+
public T First(Point cell)
55+
{
56+
lock (lockObject)
57+
{
58+
List<T> contents;
59+
Grid.TryGetValue(Clamp(cell), out contents);
60+
if (contents != null && contents.Count > 0)
61+
{
62+
return contents[0];
63+
}
64+
return default(T);
65+
}
66+
}
5567

56-
/// <summary>
57-
/// Adds a given item to a given cell.
58-
/// If the cell already contains the item, it is not added a second time.
59-
/// </summary>
60-
/// <param name="item">The item to add</param>
61-
/// <param name="cell">The cell to add the item to</param>
62-
public void Add(T item, Point cell)
63-
{
64-
lock (lockObject)
65-
{
66-
Point c = Clamp(cell);
67-
AddToGrid(item, c);
68-
AddToItems(item, c);
69-
}
70-
}
68+
/// <summary>
69+
/// Adds a given item to a given cell.
70+
/// If the cell already contains the item, it is not added a second time.
71+
/// </summary>
72+
/// <param name="item">The item to add</param>
73+
/// <param name="cell">The cell to add the item to</param>
74+
public void Add(T item, Point cell)
75+
{
76+
lock (lockObject)
77+
{
78+
var c = Clamp(cell);
79+
AddToGrid(item, c);
80+
AddToItems(item, c);
81+
}
82+
}
7183

72-
private void AddToGrid(T item, Point cell)
73-
{
74-
List<T> l;
75-
Grid.TryGetValue(cell, out l);
76-
if (l == null)
77-
{
78-
if (ListOfItemQueue.Count > 0)
79-
{
80-
l = ListOfItemQueue.Dequeue();
81-
}
82-
else
83-
{
84-
l = new List<T>();
85-
}
86-
l.Add(item);
87-
Grid.Add(cell, l);
88-
}
89-
else
90-
{
91-
if (!l.Contains(item))
92-
{
93-
l.Add(item);
94-
}
95-
}
96-
}
84+
private void AddToGrid(T item, Point cell)
85+
{
86+
List<T> l;
87+
Grid.TryGetValue(cell, out l);
88+
if (l == null)
89+
{
90+
if (ListOfItemQueue.Count > 0)
91+
{
92+
l = ListOfItemQueue.Dequeue();
93+
}
94+
else
95+
{
96+
l = new List<T>();
97+
}
98+
l.Add(item);
99+
Grid.Add(cell, l);
100+
}
101+
else
102+
{
103+
if (!l.Contains(item))
104+
{
105+
l.Add(item);
106+
}
107+
}
108+
}
97109

98-
private void AddToItems(T item, Point cell)
99-
{
100-
List<Point> pl;
101-
ItemDictionary.TryGetValue(item, out pl);
102-
if (pl == null)
103-
{
104-
if (ListOfPointQueue.Count > 0)
105-
{
106-
pl = ListOfPointQueue.Dequeue();
107-
}
108-
else
109-
{
110-
pl = new List<Point>();
111-
}
112-
pl.Add(cell);
113-
ItemDictionary.Add(item, pl);
114-
}
115-
else
116-
{
117-
if (!pl.Contains(cell))
118-
{
119-
pl.Add(cell);
120-
}
121-
}
122-
}
110+
private void AddToItems(T item, Point cell)
111+
{
112+
List<Point> pl;
113+
ItemDictionary.TryGetValue(item, out pl);
114+
if (pl == null)
115+
{
116+
if (ListOfPointQueue.Count > 0)
117+
{
118+
pl = ListOfPointQueue.Dequeue();
119+
}
120+
else
121+
{
122+
pl = new List<Point>();
123+
}
124+
pl.Add(cell);
125+
ItemDictionary.Add(item, pl);
126+
}
127+
else
128+
{
129+
if (!pl.Contains(cell))
130+
{
131+
pl.Add(cell);
132+
}
133+
}
134+
}
123135

124-
/// <summary>
125-
/// Removes all items from the given cell.
126-
/// If the items don't occupy another cell, they are removed as well.
127-
/// </summary>
128-
/// <param name="cell">The cell to remove items from</param>
129-
public void Remove(Point cell)
130-
{
131-
lock (lockObject)
132-
{
133-
Point c = Clamp(cell);
134-
List<T> l;
135-
Grid.TryGetValue(c, out l);
136+
/// <summary>
137+
/// Removes all items from the given cell.
138+
/// If the items don't occupy another cell, they are removed as well.
139+
/// </summary>
140+
/// <param name="cell">The cell to remove items from</param>
141+
public void Remove(Point cell)
142+
{
143+
lock (lockObject)
144+
{
145+
var c = Clamp(cell);
146+
List<T> l;
147+
Grid.TryGetValue(c, out l);
136148

137-
if (l != null)
138-
{
139-
foreach (T i in l)
140-
{
141-
List<Point> pl;
142-
ItemDictionary.TryGetValue(i, out pl);
143-
if (pl != null)
144-
{
145-
pl.Remove(c);
146-
if (pl.Count == 0)
147-
{
148-
ListOfPointQueue.Enqueue(pl);
149-
ItemDictionary.Remove(i);
150-
}
151-
}
152-
}
153-
l.Clear();
154-
ListOfItemQueue.Enqueue(l);
155-
Grid.Remove(cell);
156-
}
157-
}
158-
}
149+
if (l != null)
150+
{
151+
foreach (var i in l)
152+
{
153+
List<Point> pl;
154+
ItemDictionary.TryGetValue(i, out pl);
155+
if (pl != null)
156+
{
157+
pl.Remove(c);
158+
if (pl.Count == 0)
159+
{
160+
ListOfPointQueue.Enqueue(pl);
161+
ItemDictionary.Remove(i);
162+
}
163+
}
164+
}
165+
l.Clear();
166+
ListOfItemQueue.Enqueue(l);
167+
Grid.Remove(cell);
168+
}
169+
}
170+
}
159171

160-
/// <summary>
161-
/// Removes all occurrences of the given item and re-adds it at the new given cell.
162-
/// If the item hasn't been in the grid before, this will just add it.
163-
/// </summary>
164-
/// <param name="item">The item to move</param>
165-
/// <param name="cell">The cell to move it to</param>
166-
public void Move(T item, Point cell)
167-
{
168-
lock (lockObject)
169-
{
170-
Remove(item);
171-
Add(item, cell);
172-
}
173-
}
172+
/// <summary>
173+
/// Removes all occurrences of the given item and re-adds it at the new given cell.
174+
/// If the item hasn't been in the grid before, this will just add it.
175+
/// </summary>
176+
/// <param name="item">The item to move</param>
177+
/// <param name="cell">The cell to move it to</param>
178+
public void Move(T item, Point cell)
179+
{
180+
lock (lockObject)
181+
{
182+
Remove(item);
183+
Add(item, cell);
184+
}
185+
}
174186

175-
public bool IsEmpty(Point cell)
176-
{
177-
lock (lockObject)
178-
{
179-
return Get(cell).Length == 0;
180-
}
181-
}
182-
}
187+
public bool IsEmpty(Point cell)
188+
{
189+
lock (lockObject)
190+
{
191+
return Get(cell).Length == 0;
192+
}
193+
}
194+
}
183195
}

0 commit comments

Comments
 (0)