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
1528using System . Collections . Generic ;
16- using System . Linq ;
1729using Microsoft . Xna . Framework ;
1830
1931namespace 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