diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index ce92eb6..b6546a2 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -1462,6 +1462,7 @@ m_Name: m_EditorClassIdentifier: config: {fileID: 128843415} + roads: {fileID: 1714391592} --- !u!114 &1812881470 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index ce92eb6..b6546a2 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -1462,6 +1462,7 @@ m_Name: m_EditorClassIdentifier: config: {fileID: 128843415} + roads: {fileID: 1714391592} --- !u!114 &1812881470 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Buttons/LoadButton.cs b/Assets/Scripts/Buttons/LoadButton.cs index a9044e1..11c32e8 100644 --- a/Assets/Scripts/Buttons/LoadButton.cs +++ b/Assets/Scripts/Buttons/LoadButton.cs @@ -10,6 +10,7 @@ public class LoadButton : MonoBehaviour, IPointerDownHandler { public Config config; + public Transform roads; public void OnPointerDown(PointerEventData eventData) { config.onClick(); @@ -20,7 +21,27 @@ config.roadNetwork.clear(); string fileContent = File.ReadAllText(filePath); SaveStruct saveData = JsonUtility.FromJson(fileContent); - // todo: put the saved nodes and nodes into the world - print(saveData.nodes.Count); + List nodes = new List(); + List<(SpawnNodeData, List)> spawnNodeData = new List<(SpawnNodeData, List)>(); + foreach (SaveNode saveNode in saveData.nodes) { + Node node = null; + if (saveNode.type == "spawn") { + node = new SpawnNode(saveNode.position, roads, config).init(); + spawnNodeData.Add((node.nodeData as SpawnNodeData, saveNode.targets)); + } else if (saveNode.type == "exit") { + node = new ExitNode(saveNode.position, roads, config).init(); + } else { + node = new CustomNode(saveNode.position, roads, config).init(); + } + nodes.Add(node); + } + foreach ((SpawnNodeData, List) pair in spawnNodeData) { + SpawnNodeData nodeData = pair.Item1; + List targets = pair.Item2; + foreach (int target in targets) { + nodeData.targets.Add(nodes[target].nodeData as ExitNodeData); + } + } + // todo: generate roads } } diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index ce92eb6..b6546a2 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -1462,6 +1462,7 @@ m_Name: m_EditorClassIdentifier: config: {fileID: 128843415} + roads: {fileID: 1714391592} --- !u!114 &1812881470 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Buttons/LoadButton.cs b/Assets/Scripts/Buttons/LoadButton.cs index a9044e1..11c32e8 100644 --- a/Assets/Scripts/Buttons/LoadButton.cs +++ b/Assets/Scripts/Buttons/LoadButton.cs @@ -10,6 +10,7 @@ public class LoadButton : MonoBehaviour, IPointerDownHandler { public Config config; + public Transform roads; public void OnPointerDown(PointerEventData eventData) { config.onClick(); @@ -20,7 +21,27 @@ config.roadNetwork.clear(); string fileContent = File.ReadAllText(filePath); SaveStruct saveData = JsonUtility.FromJson(fileContent); - // todo: put the saved nodes and nodes into the world - print(saveData.nodes.Count); + List nodes = new List(); + List<(SpawnNodeData, List)> spawnNodeData = new List<(SpawnNodeData, List)>(); + foreach (SaveNode saveNode in saveData.nodes) { + Node node = null; + if (saveNode.type == "spawn") { + node = new SpawnNode(saveNode.position, roads, config).init(); + spawnNodeData.Add((node.nodeData as SpawnNodeData, saveNode.targets)); + } else if (saveNode.type == "exit") { + node = new ExitNode(saveNode.position, roads, config).init(); + } else { + node = new CustomNode(saveNode.position, roads, config).init(); + } + nodes.Add(node); + } + foreach ((SpawnNodeData, List) pair in spawnNodeData) { + SpawnNodeData nodeData = pair.Item1; + List targets = pair.Item2; + foreach (int target in targets) { + nodeData.targets.Add(nodes[target].nodeData as ExitNodeData); + } + } + // todo: generate roads } } diff --git a/Assets/Scripts/Buttons/SaveStructs.cs b/Assets/Scripts/Buttons/SaveStructs.cs index 4596487..6a0bc86 100644 --- a/Assets/Scripts/Buttons/SaveStructs.cs +++ b/Assets/Scripts/Buttons/SaveStructs.cs @@ -7,6 +7,7 @@ public struct SaveNode { public Vector3 position; public string type; + public List targets; } [Serializable] diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index ce92eb6..b6546a2 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -1462,6 +1462,7 @@ m_Name: m_EditorClassIdentifier: config: {fileID: 128843415} + roads: {fileID: 1714391592} --- !u!114 &1812881470 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Buttons/LoadButton.cs b/Assets/Scripts/Buttons/LoadButton.cs index a9044e1..11c32e8 100644 --- a/Assets/Scripts/Buttons/LoadButton.cs +++ b/Assets/Scripts/Buttons/LoadButton.cs @@ -10,6 +10,7 @@ public class LoadButton : MonoBehaviour, IPointerDownHandler { public Config config; + public Transform roads; public void OnPointerDown(PointerEventData eventData) { config.onClick(); @@ -20,7 +21,27 @@ config.roadNetwork.clear(); string fileContent = File.ReadAllText(filePath); SaveStruct saveData = JsonUtility.FromJson(fileContent); - // todo: put the saved nodes and nodes into the world - print(saveData.nodes.Count); + List nodes = new List(); + List<(SpawnNodeData, List)> spawnNodeData = new List<(SpawnNodeData, List)>(); + foreach (SaveNode saveNode in saveData.nodes) { + Node node = null; + if (saveNode.type == "spawn") { + node = new SpawnNode(saveNode.position, roads, config).init(); + spawnNodeData.Add((node.nodeData as SpawnNodeData, saveNode.targets)); + } else if (saveNode.type == "exit") { + node = new ExitNode(saveNode.position, roads, config).init(); + } else { + node = new CustomNode(saveNode.position, roads, config).init(); + } + nodes.Add(node); + } + foreach ((SpawnNodeData, List) pair in spawnNodeData) { + SpawnNodeData nodeData = pair.Item1; + List targets = pair.Item2; + foreach (int target in targets) { + nodeData.targets.Add(nodes[target].nodeData as ExitNodeData); + } + } + // todo: generate roads } } diff --git a/Assets/Scripts/Buttons/SaveStructs.cs b/Assets/Scripts/Buttons/SaveStructs.cs index 4596487..6a0bc86 100644 --- a/Assets/Scripts/Buttons/SaveStructs.cs +++ b/Assets/Scripts/Buttons/SaveStructs.cs @@ -7,6 +7,7 @@ public struct SaveNode { public Vector3 position; public string type; + public List targets; } [Serializable] diff --git a/Assets/Scripts/Roads/Node/ExitNodeData.cs b/Assets/Scripts/Roads/Node/ExitNodeData.cs index c634426..5cbef39 100644 --- a/Assets/Scripts/Roads/Node/ExitNodeData.cs +++ b/Assets/Scripts/Roads/Node/ExitNodeData.cs @@ -2,14 +2,5 @@ using System.Collections.Generic; using UnityEngine; -public class ExitNodeData: MonoBehaviour { - public ExitNode node; - public Config config; - - void Start() { - node = new ExitNode(transform.position, transform, config); - } - - void Update() { - } +public class ExitNodeData : NodeData { } diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index ce92eb6..b6546a2 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -1462,6 +1462,7 @@ m_Name: m_EditorClassIdentifier: config: {fileID: 128843415} + roads: {fileID: 1714391592} --- !u!114 &1812881470 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Buttons/LoadButton.cs b/Assets/Scripts/Buttons/LoadButton.cs index a9044e1..11c32e8 100644 --- a/Assets/Scripts/Buttons/LoadButton.cs +++ b/Assets/Scripts/Buttons/LoadButton.cs @@ -10,6 +10,7 @@ public class LoadButton : MonoBehaviour, IPointerDownHandler { public Config config; + public Transform roads; public void OnPointerDown(PointerEventData eventData) { config.onClick(); @@ -20,7 +21,27 @@ config.roadNetwork.clear(); string fileContent = File.ReadAllText(filePath); SaveStruct saveData = JsonUtility.FromJson(fileContent); - // todo: put the saved nodes and nodes into the world - print(saveData.nodes.Count); + List nodes = new List(); + List<(SpawnNodeData, List)> spawnNodeData = new List<(SpawnNodeData, List)>(); + foreach (SaveNode saveNode in saveData.nodes) { + Node node = null; + if (saveNode.type == "spawn") { + node = new SpawnNode(saveNode.position, roads, config).init(); + spawnNodeData.Add((node.nodeData as SpawnNodeData, saveNode.targets)); + } else if (saveNode.type == "exit") { + node = new ExitNode(saveNode.position, roads, config).init(); + } else { + node = new CustomNode(saveNode.position, roads, config).init(); + } + nodes.Add(node); + } + foreach ((SpawnNodeData, List) pair in spawnNodeData) { + SpawnNodeData nodeData = pair.Item1; + List targets = pair.Item2; + foreach (int target in targets) { + nodeData.targets.Add(nodes[target].nodeData as ExitNodeData); + } + } + // todo: generate roads } } diff --git a/Assets/Scripts/Buttons/SaveStructs.cs b/Assets/Scripts/Buttons/SaveStructs.cs index 4596487..6a0bc86 100644 --- a/Assets/Scripts/Buttons/SaveStructs.cs +++ b/Assets/Scripts/Buttons/SaveStructs.cs @@ -7,6 +7,7 @@ public struct SaveNode { public Vector3 position; public string type; + public List targets; } [Serializable] diff --git a/Assets/Scripts/Roads/Node/ExitNodeData.cs b/Assets/Scripts/Roads/Node/ExitNodeData.cs index c634426..5cbef39 100644 --- a/Assets/Scripts/Roads/Node/ExitNodeData.cs +++ b/Assets/Scripts/Roads/Node/ExitNodeData.cs @@ -2,14 +2,5 @@ using System.Collections.Generic; using UnityEngine; -public class ExitNodeData: MonoBehaviour { - public ExitNode node; - public Config config; - - void Start() { - node = new ExitNode(transform.position, transform, config); - } - - void Update() { - } +public class ExitNodeData : NodeData { } diff --git a/Assets/Scripts/Roads/Node/Node.cs b/Assets/Scripts/Roads/Node/Node.cs index 7eda931..aa211d1 100644 --- a/Assets/Scripts/Roads/Node/Node.cs +++ b/Assets/Scripts/Roads/Node/Node.cs @@ -11,14 +11,24 @@ public GameObject gameObject; public Vector3 direction; public Config config; + private Transform parent; + public NodeData nodeData; public Node(Vector3 position, Transform parent, Config config) { + this.position = position; + this.parent = parent; + this.config = config; + } + + public Node init() where T: NodeData { gameObject = new GameObject(); gameObject.transform.position = position; gameObject.transform.parent = parent; gameObject.AddComponent().radius = 1f; gameObject.layer = 7; - gameObject.AddComponent().node = this; + nodeData = gameObject.AddComponent(); + nodeData.node = this; + nodeData.config = config; GameObject nodeCircle = new GameObject(); nodeCircle.AddComponent().material = config.roadEditMaterial; @@ -31,9 +41,8 @@ nodeRoad.AddComponent().mesh = fullCircle.mesh; nodeRoad.transform.parent = gameObject.transform; nodeRoad.transform.localPosition = Vector3.zero; - this.position = position; - this.config = config; update(); + return this; } public Node getOther(Node caller) { diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index ce92eb6..b6546a2 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -1462,6 +1462,7 @@ m_Name: m_EditorClassIdentifier: config: {fileID: 128843415} + roads: {fileID: 1714391592} --- !u!114 &1812881470 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Buttons/LoadButton.cs b/Assets/Scripts/Buttons/LoadButton.cs index a9044e1..11c32e8 100644 --- a/Assets/Scripts/Buttons/LoadButton.cs +++ b/Assets/Scripts/Buttons/LoadButton.cs @@ -10,6 +10,7 @@ public class LoadButton : MonoBehaviour, IPointerDownHandler { public Config config; + public Transform roads; public void OnPointerDown(PointerEventData eventData) { config.onClick(); @@ -20,7 +21,27 @@ config.roadNetwork.clear(); string fileContent = File.ReadAllText(filePath); SaveStruct saveData = JsonUtility.FromJson(fileContent); - // todo: put the saved nodes and nodes into the world - print(saveData.nodes.Count); + List nodes = new List(); + List<(SpawnNodeData, List)> spawnNodeData = new List<(SpawnNodeData, List)>(); + foreach (SaveNode saveNode in saveData.nodes) { + Node node = null; + if (saveNode.type == "spawn") { + node = new SpawnNode(saveNode.position, roads, config).init(); + spawnNodeData.Add((node.nodeData as SpawnNodeData, saveNode.targets)); + } else if (saveNode.type == "exit") { + node = new ExitNode(saveNode.position, roads, config).init(); + } else { + node = new CustomNode(saveNode.position, roads, config).init(); + } + nodes.Add(node); + } + foreach ((SpawnNodeData, List) pair in spawnNodeData) { + SpawnNodeData nodeData = pair.Item1; + List targets = pair.Item2; + foreach (int target in targets) { + nodeData.targets.Add(nodes[target].nodeData as ExitNodeData); + } + } + // todo: generate roads } } diff --git a/Assets/Scripts/Buttons/SaveStructs.cs b/Assets/Scripts/Buttons/SaveStructs.cs index 4596487..6a0bc86 100644 --- a/Assets/Scripts/Buttons/SaveStructs.cs +++ b/Assets/Scripts/Buttons/SaveStructs.cs @@ -7,6 +7,7 @@ public struct SaveNode { public Vector3 position; public string type; + public List targets; } [Serializable] diff --git a/Assets/Scripts/Roads/Node/ExitNodeData.cs b/Assets/Scripts/Roads/Node/ExitNodeData.cs index c634426..5cbef39 100644 --- a/Assets/Scripts/Roads/Node/ExitNodeData.cs +++ b/Assets/Scripts/Roads/Node/ExitNodeData.cs @@ -2,14 +2,5 @@ using System.Collections.Generic; using UnityEngine; -public class ExitNodeData: MonoBehaviour { - public ExitNode node; - public Config config; - - void Start() { - node = new ExitNode(transform.position, transform, config); - } - - void Update() { - } +public class ExitNodeData : NodeData { } diff --git a/Assets/Scripts/Roads/Node/Node.cs b/Assets/Scripts/Roads/Node/Node.cs index 7eda931..aa211d1 100644 --- a/Assets/Scripts/Roads/Node/Node.cs +++ b/Assets/Scripts/Roads/Node/Node.cs @@ -11,14 +11,24 @@ public GameObject gameObject; public Vector3 direction; public Config config; + private Transform parent; + public NodeData nodeData; public Node(Vector3 position, Transform parent, Config config) { + this.position = position; + this.parent = parent; + this.config = config; + } + + public Node init() where T: NodeData { gameObject = new GameObject(); gameObject.transform.position = position; gameObject.transform.parent = parent; gameObject.AddComponent().radius = 1f; gameObject.layer = 7; - gameObject.AddComponent().node = this; + nodeData = gameObject.AddComponent(); + nodeData.node = this; + nodeData.config = config; GameObject nodeCircle = new GameObject(); nodeCircle.AddComponent().material = config.roadEditMaterial; @@ -31,9 +41,8 @@ nodeRoad.AddComponent().mesh = fullCircle.mesh; nodeRoad.transform.parent = gameObject.transform; nodeRoad.transform.localPosition = Vector3.zero; - this.position = position; - this.config = config; update(); + return this; } public Node getOther(Node caller) { diff --git a/Assets/Scripts/Roads/Node/NodeData.cs b/Assets/Scripts/Roads/Node/NodeData.cs index 3cb52b8..24b2c47 100644 --- a/Assets/Scripts/Roads/Node/NodeData.cs +++ b/Assets/Scripts/Roads/Node/NodeData.cs @@ -4,6 +4,7 @@ public class NodeData : MonoBehaviour { public Node node = null; + public Config config; public virtual void Start() { node.config.roadNetwork.nodes.Add(node); diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index ce92eb6..b6546a2 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -1462,6 +1462,7 @@ m_Name: m_EditorClassIdentifier: config: {fileID: 128843415} + roads: {fileID: 1714391592} --- !u!114 &1812881470 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Buttons/LoadButton.cs b/Assets/Scripts/Buttons/LoadButton.cs index a9044e1..11c32e8 100644 --- a/Assets/Scripts/Buttons/LoadButton.cs +++ b/Assets/Scripts/Buttons/LoadButton.cs @@ -10,6 +10,7 @@ public class LoadButton : MonoBehaviour, IPointerDownHandler { public Config config; + public Transform roads; public void OnPointerDown(PointerEventData eventData) { config.onClick(); @@ -20,7 +21,27 @@ config.roadNetwork.clear(); string fileContent = File.ReadAllText(filePath); SaveStruct saveData = JsonUtility.FromJson(fileContent); - // todo: put the saved nodes and nodes into the world - print(saveData.nodes.Count); + List nodes = new List(); + List<(SpawnNodeData, List)> spawnNodeData = new List<(SpawnNodeData, List)>(); + foreach (SaveNode saveNode in saveData.nodes) { + Node node = null; + if (saveNode.type == "spawn") { + node = new SpawnNode(saveNode.position, roads, config).init(); + spawnNodeData.Add((node.nodeData as SpawnNodeData, saveNode.targets)); + } else if (saveNode.type == "exit") { + node = new ExitNode(saveNode.position, roads, config).init(); + } else { + node = new CustomNode(saveNode.position, roads, config).init(); + } + nodes.Add(node); + } + foreach ((SpawnNodeData, List) pair in spawnNodeData) { + SpawnNodeData nodeData = pair.Item1; + List targets = pair.Item2; + foreach (int target in targets) { + nodeData.targets.Add(nodes[target].nodeData as ExitNodeData); + } + } + // todo: generate roads } } diff --git a/Assets/Scripts/Buttons/SaveStructs.cs b/Assets/Scripts/Buttons/SaveStructs.cs index 4596487..6a0bc86 100644 --- a/Assets/Scripts/Buttons/SaveStructs.cs +++ b/Assets/Scripts/Buttons/SaveStructs.cs @@ -7,6 +7,7 @@ public struct SaveNode { public Vector3 position; public string type; + public List targets; } [Serializable] diff --git a/Assets/Scripts/Roads/Node/ExitNodeData.cs b/Assets/Scripts/Roads/Node/ExitNodeData.cs index c634426..5cbef39 100644 --- a/Assets/Scripts/Roads/Node/ExitNodeData.cs +++ b/Assets/Scripts/Roads/Node/ExitNodeData.cs @@ -2,14 +2,5 @@ using System.Collections.Generic; using UnityEngine; -public class ExitNodeData: MonoBehaviour { - public ExitNode node; - public Config config; - - void Start() { - node = new ExitNode(transform.position, transform, config); - } - - void Update() { - } +public class ExitNodeData : NodeData { } diff --git a/Assets/Scripts/Roads/Node/Node.cs b/Assets/Scripts/Roads/Node/Node.cs index 7eda931..aa211d1 100644 --- a/Assets/Scripts/Roads/Node/Node.cs +++ b/Assets/Scripts/Roads/Node/Node.cs @@ -11,14 +11,24 @@ public GameObject gameObject; public Vector3 direction; public Config config; + private Transform parent; + public NodeData nodeData; public Node(Vector3 position, Transform parent, Config config) { + this.position = position; + this.parent = parent; + this.config = config; + } + + public Node init() where T: NodeData { gameObject = new GameObject(); gameObject.transform.position = position; gameObject.transform.parent = parent; gameObject.AddComponent().radius = 1f; gameObject.layer = 7; - gameObject.AddComponent().node = this; + nodeData = gameObject.AddComponent(); + nodeData.node = this; + nodeData.config = config; GameObject nodeCircle = new GameObject(); nodeCircle.AddComponent().material = config.roadEditMaterial; @@ -31,9 +41,8 @@ nodeRoad.AddComponent().mesh = fullCircle.mesh; nodeRoad.transform.parent = gameObject.transform; nodeRoad.transform.localPosition = Vector3.zero; - this.position = position; - this.config = config; update(); + return this; } public Node getOther(Node caller) { diff --git a/Assets/Scripts/Roads/Node/NodeData.cs b/Assets/Scripts/Roads/Node/NodeData.cs index 3cb52b8..24b2c47 100644 --- a/Assets/Scripts/Roads/Node/NodeData.cs +++ b/Assets/Scripts/Roads/Node/NodeData.cs @@ -4,6 +4,7 @@ public class NodeData : MonoBehaviour { public Node node = null; + public Config config; public virtual void Start() { node.config.roadNetwork.nodes.Add(node); diff --git a/Assets/Scripts/Roads/Node/SpawnNodeData.cs b/Assets/Scripts/Roads/Node/SpawnNodeData.cs index 50baffb..6089504 100644 --- a/Assets/Scripts/Roads/Node/SpawnNodeData.cs +++ b/Assets/Scripts/Roads/Node/SpawnNodeData.cs @@ -3,20 +3,18 @@ using UnityEngine; public class SpawnNodeData : NodeData { - public Config config; - public ExitNodeData[] targets; + public List targets = new List(); public float timeLeft = 0f, interval = 5f; override public void Start() { - node = new SpawnNode(transform.position, transform, config); base.Start(); } void Update() { timeLeft -= Time.deltaTime; - if (timeLeft <= 0f) { + if (timeLeft <= 0f && targets.Count > 0) { timeLeft = interval; - ExitNodeData target = targets[Random.Range(0, targets.Length)]; + ExitNodeData target = targets[Random.Range(0, targets.Count)]; Route route = new Route(node, target.node); if (route.isValid && route.roads[0].carsOnRoute.Count == 0) { new Car(route, transform, config); diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index ce92eb6..b6546a2 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -1462,6 +1462,7 @@ m_Name: m_EditorClassIdentifier: config: {fileID: 128843415} + roads: {fileID: 1714391592} --- !u!114 &1812881470 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Buttons/LoadButton.cs b/Assets/Scripts/Buttons/LoadButton.cs index a9044e1..11c32e8 100644 --- a/Assets/Scripts/Buttons/LoadButton.cs +++ b/Assets/Scripts/Buttons/LoadButton.cs @@ -10,6 +10,7 @@ public class LoadButton : MonoBehaviour, IPointerDownHandler { public Config config; + public Transform roads; public void OnPointerDown(PointerEventData eventData) { config.onClick(); @@ -20,7 +21,27 @@ config.roadNetwork.clear(); string fileContent = File.ReadAllText(filePath); SaveStruct saveData = JsonUtility.FromJson(fileContent); - // todo: put the saved nodes and nodes into the world - print(saveData.nodes.Count); + List nodes = new List(); + List<(SpawnNodeData, List)> spawnNodeData = new List<(SpawnNodeData, List)>(); + foreach (SaveNode saveNode in saveData.nodes) { + Node node = null; + if (saveNode.type == "spawn") { + node = new SpawnNode(saveNode.position, roads, config).init(); + spawnNodeData.Add((node.nodeData as SpawnNodeData, saveNode.targets)); + } else if (saveNode.type == "exit") { + node = new ExitNode(saveNode.position, roads, config).init(); + } else { + node = new CustomNode(saveNode.position, roads, config).init(); + } + nodes.Add(node); + } + foreach ((SpawnNodeData, List) pair in spawnNodeData) { + SpawnNodeData nodeData = pair.Item1; + List targets = pair.Item2; + foreach (int target in targets) { + nodeData.targets.Add(nodes[target].nodeData as ExitNodeData); + } + } + // todo: generate roads } } diff --git a/Assets/Scripts/Buttons/SaveStructs.cs b/Assets/Scripts/Buttons/SaveStructs.cs index 4596487..6a0bc86 100644 --- a/Assets/Scripts/Buttons/SaveStructs.cs +++ b/Assets/Scripts/Buttons/SaveStructs.cs @@ -7,6 +7,7 @@ public struct SaveNode { public Vector3 position; public string type; + public List targets; } [Serializable] diff --git a/Assets/Scripts/Roads/Node/ExitNodeData.cs b/Assets/Scripts/Roads/Node/ExitNodeData.cs index c634426..5cbef39 100644 --- a/Assets/Scripts/Roads/Node/ExitNodeData.cs +++ b/Assets/Scripts/Roads/Node/ExitNodeData.cs @@ -2,14 +2,5 @@ using System.Collections.Generic; using UnityEngine; -public class ExitNodeData: MonoBehaviour { - public ExitNode node; - public Config config; - - void Start() { - node = new ExitNode(transform.position, transform, config); - } - - void Update() { - } +public class ExitNodeData : NodeData { } diff --git a/Assets/Scripts/Roads/Node/Node.cs b/Assets/Scripts/Roads/Node/Node.cs index 7eda931..aa211d1 100644 --- a/Assets/Scripts/Roads/Node/Node.cs +++ b/Assets/Scripts/Roads/Node/Node.cs @@ -11,14 +11,24 @@ public GameObject gameObject; public Vector3 direction; public Config config; + private Transform parent; + public NodeData nodeData; public Node(Vector3 position, Transform parent, Config config) { + this.position = position; + this.parent = parent; + this.config = config; + } + + public Node init() where T: NodeData { gameObject = new GameObject(); gameObject.transform.position = position; gameObject.transform.parent = parent; gameObject.AddComponent().radius = 1f; gameObject.layer = 7; - gameObject.AddComponent().node = this; + nodeData = gameObject.AddComponent(); + nodeData.node = this; + nodeData.config = config; GameObject nodeCircle = new GameObject(); nodeCircle.AddComponent().material = config.roadEditMaterial; @@ -31,9 +41,8 @@ nodeRoad.AddComponent().mesh = fullCircle.mesh; nodeRoad.transform.parent = gameObject.transform; nodeRoad.transform.localPosition = Vector3.zero; - this.position = position; - this.config = config; update(); + return this; } public Node getOther(Node caller) { diff --git a/Assets/Scripts/Roads/Node/NodeData.cs b/Assets/Scripts/Roads/Node/NodeData.cs index 3cb52b8..24b2c47 100644 --- a/Assets/Scripts/Roads/Node/NodeData.cs +++ b/Assets/Scripts/Roads/Node/NodeData.cs @@ -4,6 +4,7 @@ public class NodeData : MonoBehaviour { public Node node = null; + public Config config; public virtual void Start() { node.config.roadNetwork.nodes.Add(node); diff --git a/Assets/Scripts/Roads/Node/SpawnNodeData.cs b/Assets/Scripts/Roads/Node/SpawnNodeData.cs index 50baffb..6089504 100644 --- a/Assets/Scripts/Roads/Node/SpawnNodeData.cs +++ b/Assets/Scripts/Roads/Node/SpawnNodeData.cs @@ -3,20 +3,18 @@ using UnityEngine; public class SpawnNodeData : NodeData { - public Config config; - public ExitNodeData[] targets; + public List targets = new List(); public float timeLeft = 0f, interval = 5f; override public void Start() { - node = new SpawnNode(transform.position, transform, config); base.Start(); } void Update() { timeLeft -= Time.deltaTime; - if (timeLeft <= 0f) { + if (timeLeft <= 0f && targets.Count > 0) { timeLeft = interval; - ExitNodeData target = targets[Random.Range(0, targets.Length)]; + ExitNodeData target = targets[Random.Range(0, targets.Count)]; Route route = new Route(node, target.node); if (route.isValid && route.roads[0].carsOnRoute.Count == 0) { new Car(route, transform, config); diff --git a/Assets/Scripts/Roads/RoadNetwork.cs b/Assets/Scripts/Roads/RoadNetwork.cs index 888aa5b..b098bfe 100644 --- a/Assets/Scripts/Roads/RoadNetwork.cs +++ b/Assets/Scripts/Roads/RoadNetwork.cs @@ -9,7 +9,6 @@ public List roads = new List(); public void clear() { - foreach (Road road in roads) { road.delete(); } diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index ce92eb6..b6546a2 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -1462,6 +1462,7 @@ m_Name: m_EditorClassIdentifier: config: {fileID: 128843415} + roads: {fileID: 1714391592} --- !u!114 &1812881470 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Buttons/LoadButton.cs b/Assets/Scripts/Buttons/LoadButton.cs index a9044e1..11c32e8 100644 --- a/Assets/Scripts/Buttons/LoadButton.cs +++ b/Assets/Scripts/Buttons/LoadButton.cs @@ -10,6 +10,7 @@ public class LoadButton : MonoBehaviour, IPointerDownHandler { public Config config; + public Transform roads; public void OnPointerDown(PointerEventData eventData) { config.onClick(); @@ -20,7 +21,27 @@ config.roadNetwork.clear(); string fileContent = File.ReadAllText(filePath); SaveStruct saveData = JsonUtility.FromJson(fileContent); - // todo: put the saved nodes and nodes into the world - print(saveData.nodes.Count); + List nodes = new List(); + List<(SpawnNodeData, List)> spawnNodeData = new List<(SpawnNodeData, List)>(); + foreach (SaveNode saveNode in saveData.nodes) { + Node node = null; + if (saveNode.type == "spawn") { + node = new SpawnNode(saveNode.position, roads, config).init(); + spawnNodeData.Add((node.nodeData as SpawnNodeData, saveNode.targets)); + } else if (saveNode.type == "exit") { + node = new ExitNode(saveNode.position, roads, config).init(); + } else { + node = new CustomNode(saveNode.position, roads, config).init(); + } + nodes.Add(node); + } + foreach ((SpawnNodeData, List) pair in spawnNodeData) { + SpawnNodeData nodeData = pair.Item1; + List targets = pair.Item2; + foreach (int target in targets) { + nodeData.targets.Add(nodes[target].nodeData as ExitNodeData); + } + } + // todo: generate roads } } diff --git a/Assets/Scripts/Buttons/SaveStructs.cs b/Assets/Scripts/Buttons/SaveStructs.cs index 4596487..6a0bc86 100644 --- a/Assets/Scripts/Buttons/SaveStructs.cs +++ b/Assets/Scripts/Buttons/SaveStructs.cs @@ -7,6 +7,7 @@ public struct SaveNode { public Vector3 position; public string type; + public List targets; } [Serializable] diff --git a/Assets/Scripts/Roads/Node/ExitNodeData.cs b/Assets/Scripts/Roads/Node/ExitNodeData.cs index c634426..5cbef39 100644 --- a/Assets/Scripts/Roads/Node/ExitNodeData.cs +++ b/Assets/Scripts/Roads/Node/ExitNodeData.cs @@ -2,14 +2,5 @@ using System.Collections.Generic; using UnityEngine; -public class ExitNodeData: MonoBehaviour { - public ExitNode node; - public Config config; - - void Start() { - node = new ExitNode(transform.position, transform, config); - } - - void Update() { - } +public class ExitNodeData : NodeData { } diff --git a/Assets/Scripts/Roads/Node/Node.cs b/Assets/Scripts/Roads/Node/Node.cs index 7eda931..aa211d1 100644 --- a/Assets/Scripts/Roads/Node/Node.cs +++ b/Assets/Scripts/Roads/Node/Node.cs @@ -11,14 +11,24 @@ public GameObject gameObject; public Vector3 direction; public Config config; + private Transform parent; + public NodeData nodeData; public Node(Vector3 position, Transform parent, Config config) { + this.position = position; + this.parent = parent; + this.config = config; + } + + public Node init() where T: NodeData { gameObject = new GameObject(); gameObject.transform.position = position; gameObject.transform.parent = parent; gameObject.AddComponent().radius = 1f; gameObject.layer = 7; - gameObject.AddComponent().node = this; + nodeData = gameObject.AddComponent(); + nodeData.node = this; + nodeData.config = config; GameObject nodeCircle = new GameObject(); nodeCircle.AddComponent().material = config.roadEditMaterial; @@ -31,9 +41,8 @@ nodeRoad.AddComponent().mesh = fullCircle.mesh; nodeRoad.transform.parent = gameObject.transform; nodeRoad.transform.localPosition = Vector3.zero; - this.position = position; - this.config = config; update(); + return this; } public Node getOther(Node caller) { diff --git a/Assets/Scripts/Roads/Node/NodeData.cs b/Assets/Scripts/Roads/Node/NodeData.cs index 3cb52b8..24b2c47 100644 --- a/Assets/Scripts/Roads/Node/NodeData.cs +++ b/Assets/Scripts/Roads/Node/NodeData.cs @@ -4,6 +4,7 @@ public class NodeData : MonoBehaviour { public Node node = null; + public Config config; public virtual void Start() { node.config.roadNetwork.nodes.Add(node); diff --git a/Assets/Scripts/Roads/Node/SpawnNodeData.cs b/Assets/Scripts/Roads/Node/SpawnNodeData.cs index 50baffb..6089504 100644 --- a/Assets/Scripts/Roads/Node/SpawnNodeData.cs +++ b/Assets/Scripts/Roads/Node/SpawnNodeData.cs @@ -3,20 +3,18 @@ using UnityEngine; public class SpawnNodeData : NodeData { - public Config config; - public ExitNodeData[] targets; + public List targets = new List(); public float timeLeft = 0f, interval = 5f; override public void Start() { - node = new SpawnNode(transform.position, transform, config); base.Start(); } void Update() { timeLeft -= Time.deltaTime; - if (timeLeft <= 0f) { + if (timeLeft <= 0f && targets.Count > 0) { timeLeft = interval; - ExitNodeData target = targets[Random.Range(0, targets.Length)]; + ExitNodeData target = targets[Random.Range(0, targets.Count)]; Route route = new Route(node, target.node); if (route.isValid && route.roads[0].carsOnRoute.Count == 0) { new Car(route, transform, config); diff --git a/Assets/Scripts/Roads/RoadNetwork.cs b/Assets/Scripts/Roads/RoadNetwork.cs index 888aa5b..b098bfe 100644 --- a/Assets/Scripts/Roads/RoadNetwork.cs +++ b/Assets/Scripts/Roads/RoadNetwork.cs @@ -9,7 +9,6 @@ public List roads = new List(); public void clear() { - foreach (Road road in roads) { road.delete(); } diff --git a/Assets/Scripts/Roads/Roads.cs b/Assets/Scripts/Roads/Roads.cs index ea6b2af..ec04f26 100644 --- a/Assets/Scripts/Roads/Roads.cs +++ b/Assets/Scripts/Roads/Roads.cs @@ -26,7 +26,7 @@ pullingNode = hit.transform.gameObject.GetComponent().node; return; } - pullingNode = new CustomNode(groundPosition, transform, config); + pullingNode = new CustomNode(groundPosition, transform, config).init(); } private void endRoad(Ray ray, Vector3 groundPosition) { @@ -35,7 +35,7 @@ if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { endNode = hit.transform.gameObject.GetComponent().node; } else { - endNode = new CustomNode(groundPosition, transform, config); + endNode = new CustomNode(groundPosition, transform, config).init(); } Road road = new Road(pullingNode, endNode, config); if (pullingNode.roads.Contains(road)) {