Skip to content

Commit

Permalink
Replace tetromino scriptable object with static data
Browse files Browse the repository at this point in the history
  • Loading branch information
adamgraham committed Aug 10, 2021
1 parent 9830be5 commit a8b28e6
Show file tree
Hide file tree
Showing 18 changed files with 128 additions and 208 deletions.
54 changes: 54 additions & 0 deletions Assets/Scripts/Data.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Collections.Generic;
using UnityEngine;

public static class Data
{
public static readonly float cos = Mathf.Cos(Mathf.PI / 2f);
public static readonly float sin = Mathf.Sin(Mathf.PI / 2f);
public static readonly float[] RotationMatrix = new float[] { cos, sin, -sin, cos };

public static readonly Dictionary<Tetromino, Vector2Int[]> Cells = new Dictionary<Tetromino, Vector2Int[]>()
{
{ Tetromino.I, new Vector2Int[] { new Vector2Int(-1, 1), new Vector2Int( 0, 1), new Vector2Int( 1, 1), new Vector2Int( 2, 1) } },
{ Tetromino.J, new Vector2Int[] { new Vector2Int(-1, 1), new Vector2Int(-1, 0), new Vector2Int( 0, 0), new Vector2Int( 1, 0) } },
{ Tetromino.L, new Vector2Int[] { new Vector2Int( 1, 1), new Vector2Int(-1, 0), new Vector2Int( 0, 0), new Vector2Int( 1, 0) } },
{ Tetromino.O, new Vector2Int[] { new Vector2Int( 0, 1), new Vector2Int( 1, 1), new Vector2Int( 0, 0), new Vector2Int( 1, 0) } },
{ Tetromino.S, new Vector2Int[] { new Vector2Int( 0, 1), new Vector2Int( 1, 1), new Vector2Int(-1, 0), new Vector2Int( 0, 0) } },
{ Tetromino.T, new Vector2Int[] { new Vector2Int( 0, 1), new Vector2Int(-1, 0), new Vector2Int( 0, 0), new Vector2Int( 1, 0) } },
{ Tetromino.Z, new Vector2Int[] { new Vector2Int(-1, 1), new Vector2Int( 0, 1), new Vector2Int( 0, 0), new Vector2Int( 1, 0) } },
};

private static readonly Vector2Int[,] WallKicksI = new Vector2Int[,] {
{ new Vector2Int(0, 0), new Vector2Int(-2, 0), new Vector2Int( 1, 0), new Vector2Int(-2,-1), new Vector2Int( 1, 2) },
{ new Vector2Int(0, 0), new Vector2Int( 2, 0), new Vector2Int(-1, 0), new Vector2Int( 2, 1), new Vector2Int(-1,-2) },
{ new Vector2Int(0, 0), new Vector2Int(-1, 0), new Vector2Int( 2, 0), new Vector2Int(-1, 2), new Vector2Int( 2,-1) },
{ new Vector2Int(0, 0), new Vector2Int( 1, 0), new Vector2Int(-2, 0), new Vector2Int( 1,-2), new Vector2Int(-2, 1) },
{ new Vector2Int(0, 0), new Vector2Int( 2, 0), new Vector2Int(-1, 0), new Vector2Int( 2, 1), new Vector2Int(-1,-2) },
{ new Vector2Int(0, 0), new Vector2Int(-2, 0), new Vector2Int( 1, 0), new Vector2Int(-2,-1), new Vector2Int( 1, 2) },
{ new Vector2Int(0, 0), new Vector2Int( 1, 0), new Vector2Int(-2, 0), new Vector2Int( 1,-2), new Vector2Int(-2, 1) },
{ new Vector2Int(0, 0), new Vector2Int(-1, 0), new Vector2Int( 2, 0), new Vector2Int(-1, 2), new Vector2Int( 2,-1) },
};

private static readonly Vector2Int[,] WallKicksJLOSTZ = new Vector2Int[,] {
{ new Vector2Int(0, 0), new Vector2Int(-1, 0), new Vector2Int(-1, 1), new Vector2Int(0,-2), new Vector2Int(-1,-2) },
{ new Vector2Int(0, 0), new Vector2Int( 1, 0), new Vector2Int( 1,-1), new Vector2Int(0, 2), new Vector2Int( 1, 2) },
{ new Vector2Int(0, 0), new Vector2Int( 1, 0), new Vector2Int( 1,-1), new Vector2Int(0, 2), new Vector2Int( 1, 2) },
{ new Vector2Int(0, 0), new Vector2Int(-1, 0), new Vector2Int(-1, 1), new Vector2Int(0,-2), new Vector2Int(-1,-2) },
{ new Vector2Int(0, 0), new Vector2Int( 1, 0), new Vector2Int( 1, 1), new Vector2Int(0,-2), new Vector2Int( 1,-2) },
{ new Vector2Int(0, 0), new Vector2Int(-1, 0), new Vector2Int(-1,-1), new Vector2Int(0, 2), new Vector2Int(-1, 2) },
{ new Vector2Int(0, 0), new Vector2Int(-1, 0), new Vector2Int(-1,-1), new Vector2Int(0, 2), new Vector2Int(-1, 2) },
{ new Vector2Int(0, 0), new Vector2Int( 1, 0), new Vector2Int( 1, 1), new Vector2Int(0,-2), new Vector2Int( 1,-2) },
};

public static readonly Dictionary<Tetromino, Vector2Int[,]> WallKicks = new Dictionary<Tetromino, Vector2Int[,]>()
{
{ Tetromino.I, WallKicksI },
{ Tetromino.J, WallKicksJLOSTZ },
{ Tetromino.L, WallKicksJLOSTZ },
{ Tetromino.O, WallKicksJLOSTZ },
{ Tetromino.S, WallKicksJLOSTZ },
{ Tetromino.T, WallKicksJLOSTZ },
{ Tetromino.Z, WallKicksJLOSTZ },
};

}
11 changes: 11 additions & 0 deletions Assets/Scripts/Data.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 63 additions & 4 deletions Assets/Scripts/Tetromino.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,69 @@
using UnityEngine;
using UnityEngine.Tilemaps;

