CoinCreation.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5. using JetBrains.Annotations;
  6. using Pools;
  7. using Roads;
  8. //Kreuzungen und Slalom-Referenzpunkte für die Route
  9. [Serializable]
  10. public struct RoutePoint
  11. {
  12. //Point position
  13. public string name;
  14. public float x;
  15. public float z;
  16. public bool slalom;
  17. public int stepsizes;
  18. }
  19. public class CoinCreation : MonoBehaviour
  20. {
  21. // Parameters to determine stepsize.
  22. float route_lenght;
  23. [SerializeField] public int Coin_Number;
  24. [SerializeField] public float Distance_Cross; // Test for cross coin drawing
  25. [SerializeField] public float Distance_Slalom;
  26. //[SerializeField] public bool Draw_Cross; // Maybe draw on Line
  27. public float stepsize; // public float stepsize = 10; //TODO welcher Wert ist hier passend?
  28. List<float> differeces = new List<float>();
  29. public int counter = 0; // How many Coins were drawn on the road
  30. public CoinPool coinPool;
  31. public List<RoutePoint> points = new List<RoutePoint>();
  32. int pointIndex = 0;
  33. private Vector3 coin_position;
  34. private GameObject coin;
  35. RoutePoint start_point;
  36. RoutePoint end_point;
  37. private void Start()
  38. {
  39. CalcRoute();
  40. PlaceCoins();
  41. PlaceCoins();
  42. }
  43. int points_size = 30; //needs to be changed if Route changes!
  44. // Determine route length
  45. public void CalcRoute()
  46. {
  47. route_lenght = 0;
  48. // Compute the whole route length.
  49. for (int idx = 0; idx < points.Count - 1; ++idx)
  50. {
  51. float diff_x = System.Math.Abs(points[idx].x - points[idx + 1].x);
  52. float diff_z = System.Math.Abs(points[idx].z - points[idx + 1].z);
  53. // If we calculate route length in slalom part -> use Pythagoras to
  54. // calculate Hypothenuse.
  55. if (points[idx].slalom && points[idx + 1].slalom)
  56. {
  57. float pythagoras = (float)System.Math.Sqrt(System.Math.Pow(diff_x, 2) + System.Math.Pow(diff_z, 2));
  58. route_lenght = route_lenght + pythagoras;
  59. differeces.Add(pythagoras);
  60. }
  61. // Add distance of x-direction or z-direction
  62. else
  63. {
  64. route_lenght = route_lenght + diff_x + diff_z;
  65. differeces.Add(diff_x + diff_z);
  66. }
  67. }
  68. Debug.Log(differeces.Count);
  69. }
  70. // Wird von CoinUpdate aufgerufen, das in jeder Kreuzung der Route enthalten ist
  71. public void PlaceCoins()
  72. {
  73. if ((pointIndex + 1) < points_size) // Check: sind noch mindestens 2 Punkte in der Liste?
  74. {
  75. do
  76. {
  77. //Abstand zwischen den Coins berechnen (Orientiert an Tills-Code-Beispiel)
  78. start_point = points[pointIndex];
  79. start_point.x = start_point.x + 190f; //190 = offset wegen relativen Kreuzungskoordinaten aus Hierachie
  80. // Compute corresponding stepsize
  81. // TODO: Calculate correct coin number, due round of number, for evaluation
  82. stepsize = (differeces[pointIndex] / route_lenght) * (float)Coin_Number;
  83. Debug.Log("<color=red>ACTUAL COIN SIZE: " + stepsize.ToString());
  84. pointIndex++; //Index für nächsten Punkt aus Liste holen
  85. end_point = points[pointIndex];
  86. end_point.x = end_point.x + 190f; //190 = offset wegen relativen Kreuzungskoordinaten aus Hierachie
  87. float distance_x = System.Math.Abs(start_point.x - end_point.x);
  88. float distance_z = System.Math.Abs(start_point.z - end_point.z);
  89. float space_x = 0; //default
  90. float space_z = 0; //default
  91. Vector3 rotationVector = new Vector3(0f, 0f, 0f);
  92. int idx = 1;
  93. bool slalom = false;
  94. bool slalom_x_dir = true;
  95. if (distance_x > 0) // wenn entlang der x Richung verteilt wird
  96. {
  97. space_x = distance_x / stepsize;
  98. Debug.Log("Coin-Creation: Place Coins along x axis.");
  99. rotationVector = new Vector3(0f, 0f, 0f);
  100. }
  101. if (distance_z > 0) // wenn entlang der z Richung verteilt wird
  102. {
  103. space_z = distance_z / stepsize;
  104. Debug.Log("Coin-Creation: Place Coins along z axis.");
  105. rotationVector = new Vector3(0f, 90f, 0f);
  106. }
  107. if (start_point.slalom && end_point.slalom)
  108. {
  109. Debug.Log("Coin-Creation: Place Coins for Slalom.");
  110. idx = 0;
  111. // Check if slalom along x-axis (slalom_x_dir) or along z-axis (!slalom_x_dir)
  112. if (!points[pointIndex - 2].slalom)
  113. {
  114. if (start_point.x - points[pointIndex - 2].x == 0)
  115. {
  116. slalom_x_dir = false;
  117. }
  118. else
  119. {
  120. slalom_x_dir = true;
  121. }
  122. }
  123. else
  124. {
  125. slalom = true;
  126. }
  127. }
  128. for (int i = idx; i < System.Math.Round(stepsize); ++i)
  129. {
  130. //Coin-Position berechnen
  131. if (!slalom || (slalom && i != 0))
  132. {
  133. if (start_point.x - end_point.x > 0)
  134. {
  135. coin_position.x = start_point.x - i * space_x;
  136. }
  137. if (start_point.x - end_point.x <= 0)
  138. {
  139. coin_position.x = start_point.x + i * space_x;
  140. }
  141. if (start_point.z - end_point.z > 0)
  142. {
  143. coin_position.z = start_point.z - i * space_z;
  144. }
  145. if (start_point.z - end_point.z <= 0)
  146. {
  147. coin_position.z = start_point.z + i * space_z;
  148. }
  149. if (slalom || (start_point.slalom && end_point.slalom))
  150. {
  151. rotationVector = new Vector3(0f, 0, 0f);
  152. }
  153. }
  154. else
  155. {
  156. if (slalom_x_dir)
  157. {
  158. coin_position.x = start_point.x;
  159. if (start_point.z - end_point.z > 0)
  160. {
  161. coin_position.z = start_point.z - Distance_Slalom;
  162. }
  163. if (start_point.z - end_point.z <= 0)
  164. {
  165. coin_position.z = start_point.z + Distance_Slalom;
  166. }
  167. rotationVector = new Vector3(0f, 0f, 0f);
  168. }
  169. else
  170. {
  171. if (start_point.x - end_point.x > 0)
  172. {
  173. coin_position.x = start_point.x - Distance_Slalom;
  174. }
  175. if (start_point.x - end_point.x <= 0)
  176. {
  177. coin_position.x = start_point.x + Distance_Slalom;
  178. }
  179. coin_position.z = start_point.z + Distance_Slalom;
  180. rotationVector = new Vector3(0f, 90f, 0f);
  181. }
  182. }
  183. coin_position.y = 0.6f;
  184. if ((distance_x - Distance_Cross < System.Math.Abs(coin_position.x - start_point.x) && distance_x != 0 && !end_point.slalom)
  185. || (distance_z - Distance_Cross < System.Math.Abs(coin_position.z - start_point.z) && distance_z != 0 && !end_point.slalom))
  186. {
  187. break;
  188. }
  189. counter++;
  190. //Coin erstellen
  191. coin = coinPool.GetItem();
  192. coin.transform.position = coin_position;
  193. coin.transform.rotation = Quaternion.Euler(rotationVector);
  194. if (((i + 1) == stepsize) && (coin != null)) { Debug.Log("CoinCreation: Coin placement finished."); }
  195. }
  196. Debug.Log(counter);
  197. } while (end_point.slalom); //Da diese Funktion nur von Kreuzungen und nicht von Slalom-Referenzpunkten aufgerufen wird
  198. // muss hier eine Schleife über die einzelnen Slalom-Abschnitte gemacht werden
  199. }
  200. else
  201. {
  202. Debug.Log("The End of the Route is near, so no new coins needed!");
  203. }
  204. }
  205. }