Browse Source

automated Coin Placement (untested) part 2

lisa 2 years ago
parent
commit
42e86f11e0

+ 8 - 0
Assets/Coin.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: cc9aacf25a3ec0244b59ad0d0b83146d
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 73 - 0
Assets/Coin/CoinCreation.cs

@@ -0,0 +1,73 @@
+using System;
+using System.Collections.Generic;
+using UnityEngine;
+using System.Linq;
+using JetBrains.Annotations;
+using Pools;
+using Roads;
+
+[Serializable]
+public struct RoutePoint
+{
+    //Point position
+    public string name;
+    public float x;
+    public float z;
+    public bool slalom;
+}
+
+public class CoinCreation : MonoBehaviour
+{
+    public int stepsize;
+    public CoinPool coinPool;
+    public List<RoutePoint> points = new List<RoutePoint>();
+    int pointIndex = 0;
+    private Vector3 coin_position;
+    private GameObject coin;
+
+    private void Start()
+    {
+        PlaceCoins();
+    }
+
+    public void PlaceCoins()
+    {
+        //Abstand zwischen den Coins berechnen
+
+        RoutePoint start_point = points[pointIndex];
+        start_point.x = start_point.x + 190f; //offset wegen relativen Kreuzungskoordinaten 
+
+        pointIndex++;
+
+        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;
+        }
+
+
+        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 erstellen
+                coin = coinPool.GetItem();
+                coin.transform.position = coin_position;
+                coin.transform.rotation = Quaternion.Euler(0f, 0f, 90f);
+        }
+    }
+}

+ 11 - 0
Assets/Coin/CoinCreation.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 82e8d665ae879914eb9366d6565125fb
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 28 - 0
Assets/Coin/CoinUpdate.cs

@@ -0,0 +1,28 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using Pools;
+
+//Jede Kreuzung die eine Richtungsänderung der Route hervorruft braucht dieses Skript 
+//(jedoch nicht die slalom referenzpunkte)
+
+public class CoinUpdate : MonoBehaviour
+{
+    public CoinCreation coinCreator;
+
+    // Start is called before the first frame update
+    void Start()
+    {
+        coinCreator = GameObject.FindGameObjectWithTag("Route").GetComponent<CoinCreation>();
+    }
+
+    //Keep count of collisions with player
+    private void OnTriggerEnter(Collider other)
+    {
+        if (other.gameObject.CompareTag("bike"))
+        {
+            Debug.Log("New Coins Coming!");
+            coinCreator.PlaceCoins();
+        }
+    }
+}

+ 11 - 0
Assets/Coin/CoinUpdate.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 49ae0bf5cdc1fa349adc6dd13fcf63c5
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 61 - 0
Assets/Scripts/Pools/CoinPool.cs

@@ -0,0 +1,61 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+namespace Pools
+{
+    public class CoinPool : MonoBehaviour
+    {
+        public int initialSize = 10;
+        public int amountIncrease = 5;
+        public GameObject prefab;
+        private HashSet<GameObject> active;
+
+        private Queue<GameObject> inactive;
+
+        private void Awake()
+        {
+            active = new HashSet<GameObject>();
+            inactive = new Queue<GameObject>();
+            IncreasePool(initialSize);
+        }
+
+        private void IncreasePool(int amount)
+        {
+            for (var i = 0; i < amount; i++)
+            {
+                var item = Instantiate(prefab, transform);
+                item.SetActive(false);
+                inactive.Enqueue(item);
+            }
+        }
+
+
+        public GameObject GetItem()
+        {
+            GameObject item;
+            if (inactive.Count > 0)
+            {
+                item = inactive.Dequeue();
+            }
+            else
+            {
+                IncreasePool(amountIncrease);
+                item = inactive.Dequeue();
+            }
+
+            item.transform.SetPositionAndRotation(Vector3.zero, Quaternion.Euler(Vector3.zero));
+            item.SetActive(true);
+            active.Add(item);
+            return item;
+        }
+
+        public void ReturnToPool(GameObject item)
+        {
+            if (active.Remove(item))
+            {
+                item.SetActive(false);
+                inactive.Enqueue(item);
+            }
+        }
+    }
+}

+ 11 - 0
Assets/Scripts/Pools/CoinPool.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 1be5f90cb5908824e97b8a762c04c3ba
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: