@@ -18,26 +18,21 @@ public class Piece : MonoBehaviour
18
18
19
19
public void Initialize ( Board board , Vector3Int position , TetrominoData data )
20
20
{
21
+ this . data = data ;
21
22
this . board = board ;
22
23
this . position = position ;
23
24
this . rotationIndex = 0 ;
24
- this . data = data ;
25
25
26
26
this . stepTime = Time . time + this . stepDelay ;
27
27
this . moveTime = Time . time + this . moveDelay ;
28
28
this . lockTime = 0f ;
29
29
30
- UpdateCells ( ) ;
31
- }
32
-
33
- private void UpdateCells ( )
34
- {
35
30
if ( this . cells == null ) {
36
- this . cells = new Vector3Int [ this . data . cells . Length ] ;
31
+ this . cells = new Vector3Int [ data . cells . Length ] ;
37
32
}
38
33
39
34
for ( int i = 0 ; i < this . cells . Length ; i ++ ) {
40
- this . cells [ i ] = ( Vector3Int ) this . data . rotations [ this . rotationIndex , i ] ;
35
+ this . cells [ i ] = ( Vector3Int ) data . cells [ i ] ;
41
36
}
42
37
}
43
38
@@ -144,26 +139,59 @@ private bool Move(Vector2Int translation)
144
139
145
140
private void Rotate ( int direction )
146
141
{
147
- // Store the current rotation in case the rotation fails and we need to revert
142
+ // Store the current rotation in case the rotation fails
143
+ // and we need to revert
148
144
int originalRotation = this . rotationIndex ;
149
145
150
- // Update the cell data to the new rotation
146
+ // Rotate all of the cells using a rotation matrix
151
147
this . rotationIndex = Wrap ( this . rotationIndex + direction , 0 , 4 ) ;
152
- UpdateCells ( ) ;
148
+ ApplyRotationMatrix ( direction ) ;
153
149
154
150
// Revert the rotation if the wall kick tests fail
155
- if ( ! TestWallKicks ( direction ) )
151
+ if ( ! TestWallKicks ( this . rotationIndex , direction ) )
156
152
{
157
153
this . rotationIndex = originalRotation ;
158
- UpdateCells ( ) ;
154
+ ApplyRotationMatrix ( - direction ) ;
155
+ }
156
+ }
157
+
158
+ private void ApplyRotationMatrix ( int direction )
159
+ {
160
+ float [ ] matrix = Data . RotationMatrix ;
161
+
162
+ // Rotate all of the cells using the rotation matrix
163
+ for ( int i = 0 ; i < this . cells . Length ; i ++ )
164
+ {
165
+ Vector3 cell = this . cells [ i ] ;
166
+
167
+ int x , y ;
168
+
169
+ switch ( this . data . tetromino )
170
+ {
171
+ case Tetromino . I :
172
+ case Tetromino . O :
173
+ // "I" and "O" are rotated from an offset center point
174
+ cell . x -= 0.5f ;
175
+ cell . y -= 0.5f ;
176
+ x = Mathf . CeilToInt ( ( cell . x * matrix [ 0 ] * direction ) + ( cell . y * matrix [ 1 ] * direction ) ) ;
177
+ y = Mathf . CeilToInt ( ( cell . x * matrix [ 2 ] * direction ) + ( cell . y * matrix [ 3 ] * direction ) ) ;
178
+ break ;
179
+
180
+ default :
181
+ x = Mathf . RoundToInt ( ( cell . x * matrix [ 0 ] * direction ) + ( cell . y * matrix [ 1 ] * direction ) ) ;
182
+ y = Mathf . RoundToInt ( ( cell . x * matrix [ 2 ] * direction ) + ( cell . y * matrix [ 3 ] * direction ) ) ;
183
+ break ;
184
+ }
185
+
186
+ this . cells [ i ] = new Vector3Int ( x , y , 0 ) ;
159
187
}
160
188
}
161
189
162
- private bool TestWallKicks ( int rotationDirection )
190
+ private bool TestWallKicks ( int rotationIndex , int rotationDirection )
163
191
{
164
- int wallKickIndex = GetWallKickIndex ( rotationDirection ) ;
192
+ int wallKickIndex = GetWallKickIndex ( rotationIndex , rotationDirection ) ;
165
193
166
- for ( int i = 0 ; i < 5 ; i ++ )
194
+ for ( int i = 0 ; i < this . data . wallKicks . GetLength ( 1 ) ; i ++ )
167
195
{
168
196
Vector2Int translation = this . data . wallKicks [ wallKickIndex , i ] ;
169
197
@@ -175,9 +203,9 @@ private bool TestWallKicks(int rotationDirection)
175
203
return false ;
176
204
}
177
205
178
- private int GetWallKickIndex ( int rotationDirection )
206
+ private int GetWallKickIndex ( int rotationIndex , int rotationDirection )
179
207
{
180
- int wallKickIndex = this . rotationIndex * 2 ;
208
+ int wallKickIndex = rotationIndex * 2 ;
181
209
182
210
if ( rotationDirection < 0 ) {
183
211
wallKickIndex -- ;
0 commit comments