123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using System.Linq;
- using JetBrains.Annotations;
- using Pools;
- using Roads;
- //Kreuzungen und Slalom-Referenzpunkte für die Route
- [Serializable]
- public struct RoutePoint
- {
- //Point position
- public string name;
- public float x;
- public float z;
- public bool slalom;
- public int stepsizes;
- }
- public class CoinCreation : MonoBehaviour
- {
- // Parameters to determine stepsize.
- float route_lenght;
- [SerializeField] public int Coin_Number;
- [SerializeField] public float Distance_Cross; // Test for cross coin drawing
- [SerializeField] public float Distance_Slalom;
- //[SerializeField] public bool Draw_Cross; // Maybe draw on Line
- public float stepsize; // public float stepsize = 10; //TODO welcher Wert ist hier passend?
- List<float> differeces = new List<float>();
- int counter = 0; // How many Coins were drawn on the road
- public CoinPool coinPool;
- public List<RoutePoint> points = new List<RoutePoint>();
- int pointIndex = 0;
- private Vector3 coin_position;
- private GameObject coin;
- RoutePoint start_point;
- RoutePoint end_point;
- private void Start()
- {
- CalcRoute();
- PlaceCoins();
- }
- int points_size = 30; //needs to be changed if Route changes!
- // Determine route length
- public void CalcRoute()
- {
- route_lenght = 0;
- // Compute the whole route length.
- for (int idx = 0; idx < points.Count - 1; ++idx)
- {
- float diff_x = System.Math.Abs(points[idx].x - points[idx + 1].x);
- float diff_z = System.Math.Abs(points[idx].z - points[idx + 1].z);
- // If we calculate route length in slalom part -> use Pythagoras to
- // calculate Hypothenuse.
- if (points[idx].slalom && points[idx + 1].slalom)
- {
- float pythagoras = (float)System.Math.Sqrt(System.Math.Pow(diff_x, 2) + System.Math.Pow(diff_z, 2));
- route_lenght = route_lenght + pythagoras;
- differeces.Add(pythagoras);
- }
- // Add distance of x-direction or z-direction
- else
- {
- route_lenght = route_lenght + diff_x + diff_z;
- differeces.Add(diff_x + diff_z);
- }
- }
- Debug.Log(differeces.Count);
- }
- // Wird von CoinUpdate aufgerufen, das in jeder Kreuzung der Route enthalten ist
- public void PlaceCoins()
- {
- if ((pointIndex + 1) < points_size) // Check: sind noch mindestens 2 Punkte in der Liste?
- {
- do
- {
- //Abstand zwischen den Coins berechnen (Orientiert an Tills-Code-Beispiel)
- start_point = points[pointIndex];
- start_point.x = start_point.x + 190f; //190 = offset wegen relativen Kreuzungskoordinaten aus Hierachie
- // Compute corresponding stepsize
- // TODO: Calculate correct coin number, due round of number, for evaluation
- stepsize = (differeces[pointIndex] / route_lenght) * (float)Coin_Number;
- Debug.Log("<color=red>ACTUAL COIN SIZE: " + stepsize.ToString());
- pointIndex++; //Index für nächsten Punkt aus Liste holen
- end_point = points[pointIndex];
- end_point.x = end_point.x + 190f; //190 = offset wegen relativen Kreuzungskoordinaten aus Hierachie
- 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
- Vector3 rotationVector = new Vector3(0f, 0f, 0f);
- int idx = 1;
- bool slalom = false;
- bool slalom_x_dir = true;
- if (start_point.slalom && end_point.slalom)
- {
- Debug.Log("Coin-Creation: Place Coins for Slalom.");
- idx = 0;
- // Check if slalom along x-axis (slalom_x_dir) or along z-axis (!slalom_x_dir)
- if (!points[pointIndex - 2].slalom)
- {
- if (start_point.x - points[pointIndex - 2].x == 0)
- {
- slalom_x_dir = false;
- }
- else
- {
- slalom_x_dir = true;
- }
- }
- else
- {
- slalom = true;
- }
- }
- if (distance_x > 0) // wenn entlang der x Richung verteilt wird
- {
- space_x = distance_x / stepsize;
- Debug.Log("Coin-Creation: Place Coins along x axis.");
- rotationVector = new Vector3(0f, 0f, 90f);
- }
- if (distance_z > 0) // wenn entlang der z Richung verteilt wird
- {
- space_z = distance_z / stepsize;
- Debug.Log("Coin-Creation: Place Coins along z axis.");
- rotationVector = new Vector3(0f, 90f, 90f);
- }
- for (int i = idx; i < System.Math.Round(stepsize); ++i)
- {
- //Coin-Position berechnen
- if (!slalom || (slalom && i != 0))
- {
- if (start_point.x - end_point.x > 0)
- {
- coin_position.x = start_point.x - i * space_x;
- }
- if (start_point.x - end_point.x <= 0)
- {
- coin_position.x = start_point.x + i * space_x;
- }
- if (start_point.z - end_point.z > 0)
- {
- coin_position.z = start_point.z - i * space_z;
- }
- if (start_point.z - end_point.z <= 0)
- {
- coin_position.z = start_point.z + i * space_z;
- }
- }
- else
- {
- if (slalom_x_dir)
- {
- coin_position.x = start_point.x;
- if (start_point.z - end_point.z > 0)
- {
- coin_position.z = start_point.z - Distance_Slalom;
- }
- if (start_point.z - end_point.z <= 0)
- {
- coin_position.z = start_point.z + Distance_Slalom;
- }
- }
- else
- {
- if (start_point.x - end_point.x > 0)
- {
- coin_position.x = start_point.x - Distance_Slalom;
- }
- if (start_point.x - end_point.x <= 0)
- {
- coin_position.x = start_point.x + Distance_Slalom;
- }
- coin_position.z = start_point.z + Distance_Slalom;
- }
- }
- coin_position.y = 0.5f;
- if ((distance_x - Distance_Cross < System.Math.Abs(coin_position.x - start_point.x) && distance_x != 0 && !end_point.slalom)
- || (distance_z - Distance_Cross < System.Math.Abs(coin_position.z - start_point.z) && distance_z != 0 && !end_point.slalom))
- {
- break;
- }
- counter++;
- //Coin erstellen
- coin = coinPool.GetItem();
- coin.transform.position = coin_position;
- coin.transform.rotation = Quaternion.Euler(rotationVector);
- if (((i + 1) == stepsize) && (coin != null)) { Debug.Log("CoinCreation: Coin placement finished."); }
- }
- Debug.Log(counter);
- } while (end_point.slalom); //Da diese Funktion nur von Kreuzungen und nicht von Slalom-Referenzpunkten aufgerufen wird
- // muss hier eine Schleife über die einzelnen Slalom-Abschnitte gemacht werden
- }
- else
- {
- Debug.Log("The End of the Route is near, so no new coins needed!");
- }
- }
- }
|