diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 0b9ae6c..a15a27f 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -199,6 +199,7 @@ m_Component: - component: {fileID: 128843416} - component: {fileID: 128843415} + - component: {fileID: 128843417} m_Layer: 0 m_Name: Config m_TagString: Untagged @@ -248,6 +249,23 @@ m_Father: {fileID: 0} m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &128843417 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 128843414} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: aebe2e02a7b13a742a3a7a9c6c691142, type: 3} + m_Name: + m_EditorClassIdentifier: + config: {fileID: 128843415} + time: 0 + mode: 0 + greenTime: 10 + redTime: 2 --- !u!1 &140411785 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 0b9ae6c..a15a27f 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -199,6 +199,7 @@ m_Component: - component: {fileID: 128843416} - component: {fileID: 128843415} + - component: {fileID: 128843417} m_Layer: 0 m_Name: Config m_TagString: Untagged @@ -248,6 +249,23 @@ m_Father: {fileID: 0} m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &128843417 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 128843414} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: aebe2e02a7b13a742a3a7a9c6c691142, type: 3} + m_Name: + m_EditorClassIdentifier: + config: {fileID: 128843415} + time: 0 + mode: 0 + greenTime: 10 + redTime: 2 --- !u!1 &140411785 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Roads/Node/NodeData.cs b/Assets/Scripts/Roads/Node/NodeData.cs index 5ed603e..3113b6f 100644 --- a/Assets/Scripts/Roads/Node/NodeData.cs +++ b/Assets/Scripts/Roads/Node/NodeData.cs @@ -22,6 +22,7 @@ if (customNode.lightPhase == 0) { node.circleObject.GetComponent().material = config.roadEditMaterial; node.textObject.GetComponent().text = ""; + customNode.isPassable = true; return; } if (customNode.isPassable) { diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 0b9ae6c..a15a27f 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -199,6 +199,7 @@ m_Component: - component: {fileID: 128843416} - component: {fileID: 128843415} + - component: {fileID: 128843417} m_Layer: 0 m_Name: Config m_TagString: Untagged @@ -248,6 +249,23 @@ m_Father: {fileID: 0} m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &128843417 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 128843414} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: aebe2e02a7b13a742a3a7a9c6c691142, type: 3} + m_Name: + m_EditorClassIdentifier: + config: {fileID: 128843415} + time: 0 + mode: 0 + greenTime: 10 + redTime: 2 --- !u!1 &140411785 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Roads/Node/NodeData.cs b/Assets/Scripts/Roads/Node/NodeData.cs index 5ed603e..3113b6f 100644 --- a/Assets/Scripts/Roads/Node/NodeData.cs +++ b/Assets/Scripts/Roads/Node/NodeData.cs @@ -22,6 +22,7 @@ if (customNode.lightPhase == 0) { node.circleObject.GetComponent().material = config.roadEditMaterial; node.textObject.GetComponent().text = ""; + customNode.isPassable = true; return; } if (customNode.isPassable) { diff --git a/Assets/Scripts/Roads/Roads.cs b/Assets/Scripts/Roads/Roads.cs index ded4420..983c56a 100644 --- a/Assets/Scripts/Roads/Roads.cs +++ b/Assets/Scripts/Roads/Roads.cs @@ -96,8 +96,7 @@ return; } drawMode = DrawMode.DragRoad; - customNode.lightPhase++; // todo: increment or decrement - customNode.isPassable = !customNode.isPassable; + customNode.lightPhase++; } else if (Input.GetAxis("Fire2") != 0.0f) { if (drawMode == DrawMode.DragRoad) { return; diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 0b9ae6c..a15a27f 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -199,6 +199,7 @@ m_Component: - component: {fileID: 128843416} - component: {fileID: 128843415} + - component: {fileID: 128843417} m_Layer: 0 m_Name: Config m_TagString: Untagged @@ -248,6 +249,23 @@ m_Father: {fileID: 0} m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &128843417 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 128843414} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: aebe2e02a7b13a742a3a7a9c6c691142, type: 3} + m_Name: + m_EditorClassIdentifier: + config: {fileID: 128843415} + time: 0 + mode: 0 + greenTime: 10 + redTime: 2 --- !u!1 &140411785 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Roads/Node/NodeData.cs b/Assets/Scripts/Roads/Node/NodeData.cs index 5ed603e..3113b6f 100644 --- a/Assets/Scripts/Roads/Node/NodeData.cs +++ b/Assets/Scripts/Roads/Node/NodeData.cs @@ -22,6 +22,7 @@ if (customNode.lightPhase == 0) { node.circleObject.GetComponent().material = config.roadEditMaterial; node.textObject.GetComponent().text = ""; + customNode.isPassable = true; return; } if (customNode.isPassable) { diff --git a/Assets/Scripts/Roads/Roads.cs b/Assets/Scripts/Roads/Roads.cs index ded4420..983c56a 100644 --- a/Assets/Scripts/Roads/Roads.cs +++ b/Assets/Scripts/Roads/Roads.cs @@ -96,8 +96,7 @@ return; } drawMode = DrawMode.DragRoad; - customNode.lightPhase++; // todo: increment or decrement - customNode.isPassable = !customNode.isPassable; + customNode.lightPhase++; } else if (Input.GetAxis("Fire2") != 0.0f) { if (drawMode == DrawMode.DragRoad) { return; diff --git a/Assets/Scripts/TrafficLights.cs b/Assets/Scripts/TrafficLights.cs new file mode 100644 index 0000000..5088bc0 --- /dev/null +++ b/Assets/Scripts/TrafficLights.cs @@ -0,0 +1,54 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public enum TrafficLightMode { + Green, + Red, +} + +public class TrafficLights : MonoBehaviour { + public Config config; + public float time; + public TrafficLightMode mode = TrafficLightMode.Green; + public float greenTime = 10f; + public float redTime = 2f; + + private void handleTurnGreen(List nodes) { + foreach (CustomNode node in nodes) { + // todo: select only correct nodes + node.isPassable = true; + } + + } + + private void handleTurnRed(List nodes) { + foreach (CustomNode node in nodes) { + node.isPassable = false; + } + } + + void Update() { + time -= Time.deltaTime; + if (time <= 0f) { + List nodes = new List(); + foreach (Node node in config.roadNetwork.nodes) { + if (node is CustomNode) { + CustomNode customNode = (CustomNode) node; + if (customNode.lightPhase != 0) { + nodes.Add(customNode); + } + } + } + if (mode == TrafficLightMode.Green) { + mode = TrafficLightMode.Red; + time += redTime; + handleTurnRed(nodes); + } else { + mode = TrafficLightMode.Green; + time += greenTime; + handleTurnGreen(nodes); + } + } + } +}