gimm-platformer-project/Assets/Scripts/tetrominoUserControl.cs

124 lines
3.7 KiB
C#
Raw Normal View History

using UnityEngine;
using UnityEngine.SceneManagement;
public class tetrominoUserControl : MonoBehaviour
{
public GameObject activeTetromino;
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("tetrisMoveLeft") && activeTetromino != null)
{
if (!checkMoveCollisions(Vector2.left))
{
activeTetromino.gameObject.transform.position += Vector3.left * 1.0f;
GetComponent<AudioSource>().Play();
}
}
if (Input.GetButtonDown("tetrisMoveRight") && activeTetromino != null)
{
if (!checkMoveCollisions(Vector2.right))
{
activeTetromino.gameObject.transform.position += Vector3.right * 1.0f;
GetComponent<AudioSource>().Play();
}
}
2021-10-14 12:56:24 -06:00
if (Input.GetButtonDown("tetrisDrop") && activeTetromino != null)
{
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
}
if (Input.GetButtonDown("Quit"))
{
SceneManager.LoadScene("titleScreen");
}
}
public void setActiveTetromino(GameObject tetromino)
{
activeTetromino = tetromino;
}
public void unsetActiveTetromino()
{
activeTetromino = null;
}
public bool checkMoveCollisions(Vector2 direction)
{
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;
}
}
if (wouldCollide == true)
{
return true;
}
else
{
return false;
}
}
2021-10-28 17:08:26 -06:00
2021-10-31 13:43:04 -06:00
public 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.
if (wouldCollide == true)
{
activeTetromino.gameObject.transform.Rotate(0,0,degrees * -1,Space.World);
}
}
}