Skip to content

Commit a8b28e6

Browse files
committed
Replace tetromino scriptable object with static data
1 parent 9830be5 commit a8b28e6

18 files changed

+128
-208
lines changed

β€ŽAssets/Scripts/Data.csβ€Ž

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System.Collections.Generic;
2+
using UnityEngine;
3+
4+
public static class Data
5+
{
6+
public static readonly float cos = Mathf.Cos(Mathf.PI / 2f);
7+
public static readonly float sin = Mathf.Sin(Mathf.PI / 2f);
8+
public static readonly float[] RotationMatrix = new float[] { cos, sin, -sin, cos };
9+
10+
public static readonly Dictionary<Tetromino, Vector2Int[]> Cells = new Dictionary<Tetromino, Vector2Int[]>()
11+
{
12+
{ Tetromino.I, new Vector2Int[] { new Vector2Int(-1, 1), new Vector2Int( 0, 1), new Vector2Int( 1, 1), new Vector2Int( 2, 1) } },
13+
{ Tetromino.J, new Vector2Int[] { new Vector2Int(-1, 1), new Vector2Int(-1, 0), new Vector2Int( 0, 0), new Vector2Int( 1, 0) } },
14+
{ Tetromino.L, new Vector2Int[] { new Vector2Int( 1, 1), new Vector2Int(-1, 0), new Vector2Int( 0, 0), new Vector2Int( 1, 0) } },
15+
{ Tetromino.O, new Vector2Int[] { new Vector2Int( 0, 1), new Vector2Int( 1, 1), new Vector2Int( 0, 0), new Vector2Int( 1, 0) } },
16+
{ Tetromino.S, new Vector2Int[] { new Vector2Int( 0, 1), new Vector2Int( 1, 1), new Vector2Int(-1, 0), new Vector2Int( 0, 0) } },
17+
{ Tetromino.T, new Vector2Int[] { new Vector2Int( 0, 1), new Vector2Int(-1, 0), new Vector2Int( 0, 0), new Vector2Int( 1, 0) } },
18+
{ Tetromino.Z, new Vector2Int[] { new Vector2Int(-1, 1), new Vector2Int( 0, 1), new Vector2Int( 0, 0), new Vector2Int( 1, 0) } },
19+
};
20+
21+
private static readonly Vector2Int[,] WallKicksI = new Vector2Int[,] {
22+
{ new Vector2Int(0, 0), new Vector2Int(-2, 0), new Vector2Int( 1, 0), new Vector2Int(-2,-1), new Vector2Int( 1, 2) },
23+
{ new Vector2Int(0, 0), new Vector2Int( 2, 0), new Vector2Int(-1, 0), new Vector2Int( 2, 1), new Vector2Int(-1,-2) },
24+
{ new Vector2Int(0, 0), new Vector2Int(-1, 0), new Vector2Int( 2, 0), new Vector2Int(-1, 2), new Vector2Int( 2,-1) },
25+
{ new Vector2Int(0, 0), new Vector2Int( 1, 0), new Vector2Int(-2, 0), new Vector2Int( 1,-2), new Vector2Int(-2, 1) },
26+
{ new Vector2Int(0, 0), new Vector2Int( 2, 0), new Vector2Int(-1, 0), new Vector2Int( 2, 1), new Vector2Int(-1,-2) },
27+
{ new Vector2Int(0, 0), new Vector2Int(-2, 0), new Vector2Int( 1, 0), new Vector2Int(-2,-1), new Vector2Int( 1, 2) },
28+
{ new Vector2Int(0, 0), new Vector2Int( 1, 0), new Vector2Int(-2, 0), new Vector2Int( 1,-2), new Vector2Int(-2, 1) },
29+
{ new Vector2Int(0, 0), new Vector2Int(-1, 0), new Vector2Int( 2, 0), new Vector2Int(-1, 2), new Vector2Int( 2,-1) },
30+
};
31+
32+
private static readonly Vector2Int[,] WallKicksJLOSTZ = new Vector2Int[,] {
33+
{ new Vector2Int(0, 0), new Vector2Int(-1, 0), new Vector2Int(-1, 1), new Vector2Int(0,-2), new Vector2Int(-1,-2) },
34+
{ new Vector2Int(0, 0), new Vector2Int( 1, 0), new Vector2Int( 1,-1), new Vector2Int(0, 2), new Vector2Int( 1, 2) },
35+
{ new Vector2Int(0, 0), new Vector2Int( 1, 0), new Vector2Int( 1,-1), new Vector2Int(0, 2), new Vector2Int( 1, 2) },
36+
{ new Vector2Int(0, 0), new Vector2Int(-1, 0), new Vector2Int(-1, 1), new Vector2Int(0,-2), new Vector2Int(-1,-2) },
37+
{ new Vector2Int(0, 0), new Vector2Int( 1, 0), new Vector2Int( 1, 1), new Vector2Int(0,-2), new Vector2Int( 1,-2) },
38+
{ new Vector2Int(0, 0), new Vector2Int(-1, 0), new Vector2Int(-1,-1), new Vector2Int(0, 2), new Vector2Int(-1, 2) },
39+
{ new Vector2Int(0, 0), new Vector2Int(-1, 0), new Vector2Int(-1,-1), new Vector2Int(0, 2), new Vector2Int(-1, 2) },
40+
{ new Vector2Int(0, 0), new Vector2Int( 1, 0), new Vector2Int( 1, 1), new Vector2Int(0,-2), new Vector2Int( 1,-2) },
41+
};
42+
43+
public static readonly Dictionary<Tetromino, Vector2Int[,]> WallKicks = new Dictionary<Tetromino, Vector2Int[,]>()
44+
{
45+
{ Tetromino.I, WallKicksI },
46+
{ Tetromino.J, WallKicksJLOSTZ },
47+
{ Tetromino.L, WallKicksJLOSTZ },
48+
{ Tetromino.O, WallKicksJLOSTZ },
49+
{ Tetromino.S, WallKicksJLOSTZ },
50+
{ Tetromino.T, WallKicksJLOSTZ },
51+
{ Tetromino.Z, WallKicksJLOSTZ },
52+
};
53+
54+
}

