anF
Tetris Block Phasing through grid
using UnityEngine;
public class Tetromino : MonoBehaviour
{
float fall = 0;
public float fallSpeed = 1;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
CheckUserInput();
}
void CheckUserInput()
{
if (Input.GetKeyDown(KeyCode.RightArrow))
{
SafeMove(new Vector3(1, 0, 0));
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
SafeMove(new Vector3(-1, 0, 0));
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
SafeRotate(90);
}
else if (Input.GetKeyDown(KeyCode.DownArrow) || Time.time - fall >= fallSpeed)
{
SafeMove(new Vector3(0, -1, 0));
fall = Time.time;
}
}
void SafeMove(Vector3 vec)
{
transform.position += vec;
if (!CheckIsValidPosition())
{
transform.position -= vec;
}
}
void SafeRotate(float degree)
{
transform.Rotate(0, 0, degree);
if (!CheckIsValidPosition())
{
transform.Rotate(0, 0, -degree);
}
}
bool CheckIsValidPosition()
{
foreach (Transform mino in transform) { Vector2 pos = FindObjectOfType<Game>().Round(mino.position); if (!FindObjectOfType<Game>().CheckInsideGrid(pos)) { return false; } } return true; } }
foreach (Transform mino in transform) { Vector2 pos = FindObjectOfType<Game>().Round(mino.position); if (!FindObjectOfType<Game>().CheckInsideGrid(pos)) { return false; } } return true; } }
29 replies