@@ -9,12 +9,12 @@ public class Grid
9
9
{
10
10
public const int NUM_CELLS = 10 ;
11
11
12
- public const int CELL_SIZE = 20 ;
12
+ public const int CELL_SIZE = 5 ;
13
13
14
14
private Unit [ , ] cells = new Unit [ NUM_CELLS , NUM_CELLS ] ;
15
15
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 ; }
18
18
19
19
20
20
@@ -34,7 +34,7 @@ public Grid()
34
34
35
35
//Add unit to grid
36
36
//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 )
38
38
{
39
39
//Determine which grid cell it's in
40
40
Vector2Int cellPos = ConvertFromWorldToCell ( newUnit . transform . position ) ;
@@ -53,23 +53,25 @@ public void Add(Unit newUnit)
53
53
54
54
nextUnit . prev = newUnit ;
55
55
}
56
+
57
+ if ( isNewUnit )
58
+ {
59
+ unitCount += 1 ;
60
+ }
56
61
}
57
62
58
63
59
64
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 )
62
68
{
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 ) ;
65
71
66
72
//See which cell it's moving to
67
73
Vector2Int newCellPos = ConvertFromWorldToCell ( newPos ) ;
68
74
69
- //TODO: Validate if this is a valid cell pos
70
-
71
- unit . transform . position = newPos ;
72
-
73
75
//If it didn't change cell, we are done
74
76
if ( oldCellPos . x == newCellPos . x && oldCellPos . y == newCellPos . y )
75
77
{
@@ -114,8 +116,15 @@ public Vector2Int ConvertFromWorldToCell(Vector3 pos)
114
116
{
115
117
//Dividing coordinate by cell size converts from world space to cell space
116
118
//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
119
128
120
129
Vector2Int cellPos = new Vector2Int ( cellX , cellY ) ;
121
130
@@ -124,6 +133,23 @@ public Vector2Int ConvertFromWorldToCell(Vector3 pos)
124
133
125
134
126
135
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
+
127
153
//
128
154
// Fighting
129
155
//
@@ -136,17 +162,17 @@ public void HandleMelee()
136
162
{
137
163
for ( int y = 0 ; y < NUM_CELLS ; y ++ )
138
164
{
139
- HandleCell ( new Vector2Int ( x , y ) ) ;
165
+ HandleCell ( x , y ) ;
140
166
}
141
167
}
142
168
}
143
169
144
170
145
171
146
172
//Handles fight for a single cell
147
- private void HandleCell ( Vector2Int cellPos )
173
+ private void HandleCell ( int x , int y )
148
174
{
149
- Unit unit = cells [ cellPos . x , cellPos . y ] ;
175
+ Unit unit = cells [ x , y ] ;
150
176
151
177
//Make each unit fight all other units once in this cell
152
178
//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)
164
190
//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)
165
191
//We also have to check that there's a surrounding cell because the current cell might be the border
166
192
//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 )
168
194
{
169
- HandleUnit ( unit , cells [ cellPos . x - 1 , cellPos . y - 1 ] ) ;
195
+ HandleUnit ( unit , cells [ x - 1 , y - 1 ] ) ;
170
196
}
171
- if ( cellPos . x > 0 )
197
+ if ( x > 0 )
172
198
{
173
- HandleUnit ( unit , cells [ cellPos . x - 1 , cellPos . y - 0 ] ) ;
199
+ HandleUnit ( unit , cells [ x - 1 , y - 0 ] ) ;
174
200
}
175
- if ( cellPos . y > 0 )
201
+ if ( y > 0 )
176
202
{
177
- HandleUnit ( unit , cells [ cellPos . x - 0 , cellPos . y - 1 ] ) ;
203
+ HandleUnit ( unit , cells [ x - 0 , y - 1 ] ) ;
178
204
}
179
- if ( cellPos . x > 0 && cellPos . y < NUM_CELLS - 1 )
205
+ if ( x > 0 && y < NUM_CELLS - 1 )
180
206
{
181
- HandleUnit ( unit , cells [ cellPos . x - 1 , cellPos . y + 1 ] ) ;
207
+ HandleUnit ( unit , cells [ x - 1 , y + 1 ] ) ;
182
208
}
183
209
184
210
unit = unit . next ;
@@ -187,13 +213,13 @@ private void HandleCell(Vector2Int cellPos)
187
213
188
214
189
215
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
191
217
private void HandleUnit ( Unit unit , Unit other )
192
218
{
193
219
while ( other != null )
194
220
{
195
221
//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 )
197
223
{
198
224
HandleAttack ( unit , other ) ;
199
225
}
@@ -208,7 +234,8 @@ private void HandleUnit(Unit unit, Unit other)
208
234
private void HandleAttack ( Unit one , Unit two )
209
235
{
210
236
//Insert fighting mechanic
211
- Debug . Log ( "Two units are fighting! monkaS" ) ;
237
+ one . StartFighting ( ) ;
238
+ two . StartFighting ( ) ;
212
239
}
213
240
}
214
241
}
0 commit comments