@@ -9,12 +9,12 @@ public class Grid
99 {
1010 public const int NUM_CELLS = 10 ;
1111
12- public const int CELL_SIZE = 20 ;
12+ public const int CELL_SIZE = 5 ;
1313
1414 private Unit [ , ] cells = new Unit [ NUM_CELLS , NUM_CELLS ] ;
1515
16- //If two units are within this distance they can attack each other
17- private const float ATTACK_DISTANCE = 0.5f ;
16+ //How many units do we have on the grid, which should be faster than to iterate through all cells and count them
17+ public int unitCount { get ; private set ; }
1818
1919
2020
@@ -34,7 +34,7 @@ public Grid()
3434
3535 //Add unit to grid
3636 //This is also used when a unit already on the grid is moving into a new cell
37- public void Add ( Unit newUnit )
37+ public void Add ( Unit newUnit , bool isNewUnit = false )
3838 {
3939 //Determine which grid cell it's in
4040 Vector2Int cellPos = ConvertFromWorldToCell ( newUnit . transform . position ) ;
@@ -53,23 +53,25 @@ public void Add(Unit newUnit)
5353
5454 nextUnit . prev = newUnit ;
5555 }
56+
57+ if ( isNewUnit )
58+ {
59+ unitCount += 1 ;
60+ }
5661 }
5762
5863
5964
60- //Move a unit on the grid
61- public void Move ( Unit unit , Vector3 newPos )
65+ //Move a unit on the grid = see if it has changed cell
66+ //Make sure newPos is a valid position inside of the grid
67+ public void Move ( Unit unit , Vector3 oldPos , Vector3 newPos )
6268 {
63- //See what cell it was in
64- Vector2Int oldCellPos = ConvertFromWorldToCell ( unit . transform . position ) ;
69+ //See what cell it was in before we assign the new position
70+ Vector2Int oldCellPos = ConvertFromWorldToCell ( oldPos ) ;
6571
6672 //See which cell it's moving to
6773 Vector2Int newCellPos = ConvertFromWorldToCell ( newPos ) ;
6874
69- //TODO: Validate if this is a valid cell pos
70-
71- unit . transform . position = newPos ;
72-
7375 //If it didn't change cell, we are done
7476 if ( oldCellPos . x == newCellPos . x && oldCellPos . y == newCellPos . y )
7577 {
@@ -114,8 +116,15 @@ public Vector2Int ConvertFromWorldToCell(Vector3 pos)
114116 {
115117 //Dividing coordinate by cell size converts from world space to cell space
116118 //Casting to int converts from cell space to cell index
117- int cellX = ( int ) ( pos . x / CELL_SIZE ) ;
118- int cellY = ( int ) ( pos . z / CELL_SIZE ) ; //z instead of y because y is up in Unity's coordinate system
119+ //int cellX = (int)(pos.x / CELL_SIZE);
120+ //int cellY = (int)(pos.z / CELL_SIZE); //z instead of y because y is up in Unity's coordinate system
121+
122+ //Casting to int in C# doesnt work in same way as in C++ so we have to use FloorToInt instead
123+ //It works like this if cell size is 2:
124+ //pos.x is 1.8, then cellX will be 1.8/2 = 0.9 -> 0
125+ //pos.x is 2.1, then cellX will be 2.1/2 = 1.05 -> 1
126+ int cellX = Mathf . FloorToInt ( pos . x / CELL_SIZE ) ;
127+ int cellY = Mathf . FloorToInt ( pos . z / CELL_SIZE ) ; //z instead of y because y is up in Unity's coordinate system
119128
120129 Vector2Int cellPos = new Vector2Int ( cellX , cellY ) ;
121130
@@ -124,6 +133,23 @@ public Vector2Int ConvertFromWorldToCell(Vector3 pos)
124133
125134
126135
136+ //Test if a position is a valid position (= is inside of the grid)
137+ public bool IsPosValid ( Vector3 pos )
138+ {
139+ Vector2Int cellPos = ConvertFromWorldToCell ( pos ) ;
140+
141+ if ( cellPos . x >= 0 && cellPos . x < NUM_CELLS && cellPos . y >= 0 && cellPos . y < NUM_CELLS )
142+ {
143+ return true ;
144+ }
145+ else
146+ {
147+ return false ;
148+ }
149+ }
150+
151+
152+
127153 //
128154 // Fighting
129155 //
@@ -136,17 +162,17 @@ public void HandleMelee()
136162 {
137163 for ( int y = 0 ; y < NUM_CELLS ; y ++ )
138164 {
139- HandleCell ( new Vector2Int ( x , y ) ) ;
165+ HandleCell ( x , y ) ;
140166 }
141167 }
142168 }
143169
144170
145171
146172 //Handles fight for a single cell
147- private void HandleCell ( Vector2Int cellPos )
173+ private void HandleCell ( int x , int y )
148174 {
149- Unit unit = cells [ cellPos . x , cellPos . y ] ;
175+ Unit unit = cells [ x , y ] ;
150176
151177 //Make each unit fight all other units once in this cell
152178 //It works like this: If the units in the cell are linked like: A-B-C-D
@@ -164,21 +190,21 @@ private void HandleCell(Vector2Int cellPos)
164190 //But we cant check all 8 cells because then some units might fight each other two times, so we only check half (it doesnt matter which half)
165191 //We also have to check that there's a surrounding cell because the current cell might be the border
166192 //This assumes attack distance is less than cell size, or we might have to check more cells
167- if ( cellPos . x > 0 && cellPos . y > 0 )
193+ if ( x > 0 && y > 0 )
168194 {
169- HandleUnit ( unit , cells [ cellPos . x - 1 , cellPos . y - 1 ] ) ;
195+ HandleUnit ( unit , cells [ x - 1 , y - 1 ] ) ;
170196 }
171- if ( cellPos . x > 0 )
197+ if ( x > 0 )
172198 {
173- HandleUnit ( unit , cells [ cellPos . x - 1 , cellPos . y - 0 ] ) ;
199+ HandleUnit ( unit , cells [ x - 1 , y - 0 ] ) ;
174200 }
175- if ( cellPos . y > 0 )
201+ if ( y > 0 )
176202 {
177- HandleUnit ( unit , cells [ cellPos . x - 0 , cellPos . y - 1 ] ) ;
203+ HandleUnit ( unit , cells [ x - 0 , y - 1 ] ) ;
178204 }
179- if ( cellPos . x > 0 && cellPos . y < NUM_CELLS - 1 )
205+ if ( x > 0 && y < NUM_CELLS - 1 )
180206 {
181- HandleUnit ( unit , cells [ cellPos . x - 1 , cellPos . y + 1 ] ) ;
207+ HandleUnit ( unit , cells [ x - 1 , y + 1 ] ) ;
182208 }
183209
184210 unit = unit . next ;
@@ -187,13 +213,13 @@ private void HandleCell(Vector2Int cellPos)
187213
188214
189215
190- //Handles fight for a single unit versus a linked-list of units
216+ //Handles fight for a single unit vs a linked-list of units
191217 private void HandleUnit ( Unit unit , Unit other )
192218 {
193219 while ( other != null )
194220 {
195221 //Make them fight if they have similar position - use square distance because it's faster
196- if ( ( unit . transform . position - other . transform . position ) . sqrMagnitude < ATTACK_DISTANCE * ATTACK_DISTANCE )
222+ if ( ( unit . transform . position - other . transform . position ) . sqrMagnitude < Unit . ATTACK_DISTANCE * Unit . ATTACK_DISTANCE )
197223 {
198224 HandleAttack ( unit , other ) ;
199225 }
@@ -208,7 +234,8 @@ private void HandleUnit(Unit unit, Unit other)
208234 private void HandleAttack ( Unit one , Unit two )
209235 {
210236 //Insert fighting mechanic
211- Debug . Log ( "Two units are fighting! monkaS" ) ;
237+ one . StartFighting ( ) ;
238+ two . StartFighting ( ) ;
212239 }
213240 }
214241}
0 commit comments