2021-10-01 15:30:01 -06:00
|
|
|
using UnityEngine;
|
2021-10-21 13:18:47 -06:00
|
|
|
using UnityEngine.SceneManagement;
|
2021-10-31 13:03:45 -06:00
|
|
|
using Random = UnityEngine.Random;
|
2021-10-01 15:30:01 -06:00
|
|
|
|
2022-10-07 14:54:24 -06:00
|
|
|
public class Tetromino : MonoBehaviour
|
2021-10-01 15:30:01 -06:00
|
|
|
{
|
2021-10-07 13:56:22 -06:00
|
|
|
public float timeBetweenSteps;
|
2021-10-01 15:30:01 -06:00
|
|
|
public bool active = true;
|
2021-10-31 13:03:45 -06:00
|
|
|
|
2022-10-07 14:54:24 -06:00
|
|
|
private TetrominoUserControl tetrominoUserControl;
|
|
|
|
private TetrominoSpawnManager tetrominoSpawnManager;
|
2021-10-07 13:56:22 -06:00
|
|
|
|
|
|
|
private float stepTimer = 0.0f;
|
|
|
|
private bool wouldCollide = false;
|
2021-10-31 13:03:45 -06:00
|
|
|
|
|
|
|
private Color[] tetrominoColors = new Color[7]
|
|
|
|
{
|
|
|
|
new Color(0.05f,0.04f,0.36f,1),
|
|
|
|
new Color(0.54f,0.31f,0.04f,1),
|
|
|
|
new Color(0.54f,0.20f,0.04f,1),
|
|
|
|
new Color(0,0.30f,0.04f,1),
|
|
|
|
new Color(0.02f,0.27f,0.34f,1),
|
|
|
|
new Color(0.28f,0.02f,0.36f,1),
|
|
|
|
new Color(0.53f,0,0.04f,1)
|
|
|
|
};
|
|
|
|
|
|
|
|
private Color[] overlayColors = new Color[7]
|
|
|
|
{
|
|
|
|
new Color(0.31f,0.5f,0.66f,1),
|
|
|
|
new Color(0.82f,0.66f,0.19f,1),
|
|
|
|
new Color(0.82f,0.42f,0.19f,1),
|
|
|
|
new Color(0.55f,0.76f,0.18f,1),
|
|
|
|
new Color(0.24f,0.49f,0.57f,1),
|
|
|
|
new Color(0.44f,0.14f,0.54f,1),
|
|
|
|
new Color(0.79f,0.20f,0.23f,1)
|
|
|
|
};
|
2021-10-01 15:30:01 -06:00
|
|
|
|
|
|
|
// Start is called before the first frame update
|
|
|
|
void Start()
|
|
|
|
{
|
2022-10-07 14:54:24 -06:00
|
|
|
tetrominoSpawnManager = FindObjectOfType<TetrominoSpawnManager>();
|
|
|
|
tetrominoUserControl = FindObjectOfType<TetrominoUserControl>();
|
|
|
|
tetrominoUserControl.setActiveTetromino(gameObject);
|
2021-10-31 13:03:45 -06:00
|
|
|
|
|
|
|
//set tetromino color to a random pairing
|
|
|
|
int newColor = Random.Range(1, 7);
|
|
|
|
|
|
|
|
foreach (Transform child in transform)
|
|
|
|
{
|
|
|
|
//First the tetromino base color
|
|
|
|
child.GetComponent<SpriteRenderer>().color = tetrominoColors[newColor];
|
|
|
|
//then the overlay
|
|
|
|
child.GetChild(0).GetComponent<SpriteRenderer>().color = overlayColors[newColor];
|
|
|
|
}
|
2021-10-01 15:30:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
2021-10-07 13:56:22 -06:00
|
|
|
stepTimer += Time.deltaTime;
|
2021-10-01 15:30:01 -06:00
|
|
|
|
2021-10-07 13:56:22 -06:00
|
|
|
if (stepTimer >= timeBetweenSteps && active)
|
2021-10-01 15:30:01 -06:00
|
|
|
{
|
2021-10-07 13:56:22 -06:00
|
|
|
stepTimer = stepTimer - timeBetweenSteps;
|
2021-10-01 15:30:01 -06:00
|
|
|
nextStep();
|
|
|
|
}
|
2021-10-07 13:56:22 -06:00
|
|
|
|
2021-10-01 15:30:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void nextStep()
|
|
|
|
{
|
2021-10-07 13:56:22 -06:00
|
|
|
//I want to run this for each piece of the tetromino.
|
|
|
|
foreach (Transform child in transform)
|
|
|
|
{
|
2021-10-31 13:43:04 -06:00
|
|
|
if (checkFallCollision(child))
|
|
|
|
{
|
2021-10-07 13:56:22 -06:00
|
|
|
wouldCollide = true;
|
|
|
|
break;
|
2021-10-31 13:43:04 -06:00
|
|
|
}
|
2021-10-07 13:56:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (wouldCollide == false)
|
2021-10-01 15:30:01 -06:00
|
|
|
{
|
|
|
|
Fall();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-18 02:17:36 -06:00
|
|
|
GetComponent<AudioSource>().Play();
|
2021-10-01 15:30:01 -06:00
|
|
|
active = false;
|
2022-10-07 14:54:24 -06:00
|
|
|
tetrominoUserControl.unsetActiveTetromino();
|
|
|
|
tetrominoSpawnManager.spawnTetromino();
|
2021-10-01 15:30:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-31 13:43:04 -06:00
|
|
|
private bool checkFallCollision(Transform pieceToCheck) //returns true if the next step collides with something
|
2021-10-01 15:30:01 -06:00
|
|
|
{
|
2021-10-07 13:56:22 -06:00
|
|
|
//Cast a ray to the next position of the tetromino.
|
2021-10-08 20:58:19 -06:00
|
|
|
//NOTE: disabled "Queries Start in Colliders" in Project Settings>Physics 2D (thank you again, Tavi).
|
2021-10-07 13:56:22 -06:00
|
|
|
RaycastHit2D collision = Physics2D.Raycast(pieceToCheck.position, Vector2.down, 1.0f);
|
2021-10-31 13:43:04 -06:00
|
|
|
|
|
|
|
//Only go further if the ray collided with something, and if that something is not another
|
|
|
|
//piece of the same tetromino.
|
2021-10-21 10:51:06 -06:00
|
|
|
if (collision.collider != null && collision.collider.transform.parent != pieceToCheck.parent)
|
2021-10-01 15:30:01 -06:00
|
|
|
{
|
2021-10-07 13:56:22 -06:00
|
|
|
Debug.Log("Collided with object: " + collision.collider.name);
|
2021-10-21 12:05:56 -06:00
|
|
|
//We hit something, but we only want to collide with the ground or other tetrominoes.
|
2021-10-01 15:30:01 -06:00
|
|
|
if (collision.collider.CompareTag("Ground") || collision.collider.CompareTag("Tetromino"))
|
|
|
|
{
|
2021-10-07 13:56:22 -06:00
|
|
|
ScoreCounter.scoreAmount += 100;
|
|
|
|
return true;
|
2021-10-01 15:30:01 -06:00
|
|
|
}
|
2021-10-21 13:18:47 -06:00
|
|
|
//We do also want to check if we're crushing the player.
|
|
|
|
if (collision.collider.CompareTag("Player"))
|
|
|
|
{
|
|
|
|
SceneManager.LoadScene("loseScreen");
|
|
|
|
}
|
2021-10-01 15:30:01 -06:00
|
|
|
}
|
2021-10-08 20:58:19 -06:00
|
|
|
|
|
|
|
return false;
|
2021-10-01 15:30:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fall()
|
|
|
|
{
|
|
|
|
transform.position += Vector3.down * 1.0f;
|
|
|
|
}
|
2021-10-21 13:18:47 -06:00
|
|
|
|
2021-10-01 15:30:01 -06:00
|
|
|
}
|