CoinCreation.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. if (start_point.slalom || (start_point.slalom && end_point.slalom))
  51. {
  52. //TODO Was muss beim schiefen verteilen für den Slalom angepasst werden? z.B. stepsize?
  53. Debug.Log("Coin-Creation: Place Coins for Slalom.");
  54. }
  55. if (distance_x > 0) // wenn entlang der x Richung verteilt wird
  56. {
  57. space_x = distance_x / stepsize;
  58. Debug.Log("Coin-Creation: Place Coins along x axis.");
  59. }
  60. if (distance_z > 0) // wenn entlang der z Richung verteilt wird
  61. {
  62. space_z = distance_z / stepsize;
  63. Debug.Log("Coin-Creation: Place Coins along z axis.");
  64. }
  65. for (int i = 0; i < stepsize; ++i)
  66. {
  67. //Coin-Position berechnen
  68. coin_position.x = start_point.x + i * space_x;
  69. coin_position.y = 0.5f;
  70. coin_position.z = start_point.z + i * space_z;
  71. //Coin erstellen
  72. coin = coinPool.GetItem();
  73. coin.transform.position = coin_position;
  74. coin.transform.rotation = Quaternion.Euler(0f, 0f, 90f);
  75. if (((i + 1) == stepsize) && (coin != null)) { Debug.Log("CoinCreation: Coin placement finished."); }
  76. }
  77. } while (end_point.slalom); //Da diese Funktion nur von Kreuzungen und nicht von Slalom-Referenzpunkten aufgerufen wird
  78. // muss hier eine Schleife über die einzelnen Slalom-Abschnitte gemacht werden
  79. }
  80. else
  81. {
  82. Debug.Log("The End of the Route is near, so no new coins needed!");
  83. }
  84. }
  85. }