diff --git a/Assets/Scripts/Data.cs b/Assets/Scripts/Data.cs new file mode 100644 index 0000000..1f585d6 --- /dev/null +++ b/Assets/Scripts/Data.cs @@ -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 Cells = new Dictionary() + { + { 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 WallKicks = new Dictionary() + { + { Tetromino.I, WallKicksI }, + { Tetromino.J, WallKicksJLOSTZ }, + { Tetromino.L, WallKicksJLOSTZ }, + { Tetromino.O, WallKicksJLOSTZ }, + { Tetromino.S, WallKicksJLOSTZ }, + { Tetromino.T, WallKicksJLOSTZ }, + { Tetromino.Z, WallKicksJLOSTZ }, + }; + +} diff --git a/Assets/Scripts/Data.cs.meta b/Assets/Scripts/Data.cs.meta new file mode 100644 index 0000000..6c3f7b6 --- /dev/null +++ b/Assets/Scripts/Data.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a38294e7cf8abb746ac2d98dadc7fc8c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Tetromino.cs b/Assets/Scripts/Tetromino.cs index 0236688..6b2b8d9 100644 --- a/Assets/Scripts/Tetromino.cs +++ b/Assets/Scripts/Tetromino.cs @@ -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); + } + } + } } diff --git a/Assets/Tetrominoes.meta b/Assets/Tetrominoes.meta deleted file mode 100644 index 24ae694..0000000 --- a/Assets/Tetrominoes.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 0ae686308b0ce36428ed7d8536a3f370 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Tetrominoes/Tetromino-I.asset b/Assets/Tetrominoes/Tetromino-I.asset deleted file mode 100644 index 472e61f..0000000 --- a/Assets/Tetrominoes/Tetromino-I.asset +++ /dev/null @@ -1,20 +0,0 @@ -%YAML 1.1 -%TAG !u! tag:unity3d.com,2011: ---- !u!114 &11400000 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 0} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 8cd6f19bdc364ed40a8b615b062c98ac, type: 3} - m_Name: Tetromino-I - m_EditorClassIdentifier: - block: {fileID: 11400000, guid: aa44b5216a5f39e4881c5323564afe81, type: 2} - cells: - - {x: -1, y: 0} - - {x: 0, y: 0} - - {x: 1, y: 0} - - {x: 2, y: 0} diff --git a/Assets/Tetrominoes/Tetromino-I.asset.meta b/Assets/Tetrominoes/Tetromino-I.asset.meta deleted file mode 100644 index 9a709aa..0000000 --- a/Assets/Tetrominoes/Tetromino-I.asset.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 7d21b252b2497f641bfbead1606ba793 -NativeFormatImporter: - externalObjects: {} - mainObjectFileID: 0 - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Tetrominoes/Tetromino-J.asset b/Assets/Tetrominoes/Tetromino-J.asset deleted file mode 100644 index 0aa1cef..0000000 --- a/Assets/Tetrominoes/Tetromino-J.asset +++ /dev/null @@ -1,20 +0,0 @@ -%YAML 1.1 -%TAG !u! tag:unity3d.com,2011: ---- !u!114 &11400000 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 0} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 8cd6f19bdc364ed40a8b615b062c98ac, type: 3} - m_Name: Tetromino-J - m_EditorClassIdentifier: - block: {fileID: 11400000, guid: f6c4109fd0d185b419e5e665b07c42f0, type: 2} - cells: - - {x: -1, y: 1} - - {x: -1, y: 0} - - {x: 0, y: 0} - - {x: 1, y: 0} diff --git a/Assets/Tetrominoes/Tetromino-J.asset.meta b/Assets/Tetrominoes/Tetromino-J.asset.meta deleted file mode 100644 index 20728ff..0000000 --- a/Assets/Tetrominoes/Tetromino-J.asset.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 5de7a906ab4625248bb6f3990a34a34c -NativeFormatImporter: - externalObjects: {} - mainObjectFileID: 0 - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Tetrominoes/Tetromino-L.asset b/Assets/Tetrominoes/Tetromino-L.asset deleted file mode 100644 index 04f438b..0000000 --- a/Assets/Tetrominoes/Tetromino-L.asset +++ /dev/null @@ -1,20 +0,0 @@ -%YAML 1.1 -%TAG !u! tag:unity3d.com,2011: ---- !u!114 &11400000 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 0} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 8cd6f19bdc364ed40a8b615b062c98ac, type: 3} - m_Name: Tetromino-L - m_EditorClassIdentifier: - block: {fileID: 11400000, guid: 89eae29ecf56ea14c8b3927213a00066, type: 2} - cells: - - {x: 1, y: 1} - - {x: -1, y: 0} - - {x: 0, y: 0} - - {x: 1, y: 0} diff --git a/Assets/Tetrominoes/Tetromino-L.asset.meta b/Assets/Tetrominoes/Tetromino-L.asset.meta deleted file mode 100644 index 0d85b1d..0000000 --- a/Assets/Tetrominoes/Tetromino-L.asset.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 522468845f941cd468a9f590907fdd41 -NativeFormatImporter: - externalObjects: {} - mainObjectFileID: 0 - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Tetrominoes/Tetromino-O.asset b/Assets/Tetrominoes/Tetromino-O.asset deleted file mode 100644 index f1991c0..0000000 --- a/Assets/Tetrominoes/Tetromino-O.asset +++ /dev/null @@ -1,20 +0,0 @@ -%YAML 1.1 -%TAG !u! tag:unity3d.com,2011: ---- !u!114 &11400000 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 0} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 8cd6f19bdc364ed40a8b615b062c98ac, type: 3} - m_Name: Tetromino-O - m_EditorClassIdentifier: - block: {fileID: 11400000, guid: 66df2645ff909154b9c8f7c006a6af32, type: 2} - cells: - - {x: 0, y: 1} - - {x: 1, y: 1} - - {x: 0, y: 0} - - {x: 1, y: 0} diff --git a/Assets/Tetrominoes/Tetromino-O.asset.meta b/Assets/Tetrominoes/Tetromino-O.asset.meta deleted file mode 100644 index 7127e31..0000000 --- a/Assets/Tetrominoes/Tetromino-O.asset.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 53a433330362ac04f9725c46dd4e564d -NativeFormatImporter: - externalObjects: {} - mainObjectFileID: 0 - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Tetrominoes/Tetromino-S.asset b/Assets/Tetrominoes/Tetromino-S.asset deleted file mode 100644 index 04574d7..0000000 --- a/Assets/Tetrominoes/Tetromino-S.asset +++ /dev/null @@ -1,20 +0,0 @@ -%YAML 1.1 -%TAG !u! tag:unity3d.com,2011: ---- !u!114 &11400000 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 0} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 8cd6f19bdc364ed40a8b615b062c98ac, type: 3} - m_Name: Tetromino-S - m_EditorClassIdentifier: - block: {fileID: 11400000, guid: ae5c5dcd9d2744248ba2beb6b8dab80f, type: 2} - cells: - - {x: 0, y: 1} - - {x: 1, y: 1} - - {x: -1, y: 0} - - {x: 0, y: 0} diff --git a/Assets/Tetrominoes/Tetromino-S.asset.meta b/Assets/Tetrominoes/Tetromino-S.asset.meta deleted file mode 100644 index df7d7d0..0000000 --- a/Assets/Tetrominoes/Tetromino-S.asset.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 581e54ff100c0574dbd055bbd95d1b85 -NativeFormatImporter: - externalObjects: {} - mainObjectFileID: 0 - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Tetrominoes/Tetromino-T.asset b/Assets/Tetrominoes/Tetromino-T.asset deleted file mode 100644 index 37b5465..0000000 --- a/Assets/Tetrominoes/Tetromino-T.asset +++ /dev/null @@ -1,20 +0,0 @@ -%YAML 1.1 -%TAG !u! tag:unity3d.com,2011: ---- !u!114 &11400000 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 0} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 8cd6f19bdc364ed40a8b615b062c98ac, type: 3} - m_Name: Tetromino-T - m_EditorClassIdentifier: - block: {fileID: 11400000, guid: 8316c87896e7b6448a324d8a66e43174, type: 2} - cells: - - {x: 0, y: 1} - - {x: -1, y: 0} - - {x: 0, y: 0} - - {x: 1, y: 0} diff --git a/Assets/Tetrominoes/Tetromino-T.asset.meta b/Assets/Tetrominoes/Tetromino-T.asset.meta deleted file mode 100644 index 1965690..0000000 --- a/Assets/Tetrominoes/Tetromino-T.asset.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 406fafdc78ef0634787f484b0f242051 -NativeFormatImporter: - externalObjects: {} - mainObjectFileID: 0 - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Tetrominoes/Tetromino-Z.asset b/Assets/Tetrominoes/Tetromino-Z.asset deleted file mode 100644 index d38229c..0000000 --- a/Assets/Tetrominoes/Tetromino-Z.asset +++ /dev/null @@ -1,20 +0,0 @@ -%YAML 1.1 -%TAG !u! tag:unity3d.com,2011: ---- !u!114 &11400000 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 0} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 8cd6f19bdc364ed40a8b615b062c98ac, type: 3} - m_Name: Tetromino-Z - m_EditorClassIdentifier: - block: {fileID: 11400000, guid: be0a34e2a842ae742b4fa8541a5f9fcf, type: 2} - cells: - - {x: -1, y: 1} - - {x: 0, y: 1} - - {x: 0, y: 0} - - {x: 1, y: 0} diff --git a/Assets/Tetrominoes/Tetromino-Z.asset.meta b/Assets/Tetrominoes/Tetromino-Z.asset.meta deleted file mode 100644 index 9b033b5..0000000 --- a/Assets/Tetrominoes/Tetromino-Z.asset.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: f02664c95e021b042967c8756e2e0471 -NativeFormatImporter: - externalObjects: {} - mainObjectFileID: 0 - userData: - assetBundleName: - assetBundleVariant: