-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClusterSpawnerController.cs
206 lines (164 loc) · 6.66 KB
/
ClusterSpawnerController.cs
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinSpawner_Controller : MonoBehaviour
{
public Game_Manager gm;
private GameObject level;
[Space(10)]
public List<Vector2> coinSpots = new List<Vector2>();
private int coinSpawnProbability;
private int coinTypeToSpawn;
private int percentChanceOf_1Coin;
private int percentChanceOf_3Coins;
private int percentChanceOf_10Coins;
public GameObject coinClusterPrefab;
private GameObject coinCluster;
[Space(10)]
public List<Vector2> powerUpSpots = new List<Vector2>();
private int powerUpSpawnProbability;
private int coinMagnet_Probability;
private int heart_Probability;
public GameObject coinMagnetPrefab;
private GameObject coinMagnet;
private bool coinMagnetActive = false;
public GameObject heartPrefab;
private GameObject heart;
// Use this for initialization
void Start()
{
Invoke("SpawnCoins", 0.2f);
Invoke("SpawnPowerUps", 0.2f);
level = this.transform.parent.gameObject;
}
public void TriggerCoinSpawns()
{
coinCluster.GetComponent<CoinCluster_Controller>().StartCoroutine("SpawnCoins");
}
void GetProbabilities()
{
coinMagnetActive = gm.coinMagentActive;
if (coinMagnetActive) // increase coin spawn probability & amount when Magnet is active
{
coinSpawnProbability = 100;
percentChanceOf_1Coin = 20;
percentChanceOf_3Coins = 30;
percentChanceOf_10Coins = 50;
}
if (!coinMagnetActive)
{
coinSpawnProbability = 80;
percentChanceOf_1Coin = 60;
percentChanceOf_3Coins = 25;
percentChanceOf_10Coins = 15;
// if the player has seen a 10 Coin Cluster within 8 levels, decrease probability
if ((gm.currentLevel - PlayerPrefs.GetInt("LLTS_10Coins")) < 8)
{
coinSpawnProbability = 80;
percentChanceOf_1Coin = 70;
percentChanceOf_3Coins = 25;
percentChanceOf_10Coins = 5;
}
}
if (percentChanceOf_1Coin + percentChanceOf_3Coins + percentChanceOf_10Coins != 100)
{
Debug.Log("Error: Percent chances of coins do no equal 100");
return;
}
}
public void SpawnCoins()
{
// Roll to see if Coin Cluster should spawn
var spawnRoll = UnityEngine.Random.Range(0, 101);
// Spawn Cluster
if (spawnRoll <= coinSpawnProbability)
{
// Roll to see which Coin Amount to spawn
var coinTypeRoll = UnityEngine.Random.Range(0, 101);
if (coinTypeRoll <= percentChanceOf_1Coin)
coinTypeToSpawn = 0;
if (coinTypeRoll > percentChanceOf_1Coin && coinTypeRoll <= percentChanceOf_1Coin + percentChanceOf_3Coins)
coinTypeToSpawn = 1;
if (coinTypeRoll > percentChanceOf_1Coin + percentChanceOf_3Coins)
{
coinTypeToSpawn = 2;
PlayerPrefs.SetInt("LLTS_10Coins", gm.currentLevel);
}
// Roll to chose Cluster Position
var positionRoll = UnityEngine.Random.Range(0, coinSpots.Count);
coinCluster = Instantiate(coinClusterPrefab, level.transform);
coinCluster.GetComponent<CoinCluster_Controller>().coinSpawnPos = coinSpots[positionRoll];
if (coinTypeToSpawn == 0)
coinCluster.GetComponent<CoinCluster_Controller>().coinsToSpawn = 1;
if (coinTypeToSpawn == 1)
coinCluster.GetComponent<CoinCluster_Controller>().coinsToSpawn = 3;
if (coinTypeToSpawn == 2)
coinCluster.GetComponent<CoinCluster_Controller>().coinsToSpawn = 10;
coinCluster.GetComponent<CoinCluster_Controller>().StartCoroutine("SpawnCoins");
}
}
public void SpawnPowerUps()
{
// Weigh Power Up spawn probability based on the Last Level To See a Power Up
powerUpSpawnProbability = Mathf.Clamp((gm.currentLevel - PlayerPrefs.GetInt("LLTS_PowerUp")) * 2, 0, 50);
if (gm.canSpawnHearts)
{
coinMagnet_Probability = 50;
heart_Probability = 50;
if (gm.coinMagentActive)
{
coinMagnet_Probability = 0;
heart_Probability = 100;
}
}
if (!gm.canSpawnHearts)
{
coinMagnet_Probability = 100;
heart_Probability = 0;
if (gm.coinMagentActive)
{
coinMagnet_Probability = 0;
heart_Probability = 0;
}
}
// Roll to see if a Power Up should spawn
var powerUpSpawnRoll = UnityEngine.Random.Range(0, 101);
if (powerUpSpawnRoll <= powerUpSpawnProbability)
{
PlayerPrefs.SetInt("LLTS_PowerUp", gm.currentLevel);
var positionRoll = UnityEngine.Random.Range(0, powerUpSpots.Count);
var powerUpTypeRoll = UnityEngine.Random.Range(0, 101);
if (powerUpTypeRoll <= coinMagnet_Probability)
{
coinMagnet = Instantiate(powerUp_CoinMagnet, level.transform);
if (level.transform.localEulerAngles.y == 180)
coinMagnet.transform.localEulerAngles = new Vector3(0, 180, 0);
coinMagnet.transform.localPosition = powerUpSpots[positionRoll];
}
if (heart_Probability != 0 && powerUpTypeRoll > heart_Probability)
{
heart = Instantiate(powerUp_Heart, level.transform);
if (level.transform.localEulerAngles.y == 180)
heart.transform.localEulerAngles = new Vector3(0, 180, 0);
heart.transform.localPosition = powerUpSpots[positionRoll];
}
}
}
// Draw debug Gizmos
private void OnDrawGizmos()
{
// Show Coin Spawn Locations
Gizmos.color = Color.yellow;
Vector2 spawnerPos = new Vector2(this.transform.position.x, this.transform.position.y);
for (int i = 0; i < coinSpots.Count; i++)
{
Gizmos.DrawWireSphere(coinSpots[i] + spawnerPos, 0.8f);
}
// Show Power Up Spawn Locations
Gizmos.color = Color.red;
for (int i = 0; i < powerUpSpots.Count; i++)
{
Gizmos.DrawWireSphere(powerUpSpots[i] + spawnerPos, 0.3f);
}
}
}