CoinCreation.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. }
  18. public class CoinCreation : MonoBehaviour
  19. {
  20. public float stepsize = 10; //TODO welcher Wert ist hier passend?
  21. public CoinPool coinPool;
  22. public List<RoutePoint> points = new List<RoutePoint>();
  23. int pointIndex = 0;
  24. private Vector3 coin_position;
  25. private GameObject coin;
  26. RoutePoint start_point;
  27. RoutePoint end_point;
  28. private void Start()
  29. {
  30. PlaceCoins();
  31. }
  32. int points_size = 30; //needs to be changed if Route changes!
  33. // Wird von CoinUpdate aufgerufen, das in jeder Kreuzung der Route enthalten ist
  34. public void PlaceCoins()
  35. {
  36. if ((pointIndex + 1) < points_size) // Check: sind noch mindestens 2 Punkte in der Liste?
  37. {
  38. do
  39. {
  40. //Abstand zwischen den Coins berechnen (Orientiert an Tills-Code-Beispiel)
  41. start_point = points[pointIndex];
  42. start_point.x = start_point.x + 190f; //190 = offset wegen relativen Kreuzungskoordinaten aus Hierachie
  43. pointIndex++; //Index für nächsten Punkt aus Liste holen
  44. end_point = points[pointIndex];
  45. end_point.x = end_point.x + 190f; //190 = offset wegen relativen Kreuzungskoordinaten aus Hierachie
  46. float distance_x = System.Math.Abs(start_point.x - end_point.x);
  47. float distance_z = System.Math.Abs(start_point.z - end_point.z);
  48. float space_x = 0; //default
  49. float space_z = 0; //default
  50. Vector3 rotationVector = new Vector3(0f, 0f, 0f);
  51. if (start_point.slalom || (start_point.slalom && end_point.slalom))
  52. {
  53. //TODO Was muss beim schiefen verteilen für den Slalom angepasst werden? z.B. stepsize?
  54. Debug.Log("Coin-Creation: Place Coins for Slalom.");
  55. /*
  56. space_x = distance_x / stepsize;
  57. space_z = distance_z / stepsize;
  58. rotationVector = new Vector3(0f, 90f, 90f);
  59. */
  60. }
  61. if (distance_x > 0) // wenn entlang der x Richung verteilt wird
  62. {
  63. space_x = distance_x / stepsize;
  64. Debug.Log("Coin-Creation: Place Coins along x axis.");
  65. rotationVector = new Vector3(0f, 0f, 90f);
  66. }
  67. if (distance_z > 0) // wenn entlang der z Richung verteilt wird
  68. {
  69. space_z = distance_z / stepsize;
  70. Debug.Log("Coin-Creation: Place Coins along z axis.");
  71. rotationVector = new Vector3(0f, 90f, 90f);
  72. }
  73. for (int i = 0; i < stepsize; ++i)
  74. {
  75. //Coin-Position berechnen
  76. if (start_point.x - end_point.x > 0)
  77. {
  78. coin_position.x = start_point.x - i * space_x;
  79. }
  80. if (start_point.x - end_point.x <= 0)
  81. {
  82. coin_position.x = start_point.x + i * space_x;
  83. }
  84. if (start_point.z - end_point.z > 0)
  85. {
  86. coin_position.z = start_point.z - i * space_z;
  87. }
  88. if (start_point.z - end_point.z <= 0)
  89. {
  90. coin_position.z = start_point.z + i * space_z;
  91. }
  92. coin_position.y = 0.5f;
  93. //Coin erstellen
  94. coin = coinPool.GetItem();
  95. coin.transform.position = coin_position;
  96. coin.transform.rotation = Quaternion.Euler(rotationVector);
  97. if (((i + 1) == stepsize) && (coin != null)) { Debug.Log("CoinCreation: Coin placement finished."); }
  98. }
  99. } while (end_point.slalom); //Da diese Funktion nur von Kreuzungen und nicht von Slalom-Referenzpunkten aufgerufen wird
  100. // muss hier eine Schleife über die einzelnen Slalom-Abschnitte gemacht werden
  101. }
  102. else
  103. {
  104. Debug.Log("The End of the Route is near, so no new coins needed!");
  105. }
  106. }
  107. }