β€ŽAssets/Scripts/Data.cs.metaβ€Ž

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€ŽAssets/Scripts/Tetromino.csβ€Ž

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,69 @@
11
using UnityEngine;
22
using UnityEngine.Tilemaps;
33

4-
[CreateAssetMenu(menuName = "Tetris/Tetromino")]
5-
public class Tetromino : ScriptableObject
4+
public enum Tetromino
65
{
7-
public Tile block;
8-
public Vector2Int[] cells = new Vector2Int[4];
6+
I, J, L, O, S, T, Z
7+
}
8+
9+
[System.Serializable]
10+
public struct TetrominoData
11+
{
12+
public Tile tile;
13+
public Tetromino tetromino;
14+
15+
public Vector2Int[] cells { get; private set; }
16+
public Vector2Int[,] wallKicks { get; private set; }
17+
public Vector2Int[,] rotations { get; private set; }
18+
19+
public void Initialize()
20+
{
21+
this.cells = Data.Cells[this.tetromino];
22+
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+
}
67+
}
968

1069
}

β€ŽAssets/Tetrominoes.metaβ€Ž

Lines changed: 0 additions & 8 deletions
This file was deleted.

β€ŽAssets/Tetrominoes/Tetromino-I.assetβ€Ž

Lines changed: 0 additions & 20 deletions
This file was deleted.

β€ŽAssets/Tetrominoes/Tetromino-I.asset.metaβ€Ž

Lines changed: 0 additions & 8 deletions
This file was deleted.

β€ŽAssets/Tetrominoes/Tetromino-J.assetβ€Ž

Lines changed: 0 additions & 20 deletions
This file was deleted.

β€ŽAssets/Tetrominoes/Tetromino-J.asset.metaβ€Ž

Lines changed: 0 additions & 8 deletions
This file was deleted.

β€ŽAssets/Tetrominoes/Tetromino-L.assetβ€Ž

Lines changed: 0 additions & 20 deletions
This file was deleted.

β€ŽAssets/Tetrominoes/Tetromino-L.asset.metaβ€Ž

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
Β (0)