diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index c082a53..0bd7901 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -1202,6 +1202,8 @@ config: {fileID: 128843415} targets: - {fileID: 349804700} + timeLeft: 0 + interval: 5 --- !u!1 &1551909212 GameObject: m_ObjectHideFlags: 0 @@ -1475,3 +1477,5 @@ config: {fileID: 128843415} targets: - {fileID: 1551909214} + timeLeft: 0 + interval: 5 diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index c082a53..0bd7901 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -1202,6 +1202,8 @@ config: {fileID: 128843415} targets: - {fileID: 349804700} + timeLeft: 0 + interval: 5 --- !u!1 &1551909212 GameObject: m_ObjectHideFlags: 0 @@ -1475,3 +1477,5 @@ config: {fileID: 128843415} targets: - {fileID: 1551909214} + timeLeft: 0 + interval: 5 diff --git a/Assets/Scripts/Car.meta b/Assets/Scripts/Car.meta new file mode 100644 index 0000000..f6d84f8 --- /dev/null +++ b/Assets/Scripts/Car.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: ba5bff368fa5ec749b9dbd9472078046 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index c082a53..0bd7901 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -1202,6 +1202,8 @@ config: {fileID: 128843415} targets: - {fileID: 349804700} + timeLeft: 0 + interval: 5 --- !u!1 &1551909212 GameObject: m_ObjectHideFlags: 0 @@ -1475,3 +1477,5 @@ config: {fileID: 128843415} targets: - {fileID: 1551909214} + timeLeft: 0 + interval: 5 diff --git a/Assets/Scripts/Car.meta b/Assets/Scripts/Car.meta new file mode 100644 index 0000000..f6d84f8 --- /dev/null +++ b/Assets/Scripts/Car.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: ba5bff368fa5ec749b9dbd9472078046 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs new file mode 100644 index 0000000..f9c539a --- /dev/null +++ b/Assets/Scripts/Car/Car.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Car { + private Route route; + private Road currentRoad; + private float currentPosition = 0f, speed = 0.25f; + private int roadIndex = 0; + GameObject gameObject; + + public Car(Route route, Transform parent, Config config) { + this.route = route; + currentRoad = route.roads[0]; + + gameObject = new GameObject(); + FlatCircleRenderer renderer = new FlatCircleRenderer(0.8f, 0.2f, 32); + gameObject.AddComponent().material = config.carMaterial; + gameObject.AddComponent().mesh = renderer.mesh; + gameObject.transform.parent = parent; + CarData carData = gameObject.AddComponent(); + carData.car = this; + carData.Update(); + } + + public Vector3 getPosition() { + return currentRoad.path.getPosition(currentPosition); + } + + public void step(float deltaTime) { + currentPosition += deltaTime * speed; + if (currentPosition > 1f) { + currentPosition -= 1f; + roadIndex++; + if (roadIndex > route.roads.Count-1) { + GameObject.Destroy(gameObject); + return; + } + currentRoad = route.roads[roadIndex]; + } + } +} diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index c082a53..0bd7901 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -1202,6 +1202,8 @@ config: {fileID: 128843415} targets: - {fileID: 349804700} + timeLeft: 0 + interval: 5 --- !u!1 &1551909212 GameObject: m_ObjectHideFlags: 0 @@ -1475,3 +1477,5 @@ config: {fileID: 128843415} targets: - {fileID: 1551909214} + timeLeft: 0 + interval: 5 diff --git a/Assets/Scripts/Car.meta b/Assets/Scripts/Car.meta new file mode 100644 index 0000000..f6d84f8 --- /dev/null +++ b/Assets/Scripts/Car.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: ba5bff368fa5ec749b9dbd9472078046 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs new file mode 100644 index 0000000..f9c539a --- /dev/null +++ b/Assets/Scripts/Car/Car.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Car { + private Route route; + private Road currentRoad; + private float currentPosition = 0f, speed = 0.25f; + private int roadIndex = 0; + GameObject gameObject; + + public Car(Route route, Transform parent, Config config) { + this.route = route; + currentRoad = route.roads[0]; + + gameObject = new GameObject(); + FlatCircleRenderer renderer = new FlatCircleRenderer(0.8f, 0.2f, 32); + gameObject.AddComponent().material = config.carMaterial; + gameObject.AddComponent().mesh = renderer.mesh; + gameObject.transform.parent = parent; + CarData carData = gameObject.AddComponent(); + carData.car = this; + carData.Update(); + } + + public Vector3 getPosition() { + return currentRoad.path.getPosition(currentPosition); + } + + public void step(float deltaTime) { + currentPosition += deltaTime * speed; + if (currentPosition > 1f) { + currentPosition -= 1f; + roadIndex++; + if (roadIndex > route.roads.Count-1) { + GameObject.Destroy(gameObject); + return; + } + currentRoad = route.roads[roadIndex]; + } + } +} diff --git a/Assets/Scripts/Car/Car.cs.meta b/Assets/Scripts/Car/Car.cs.meta new file mode 100644 index 0000000..a4f656d --- /dev/null +++ b/Assets/Scripts/Car/Car.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 11690c0b61a8b5641817bf9981c8b917 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index c082a53..0bd7901 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -1202,6 +1202,8 @@ config: {fileID: 128843415} targets: - {fileID: 349804700} + timeLeft: 0 + interval: 5 --- !u!1 &1551909212 GameObject: m_ObjectHideFlags: 0 @@ -1475,3 +1477,5 @@ config: {fileID: 128843415} targets: - {fileID: 1551909214} + timeLeft: 0 + interval: 5 diff --git a/Assets/Scripts/Car.meta b/Assets/Scripts/Car.meta new file mode 100644 index 0000000..f6d84f8 --- /dev/null +++ b/Assets/Scripts/Car.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: ba5bff368fa5ec749b9dbd9472078046 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs new file mode 100644 index 0000000..f9c539a --- /dev/null +++ b/Assets/Scripts/Car/Car.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Car { + private Route route; + private Road currentRoad; + private float currentPosition = 0f, speed = 0.25f; + private int roadIndex = 0; + GameObject gameObject; + + public Car(Route route, Transform parent, Config config) { + this.route = route; + currentRoad = route.roads[0]; + + gameObject = new GameObject(); + FlatCircleRenderer renderer = new FlatCircleRenderer(0.8f, 0.2f, 32); + gameObject.AddComponent().material = config.carMaterial; + gameObject.AddComponent().mesh = renderer.mesh; + gameObject.transform.parent = parent; + CarData carData = gameObject.AddComponent(); + carData.car = this; + carData.Update(); + } + + public Vector3 getPosition() { + return currentRoad.path.getPosition(currentPosition); + } + + public void step(float deltaTime) { + currentPosition += deltaTime * speed; + if (currentPosition > 1f) { + currentPosition -= 1f; + roadIndex++; + if (roadIndex > route.roads.Count-1) { + GameObject.Destroy(gameObject); + return; + } + currentRoad = route.roads[roadIndex]; + } + } +} diff --git a/Assets/Scripts/Car/Car.cs.meta b/Assets/Scripts/Car/Car.cs.meta new file mode 100644 index 0000000..a4f656d --- /dev/null +++ b/Assets/Scripts/Car/Car.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 11690c0b61a8b5641817bf9981c8b917 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/CarData.cs b/Assets/Scripts/Car/CarData.cs new file mode 100644 index 0000000..db481e2 --- /dev/null +++ b/Assets/Scripts/Car/CarData.cs @@ -0,0 +1,15 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class CarData : MonoBehaviour { + public Car car; + + void Start() { + } + + public void Update() { + car.step(Time.deltaTime); + transform.position = car.getPosition(); + } +} diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index c082a53..0bd7901 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -1202,6 +1202,8 @@ config: {fileID: 128843415} targets: - {fileID: 349804700} + timeLeft: 0 + interval: 5 --- !u!1 &1551909212 GameObject: m_ObjectHideFlags: 0 @@ -1475,3 +1477,5 @@ config: {fileID: 128843415} targets: - {fileID: 1551909214} + timeLeft: 0 + interval: 5 diff --git a/Assets/Scripts/Car.meta b/Assets/Scripts/Car.meta new file mode 100644 index 0000000..f6d84f8 --- /dev/null +++ b/Assets/Scripts/Car.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: ba5bff368fa5ec749b9dbd9472078046 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs new file mode 100644 index 0000000..f9c539a --- /dev/null +++ b/Assets/Scripts/Car/Car.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Car { + private Route route; + private Road currentRoad; + private float currentPosition = 0f, speed = 0.25f; + private int roadIndex = 0; + GameObject gameObject; + + public Car(Route route, Transform parent, Config config) { + this.route = route; + currentRoad = route.roads[0]; + + gameObject = new GameObject(); + FlatCircleRenderer renderer = new FlatCircleRenderer(0.8f, 0.2f, 32); + gameObject.AddComponent().material = config.carMaterial; + gameObject.AddComponent().mesh = renderer.mesh; + gameObject.transform.parent = parent; + CarData carData = gameObject.AddComponent(); + carData.car = this; + carData.Update(); + } + + public Vector3 getPosition() { + return currentRoad.path.getPosition(currentPosition); + } + + public void step(float deltaTime) { + currentPosition += deltaTime * speed; + if (currentPosition > 1f) { + currentPosition -= 1f; + roadIndex++; + if (roadIndex > route.roads.Count-1) { + GameObject.Destroy(gameObject); + return; + } + currentRoad = route.roads[roadIndex]; + } + } +} diff --git a/Assets/Scripts/Car/Car.cs.meta b/Assets/Scripts/Car/Car.cs.meta new file mode 100644 index 0000000..a4f656d --- /dev/null +++ b/Assets/Scripts/Car/Car.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 11690c0b61a8b5641817bf9981c8b917 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/CarData.cs b/Assets/Scripts/Car/CarData.cs new file mode 100644 index 0000000..db481e2 --- /dev/null +++ b/Assets/Scripts/Car/CarData.cs @@ -0,0 +1,15 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class CarData : MonoBehaviour { + public Car car; + + void Start() { + } + + public void Update() { + car.step(Time.deltaTime); + transform.position = car.getPosition(); + } +} diff --git a/Assets/Scripts/Car/CarData.cs.meta b/Assets/Scripts/Car/CarData.cs.meta new file mode 100644 index 0000000..19b549e --- /dev/null +++ b/Assets/Scripts/Car/CarData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: eebec7c9a76a44343b0e938768b8fd2b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index c082a53..0bd7901 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -1202,6 +1202,8 @@ config: {fileID: 128843415} targets: - {fileID: 349804700} + timeLeft: 0 + interval: 5 --- !u!1 &1551909212 GameObject: m_ObjectHideFlags: 0 @@ -1475,3 +1477,5 @@ config: {fileID: 128843415} targets: - {fileID: 1551909214} + timeLeft: 0 + interval: 5 diff --git a/Assets/Scripts/Car.meta b/Assets/Scripts/Car.meta new file mode 100644 index 0000000..f6d84f8 --- /dev/null +++ b/Assets/Scripts/Car.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: ba5bff368fa5ec749b9dbd9472078046 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs new file mode 100644 index 0000000..f9c539a --- /dev/null +++ b/Assets/Scripts/Car/Car.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Car { + private Route route; + private Road currentRoad; + private float currentPosition = 0f, speed = 0.25f; + private int roadIndex = 0; + GameObject gameObject; + + public Car(Route route, Transform parent, Config config) { + this.route = route; + currentRoad = route.roads[0]; + + gameObject = new GameObject(); + FlatCircleRenderer renderer = new FlatCircleRenderer(0.8f, 0.2f, 32); + gameObject.AddComponent().material = config.carMaterial; + gameObject.AddComponent().mesh = renderer.mesh; + gameObject.transform.parent = parent; + CarData carData = gameObject.AddComponent(); + carData.car = this; + carData.Update(); + } + + public Vector3 getPosition() { + return currentRoad.path.getPosition(currentPosition); + } + + public void step(float deltaTime) { + currentPosition += deltaTime * speed; + if (currentPosition > 1f) { + currentPosition -= 1f; + roadIndex++; + if (roadIndex > route.roads.Count-1) { + GameObject.Destroy(gameObject); + return; + } + currentRoad = route.roads[roadIndex]; + } + } +} diff --git a/Assets/Scripts/Car/Car.cs.meta b/Assets/Scripts/Car/Car.cs.meta new file mode 100644 index 0000000..a4f656d --- /dev/null +++ b/Assets/Scripts/Car/Car.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 11690c0b61a8b5641817bf9981c8b917 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/CarData.cs b/Assets/Scripts/Car/CarData.cs new file mode 100644 index 0000000..db481e2 --- /dev/null +++ b/Assets/Scripts/Car/CarData.cs @@ -0,0 +1,15 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class CarData : MonoBehaviour { + public Car car; + + void Start() { + } + + public void Update() { + car.step(Time.deltaTime); + transform.position = car.getPosition(); + } +} diff --git a/Assets/Scripts/Car/CarData.cs.meta b/Assets/Scripts/Car/CarData.cs.meta new file mode 100644 index 0000000..19b549e --- /dev/null +++ b/Assets/Scripts/Car/CarData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: eebec7c9a76a44343b0e938768b8fd2b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/Route.cs b/Assets/Scripts/Car/Route.cs new file mode 100644 index 0000000..95154a0 --- /dev/null +++ b/Assets/Scripts/Car/Route.cs @@ -0,0 +1,64 @@ +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + +public class Route { + public Node start, end; + public List roads = new List(); + public bool isValid = false; + + public Route(Node start, Node end) { + this.start = start; + this.end = end; + if (start.roads.Count == 0) { + return; + } + Dictionary cameFrom = new Dictionary(); + cameFrom[start] = null; + Dictionary travelDistance = new Dictionary(); + travelDistance.Add(start, 0f); + Dictionary totalDistance = new Dictionary(); + totalDistance.Add(start, (start.position-end.position).magnitude); + HashSet openNodes = new HashSet(); + openNodes.Add(start); + + while (openNodes.Count > 0) { + Node currentNode = null; + float currentMinTotalDistance = float.PositiveInfinity; + foreach (Node node in openNodes) { + float distance = totalDistance[node]; + if (distance < currentMinTotalDistance) { + currentMinTotalDistance = distance; + currentNode = node; + } + } + if (currentNode == end) { + while (cameFrom[currentNode] != null) { + roads.Add(currentNode.roads.Find(it => it.nodes.Contains(cameFrom[currentNode]))); + currentNode = cameFrom[currentNode]; + } + roads.Reverse(); + isValid = true; + return; + } + openNodes.Remove(currentNode); + foreach (Road road in currentNode.roads) { + if (road.nodes[0] != currentNode) { + continue; + } + Node neighbour = road.nodes[1]; + float neighbourTravelDistance = totalDistance[currentNode] + (currentNode.position - neighbour.position).magnitude; + if ((!travelDistance.ContainsKey(neighbour)) || neighbourTravelDistance < travelDistance[neighbour]) { + cameFrom[neighbour] = currentNode; + travelDistance[neighbour] = neighbourTravelDistance; + totalDistance[neighbour] = neighbourTravelDistance + (neighbour.position - end.position).magnitude; + if (!openNodes.Contains(neighbour)) { + openNodes.Add(neighbour); + } + } + } + } + } +} + diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index c082a53..0bd7901 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -1202,6 +1202,8 @@ config: {fileID: 128843415} targets: - {fileID: 349804700} + timeLeft: 0 + interval: 5 --- !u!1 &1551909212 GameObject: m_ObjectHideFlags: 0 @@ -1475,3 +1477,5 @@ config: {fileID: 128843415} targets: - {fileID: 1551909214} + timeLeft: 0 + interval: 5 diff --git a/Assets/Scripts/Car.meta b/Assets/Scripts/Car.meta new file mode 100644 index 0000000..f6d84f8 --- /dev/null +++ b/Assets/Scripts/Car.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: ba5bff368fa5ec749b9dbd9472078046 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs new file mode 100644 index 0000000..f9c539a --- /dev/null +++ b/Assets/Scripts/Car/Car.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Car { + private Route route; + private Road currentRoad; + private float currentPosition = 0f, speed = 0.25f; + private int roadIndex = 0; + GameObject gameObject; + + public Car(Route route, Transform parent, Config config) { + this.route = route; + currentRoad = route.roads[0]; + + gameObject = new GameObject(); + FlatCircleRenderer renderer = new FlatCircleRenderer(0.8f, 0.2f, 32); + gameObject.AddComponent().material = config.carMaterial; + gameObject.AddComponent().mesh = renderer.mesh; + gameObject.transform.parent = parent; + CarData carData = gameObject.AddComponent(); + carData.car = this; + carData.Update(); + } + + public Vector3 getPosition() { + return currentRoad.path.getPosition(currentPosition); + } + + public void step(float deltaTime) { + currentPosition += deltaTime * speed; + if (currentPosition > 1f) { + currentPosition -= 1f; + roadIndex++; + if (roadIndex > route.roads.Count-1) { + GameObject.Destroy(gameObject); + return; + } + currentRoad = route.roads[roadIndex]; + } + } +} diff --git a/Assets/Scripts/Car/Car.cs.meta b/Assets/Scripts/Car/Car.cs.meta new file mode 100644 index 0000000..a4f656d --- /dev/null +++ b/Assets/Scripts/Car/Car.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 11690c0b61a8b5641817bf9981c8b917 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/CarData.cs b/Assets/Scripts/Car/CarData.cs new file mode 100644 index 0000000..db481e2 --- /dev/null +++ b/Assets/Scripts/Car/CarData.cs @@ -0,0 +1,15 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class CarData : MonoBehaviour { + public Car car; + + void Start() { + } + + public void Update() { + car.step(Time.deltaTime); + transform.position = car.getPosition(); + } +} diff --git a/Assets/Scripts/Car/CarData.cs.meta b/Assets/Scripts/Car/CarData.cs.meta new file mode 100644 index 0000000..19b549e --- /dev/null +++ b/Assets/Scripts/Car/CarData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: eebec7c9a76a44343b0e938768b8fd2b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/Route.cs b/Assets/Scripts/Car/Route.cs new file mode 100644 index 0000000..95154a0 --- /dev/null +++ b/Assets/Scripts/Car/Route.cs @@ -0,0 +1,64 @@ +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + +public class Route { + public Node start, end; + public List roads = new List(); + public bool isValid = false; + + public Route(Node start, Node end) { + this.start = start; + this.end = end; + if (start.roads.Count == 0) { + return; + } + Dictionary cameFrom = new Dictionary(); + cameFrom[start] = null; + Dictionary travelDistance = new Dictionary(); + travelDistance.Add(start, 0f); + Dictionary totalDistance = new Dictionary(); + totalDistance.Add(start, (start.position-end.position).magnitude); + HashSet openNodes = new HashSet(); + openNodes.Add(start); + + while (openNodes.Count > 0) { + Node currentNode = null; + float currentMinTotalDistance = float.PositiveInfinity; + foreach (Node node in openNodes) { + float distance = totalDistance[node]; + if (distance < currentMinTotalDistance) { + currentMinTotalDistance = distance; + currentNode = node; + } + } + if (currentNode == end) { + while (cameFrom[currentNode] != null) { + roads.Add(currentNode.roads.Find(it => it.nodes.Contains(cameFrom[currentNode]))); + currentNode = cameFrom[currentNode]; + } + roads.Reverse(); + isValid = true; + return; + } + openNodes.Remove(currentNode); + foreach (Road road in currentNode.roads) { + if (road.nodes[0] != currentNode) { + continue; + } + Node neighbour = road.nodes[1]; + float neighbourTravelDistance = totalDistance[currentNode] + (currentNode.position - neighbour.position).magnitude; + if ((!travelDistance.ContainsKey(neighbour)) || neighbourTravelDistance < travelDistance[neighbour]) { + cameFrom[neighbour] = currentNode; + travelDistance[neighbour] = neighbourTravelDistance; + totalDistance[neighbour] = neighbourTravelDistance + (neighbour.position - end.position).magnitude; + if (!openNodes.Contains(neighbour)) { + openNodes.Add(neighbour); + } + } + } + } + } +} + diff --git a/Assets/Scripts/Car/Route.cs.meta b/Assets/Scripts/Car/Route.cs.meta new file mode 100644 index 0000000..83d167e --- /dev/null +++ b/Assets/Scripts/Car/Route.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e09b87c670733cb48bf20542282bce19 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index c082a53..0bd7901 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -1202,6 +1202,8 @@ config: {fileID: 128843415} targets: - {fileID: 349804700} + timeLeft: 0 + interval: 5 --- !u!1 &1551909212 GameObject: m_ObjectHideFlags: 0 @@ -1475,3 +1477,5 @@ config: {fileID: 128843415} targets: - {fileID: 1551909214} + timeLeft: 0 + interval: 5 diff --git a/Assets/Scripts/Car.meta b/Assets/Scripts/Car.meta new file mode 100644 index 0000000..f6d84f8 --- /dev/null +++ b/Assets/Scripts/Car.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: ba5bff368fa5ec749b9dbd9472078046 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs new file mode 100644 index 0000000..f9c539a --- /dev/null +++ b/Assets/Scripts/Car/Car.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Car { + private Route route; + private Road currentRoad; + private float currentPosition = 0f, speed = 0.25f; + private int roadIndex = 0; + GameObject gameObject; + + public Car(Route route, Transform parent, Config config) { + this.route = route; + currentRoad = route.roads[0]; + + gameObject = new GameObject(); + FlatCircleRenderer renderer = new FlatCircleRenderer(0.8f, 0.2f, 32); + gameObject.AddComponent().material = config.carMaterial; + gameObject.AddComponent().mesh = renderer.mesh; + gameObject.transform.parent = parent; + CarData carData = gameObject.AddComponent(); + carData.car = this; + carData.Update(); + } + + public Vector3 getPosition() { + return currentRoad.path.getPosition(currentPosition); + } + + public void step(float deltaTime) { + currentPosition += deltaTime * speed; + if (currentPosition > 1f) { + currentPosition -= 1f; + roadIndex++; + if (roadIndex > route.roads.Count-1) { + GameObject.Destroy(gameObject); + return; + } + currentRoad = route.roads[roadIndex]; + } + } +} diff --git a/Assets/Scripts/Car/Car.cs.meta b/Assets/Scripts/Car/Car.cs.meta new file mode 100644 index 0000000..a4f656d --- /dev/null +++ b/Assets/Scripts/Car/Car.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 11690c0b61a8b5641817bf9981c8b917 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/CarData.cs b/Assets/Scripts/Car/CarData.cs new file mode 100644 index 0000000..db481e2 --- /dev/null +++ b/Assets/Scripts/Car/CarData.cs @@ -0,0 +1,15 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class CarData : MonoBehaviour { + public Car car; + + void Start() { + } + + public void Update() { + car.step(Time.deltaTime); + transform.position = car.getPosition(); + } +} diff --git a/Assets/Scripts/Car/CarData.cs.meta b/Assets/Scripts/Car/CarData.cs.meta new file mode 100644 index 0000000..19b549e --- /dev/null +++ b/Assets/Scripts/Car/CarData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: eebec7c9a76a44343b0e938768b8fd2b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/Route.cs b/Assets/Scripts/Car/Route.cs new file mode 100644 index 0000000..95154a0 --- /dev/null +++ b/Assets/Scripts/Car/Route.cs @@ -0,0 +1,64 @@ +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + +public class Route { + public Node start, end; + public List roads = new List(); + public bool isValid = false; + + public Route(Node start, Node end) { + this.start = start; + this.end = end; + if (start.roads.Count == 0) { + return; + } + Dictionary cameFrom = new Dictionary(); + cameFrom[start] = null; + Dictionary travelDistance = new Dictionary(); + travelDistance.Add(start, 0f); + Dictionary totalDistance = new Dictionary(); + totalDistance.Add(start, (start.position-end.position).magnitude); + HashSet openNodes = new HashSet(); + openNodes.Add(start); + + while (openNodes.Count > 0) { + Node currentNode = null; + float currentMinTotalDistance = float.PositiveInfinity; + foreach (Node node in openNodes) { + float distance = totalDistance[node]; + if (distance < currentMinTotalDistance) { + currentMinTotalDistance = distance; + currentNode = node; + } + } + if (currentNode == end) { + while (cameFrom[currentNode] != null) { + roads.Add(currentNode.roads.Find(it => it.nodes.Contains(cameFrom[currentNode]))); + currentNode = cameFrom[currentNode]; + } + roads.Reverse(); + isValid = true; + return; + } + openNodes.Remove(currentNode); + foreach (Road road in currentNode.roads) { + if (road.nodes[0] != currentNode) { + continue; + } + Node neighbour = road.nodes[1]; + float neighbourTravelDistance = totalDistance[currentNode] + (currentNode.position - neighbour.position).magnitude; + if ((!travelDistance.ContainsKey(neighbour)) || neighbourTravelDistance < travelDistance[neighbour]) { + cameFrom[neighbour] = currentNode; + travelDistance[neighbour] = neighbourTravelDistance; + totalDistance[neighbour] = neighbourTravelDistance + (neighbour.position - end.position).magnitude; + if (!openNodes.Contains(neighbour)) { + openNodes.Add(neighbour); + } + } + } + } + } +} + diff --git a/Assets/Scripts/Car/Route.cs.meta b/Assets/Scripts/Car/Route.cs.meta new file mode 100644 index 0000000..83d167e --- /dev/null +++ b/Assets/Scripts/Car/Route.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e09b87c670733cb48bf20542282bce19 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Config.cs b/Assets/Scripts/Config.cs index 779c97e..7d002db 100644 --- a/Assets/Scripts/Config.cs +++ b/Assets/Scripts/Config.cs @@ -3,7 +3,7 @@ using UnityEngine; public class Config : MonoBehaviour { - public Material roadMaterial, roadEditMaterial; + public Material roadMaterial, roadEditMaterial, carMaterial; public Mode mode = Mode.DeleteRoad; public Mode futureMode; public RectTransform highlight; diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index c082a53..0bd7901 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -1202,6 +1202,8 @@ config: {fileID: 128843415} targets: - {fileID: 349804700} + timeLeft: 0 + interval: 5 --- !u!1 &1551909212 GameObject: m_ObjectHideFlags: 0 @@ -1475,3 +1477,5 @@ config: {fileID: 128843415} targets: - {fileID: 1551909214} + timeLeft: 0 + interval: 5 diff --git a/Assets/Scripts/Car.meta b/Assets/Scripts/Car.meta new file mode 100644 index 0000000..f6d84f8 --- /dev/null +++ b/Assets/Scripts/Car.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: ba5bff368fa5ec749b9dbd9472078046 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs new file mode 100644 index 0000000..f9c539a --- /dev/null +++ b/Assets/Scripts/Car/Car.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Car { + private Route route; + private Road currentRoad; + private float currentPosition = 0f, speed = 0.25f; + private int roadIndex = 0; + GameObject gameObject; + + public Car(Route route, Transform parent, Config config) { + this.route = route; + currentRoad = route.roads[0]; + + gameObject = new GameObject(); + FlatCircleRenderer renderer = new FlatCircleRenderer(0.8f, 0.2f, 32); + gameObject.AddComponent().material = config.carMaterial; + gameObject.AddComponent().mesh = renderer.mesh; + gameObject.transform.parent = parent; + CarData carData = gameObject.AddComponent(); + carData.car = this; + carData.Update(); + } + + public Vector3 getPosition() { + return currentRoad.path.getPosition(currentPosition); + } + + public void step(float deltaTime) { + currentPosition += deltaTime * speed; + if (currentPosition > 1f) { + currentPosition -= 1f; + roadIndex++; + if (roadIndex > route.roads.Count-1) { + GameObject.Destroy(gameObject); + return; + } + currentRoad = route.roads[roadIndex]; + } + } +} diff --git a/Assets/Scripts/Car/Car.cs.meta b/Assets/Scripts/Car/Car.cs.meta new file mode 100644 index 0000000..a4f656d --- /dev/null +++ b/Assets/Scripts/Car/Car.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 11690c0b61a8b5641817bf9981c8b917 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/CarData.cs b/Assets/Scripts/Car/CarData.cs new file mode 100644 index 0000000..db481e2 --- /dev/null +++ b/Assets/Scripts/Car/CarData.cs @@ -0,0 +1,15 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class CarData : MonoBehaviour { + public Car car; + + void Start() { + } + + public void Update() { + car.step(Time.deltaTime); + transform.position = car.getPosition(); + } +} diff --git a/Assets/Scripts/Car/CarData.cs.meta b/Assets/Scripts/Car/CarData.cs.meta new file mode 100644 index 0000000..19b549e --- /dev/null +++ b/Assets/Scripts/Car/CarData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: eebec7c9a76a44343b0e938768b8fd2b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/Route.cs b/Assets/Scripts/Car/Route.cs new file mode 100644 index 0000000..95154a0 --- /dev/null +++ b/Assets/Scripts/Car/Route.cs @@ -0,0 +1,64 @@ +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + +public class Route { + public Node start, end; + public List roads = new List(); + public bool isValid = false; + + public Route(Node start, Node end) { + this.start = start; + this.end = end; + if (start.roads.Count == 0) { + return; + } + Dictionary cameFrom = new Dictionary(); + cameFrom[start] = null; + Dictionary travelDistance = new Dictionary(); + travelDistance.Add(start, 0f); + Dictionary totalDistance = new Dictionary(); + totalDistance.Add(start, (start.position-end.position).magnitude); + HashSet openNodes = new HashSet(); + openNodes.Add(start); + + while (openNodes.Count > 0) { + Node currentNode = null; + float currentMinTotalDistance = float.PositiveInfinity; + foreach (Node node in openNodes) { + float distance = totalDistance[node]; + if (distance < currentMinTotalDistance) { + currentMinTotalDistance = distance; + currentNode = node; + } + } + if (currentNode == end) { + while (cameFrom[currentNode] != null) { + roads.Add(currentNode.roads.Find(it => it.nodes.Contains(cameFrom[currentNode]))); + currentNode = cameFrom[currentNode]; + } + roads.Reverse(); + isValid = true; + return; + } + openNodes.Remove(currentNode); + foreach (Road road in currentNode.roads) { + if (road.nodes[0] != currentNode) { + continue; + } + Node neighbour = road.nodes[1]; + float neighbourTravelDistance = totalDistance[currentNode] + (currentNode.position - neighbour.position).magnitude; + if ((!travelDistance.ContainsKey(neighbour)) || neighbourTravelDistance < travelDistance[neighbour]) { + cameFrom[neighbour] = currentNode; + travelDistance[neighbour] = neighbourTravelDistance; + totalDistance[neighbour] = neighbourTravelDistance + (neighbour.position - end.position).magnitude; + if (!openNodes.Contains(neighbour)) { + openNodes.Add(neighbour); + } + } + } + } + } +} + diff --git a/Assets/Scripts/Car/Route.cs.meta b/Assets/Scripts/Car/Route.cs.meta new file mode 100644 index 0000000..83d167e --- /dev/null +++ b/Assets/Scripts/Car/Route.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e09b87c670733cb48bf20542282bce19 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Config.cs b/Assets/Scripts/Config.cs index 779c97e..7d002db 100644 --- a/Assets/Scripts/Config.cs +++ b/Assets/Scripts/Config.cs @@ -3,7 +3,7 @@ using UnityEngine; public class Config : MonoBehaviour { - public Material roadMaterial, roadEditMaterial; + public Material roadMaterial, roadEditMaterial, carMaterial; public Mode mode = Mode.DeleteRoad; public Mode futureMode; public RectTransform highlight; diff --git a/Assets/Scripts/Roads/Node/ExitNodeData.cs b/Assets/Scripts/Roads/Node/ExitNodeData.cs index cc59aec..c634426 100644 --- a/Assets/Scripts/Roads/Node/ExitNodeData.cs +++ b/Assets/Scripts/Roads/Node/ExitNodeData.cs @@ -3,7 +3,7 @@ using UnityEngine; public class ExitNodeData: MonoBehaviour { - private ExitNode node; + public ExitNode node; public Config config; void Start() { diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index c082a53..0bd7901 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -1202,6 +1202,8 @@ config: {fileID: 128843415} targets: - {fileID: 349804700} + timeLeft: 0 + interval: 5 --- !u!1 &1551909212 GameObject: m_ObjectHideFlags: 0 @@ -1475,3 +1477,5 @@ config: {fileID: 128843415} targets: - {fileID: 1551909214} + timeLeft: 0 + interval: 5 diff --git a/Assets/Scripts/Car.meta b/Assets/Scripts/Car.meta new file mode 100644 index 0000000..f6d84f8 --- /dev/null +++ b/Assets/Scripts/Car.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: ba5bff368fa5ec749b9dbd9472078046 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs new file mode 100644 index 0000000..f9c539a --- /dev/null +++ b/Assets/Scripts/Car/Car.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Car { + private Route route; + private Road currentRoad; + private float currentPosition = 0f, speed = 0.25f; + private int roadIndex = 0; + GameObject gameObject; + + public Car(Route route, Transform parent, Config config) { + this.route = route; + currentRoad = route.roads[0]; + + gameObject = new GameObject(); + FlatCircleRenderer renderer = new FlatCircleRenderer(0.8f, 0.2f, 32); + gameObject.AddComponent().material = config.carMaterial; + gameObject.AddComponent().mesh = renderer.mesh; + gameObject.transform.parent = parent; + CarData carData = gameObject.AddComponent(); + carData.car = this; + carData.Update(); + } + + public Vector3 getPosition() { + return currentRoad.path.getPosition(currentPosition); + } + + public void step(float deltaTime) { + currentPosition += deltaTime * speed; + if (currentPosition > 1f) { + currentPosition -= 1f; + roadIndex++; + if (roadIndex > route.roads.Count-1) { + GameObject.Destroy(gameObject); + return; + } + currentRoad = route.roads[roadIndex]; + } + } +} diff --git a/Assets/Scripts/Car/Car.cs.meta b/Assets/Scripts/Car/Car.cs.meta new file mode 100644 index 0000000..a4f656d --- /dev/null +++ b/Assets/Scripts/Car/Car.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 11690c0b61a8b5641817bf9981c8b917 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/CarData.cs b/Assets/Scripts/Car/CarData.cs new file mode 100644 index 0000000..db481e2 --- /dev/null +++ b/Assets/Scripts/Car/CarData.cs @@ -0,0 +1,15 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class CarData : MonoBehaviour { + public Car car; + + void Start() { + } + + public void Update() { + car.step(Time.deltaTime); + transform.position = car.getPosition(); + } +} diff --git a/Assets/Scripts/Car/CarData.cs.meta b/Assets/Scripts/Car/CarData.cs.meta new file mode 100644 index 0000000..19b549e --- /dev/null +++ b/Assets/Scripts/Car/CarData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: eebec7c9a76a44343b0e938768b8fd2b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/Route.cs b/Assets/Scripts/Car/Route.cs new file mode 100644 index 0000000..95154a0 --- /dev/null +++ b/Assets/Scripts/Car/Route.cs @@ -0,0 +1,64 @@ +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + +public class Route { + public Node start, end; + public List roads = new List(); + public bool isValid = false; + + public Route(Node start, Node end) { + this.start = start; + this.end = end; + if (start.roads.Count == 0) { + return; + } + Dictionary cameFrom = new Dictionary(); + cameFrom[start] = null; + Dictionary travelDistance = new Dictionary(); + travelDistance.Add(start, 0f); + Dictionary totalDistance = new Dictionary(); + totalDistance.Add(start, (start.position-end.position).magnitude); + HashSet openNodes = new HashSet(); + openNodes.Add(start); + + while (openNodes.Count > 0) { + Node currentNode = null; + float currentMinTotalDistance = float.PositiveInfinity; + foreach (Node node in openNodes) { + float distance = totalDistance[node]; + if (distance < currentMinTotalDistance) { + currentMinTotalDistance = distance; + currentNode = node; + } + } + if (currentNode == end) { + while (cameFrom[currentNode] != null) { + roads.Add(currentNode.roads.Find(it => it.nodes.Contains(cameFrom[currentNode]))); + currentNode = cameFrom[currentNode]; + } + roads.Reverse(); + isValid = true; + return; + } + openNodes.Remove(currentNode); + foreach (Road road in currentNode.roads) { + if (road.nodes[0] != currentNode) { + continue; + } + Node neighbour = road.nodes[1]; + float neighbourTravelDistance = totalDistance[currentNode] + (currentNode.position - neighbour.position).magnitude; + if ((!travelDistance.ContainsKey(neighbour)) || neighbourTravelDistance < travelDistance[neighbour]) { + cameFrom[neighbour] = currentNode; + travelDistance[neighbour] = neighbourTravelDistance; + totalDistance[neighbour] = neighbourTravelDistance + (neighbour.position - end.position).magnitude; + if (!openNodes.Contains(neighbour)) { + openNodes.Add(neighbour); + } + } + } + } + } +} + diff --git a/Assets/Scripts/Car/Route.cs.meta b/Assets/Scripts/Car/Route.cs.meta new file mode 100644 index 0000000..83d167e --- /dev/null +++ b/Assets/Scripts/Car/Route.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e09b87c670733cb48bf20542282bce19 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Config.cs b/Assets/Scripts/Config.cs index 779c97e..7d002db 100644 --- a/Assets/Scripts/Config.cs +++ b/Assets/Scripts/Config.cs @@ -3,7 +3,7 @@ using UnityEngine; public class Config : MonoBehaviour { - public Material roadMaterial, roadEditMaterial; + public Material roadMaterial, roadEditMaterial, carMaterial; public Mode mode = Mode.DeleteRoad; public Mode futureMode; public RectTransform highlight; diff --git a/Assets/Scripts/Roads/Node/ExitNodeData.cs b/Assets/Scripts/Roads/Node/ExitNodeData.cs index cc59aec..c634426 100644 --- a/Assets/Scripts/Roads/Node/ExitNodeData.cs +++ b/Assets/Scripts/Roads/Node/ExitNodeData.cs @@ -3,7 +3,7 @@ using UnityEngine; public class ExitNodeData: MonoBehaviour { - private ExitNode node; + public ExitNode node; public Config config; void Start() { diff --git a/Assets/Scripts/Roads/Node/SpawnNodeData.cs b/Assets/Scripts/Roads/Node/SpawnNodeData.cs index 356fcdb..3db61bb 100644 --- a/Assets/Scripts/Roads/Node/SpawnNodeData.cs +++ b/Assets/Scripts/Roads/Node/SpawnNodeData.cs @@ -5,12 +5,22 @@ public class SpawnNodeData : MonoBehaviour { private SpawnNode node; public Config config; - public ExitNodeData[] targets = new ExitNodeData[0]; - + public ExitNodeData[] targets; + public float timeLeft = 0f, interval = 5f; + void Start() { node = new SpawnNode(transform.position, transform, config); } void Update() { + timeLeft -= Time.deltaTime; + if (timeLeft <= 0f) { + timeLeft = interval; + ExitNodeData target = targets[Random.Range(0, targets.Length)]; + Route route = new Route(node, target.node); + if (route.isValid) { + new Car(route, transform, config); + } + } } } diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index c082a53..0bd7901 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -1202,6 +1202,8 @@ config: {fileID: 128843415} targets: - {fileID: 349804700} + timeLeft: 0 + interval: 5 --- !u!1 &1551909212 GameObject: m_ObjectHideFlags: 0 @@ -1475,3 +1477,5 @@ config: {fileID: 128843415} targets: - {fileID: 1551909214} + timeLeft: 0 + interval: 5 diff --git a/Assets/Scripts/Car.meta b/Assets/Scripts/Car.meta new file mode 100644 index 0000000..f6d84f8 --- /dev/null +++ b/Assets/Scripts/Car.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: ba5bff368fa5ec749b9dbd9472078046 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs new file mode 100644 index 0000000..f9c539a --- /dev/null +++ b/Assets/Scripts/Car/Car.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Car { + private Route route; + private Road currentRoad; + private float currentPosition = 0f, speed = 0.25f; + private int roadIndex = 0; + GameObject gameObject; + + public Car(Route route, Transform parent, Config config) { + this.route = route; + currentRoad = route.roads[0]; + + gameObject = new GameObject(); + FlatCircleRenderer renderer = new FlatCircleRenderer(0.8f, 0.2f, 32); + gameObject.AddComponent().material = config.carMaterial; + gameObject.AddComponent().mesh = renderer.mesh; + gameObject.transform.parent = parent; + CarData carData = gameObject.AddComponent(); + carData.car = this; + carData.Update(); + } + + public Vector3 getPosition() { + return currentRoad.path.getPosition(currentPosition); + } + + public void step(float deltaTime) { + currentPosition += deltaTime * speed; + if (currentPosition > 1f) { + currentPosition -= 1f; + roadIndex++; + if (roadIndex > route.roads.Count-1) { + GameObject.Destroy(gameObject); + return; + } + currentRoad = route.roads[roadIndex]; + } + } +} diff --git a/Assets/Scripts/Car/Car.cs.meta b/Assets/Scripts/Car/Car.cs.meta new file mode 100644 index 0000000..a4f656d --- /dev/null +++ b/Assets/Scripts/Car/Car.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 11690c0b61a8b5641817bf9981c8b917 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/CarData.cs b/Assets/Scripts/Car/CarData.cs new file mode 100644 index 0000000..db481e2 --- /dev/null +++ b/Assets/Scripts/Car/CarData.cs @@ -0,0 +1,15 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class CarData : MonoBehaviour { + public Car car; + + void Start() { + } + + public void Update() { + car.step(Time.deltaTime); + transform.position = car.getPosition(); + } +} diff --git a/Assets/Scripts/Car/CarData.cs.meta b/Assets/Scripts/Car/CarData.cs.meta new file mode 100644 index 0000000..19b549e --- /dev/null +++ b/Assets/Scripts/Car/CarData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: eebec7c9a76a44343b0e938768b8fd2b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/Route.cs b/Assets/Scripts/Car/Route.cs new file mode 100644 index 0000000..95154a0 --- /dev/null +++ b/Assets/Scripts/Car/Route.cs @@ -0,0 +1,64 @@ +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + +public class Route { + public Node start, end; + public List roads = new List(); + public bool isValid = false; + + public Route(Node start, Node end) { + this.start = start; + this.end = end; + if (start.roads.Count == 0) { + return; + } + Dictionary cameFrom = new Dictionary(); + cameFrom[start] = null; + Dictionary travelDistance = new Dictionary(); + travelDistance.Add(start, 0f); + Dictionary totalDistance = new Dictionary(); + totalDistance.Add(start, (start.position-end.position).magnitude); + HashSet openNodes = new HashSet(); + openNodes.Add(start); + + while (openNodes.Count > 0) { + Node currentNode = null; + float currentMinTotalDistance = float.PositiveInfinity; + foreach (Node node in openNodes) { + float distance = totalDistance[node]; + if (distance < currentMinTotalDistance) { + currentMinTotalDistance = distance; + currentNode = node; + } + } + if (currentNode == end) { + while (cameFrom[currentNode] != null) { + roads.Add(currentNode.roads.Find(it => it.nodes.Contains(cameFrom[currentNode]))); + currentNode = cameFrom[currentNode]; + } + roads.Reverse(); + isValid = true; + return; + } + openNodes.Remove(currentNode); + foreach (Road road in currentNode.roads) { + if (road.nodes[0] != currentNode) { + continue; + } + Node neighbour = road.nodes[1]; + float neighbourTravelDistance = totalDistance[currentNode] + (currentNode.position - neighbour.position).magnitude; + if ((!travelDistance.ContainsKey(neighbour)) || neighbourTravelDistance < travelDistance[neighbour]) { + cameFrom[neighbour] = currentNode; + travelDistance[neighbour] = neighbourTravelDistance; + totalDistance[neighbour] = neighbourTravelDistance + (neighbour.position - end.position).magnitude; + if (!openNodes.Contains(neighbour)) { + openNodes.Add(neighbour); + } + } + } + } + } +} + diff --git a/Assets/Scripts/Car/Route.cs.meta b/Assets/Scripts/Car/Route.cs.meta new file mode 100644 index 0000000..83d167e --- /dev/null +++ b/Assets/Scripts/Car/Route.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e09b87c670733cb48bf20542282bce19 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Config.cs b/Assets/Scripts/Config.cs index 779c97e..7d002db 100644 --- a/Assets/Scripts/Config.cs +++ b/Assets/Scripts/Config.cs @@ -3,7 +3,7 @@ using UnityEngine; public class Config : MonoBehaviour { - public Material roadMaterial, roadEditMaterial; + public Material roadMaterial, roadEditMaterial, carMaterial; public Mode mode = Mode.DeleteRoad; public Mode futureMode; public RectTransform highlight; diff --git a/Assets/Scripts/Roads/Node/ExitNodeData.cs b/Assets/Scripts/Roads/Node/ExitNodeData.cs index cc59aec..c634426 100644 --- a/Assets/Scripts/Roads/Node/ExitNodeData.cs +++ b/Assets/Scripts/Roads/Node/ExitNodeData.cs @@ -3,7 +3,7 @@ using UnityEngine; public class ExitNodeData: MonoBehaviour { - private ExitNode node; + public ExitNode node; public Config config; void Start() { diff --git a/Assets/Scripts/Roads/Node/SpawnNodeData.cs b/Assets/Scripts/Roads/Node/SpawnNodeData.cs index 356fcdb..3db61bb 100644 --- a/Assets/Scripts/Roads/Node/SpawnNodeData.cs +++ b/Assets/Scripts/Roads/Node/SpawnNodeData.cs @@ -5,12 +5,22 @@ public class SpawnNodeData : MonoBehaviour { private SpawnNode node; public Config config; - public ExitNodeData[] targets = new ExitNodeData[0]; - + public ExitNodeData[] targets; + public float timeLeft = 0f, interval = 5f; + void Start() { node = new SpawnNode(transform.position, transform, config); } void Update() { + timeLeft -= Time.deltaTime; + if (timeLeft <= 0f) { + timeLeft = interval; + ExitNodeData target = targets[Random.Range(0, targets.Length)]; + Route route = new Route(node, target.node); + if (route.isValid) { + new Car(route, transform, config); + } + } } } diff --git a/Assets/Scripts/Roads/Road.cs b/Assets/Scripts/Roads/Road.cs index a1656c2..48ebf03 100644 --- a/Assets/Scripts/Roads/Road.cs +++ b/Assets/Scripts/Roads/Road.cs @@ -4,7 +4,8 @@ public class Road { public List nodes = new List(); - Bezier path, arrow1, arrow2; + public Bezier path; + private Bezier arrow1, arrow2; private FlatBezierRenderer pathLine, roadBody, arrow1Renderer, arrow2Renderer; private Config config; private MeshCollider collider;