2021-10-10 21:42:55 -06:00
|
|
|
using UnityEngine;
|
2021-10-21 13:25:33 -06:00
|
|
|
using UnityEngine.SceneManagement;
|
2021-10-10 21:42:55 -06:00
|
|
|
|
|
|
|
public class tetrominoUserControl : MonoBehaviour
|
|
|
|
{
|
|
|
|
public GameObject activeTetromino;
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
if (Input.GetButtonDown("tetrisMoveLeft") && activeTetromino != null)
|
|
|
|
{
|
2021-10-21 11:06:46 -06:00
|
|
|
if (!checkMoveCollisions(Vector2.left))
|
|
|
|
{
|
|
|
|
activeTetromino.gameObject.transform.position += Vector3.left * 1.0f;
|
|
|
|
GetComponent<AudioSource>().Play();
|
|
|
|
}
|
2021-10-10 21:42:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Input.GetButtonDown("tetrisMoveRight") && activeTetromino != null)
|
|
|
|
{
|
2021-10-21 11:06:46 -06:00
|
|
|
if (!checkMoveCollisions(Vector2.right))
|
|
|
|
{
|
|
|
|
activeTetromino.gameObject.transform.position += Vector3.right * 1.0f;
|
|
|
|
GetComponent<AudioSource>().Play();
|
|
|
|
}
|
2021-10-10 21:42:55 -06:00
|
|
|
}
|
2021-10-14 12:56:24 -06:00
|
|
|
|
|
|
|
if (Input.GetButtonDown("tetrisDrop") && activeTetromino != null)
|
|
|
|
{
|
2021-10-21 11:06:46 -06:00
|
|
|
if (!checkMoveCollisions(Vector2.down))
|
|
|
|
{
|
|
|
|
activeTetromino.gameObject.transform.position += Vector3.down * 1.0f;
|
|
|
|
GetComponent<AudioSource>().Play();
|
|
|
|
}
|
2021-10-14 12:56:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Input.GetButtonDown("tetrisRotateLeft") && activeTetromino != null)
|
|
|
|
{
|
2021-10-31 13:43:04 -06:00
|
|
|
checkRotateCollisions(90);
|
2021-10-18 02:17:36 -06:00
|
|
|
GetComponent<AudioSource>().Play();
|
2021-10-14 12:56:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Input.GetButtonDown("tetrisRotateRight") && activeTetromino != null)
|
|
|
|
{
|
2021-10-31 13:43:04 -06:00
|
|
|
checkRotateCollisions(90);
|
2021-10-18 02:17:36 -06:00
|
|
|
GetComponent<AudioSource>().Play();
|
2021-10-14 12:56:24 -06:00
|
|
|
}
|
2021-10-21 13:25:33 -06:00
|
|
|
|
|
|
|
if (Input.GetButtonDown("Quit"))
|
|
|
|
{
|
|
|
|
SceneManager.LoadScene("titleScreen");
|
|
|
|
}
|
2021-10-10 21:42:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setActiveTetromino(GameObject tetromino)
|
|
|
|
{
|
|
|
|
activeTetromino = tetromino;
|
|
|
|
}
|
2021-10-14 13:46:33 -06:00
|
|
|
|
|
|
|
public void unsetActiveTetromino()
|
|
|
|
{
|
|
|
|
activeTetromino = null;
|
|
|
|
}
|
2021-10-21 11:06:46 -06:00
|
|
|
|
2022-10-07 13:18:54 -06:00
|
|
|
private bool checkMoveCollisions(Vector2 direction)
|
2021-10-21 11:06:46 -06:00
|
|
|
{
|
|
|
|
bool wouldCollide = false;
|
|
|
|
|
|
|
|
foreach (Transform child in activeTetromino.transform)
|
|
|
|
{
|
|
|
|
RaycastHit2D collision = Physics2D.Raycast(child.position, direction, 1.0f);
|
|
|
|
if (collision.collider != null && collision.collider.transform.parent != child.parent)
|
|
|
|
{
|
|
|
|
wouldCollide = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-10-07 13:18:54 -06:00
|
|
|
if (wouldCollide)
|
2021-10-21 11:06:46 -06:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-10-28 17:08:26 -06:00
|
|
|
|
2022-10-07 13:18:54 -06:00
|
|
|
private void checkRotateCollisions(int degrees)
|
2021-10-28 17:08:26 -06:00
|
|
|
{
|
|
|
|
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<BoxCollider2D>().OverlapCollider(new ContactFilter2D().NoFilter(), results);
|
|
|
|
//If there are collisions...
|
2021-11-01 23:29:54 -06:00
|
|
|
foreach (Collider2D a in results)
|
2021-10-28 17:08:26 -06:00
|
|
|
{
|
2021-11-01 23:29:54 -06:00
|
|
|
//and IF they aren't an extra null item in the array...
|
|
|
|
if (a != null)
|
2021-10-28 17:08:26 -06:00
|
|
|
{
|
2021-11-01 23:29:54 -06:00
|
|
|
//Only act if the overlap is not with another part of this tetromino.
|
|
|
|
if (a.gameObject.transform.parent != child.parent)
|
2021-10-28 17:08:26 -06:00
|
|
|
{
|
2021-11-01 23:29:54 -06:00
|
|
|
Debug.Log("Collision detected with " + a.gameObject.name);
|
|
|
|
wouldCollide = true;
|
2021-10-28 17:08:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//If we collided, undo the rotation.
|
2022-10-07 13:18:54 -06:00
|
|
|
if (wouldCollide)
|
2021-10-28 17:08:26 -06:00
|
|
|
{
|
|
|
|
activeTetromino.gameObject.transform.Rotate(0,0,degrees * -1,Space.World);
|
|
|
|
}
|
|
|
|
}
|
2021-10-10 21:42:55 -06:00
|
|
|
}
|