| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- internal static class GamingValues
- {
- // The chances of spawning number of fruits, has 5 int representing the chances of number from 1 to 5, the sum of all 5 chances must be 100
- internal struct FruitSpawnNumChanceRow
- {
- private int Rate_1, Rate_2, Rate_3, Rate_4; // Rate_5 is not necessary
- public FruitSpawnNumChanceRow(int rate_1, int rate_2, int rate_3, int rate_4, int rate_5)
- {
- if ((rate_1 + rate_2 + rate_3 + rate_4 + rate_5) != 100)
- {
- Debug.Log("Wrong fruit spawn rate");
- }
- Rate_1 = rate_1;
- Rate_2 = rate_2;
- Rate_3 = rate_3;
- Rate_4 = rate_4;
- }
- // Get a random number of fruit to spawn according to the chance rate
- public int GetRandomSpawnNum()
- {
- int rand = Random.Range(1, 101);
- if (rand <= Rate_1)
- {
- return 1;
- }
- else if (rand <= (Rate_1 + Rate_2))
- {
- return 2;
- }
- else if (rand <= (Rate_1 + Rate_2 + Rate_3))
- {
- return 3;
- }
- else if (rand <= (Rate_1 + Rate_2 + Rate_3 + Rate_4))
- {
- return 4;
- }
- else
- {
- return 5;
- }
- }
- }
- // For different score, chance rates of fruit count are different, elements in this dictionary represent chance rates of fruit number to spawn when scores is below the value of the key
- private static readonly Dictionary<int, FruitSpawnNumChanceRow> SpawnNumChanceRateTable = new Dictionary<int, FruitSpawnNumChanceRow>()
- {
- { 100, new FruitSpawnNumChanceRow(90, 10, 0, 0, 0) },
- { 200, new FruitSpawnNumChanceRow(90, 10, 0, 0, 0) },
- { 300, new FruitSpawnNumChanceRow(80, 15, 5, 0, 0) },
- { 400, new FruitSpawnNumChanceRow(70, 15, 10, 5, 0) },
- { 500, new FruitSpawnNumChanceRow(65, 15, 10, 5, 5) },
- { 600, new FruitSpawnNumChanceRow(60, 15, 10, 10, 5) },
- { 700, new FruitSpawnNumChanceRow(50, 20, 10, 10, 10) },
- { 800, new FruitSpawnNumChanceRow(45, 20, 15, 10, 10) },
- { 900, new FruitSpawnNumChanceRow(35, 25, 15, 10, 15) },
- { 1000, new FruitSpawnNumChanceRow(25, 25, 20, 15, 15) }
- };
- // When the score is below the key value, the probability of generating a bomb, the bomb will occupy a number of fruits
- private static readonly Dictionary<int, int> BombRateTable = new Dictionary<int, int>()
- {
- {100, 20},
- {200, 20},
- {300, 30},
- {400, 30},
- {500, 40},
- {600, 40},
- {700, 50},
- {800, 50},
- {900, 60},
- {1000, 60},
- };
- private const float BaseFruitLifetime = 10f;
- private static readonly Dictionary<int, float> FruitMoveSpeedMultiplierTable = new Dictionary<int, float>()
- {
- {100, 1.5f},
- {200, 2f},
- {300, 2.5f},
- {400, 3f},
- {500, 3.5f},
- {600, 4f},
- {700, 4.5f},
- {800, 5f},
- {900, 5.5f},
- {1000, 6f}
- };
- private static T GetTableValue<T>(int score, Dictionary<int, T> dic)
- {
- int upper_index = 0;
- for (int i = 0; i < SpawnNumChanceRateTable.Count; i++)
- {
- int upper_key = SpawnNumChanceRateTable.ElementAt(i).Key;
- upper_index = i;
- if (score > upper_key)
- {
- continue;
- }
- else
- {
- break;
- }
- }
- return dic.ElementAt(upper_index).Value;
- }
- public static float GetFruitLifetime(int wave, int score)
- {
- if (wave <= 5)
- {
- return BaseFruitLifetime;
- }
- else
- {
- float speed_multi = GetTableValue(score, FruitMoveSpeedMultiplierTable);
- return BaseFruitLifetime / speed_multi;
- }
- }
- // The max value when getting a random value of b coef in parabola euation y = ax^2 + b, which is used to describe the trajectory of a fruit
- private const float Max_BValueOfTrajectoryEquation = 0.3f;
- // The min value when getting a random value of b coef in parabola euation y = ax^2 + b, which is used to describe the trajectory of a fruit
- private const float Min_BValueOfTrajectoryEquation = 0.5f;
- // Section of score to allow spawning a ComboPomegranate
- private const int ComboPomegranate_Interval = 100;
- // How many ComboPomegranates have been spawnd
- private static int ComboPomegranate_SpawnedCount = 0;
- // The freezing time of a activated FreezingBanana
- public const float FreezeTime = 5f;
- // Interval of spawning next wave of fruits when all fruits in the field are cleared
- public const float MakeNewWaveCountdown = 3f;
- // Interval of spawning each fruit in a same wave
- public const float SpawnFruitInterval = 1.5f;
- // Lasting time of ComboPomegranate effect
- public const float ComboHitTime = 5.0f;
- // Max hit time of ComboPomegranate
- public const int ComboHitLimit = 5;
- // Initial life point
- public const int LifeCount = 3;
- // Get a random number of fruit to spawn according to the score
- public static int GetSpawnNum(int score)
- {
- FruitSpawnNumChanceRow row = GetTableValue(score, SpawnNumChanceRateTable);
- return row.GetRandomSpawnNum();
- }
- // Should it spawn a bomb, considering the bomb spawn chance rate according to the score
- public static bool DoesSpawnBomb(int score)
- {
- int rate = GetTableValue(score, BombRateTable);
- int rand = Random.Range(1, 101);
- return (rand < rate);
- }
- // Should it spawn a ComboPomegranate according to the score
- public static bool DoesSpawnComboPomegranate(int score)
- {
- int pomegranate_quota = score / ComboPomegranate_Interval + 1;
- if (ComboPomegranate_SpawnedCount < pomegranate_quota)
- {
- int rand = Random.Range(0, 100);
- if (rand < 50)
- {
- ComboPomegranate_SpawnedCount++;
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- // Reset the count of spawned ComboPomegranate to zero
- public static void ResetPomegranateSpawnedCount()
- {
- ComboPomegranate_SpawnedCount = 0;
- }
- // Output a random B coef and caculate the A and C coef of a parabola euqation paasing through the fruit spawning position and target position
- public static void CalcTrajectoryEquationCoef(Vector3 origin, Vector3 target, out float a, out float b, out float c)
- {
- b = Random.Range(Min_BValueOfTrajectoryEquation, Max_BValueOfTrajectoryEquation);
- float y0 = origin.y;
- float x0 = origin.z;
- float y1 = target.y;
- float x1 = target.z;
- a = (y1 - y0 - b * (x1 - x0)) / (x1 * x1 - x0 * x0);
- c = (y1 + y0 - a * (x1 * x1 + x0 * x0) - b * (x1 + x0)) / 2f;
- }
- }
|