@@ -18,26 +18,21 @@ public class Piece : MonoBehaviour
1818
1919 public void Initialize ( Board board , Vector3Int position , TetrominoData data )
2020 {
21+ this . data = data ;
2122 this . board = board ;
2223 this . position = position ;
2324 this . rotationIndex = 0 ;
24- this . data = data ;
2525
2626 this . stepTime = Time . time + this . stepDelay ;
2727 this . moveTime = Time . time + this . moveDelay ;
2828 this . lockTime = 0f ;
2929
30- UpdateCells ( ) ;
31- }
32-
33- private void UpdateCells ( )
34- {
3530 if ( this . cells == null ) {
36- this . cells = new Vector3Int [ this . data . cells . Length ] ;
31+ this . cells = new Vector3Int [ data . cells . Length ] ;
3732 }
3833
3934 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 ] ;
4136 }
4237 }
4338
@@ -144,26 +139,59 @@ private bool Move(Vector2Int translation)
144139
145140 private void Rotate ( int direction )
146141 {
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
148144 int originalRotation = this . rotationIndex ;
149145
150- // Update the cell data to the new rotation
146+ // Rotate all of the cells using a rotation matrix
151147 this . rotationIndex = Wrap ( this . rotationIndex + direction , 0 , 4 ) ;
152- UpdateCells ( ) ;
148+ ApplyRotationMatrix ( direction ) ;
153149
154150 // Revert the rotation if the wall kick tests fail
155- if ( ! TestWallKicks ( direction ) )
151+ if ( ! TestWallKicks ( this . rotationIndex , direction ) )
156152 {
157153 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 ) ;
159187 }
160188 }
161189
162- private bool TestWallKicks ( int rotationDirection )
190+ private bool TestWallKicks ( int rotationIndex , int rotationDirection )
163191 {
164- int wallKickIndex = GetWallKickIndex ( rotationDirection ) ;
192+ int wallKickIndex = GetWallKickIndex ( rotationIndex , rotationDirection ) ;
165193
166- for ( int i = 0 ; i < 5 ; i ++ )
194+ for ( int i = 0 ; i < this . data . wallKicks . GetLength ( 1 ) ; i ++ )
167195 {
168196 Vector2Int translation = this . data . wallKicks [ wallKickIndex , i ] ;
169197
@@ -175,9 +203,9 @@ private bool TestWallKicks(int rotationDirection)
175203 return false ;
176204 }
177205
178- private int GetWallKickIndex ( int rotationDirection )
206+ private int GetWallKickIndex ( int rotationIndex , int rotationDirection )
179207 {
180- int wallKickIndex = this . rotationIndex * 2 ;
208+ int wallKickIndex = rotationIndex * 2 ;
181209
182210 if ( rotationDirection < 0 ) {
183211 wallKickIndex -- ;
0 commit comments