Skip to content

Commit 6dbe143

Browse files
committed
Simplify rotation logic
1 parent 34d8cb8 commit 6dbe143

File tree

3 files changed

+47
-64
lines changed

3 files changed

+47
-64
lines changed

Assets/Scenes/Tetris.unity

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ Camera:
315315
far clip plane: 1000
316316
field of view: 60
317317
orthographic: 1
318-
orthographic size: 12.28125
318+
orthographic size: 12
319319
m_Depth: -1
320320
m_CullingMask:
321321
serializedVersion: 2

Assets/Scripts/Piece.cs

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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--;

Assets/Scripts/Tetromino.cs

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,56 +14,11 @@ public struct TetrominoData
1414

1515
public Vector2Int[] cells { get; private set; }
1616
public Vector2Int[,] wallKicks { get; private set; }
17-
public Vector2Int[,] rotations { get; private set; }
1817

1918
public void Initialize()
2019
{
2120
this.cells = Data.Cells[this.tetromino];
2221
this.wallKicks = Data.WallKicks[this.tetromino];
23-
24-
PrecomputeRotations();
25-
}
26-
27-
private void PrecomputeRotations()
28-
{
29-
this.rotations = new Vector2Int[4, 4];
30-
31-
// Set the initial rotation to the spawn state
32-
for (int i = 0; i < this.cells.Length; i++) {
33-
this.rotations[0, i] = this.cells[i];
34-
}
35-
36-
// Start at index 1 since we already set the initial rotation
37-
for (int i = 1; i < 4; i++)
38-
{
39-
for (int j = 0; j < this.cells.Length; j++)
40-
{
41-
// Get the cell data from the previous rotation
42-
Vector2 cell = this.rotations[i - 1, j];
43-
44-
int x, y;
45-
46-
// Calculate the x,y using a rotation matrix
47-
switch (this.tetromino)
48-
{
49-
// I, O are rotated from an offset center point
50-
case Tetromino.I:
51-
case Tetromino.O:
52-
cell.x -= 0.5f;
53-
cell.y -= 0.5f;
54-
x = Mathf.CeilToInt((cell.x * Data.RotationMatrix[0]) + (cell.y * Data.RotationMatrix[1]));
55-
y = Mathf.CeilToInt((cell.x * Data.RotationMatrix[2]) + (cell.y * Data.RotationMatrix[3]));
56-
break;
57-
58-
default:
59-
x = Mathf.RoundToInt((cell.x * Data.RotationMatrix[0]) + (cell.y * Data.RotationMatrix[1]));
60-
y = Mathf.RoundToInt((cell.x * Data.RotationMatrix[2]) + (cell.y * Data.RotationMatrix[3]));
61-
break;
62-
}
63-
64-
this.rotations[i, j] = new Vector2Int(x, y);
65-
}
66-
}
6722
}
6823

6924
}

0 commit comments

Comments
 (0)