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

97 lines
2.9 KiB
C#
Raw Normal View History

2021-10-07 13:56:22 -06:00
using System;
2021-10-01 15:30:01 -06:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2021-10-07 13:56:22 -06:00
using UnityEngine.EventSystems;
2021-10-21 13:18:47 -06:00
using UnityEngine.SceneManagement;
2021-10-01 15:30:01 -06:00
public class tetromino : MonoBehaviour
{
2021-10-07 13:56:22 -06:00
public float timeBetweenSteps;
2021-10-01 15:30:01 -06:00
public bool active = true;
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-01 15:30:01 -06:00
// Start is called before the first frame update
void Start()
{
TetrominoSpawnManager = FindObjectOfType<tetrominoSpawnManager>();
TetrominoUserControl = FindObjectOfType<tetrominoUserControl>();
TetrominoUserControl.setActiveTetromino(gameObject);
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-21 10:51:06 -06:00
2021-10-07 13:56:22 -06:00
if (validateFall(child))
{
wouldCollide = true;
break;
}
2021-10-21 10:51:06 -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;
TetrominoUserControl.unsetActiveTetromino();
TetrominoSpawnManager.spawnTetromino();
2021-10-01 15:30:01 -06:00
}
}
2021-10-07 13:56:22 -06:00
private bool validateFall(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.
//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);
// Debug.DrawRay(pieceToCheck.position,Vector3.down,Color.red,1000.0f);
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
}
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
}