CoinCreation.cs 8.9 KB

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