using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tetromino : MonoBehaviour
{
float fall = 0;
public float fallsSpeed = 1;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
CheckUserInput();
}
void CheckUserInput()
{
if (Input.GetKey(KeyCode.RightArrow))
{
transform.position += new Vector3(1, 0, 0);
if (!CheckIsValidPosition())
{
transform.position += new Vector3(-1, 0, 0);
}
}
else if (Input.GetKey(KeyCode.LeftArrow))
{
transform.position += new Vector3(-1, 0, 0);
if (!CheckIsValidPosition())
{
transform.position += new Vector3(1, 0, 0);
}
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
// Try rotating
transform.Rotate(0, 0, 90);
// Check if the new position is valid
if (!CheckIsValidPosition())
{
// If not, rotate back
transform.Rotate(0, 0, -90);
}
}
else if (Input.GetKeyDown(KeyCode.DownArrow) || Mathf.Round(Time.time) - fall >= fallsSpeed)
{
transform.position += new Vector3(0, -1, 0);
if (!CheckIsValidPosition())
{
transform.position += new Vector3(0, 1, 0);
}
fall = Mathf.Round(Time.time);
}
}
bool CheckIsValidPosition()
{
foreach (Transform mino in transform)
{
Vector2 pos = FindObjectOfType<Game>().Round(mino.position);
if (!FindObjectOfType<Game>().CheckInsideGrid(pos))
{
return false;
}
}
return true;
}
}