Browse Source

automated Coin Placement (updated)

lisa 2 years ago
parent
commit
583c6e5c7e
2 changed files with 31 additions and 23 deletions
  1. 30 22
      Assets/Coin/CoinCreation.cs
  2. 1 1
      Assets/Coin/CoinUpdate.cs

+ 30 - 22
Assets/Coin/CoinCreation.cs

@@ -30,44 +30,52 @@ public class CoinCreation : MonoBehaviour
         PlaceCoins();
     }
 
+    int points_size = points.Count;
     public void PlaceCoins()
     {
-        //Abstand zwischen den Coins berechnen
+        if ((pointIndex + 1) < points_size) // Check: sind noch mindestens 2 Punkte in der Liste?
+        {
+            //Abstand zwischen den Coins berechnen
 
-        RoutePoint start_point = points[pointIndex];
-        start_point.x = start_point.x + 190f; //offset wegen relativen Kreuzungskoordinaten 
+            RoutePoint start_point = points[pointIndex];
+            start_point.x = start_point.x + 190f; //offset wegen relativen Kreuzungskoordinaten 
 
-        pointIndex++;
+            pointIndex++;
 
-        RoutePoint end_point = points[pointIndex];
-        end_point.x = end_point.x + 190f; //offset wegen relativen Kreuzungskoordinaten
+            RoutePoint end_point = points[pointIndex];
+            end_point.x = end_point.x + 190f; //offset wegen relativen Kreuzungskoordinaten
 
-        float distance_x = System.Math.Abs(start_point.x - end_point.x);
-        float distance_z = System.Math.Abs(start_point.z - end_point.z);
-  
-        float space_x = 0; //default
-        float space_z = 0; //default
-        if (distance_x > 0) // wenn entlang der x Richung verteilt wird
-        {
-            space_x = distance_x / stepsize;
-        }
-        if (distance_z > 0) // wenn entlang der z Richung verteilt wird
-        {
-            space_z = distance_z / stepsize;
-        }
+            float distance_x = System.Math.Abs(start_point.x - end_point.x);
+            float distance_z = System.Math.Abs(start_point.z - end_point.z);
 
+            float space_x = 0; //default
+            float space_z = 0; //default
+            if (distance_x > 0) // wenn entlang der x Richung verteilt wird
+            {
+                space_x = distance_x / stepsize;
+            }
+            if (distance_z > 0) // wenn entlang der z Richung verteilt wird
+            {
+                space_z = distance_z / stepsize;
+            }
 
-        for (int i = 0; i > stepsize; ++i)
-        {
+
+            for (int i = 0; i > stepsize; ++i)
+            {
                 //position berechnen
                 coin_position.x = start_point.x + i * space_x;
                 coin_position.y = 0.5f;
-                coin_position.z = start_point.z  + i * space_z;
+                coin_position.z = start_point.z + i * space_z;
 
                 //Coin erstellen
                 coin = coinPool.GetItem();
                 coin.transform.position = coin_position;
                 coin.transform.rotation = Quaternion.Euler(0f, 0f, 90f);
+            }
+        }
+        else
+        {
+            Debug.Log("The End of the Route is near, so no new coins needed!");
         }
     }
 }

+ 1 - 1
Assets/Coin/CoinUpdate.cs

@@ -8,7 +8,7 @@ using Pools;
 
 public class CoinUpdate : MonoBehaviour
 {
-    public CoinCreation coinCreator;
+    private CoinCreation coinCreator;
 
     // Start is called before the first frame update
     void Start()