GamingValues.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. internal static class GamingValues
  6. {
  7. // 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
  8. internal struct FruitSpawnNumChanceRow
  9. {
  10. private int Rate_1, Rate_2, Rate_3, Rate_4; // Rate_5 is not necessary
  11. public FruitSpawnNumChanceRow(int rate_1, int rate_2, int rate_3, int rate_4, int rate_5)
  12. {
  13. if ((rate_1 + rate_2 + rate_3 + rate_4 + rate_5) != 100)
  14. {
  15. Debug.Log("Wrong fruit spawn rate");
  16. }
  17. Rate_1 = rate_1;
  18. Rate_2 = rate_2;
  19. Rate_3 = rate_3;
  20. Rate_4 = rate_4;
  21. }
  22. // Get a random number of fruit to spawn according to the chance rate
  23. public int GetRandomSpawnNum()
  24. {
  25. int rand = Random.Range(1, 101);
  26. if (rand <= Rate_1)
  27. {
  28. return 1;
  29. }
  30. else if (rand <= (Rate_1 + Rate_2))
  31. {
  32. return 2;
  33. }
  34. else if (rand <= (Rate_1 + Rate_2 + Rate_3))
  35. {
  36. return 3;
  37. }
  38. else if (rand <= (Rate_1 + Rate_2 + Rate_3 + Rate_4))
  39. {
  40. return 4;
  41. }
  42. else
  43. {
  44. return 5;
  45. }
  46. }
  47. }
  48. // 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
  49. private static readonly Dictionary<int, FruitSpawnNumChanceRow> SpawnNumChanceRateTable = new Dictionary<int, FruitSpawnNumChanceRow>()
  50. {
  51. { 100, new FruitSpawnNumChanceRow(90, 10, 0, 0, 0) },
  52. { 200, new FruitSpawnNumChanceRow(90, 10, 0, 0, 0) },
  53. { 300, new FruitSpawnNumChanceRow(80, 15, 5, 0, 0) },
  54. { 400, new FruitSpawnNumChanceRow(70, 15, 10, 5, 0) },
  55. { 500, new FruitSpawnNumChanceRow(65, 15, 10, 5, 5) },
  56. { 600, new FruitSpawnNumChanceRow(60, 15, 10, 10, 5) },
  57. { 700, new FruitSpawnNumChanceRow(50, 20, 10, 10, 10) },
  58. { 800, new FruitSpawnNumChanceRow(45, 20, 15, 10, 10) },
  59. { 900, new FruitSpawnNumChanceRow(35, 25, 15, 10, 15) },
  60. { 1000, new FruitSpawnNumChanceRow(25, 25, 20, 15, 15) }
  61. };
  62. // When the score is below the key value, the probability of generating a bomb, the bomb will occupy a number of fruits
  63. private static readonly Dictionary<int, int> BombRateTable = new Dictionary<int, int>()
  64. {
  65. {100, 20},
  66. {200, 20},
  67. {300, 30},
  68. {400, 30},
  69. {500, 40},
  70. {600, 40},
  71. {700, 50},
  72. {800, 50},
  73. {900, 60},
  74. {1000, 60},
  75. };
  76. private const float BaseFruitLifetime = 10f;
  77. private static readonly Dictionary<int, float> FruitMoveSpeedMultiplierTable = new Dictionary<int, float>()
  78. {
  79. {100, 1.5f},
  80. {200, 2f},
  81. {300, 2.5f},
  82. {400, 3f},
  83. {500, 3.5f},
  84. {600, 4f},
  85. {700, 4.5f},
  86. {800, 5f},
  87. {900, 5.5f},
  88. {1000, 6f}
  89. };
  90. private static T GetTableValue<T>(int score, Dictionary<int, T> dic)
  91. {
  92. int upper_index = 0;
  93. for (int i = 0; i < SpawnNumChanceRateTable.Count; i++)
  94. {
  95. int upper_key = SpawnNumChanceRateTable.ElementAt(i).Key;
  96. upper_index = i;
  97. if (score > upper_key)
  98. {
  99. continue;
  100. }
  101. else
  102. {
  103. break;
  104. }
  105. }
  106. return dic.ElementAt(upper_index).Value;
  107. }
  108. public static float GetFruitLifetime(int wave, int score)
  109. {
  110. if (wave <= 5)
  111. {
  112. return BaseFruitLifetime;
  113. }
  114. else
  115. {
  116. float speed_multi = GetTableValue(score, FruitMoveSpeedMultiplierTable);
  117. return BaseFruitLifetime / speed_multi;
  118. }
  119. }
  120. // 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
  121. private const float Max_BValueOfTrajectoryEquation = 0.3f;
  122. // 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
  123. private const float Min_BValueOfTrajectoryEquation = 0.5f;
  124. // Section of score to allow spawning a ComboPomegranate
  125. private const int ComboPomegranate_Interval = 100;
  126. // How many ComboPomegranates have been spawnd
  127. private static int ComboPomegranate_SpawnedCount = 0;
  128. // The freezing time of a activated FreezingBanana
  129. public const float FreezeTime = 5f;
  130. // Interval of spawning next wave of fruits when all fruits in the field are cleared
  131. public const float MakeNewWaveCountdown = 3f;
  132. // Interval of spawning each fruit in a same wave
  133. public const float SpawnFruitInterval = 1.5f;
  134. // Lasting time of ComboPomegranate effect
  135. public const float ComboHitTime = 5.0f;
  136. // Max hit time of ComboPomegranate
  137. public const int ComboHitLimit = 5;
  138. // Initial life point
  139. public const int LifeCount = 3;
  140. // Get a random number of fruit to spawn according to the score
  141. public static int GetSpawnNum(int score)
  142. {
  143. FruitSpawnNumChanceRow row = GetTableValue(score, SpawnNumChanceRateTable);
  144. return row.GetRandomSpawnNum();
  145. }
  146. // Should it spawn a bomb, considering the bomb spawn chance rate according to the score
  147. public static bool DoesSpawnBomb(int score)
  148. {
  149. int rate = GetTableValue(score, BombRateTable);
  150. int rand = Random.Range(1, 101);
  151. return (rand < rate);
  152. }
  153. // Should it spawn a ComboPomegranate according to the score
  154. public static bool DoesSpawnComboPomegranate(int score)
  155. {
  156. int pomegranate_quota = score / ComboPomegranate_Interval + 1;
  157. if (ComboPomegranate_SpawnedCount < pomegranate_quota)
  158. {
  159. int rand = Random.Range(0, 100);
  160. if (rand < 50)
  161. {
  162. ComboPomegranate_SpawnedCount++;
  163. return true;
  164. }
  165. else
  166. {
  167. return false;
  168. }
  169. }
  170. else
  171. {
  172. return false;
  173. }
  174. }
  175. // Reset the count of spawned ComboPomegranate to zero
  176. public static void ResetPomegranateSpawnedCount()
  177. {
  178. ComboPomegranate_SpawnedCount = 0;
  179. }
  180. // 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
  181. public static void CalcTrajectoryEquationCoef(Vector3 origin, Vector3 target, out float a, out float b, out float c)
  182. {
  183. b = Random.Range(Min_BValueOfTrajectoryEquation, Max_BValueOfTrajectoryEquation);
  184. float y0 = origin.y;
  185. float x0 = origin.z;
  186. float y1 = target.y;
  187. float x1 = target.z;
  188. a = (y1 - y0 - b * (x1 - x0)) / (x1 * x1 - x0 * x0);
  189. c = (y1 + y0 - a * (x1 * x1 + x0 * x0) - b * (x1 + x0)) / 2f;
  190. }
  191. }