Better algorithm for randomizing tetromino shapes.
This commit is contained in:
parent
5096e4ab35
commit
1c5a6a462a
1 changed files with 37 additions and 28 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
using System;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using Random = UnityEngine.Random;
|
using Random = UnityEngine.Random;
|
||||||
|
|
||||||
|
@ -15,38 +16,46 @@ public class tetrominoSpawnManager : MonoBehaviour
|
||||||
private int nextTetromino = 0;
|
private int nextTetromino = 0;
|
||||||
private GameObject nextTetrominoObject;
|
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()
|
public void spawnTetromino()
|
||||||
{
|
{
|
||||||
nextTetromino = Random.Range(1, 7);
|
Instantiate(nextTetrominoSequence[nextTetromino], spawnPoint, Quaternion.identity);
|
||||||
switch (nextTetromino)
|
|
||||||
|
nextTetromino++;
|
||||||
|
|
||||||
|
if (nextTetromino == 14)
|
||||||
{
|
{
|
||||||
case 0: //Square
|
nextTetromino = 0;
|
||||||
nextTetrominoObject = tetrominoSquare;
|
Shuffle(nextTetrominoSequence);
|
||||||
break;
|
|
||||||
case 1: //Line
|
|
||||||
nextTetrominoObject = tetrominoLine;
|
|
||||||
break;
|
|
||||||
case 2: //T-piece
|
|
||||||
nextTetrominoObject = tetrominoT;
|
|
||||||
break;
|
|
||||||
case 3: //L-piece
|
|
||||||
nextTetrominoObject = tetrominoL;
|
|
||||||
break;
|
|
||||||
case 4: //R-piece
|
|
||||||
nextTetrominoObject = tetrominoR;
|
|
||||||
break;
|
|
||||||
case 5: //S-piece
|
|
||||||
nextTetrominoObject = tetrominoS;
|
|
||||||
break;
|
|
||||||
case 6: //Z-piece
|
|
||||||
nextTetrominoObject = tetrominoZ;
|
|
||||||
break;
|
|
||||||
default: //if it's not a piece I have a prefab for yet, default to square
|
|
||||||
nextTetrominoObject = tetrominoSquare;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Instantiate(nextTetrominoObject, spawnPoint, Quaternion.identity);
|
}
|
||||||
|
|
||||||
|
//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]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue