2021-10-10 21:42:55 -06:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
public class tetrominoUserControl : MonoBehaviour
|
|
|
|
{
|
|
|
|
public GameObject activeTetromino;
|
|
|
|
|
|
|
|
// Start is called before the first frame update
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
activeTetromino.gameObject.transform.Rotate(0,0,90,Space.World);
|
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)
|
|
|
|
{
|
|
|
|
activeTetromino.gameObject.transform.Rotate(0,0,-90,Space.World);
|
2021-10-18 02:17:36 -06:00
|
|
|
GetComponent<AudioSource>().Play();
|
2021-10-14 12:56:24 -06:00
|
|
|
}
|
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
|
|
|
|
|
|
|
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-10 21:42:55 -06:00
|
|
|
}
|