-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
- Loading branch information
There are no files selected for viewing
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 }, | ||
}; | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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); | ||
} | ||
} | ||
} | ||
|
||
} |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.