Performance improvements
This commit is contained in:
@@ -5,13 +5,33 @@ using Random = UnityEngine.Random;
|
||||
|
||||
namespace Utils {
|
||||
public static class RandomUtils {
|
||||
public static int RandomGemTypeAsInt() {
|
||||
GemType[] spawnableGems = Enum.GetValues(typeof(GemType))
|
||||
.Cast<GemType>()
|
||||
.Where(gType => gType != GemType.Bomb)
|
||||
.ToArray();
|
||||
|
||||
return Random.Range(0, spawnableGems.Length);
|
||||
private static readonly GemType[] spawnableGems = BuildSpawnableGems();
|
||||
|
||||
private static GemType[] BuildSpawnableGems() {
|
||||
Array values = Enum.GetValues(typeof(GemType));
|
||||
int count = 0;
|
||||
|
||||
for (int i = 0; i < values.Length; i++) {
|
||||
if ((GemType)values.GetValue(i) != GemType.Bomb)
|
||||
count++;
|
||||
}
|
||||
|
||||
GemType[] result = new GemType[count];
|
||||
int write = 0;
|
||||
|
||||
for (int i = 0; i < values.Length; i++) {
|
||||
GemType t = (GemType)values.GetValue(i);
|
||||
if (t == GemType.Bomb)
|
||||
continue;
|
||||
|
||||
result[write++] = t;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static GemType RandomGemType() {
|
||||
return spawnableGems[Random.Range(0, spawnableGems.Length)];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user