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

62 lines
1.6 KiB
C#
Raw Normal View History

using System;
2021-10-01 15:30:01 -06:00
using UnityEngine;
2021-10-07 13:56:22 -06:00
using Random = UnityEngine.Random;
2021-10-01 15:30:01 -06:00
public class tetrominoSpawnManager : MonoBehaviour
2021-10-01 15:30:01 -06:00
{
public Vector3 spawnPoint;
public GameObject tetrominoSquare;
2021-10-07 13:56:22 -06:00
public GameObject tetrominoLine;
public GameObject tetrominoL;
public GameObject tetrominoR;
public GameObject tetrominoS;
public GameObject tetrominoT;
2021-10-14 12:56:24 -06:00
public GameObject tetrominoZ;
2021-10-07 13:56:22 -06:00
private int nextTetromino = 0;
2021-10-21 12:14:34 -06:00
private GameObject nextTetrominoObject;
private GameObject[] nextTetrominoSequence = new GameObject[14];
public void Start()
{
nextTetrominoSequence = new GameObject[14]
{
tetrominoL, tetrominoL,
tetrominoLine, tetrominoLine,
tetrominoR, tetrominoR,
tetrominoS, tetrominoS,
tetrominoSquare, tetrominoSquare,
tetrominoT, tetrominoT,
tetrominoZ, tetrominoZ
};
Shuffle(nextTetrominoSequence);
}
public void spawnTetromino()
2021-10-01 15:30:01 -06:00
{
Instantiate(nextTetrominoSequence[nextTetromino], spawnPoint, Quaternion.identity);
nextTetromino++;
if (nextTetromino == 14)
2021-10-07 13:56:22 -06:00
{
nextTetromino = 0;
Shuffle(nextTetrominoSequence);
2021-10-07 13:56:22 -06:00
}
2021-10-21 12:14:34 -06:00
}
//an algorithm for shuffling arrays, taken from https://stackoverflow.com/questions/36702548/shuffle-array-in-unity
void Shuffle(GameObject[] array)
{
int p = array.Length;
for (int n = p - 1; n > 0; n--)
{
int r = Random.Range(0, n);
(array[r], array[n]) = (array[n], array[r]);
}
2021-10-01 15:30:01 -06:00
}
}