CoinCreation.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 (start_point.slalom && end_point.slalom)
  95. {
  96. Debug.Log("Coin-Creation: Place Coins for Slalom.");
  97. idx = 0;
  98. // Check if slalom along x-axis (slalom_x_dir) or along z-axis (!slalom_x_dir)
  99. if (!points[pointIndex - 2].slalom)
  100. {
  101. if (start_point.x - points[pointIndex - 2].x == 0)
  102. {
  103. slalom_x_dir = false;
  104. }
  105. else
  106. {
  107. slalom_x_dir = true;
  108. }
  109. }
  110. else
  111. {
  112. slalom = true;
  113. }
  114. }
  115. if (distance_x > 0) // wenn entlang der x Richung verteilt wird
  116. {
  117. space_x = distance_x / stepsize;
  118. Debug.Log("Coin-Creation: Place Coins along x axis.");
  119. rotationVector = new Vector3(0f, 0f, 0f);
  120. }
  121. if (distance_z > 0) // wenn entlang der z Richung verteilt wird
  122. {
  123. space_z = distance_z / stepsize;
  124. Debug.Log("Coin-Creation: Place Coins along z axis.");
  125. rotationVector = new Vector3(0f, 90f, 0f);
  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. }
  149. else
  150. {
  151. if (slalom_x_dir)
  152. {
  153. coin_position.x = start_point.x;
  154. if (start_point.z - end_point.z > 0)
  155. {
  156. coin_position.z = start_point.z - Distance_Slalom;
  157. }
  158. if (start_point.z - end_point.z <= 0)
  159. {
  160. coin_position.z = start_point.z + Distance_Slalom;
  161. }
  162. }
  163. else
  164. {
  165. if (start_point.x - end_point.x > 0)
  166. {
  167. coin_position.x = start_point.x - Distance_Slalom;
  168. }
  169. if (start_point.x - end_point.x <= 0)
  170. {
  171. coin_position.x = start_point.x + Distance_Slalom;
  172. }
  173. coin_position.z = start_point.z + Distance_Slalom;
  174. }
  175. }
  176. coin_position.y = 0.5f;
  177. if ((distance_x - Distance_Cross < System.Math.Abs(coin_position.x - start_point.x) && distance_x != 0 && !end_point.slalom)
  178. || (distance_z - Distance_Cross < System.Math.Abs(coin_position.z - start_point.z) && distance_z != 0 && !end_point.slalom))
  179. {
  180. break;
  181. }
  182. counter++;
  183. //Coin erstellen
  184. coin = coinPool.GetItem();
  185. coin.transform.position = coin_position;
  186. coin.transform.rotation = Quaternion.Euler(rotationVector);
  187. if (((i + 1) == stepsize) && (coin != null)) { Debug.Log("CoinCreation: Coin placement finished."); }
  188. }
  189. Debug.Log(counter);
  190. } while (end_point.slalom); //Da diese Funktion nur von Kreuzungen und nicht von Slalom-Referenzpunkten aufgerufen wird
  191. // muss hier eine Schleife über die einzelnen Slalom-Abschnitte gemacht werden
  192. }
  193. else
  194. {
  195. Debug.Log("The End of the Route is near, so no new coins needed!");
  196. }
  197. }
  198. }