diff --git a/Assets/Scripts/tetrominoUserControl.cs b/Assets/Scripts/tetrominoUserControl.cs index 0ceb513..2d4d2ec 100644 --- a/Assets/Scripts/tetrominoUserControl.cs +++ b/Assets/Scripts/tetrominoUserControl.cs @@ -45,13 +45,15 @@ public class tetrominoUserControl : MonoBehaviour if (Input.GetButtonDown("tetrisRotateLeft") && activeTetromino != null) { - activeTetromino.gameObject.transform.Rotate(0,0,90,Space.World); + rotateWithChecks(90); + // activeTetromino.gameObject.transform.Rotate(0,0,90,Space.World); GetComponent().Play(); } if (Input.GetButtonDown("tetrisRotateRight") && activeTetromino != null) { - activeTetromino.gameObject.transform.Rotate(0,0,-90,Space.World); + rotateWithChecks(90); + // activeTetromino.gameObject.transform.Rotate(0,0,-90,Space.World); GetComponent().Play(); } @@ -94,4 +96,41 @@ public class tetrominoUserControl : MonoBehaviour return false; } } + + public void rotateWithChecks(int degrees) + { + bool wouldCollide = false; + Collider2D[] results = new Collider2D[10]; + + //Perform the rotation (will undo later if there's a collision). + activeTetromino.gameObject.transform.Rotate(0,0,degrees,Space.World); + + foreach (Transform child in activeTetromino.transform) + { + //Store any overlapping colliders in the results array. + child.GetComponent().OverlapCollider(new ContactFilter2D().NoFilter(), results); + //If there are collisions... + if (results.Length != 0) + { + foreach (Collider2D a in results) + { + //and IF they aren't an extra null item in the array... + if (a != null) + { + //Only act if the overlap is not with another part of this tetromino. + if (a.gameObject.transform.parent != child.parent) + { + Debug.Log("Collision detected with " + a.gameObject.name); + wouldCollide = true; + } + } + } + } + } + //If we collided, undo the rotation. + if (wouldCollide == true) + { + activeTetromino.gameObject.transform.Rotate(0,0,degrees * -1,Space.World); + } + } }