ColumnPool.cs
2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using UnityEngine;
using System.Collections;
public class ColumnPool : MonoBehaviour
{
public GameObject columnPrefab; //The column game object.
public int columnPoolSize = 5; //How many columns to keep on standby.
public float spawnRate = 3f; //How quickly columns spawn.
public float columnMin = -1f; //Minimum y value of the column position.
public float columnMax = 3.5f; //Maximum y value of the column position.
private GameObject[] columns; //Collection of pooled columns.
private int currentColumn = 0; //Index of the current column in the collection.
private Vector2 objectPoolPosition = new Vector2 (-15,-25); //A holding position for our unused columns offscreen.
private float spawnXPosition = 10f;
private float timeSinceLastSpawned;
void Start()
{
timeSinceLastSpawned = 0f;
//Initialize the columns collection.
columns = new GameObject[columnPoolSize];
//Loop through the collection...
for(int i = 0; i < columnPoolSize; i++)
{
//...and create the individual columns.
columns[i] = (GameObject)Instantiate(columnPrefab, objectPoolPosition, Quaternion.identity);
}
}
//This spawns columns as long as the game is not over.
void Update()
{
timeSinceLastSpawned += Time.deltaTime;
if (GameControl.Instance.gameOver == false && timeSinceLastSpawned >= spawnRate)
{
timeSinceLastSpawned = 0f;
//Set a random y position for the column
float spawnYPosition = Random.Range(columnMin, columnMax);
//...then set the current column to that position.
columns[currentColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);
//Increase the value of currentColumn. If the new size is too big, set it back to zero
currentColumn ++;
if (currentColumn >= columnPoolSize)
{
currentColumn = 0;
}
}
}
}