[CreateAssetMenu(menuName = "Tetris/Tetromino")]
public class Tetromino : ScriptableObject
public enum Tetromino
{
public Tile block;
public Vector2Int[] cells = new Vector2Int[4];
I, J, L, O, S, T, Z
}

[System.Serializable]
public struct TetrominoData
{
public Tile tile;
public Tetromino tetromino;

public Vector2Int[] cells { get; private set; }
public Vector2Int[,] wallKicks { get; private set; }
public Vector2Int[,] rotations { get; private set; }

public void Initialize()
{
this.cells = Data.Cells[this.tetromino];
this.wallKicks = Data.WallKicks[this.tetromino];

PrecomputeRotations();
}

private void PrecomputeRotations()
{
this.rotations = new Vector2Int[4, 4];

// Set the initial rotation to the spawn state
for (int i = 0; i < this.cells.Length; i++) {
this.rotations[0, i] = this.cells[i];
}

// Start at index 1 since we already set the initial rotation
for (int i = 1; i < 4; i++)
{
for (int j = 0; j < this.cells.Length; j++)
{
// Get the cell data from the previous rotation
Vector2 cell = this.rotations[i - 1, j];

int x, y;

// Calculate the x,y using a rotation matrix
switch (this.tetromino)
{
// I, O are rotated from an offset center point
case Tetromino.I:
case Tetromino.O:
cell.x -= 0.5f;
cell.y -= 0.5f;
x = Mathf.CeilToInt((cell.x * Data.RotationMatrix[0]) + (cell.y * Data.RotationMatrix[1]));
y = Mathf.CeilToInt((cell.x * Data.RotationMatrix[2]) + (cell.y * Data.RotationMatrix[3]));
break;

default:
x = Mathf.RoundToInt((cell.x * Data.RotationMatrix[0]) + (cell.y * Data.RotationMatrix[1]));
y = Mathf.RoundToInt((cell.x * Data.RotationMatrix[2]) + (cell.y * Data.RotationMatrix[3]));
break;
}

this.rotations[i, j] = new Vector2Int(x, y);
}
}
}

}
8 changes: 0 additions & 8 deletions Assets/Tetrominoes.meta

This file was deleted.

20 changes: 0 additions & 20 deletions Assets/Tetrominoes/Tetromino-I.asset

This file was deleted.

8 changes: 0 additions & 8 deletions Assets/Tetrominoes/Tetromino-I.asset.meta

This file was deleted.

20 changes: 0 additions & 20 deletions Assets/Tetrominoes/Tetromino-J.asset

This file was deleted.

8 changes: 0 additions & 8 deletions Assets/Tetrominoes/Tetromino-J.asset.meta

This file was deleted.

20 changes: 0 additions & 20 deletions Assets/Tetrominoes/Tetromino-L.asset

This file was deleted.

8 changes: 0 additions & 8 deletions Assets/Tetrominoes/Tetromino-L.asset.meta

This file was deleted.

20 changes: 0 additions & 20 deletions Assets/Tetrominoes/Tetromino-O.asset

This file was deleted.

8 changes: 0 additions & 8 deletions Assets/Tetrominoes/Tetromino-O.asset.meta

This file was deleted.

20 changes: 0 additions & 20 deletions Assets/Tetrominoes/Tetromino-S.asset

This file was deleted.

8 changes: 0 additions & 8 deletions Assets/Tetrominoes/Tetromino-S.asset.meta

This file was deleted.

20 changes: 0 additions & 20 deletions Assets/Tetrominoes/Tetromino-T.asset

This file was deleted.

8 changes: 0 additions & 8 deletions Assets/Tetrominoes/Tetromino-T.asset.meta

This file was deleted.

20 changes: 0 additions & 20 deletions Assets/Tetrominoes/Tetromino-Z.asset

This file was deleted.

8 changes: 0 additions & 8 deletions Assets/Tetrominoes/Tetromino-Z.asset.meta

This file was deleted.

0 comments on commit a8b28e6

Please sign in to comment.