diff --git a/Assets/Prefab/Node.prefab b/Assets/Prefab/Node.prefab index c238da9..a32a028 100644 --- a/Assets/Prefab/Node.prefab +++ b/Assets/Prefab/Node.prefab @@ -63,4 +63,4 @@ CircleCollider2D: m_UsedByComposite: 0 m_Offset: {x: 0, y: 0} serializedVersion: 2 - m_Radius: 0.06 + m_Radius: 0.25 diff --git a/Assets/Prefab/OneWayStreet.prefab b/Assets/Prefab/OneWayStreet.prefab index 24f70c9..9543f22 100644 --- a/Assets/Prefab/OneWayStreet.prefab +++ b/Assets/Prefab/OneWayStreet.prefab @@ -12,7 +12,7 @@ GameObject: - component: {fileID: 4373278067013931366} m_Layer: 0 m_Name: OneWayStreet - m_TagString: Untagged + m_TagString: Street m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 @@ -57,10 +57,10 @@ GameObject: - component: {fileID: 3507984409357578320} - component: {fileID: 3507984409357578321} - component: {fileID: 5127064513516543494} - - component: {fileID: 1253709196921217761} + - component: {fileID: 2702616802704404467} m_Layer: 0 m_Name: streetObject - m_TagString: Untagged + m_TagString: Street m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 @@ -144,8 +144,9 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: b794f37801ab66d4496dd16ee2294dee, type: 3} m_Name: m_EditorClassIdentifier: ---- !u!61 &1253709196921217761 -BoxCollider2D: + nodePrefab: {fileID: 487205225613997102, guid: def66282a1c498e4c906a02f6bbd214e, type: 3} +--- !u!70 &2702616802704404467 +CapsuleCollider2D: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} m_PrefabInstance: {fileID: 0} @@ -158,15 +159,5 @@ BoxCollider2D: m_UsedByEffector: 0 m_UsedByComposite: 0 m_Offset: {x: 0, y: 0} - m_SpriteTilingProperty: - border: {x: 0, y: 0, z: 0, w: 0} - pivot: {x: 0.5, y: 0.5} - oldSize: {x: 1, y: 1} - newSize: {x: 1, y: 1} - adaptiveTilingThreshold: 0.5 - drawMode: 0 - adaptiveTiling: 0 - m_AutoTiling: 0 - serializedVersion: 2 m_Size: {x: 1, y: 1} - m_EdgeRadius: 0 + m_Direction: 1 diff --git a/Assets/Prefab/OneWayStreetPO.prefab b/Assets/Prefab/OneWayStreetPSO.prefab similarity index 90% rename from Assets/Prefab/OneWayStreetPO.prefab rename to Assets/Prefab/OneWayStreetPSO.prefab index 446a8a5..41b3247 100644 --- a/Assets/Prefab/OneWayStreetPO.prefab +++ b/Assets/Prefab/OneWayStreetPSO.prefab @@ -9,9 +9,9 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 4378804637319760054} - - component: {fileID: 6750555896683950434} + - component: {fileID: 8711048124583188406} m_Layer: 0 - m_Name: OneWayStreetPO + m_Name: OneWayStreetPSO m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 @@ -33,7 +33,7 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!114 &6750555896683950434 +--- !u!114 &8711048124583188406 MonoBehaviour: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} @@ -57,9 +57,9 @@ GameObject: m_Component: - component: {fileID: 4378804637511363665} - component: {fileID: 4378804637511363664} - - component: {fileID: 8629378905046913581} - component: {fileID: 2156778682419509481} - - component: {fileID: 15892159919980060} + - component: {fileID: 468972932912767097} + - component: {fileID: 8159869123926418090} m_Layer: 0 m_Name: streetObject m_TagString: Untagged @@ -134,19 +134,6 @@ SpriteRenderer: m_WasSpriteAssigned: 1 m_MaskInteraction: 0 m_SpriteSortPoint: 0 ---- !u!114 &8629378905046913581 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 4378804637511363667} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 69d68f14c2a51dc4d94160eafca27a7e, type: 3} - m_Name: - m_EditorClassIdentifier: - placeable: 1 --- !u!50 &2156778682419509481 Rigidbody2D: serializedVersion: 4 @@ -168,8 +155,21 @@ Rigidbody2D: m_SleepingMode: 1 m_CollisionDetection: 1 m_Constraints: 7 ---- !u!61 &15892159919980060 -BoxCollider2D: +--- !u!114 &468972932912767097 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4378804637511363667} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 69d68f14c2a51dc4d94160eafca27a7e, type: 3} + m_Name: + m_EditorClassIdentifier: + placeable: 1 +--- !u!70 &8159869123926418090 +CapsuleCollider2D: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} m_PrefabInstance: {fileID: 0} @@ -182,15 +182,5 @@ BoxCollider2D: m_UsedByEffector: 0 m_UsedByComposite: 0 m_Offset: {x: 0, y: 0} - m_SpriteTilingProperty: - border: {x: 0, y: 0, z: 0, w: 0} - pivot: {x: 0.5, y: 0.5} - oldSize: {x: 1, y: 1} - newSize: {x: 1, y: 1} - adaptiveTilingThreshold: 0.5 - drawMode: 0 - adaptiveTiling: 0 - m_AutoTiling: 0 - serializedVersion: 2 m_Size: {x: 1, y: 1} - m_EdgeRadius: 0 + m_Direction: 1 diff --git a/Assets/Prefab/OneWayStreetPO.prefab.meta b/Assets/Prefab/OneWayStreetPSO.prefab.meta similarity index 100% rename from Assets/Prefab/OneWayStreetPO.prefab.meta rename to Assets/Prefab/OneWayStreetPSO.prefab.meta diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 96e30fa..eef447e 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -123,6 +123,67 @@ NavMeshSettings: debug: m_Flags: 0 m_NavMeshData: {fileID: 0} +--- !u!1001 &249703840 +PrefabInstance: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + m_TransformParent: {fileID: 0} + m_Modifications: + - target: {fileID: 3507984408736830881, guid: e5756f8beb5c58842922847cb8e0d875, type: 3} + propertyPath: m_Name + value: OneWayStreet + objectReference: {fileID: 0} + - target: {fileID: 3507984408736830887, guid: e5756f8beb5c58842922847cb8e0d875, type: 3} + propertyPath: m_RootOrder + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 3507984408736830887, guid: e5756f8beb5c58842922847cb8e0d875, type: 3} + propertyPath: m_LocalScale.x + value: 11 + objectReference: {fileID: 0} + - target: {fileID: 3507984408736830887, guid: e5756f8beb5c58842922847cb8e0d875, type: 3} + propertyPath: m_LocalPosition.x + value: -8.3493 + objectReference: {fileID: 0} + - target: {fileID: 3507984408736830887, guid: e5756f8beb5c58842922847cb8e0d875, type: 3} + propertyPath: m_LocalPosition.y + value: 1.6627126 + objectReference: {fileID: 0} + - target: {fileID: 3507984408736830887, guid: e5756f8beb5c58842922847cb8e0d875, type: 3} + propertyPath: m_LocalPosition.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 3507984408736830887, guid: e5756f8beb5c58842922847cb8e0d875, type: 3} + propertyPath: m_LocalRotation.w + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 3507984408736830887, guid: e5756f8beb5c58842922847cb8e0d875, type: 3} + propertyPath: m_LocalRotation.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 3507984408736830887, guid: e5756f8beb5c58842922847cb8e0d875, type: 3} + propertyPath: m_LocalRotation.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 3507984408736830887, guid: e5756f8beb5c58842922847cb8e0d875, type: 3} + propertyPath: m_LocalRotation.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 3507984408736830887, guid: e5756f8beb5c58842922847cb8e0d875, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 3507984408736830887, guid: e5756f8beb5c58842922847cb8e0d875, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 3507984408736830887, guid: e5756f8beb5c58842922847cb8e0d875, type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: 0 + objectReference: {fileID: 0} + m_RemovedComponents: [] + m_SourcePrefab: {fileID: 100100000, guid: e5756f8beb5c58842922847cb8e0d875, type: 3} --- !u!1 &519420028 GameObject: m_ObjectHideFlags: 0 @@ -205,7 +266,7 @@ Transform: m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 1 + m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1432161433 GameObject: @@ -304,7 +365,7 @@ Transform: m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 2 + m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1992284311 GameObject: @@ -367,7 +428,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: nodePrefab: {fileID: 487205225613997102, guid: def66282a1c498e4c906a02f6bbd214e, type: 3} - oneWayStreetPO: {fileID: 4378804637319760041, guid: 28a297c5a2d61c142b7389275670314c, type: 3} + oneWayStreetPSO: {fileID: 4378804637319760041, guid: 28a297c5a2d61c142b7389275670314c, type: 3} oneWayStreet: {fileID: 3507984408736830881, guid: e5756f8beb5c58842922847cb8e0d875, type: 3} --- !u!114 &1992284315 MonoBehaviour: diff --git a/Assets/Scripts/BuildingSystem/NodePlacer.cs b/Assets/Scripts/BuildingSystem/NodePlacer.cs index 612fdb8..8e77fc0 100644 --- a/Assets/Scripts/BuildingSystem/NodePlacer.cs +++ b/Assets/Scripts/BuildingSystem/NodePlacer.cs @@ -13,12 +13,10 @@ public class NodePlacer : MonoBehaviour { if (Input.GetKeyDown(KeyCode.N)) { - Debug.Log("H2213123IT"); RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); if (hit.collider != null) { - Debug.Log("HIT"); selectedPlacedNode = hit.collider.transform.gameObject.GetComponent(); } else diff --git a/Assets/Scripts/BuildingSystem/StreetBuilding.cs b/Assets/Scripts/BuildingSystem/StreetBuilding.cs index 5a6b79e..48aaae5 100644 --- a/Assets/Scripts/BuildingSystem/StreetBuilding.cs +++ b/Assets/Scripts/BuildingSystem/StreetBuilding.cs @@ -3,14 +3,15 @@ using UnityEngine; public class StreetBuilding : MonoBehaviour { - GameObject currentStreetPO; + GameObject currentStreetPSO; Vector3 startPosition; + private void Update() { if (Input.GetKeyDown(KeyCode.H)) { - if (currentStreetPO == null) + if (currentStreetPSO == null) { StartBuilding(); } @@ -20,36 +21,41 @@ public class StreetBuilding : MonoBehaviour } } - if (currentStreetPO != null && Input.GetKeyDown(KeyCode.J)) + if (currentStreetPSO != null) { - if (currentStreetPO.GetComponent().placeable) + if (Input.GetKeyDown(KeyCode.J)) { - PlaceBuilding(); + if (currentStreetPSO.GetComponent().placeable) + { + PlaceBuilding(); + } } - } } + private void StartBuilding() { startPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); startPosition.z = 0; - currentStreetPO = Instantiate(PrefabDictionary.instance.oneWayStreetPO, startPosition, Quaternion.identity); + currentStreetPSO = Instantiate(PrefabDictionary.instance.oneWayStreetPSO, startPosition, Quaternion.identity); } private void StopBuilding() { - Destroy(currentStreetPO); - currentStreetPO = null; + Destroy(currentStreetPSO); + currentStreetPSO = null; startPosition = Vector3.zero; } private void PlaceBuilding() { - Transform placedTransform = currentStreetPO.transform; + Transform placedTransform = currentStreetPSO.transform; - Instantiate(PrefabDictionary.instance.oneWayStreet, placedTransform.position, placedTransform.rotation).transform.localScale = placedTransform.localScale; + GameObject newStreet = Instantiate(PrefabDictionary.instance.oneWayStreet, placedTransform.position, placedTransform.rotation); + newStreet.transform.localScale = placedTransform.localScale; + newStreet.GetComponent().Place(); StopBuilding(); } diff --git a/Assets/Scripts/PlaceableObject.cs b/Assets/Scripts/PlaceableObject.cs deleted file mode 100644 index 1483a2a..0000000 --- a/Assets/Scripts/PlaceableObject.cs +++ /dev/null @@ -1,38 +0,0 @@ -using UnityEngine; - -public class PlaceableObject : MonoBehaviour -{ - public GameObject childObject; - public bool placeable = true; - - - private void Update() - { - ScaleOnMousePosition(); - RotateToMousePosition(); - - placeable = childObject.GetComponent().placeable; - } - - float AngleBetweenTwoPoints(Vector3 a, Vector3 b) - { - return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg; - } - - void RotateToMousePosition() - { - Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); - mousePosition.z = 0; - float angle = AngleBetweenTwoPoints(transform.position, mousePosition); - transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, angle)); - } - void ScaleOnMousePosition() - { - Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); - mousePosition.z = 0; - - transform.localScale = new Vector3(-Vector3.Distance(mousePosition, transform.position), 1, 1); - } - - -} diff --git a/Assets/Scripts/PlaceableObjectChild.cs b/Assets/Scripts/PlaceableObjectChild.cs deleted file mode 100644 index 03afe10..0000000 --- a/Assets/Scripts/PlaceableObjectChild.cs +++ /dev/null @@ -1,31 +0,0 @@ -using UnityEngine; - -public class PlaceableObjectChild : MonoBehaviour -{ - - public bool placeable = true; - - private void Update() - { - transform.localPosition = new Vector3(0.5f, 0f, 0f); - if (placeable) - { - GetComponent().color = Color.blue; - } - else - { - GetComponent().color = Color.red; - } - } - - private void OnCollisionStay2D(Collision2D collision) - { - placeable = false; - - } - - private void OnCollisionExit2D(Collision2D collision) - { - placeable = true; - } -} diff --git a/Assets/Scripts/PlaceableStreetObject.cs b/Assets/Scripts/PlaceableStreetObject.cs new file mode 100644 index 0000000..be4358d --- /dev/null +++ b/Assets/Scripts/PlaceableStreetObject.cs @@ -0,0 +1,89 @@ +using UnityEngine; + +public class PlaceableStreetObject : MonoBehaviour +{ + public GameObject childObject; + public bool placeable = true; + TrafficNode snappingNode; + + private void Update() + { + UpdateSnappingNode(); + ScaleOnMousePosition(); + RotateToMousePosition(); + placeable = childObject.GetComponent().placeable; + } + + float AngleBetweenTwoPoints(Vector3 a, Vector3 b) + { + return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg; + } + + private void UpdateSnappingNode() + { + Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); + RaycastHit2D[] hits = Physics2D.RaycastAll(mousePosition, Vector2.zero); + + + // Snapping + TrafficNode foundNode = null; + foreach (RaycastHit2D hit in hits) + { + if (hit.collider != null) + { + if (hit.collider.gameObject.tag == "TrafficNode") + { + foundNode = hit.collider.gameObject.GetComponent(); + } + } + } + + if (snappingNode != null) + { + snappingNode.gizmoSphereColor = Color.green; + } + + snappingNode = foundNode; + + if (snappingNode != null) + { + snappingNode.gizmoSphereColor = Color.red; + } + + childObject.GetComponent().snappingNode = snappingNode; + } + + + Vector3 GetEndPosition() + { + PlaceableStreetObjectChild child = childObject.GetComponent(); + Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); + + Vector3 endPosition = mousePosition; + + + // Snapping + if (snappingNode != null) + { + endPosition.x = snappingNode.gameObject.transform.position.x; + endPosition.y = snappingNode.gameObject.transform.position.y; + } + + endPosition.z = 0; + return endPosition; + } + + void RotateToMousePosition() + { + Vector3 endPosition = GetEndPosition(); + float angle = AngleBetweenTwoPoints(transform.position, endPosition); + transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, angle)); + } + void ScaleOnMousePosition() + { + Vector3 endPosition = GetEndPosition(); + transform.localScale = new Vector3(-Vector3.Distance(endPosition, transform.position), 1, 1); + } + + +} diff --git a/Assets/Scripts/PlaceableObject.cs.meta b/Assets/Scripts/PlaceableStreetObject.cs.meta similarity index 100% rename from Assets/Scripts/PlaceableObject.cs.meta rename to Assets/Scripts/PlaceableStreetObject.cs.meta diff --git a/Assets/Scripts/PlaceableStreetObjectChild.cs b/Assets/Scripts/PlaceableStreetObjectChild.cs new file mode 100644 index 0000000..a521784 --- /dev/null +++ b/Assets/Scripts/PlaceableStreetObjectChild.cs @@ -0,0 +1,95 @@ +using System.Collections.Generic; +using UnityEngine; + +public class PlaceableStreetObjectChild : MonoBehaviour +{ + + public bool placeable = true; + public List currentCollisions = new List(); + public TrafficNode snappingNode = null; + + private void Update() + { + transform.localPosition = new Vector3(0.5f, 0f, 0f); + CheckPlaceable(); + } + + private void LateUpdate() + { + if (placeable) + { + GetComponent().color = Color.blue; + } + else + { + GetComponent().color = Color.red; + } + } + + private void CheckPlaceable() + { + // DEBUG LOG + Debug.Log("--------------------------------------"); + foreach (Collision2D collision in currentCollisions) + { + Debug.Log(collision.gameObject.name); + } + + + + // If a snapping node is present -> its allowed to collide with all node of the SAME street + if (snappingNode != null) + { + bool foundBadCollision = false; + foreach (Collision2D collision in currentCollisions) + { + if (collision.collider) + { + if (collision.collider.transform.tag == "TrafficNode") + { + if (collision.collider.GetComponent().GetParentStreet() != snappingNode.GetParentStreet()) + { + foundBadCollision = true; + } + } + else if (collision.collider.transform.tag == "Street") + { + if (collision.collider.GetComponent() != snappingNode.GetParentStreet()) + { + foundBadCollision = true; + } + } + else + { + Debug.Log("No Street or node"); + foundBadCollision = true; + + } + } + else + { + Debug.Log("No collider"); + foundBadCollision = true; + } + } + placeable = !foundBadCollision; + } + else + { + if (currentCollisions.Count > 0) + { + placeable = false; + } + } + } + + private void OnCollisionEnter2D(Collision2D c) + { + currentCollisions.Add(c); + } + + private void OnCollisionExit2D(Collision2D c) + { + currentCollisions.Remove(c); + } +} diff --git a/Assets/Scripts/PlaceableObjectChild.cs.meta b/Assets/Scripts/PlaceableStreetObjectChild.cs.meta similarity index 100% rename from Assets/Scripts/PlaceableObjectChild.cs.meta rename to Assets/Scripts/PlaceableStreetObjectChild.cs.meta diff --git a/Assets/Scripts/PrefabDictionary.cs b/Assets/Scripts/PrefabDictionary.cs index fcc5635..7a81c24 100644 --- a/Assets/Scripts/PrefabDictionary.cs +++ b/Assets/Scripts/PrefabDictionary.cs @@ -4,7 +4,7 @@ public class PrefabDictionary : MonoBehaviour public static PrefabDictionary instance; public GameObject nodePrefab; - public GameObject oneWayStreetPO; + public GameObject oneWayStreetPSO; public GameObject oneWayStreet; void Awake() diff --git a/Assets/Scripts/TrafficSystem/Street.cs b/Assets/Scripts/TrafficSystem/Street.cs index 7816518..05e2659 100644 --- a/Assets/Scripts/TrafficSystem/Street.cs +++ b/Assets/Scripts/TrafficSystem/Street.cs @@ -3,23 +3,40 @@ using UnityEngine; public class Street : MonoBehaviour { + [SerializeField] + GameObject nodePrefab; + private List nodes = new List(); - private void Start() + public void Place() { - nodes.Add(Instantiate(PrefabDictionary.instance.nodePrefab, transform).GetComponent()); - nodes.Add(Instantiate(PrefabDictionary.instance.nodePrefab, transform).GetComponent()); - nodes[0].transform.localPosition = new Vector3(-0.5f, 0, 0); - nodes[1].transform.localPosition = new Vector3(0.5f, 0, 0); + CreateStartEndNodes(); - nodes[0].AddNextNode(nodes[1]); - /*CreateNode(new Vector3(0, 0, 0)); - CreateNode(new Vector3(-0.25f, 0, 0)); - RemoveNode(CreateNode(new Vector3(0.33f, 0, 0))); - CreateNode(new Vector3(-0.12f, 0, 0));*/ + float parentScaleX = Mathf.Abs(transform.parent.transform.localScale.x); + float nodeSize = 0.5f; + + int nodeCount = Mathf.RoundToInt(parentScaleX / nodeSize); + + int i = 1; // starts at 1 so the first gets ignored (start node already exists) + while (i < nodeCount) // NOT <= so endnode does not get created twice ^ + { + float xPosition = ((i * nodeSize) / parentScaleX) - 0.5f;// weird calc because street is child of object and always has scale of 1f .... + CreateNode(new Vector3(xPosition, 0, 0)); + i++; + } } + private void CreateStartEndNodes() + { + nodes.Add(Instantiate(nodePrefab, transform).GetComponent()); + nodes.Add(Instantiate(nodePrefab, transform).GetComponent()); + nodes[0].transform.localPosition = new Vector3(-0.5f, 0, 0); + nodes[1].transform.localPosition = new Vector3(0.5f, 0, 0); + nodes[0].SetParentStreet(this); + nodes[1].SetParentStreet(this); + nodes[0].AddNextNode(nodes[1]); + } private TrafficNode[] getNearestNodes(float positionX) { TrafficNode[] nearestNodes = new TrafficNode[2]; @@ -42,15 +59,22 @@ public class Street : MonoBehaviour return nearestNodes; } + // Dont create anything behind the endnode or before the startnode public TrafficNode CreateNode(Vector3 localPosition) { TrafficNode[] nearestNodes = getNearestNodes(localPosition.x); - TrafficNode newNode = Instantiate(PrefabDictionary.instance.nodePrefab, transform).GetComponent(); + TrafficNode newNode = Instantiate(nodePrefab, transform).GetComponent(); + newNode.SetParentStreet(this); newNode.transform.localPosition = localPosition; nearestNodes[0].RemoveNextNode(nearestNodes[1]); - nearestNodes[0].AddNextNode(newNode); + + if (nearestNodes[0] != nodes[nodes.Count - 1]) + { + nearestNodes[0].AddNextNode(newNode); + } + newNode.AddNextNode(nearestNodes[1]); nodes.Insert(nodes.IndexOf(nearestNodes[1]), newNode); return newNode; diff --git a/Assets/Scripts/TrafficSystem/Street.cs.meta b/Assets/Scripts/TrafficSystem/Street.cs.meta index b45a69d..9b8b960 100644 --- a/Assets/Scripts/TrafficSystem/Street.cs.meta +++ b/Assets/Scripts/TrafficSystem/Street.cs.meta @@ -3,7 +3,8 @@ guid: b794f37801ab66d4496dd16ee2294dee MonoImporter: externalObjects: {} serializedVersion: 2 - defaultReferences: [] + defaultReferences: + - nodePrefab: {fileID: 487205225613997102, guid: def66282a1c498e4c906a02f6bbd214e, type: 3} executionOrder: 0 icon: {instanceID: 0} userData: diff --git a/Assets/Scripts/TrafficSystem/StreetContainer.cs b/Assets/Scripts/TrafficSystem/StreetContainer.cs index e392684..76cac7a 100644 --- a/Assets/Scripts/TrafficSystem/StreetContainer.cs +++ b/Assets/Scripts/TrafficSystem/StreetContainer.cs @@ -3,4 +3,9 @@ using UnityEngine; public class StreetContainer : MonoBehaviour { public GameObject streetObject; + + public void Place() + { + streetObject.GetComponent().Place(); + } } diff --git a/Assets/Scripts/TrafficSystem/TrafficNode.cs b/Assets/Scripts/TrafficSystem/TrafficNode.cs index e708f24..33835aa 100644 --- a/Assets/Scripts/TrafficSystem/TrafficNode.cs +++ b/Assets/Scripts/TrafficSystem/TrafficNode.cs @@ -9,8 +9,15 @@ public class TrafficNode : MonoBehaviour [SerializeField] private float speed = 5.0f; + + public Color gizmoSphereColor = Color.green; + + private Street parentStreet; + public float GetSpeed() { return speed; } public List GetNextTrafficNodes() { return nextTrafficNodes; } + public void SetParentStreet(Street newParentStreet) { parentStreet = newParentStreet; } + public Street GetParentStreet() { return parentStreet; } public void AddNextNode(TrafficNode newNode) { @@ -22,9 +29,20 @@ public class TrafficNode : MonoBehaviour nextTrafficNodes.Remove(node); } + public void LateUpdate() + { + if (transform.parent != null) + { + Vector3 parentScale = transform.parent.lossyScale; + transform.localScale = new Vector3(1f / parentScale.x, 1f / parentScale.y, + 1f / parentScale.z); + } + + } + private void OnDrawGizmos() { - Gizmos.color = Color.green; + Gizmos.color = gizmoSphereColor; Gizmos.DrawSphere(transform.position, 0.2f); foreach (TrafficNode node in nextTrafficNodes) { diff --git a/ProjectSettings/TagManager.asset b/ProjectSettings/TagManager.asset index bb2bed5..cbbe646 100644 --- a/ProjectSettings/TagManager.asset +++ b/ProjectSettings/TagManager.asset @@ -6,6 +6,7 @@ TagManager: tags: - GameManager - TrafficNode + - Street layers: - Default - TransparentFX @@ -13,7 +14,7 @@ TagManager: - - Water - UI - - + - Blueprint - - - diff --git a/UserSettings/Layouts/default-2021.dwlt b/UserSettings/Layouts/default-2021.dwlt index 920e95d..194840a 100644 --- a/UserSettings/Layouts/default-2021.dwlt +++ b/UserSettings/Layouts/default-2021.dwlt @@ -14,12 +14,12 @@ MonoBehaviour: m_EditorClassIdentifier: m_PixelRect: serializedVersion: 2 - x: 0 + x: -1920 y: 43 width: 1920 height: 989 m_ShowMode: 4 - m_Title: Scene + m_Title: Hierarchy m_RootView: {fileID: 6} m_MinSize: {x: 875, y: 300} m_MaxSize: {x: 10000, y: 10000} @@ -48,7 +48,7 @@ MonoBehaviour: m_MinSize: {x: 300, y: 200} m_MaxSize: {x: 24288, y: 16192} vertical: 0 - controlID: 44 + controlID: 88 --- !u!114 &3 MonoBehaviour: m_ObjectHideFlags: 52 @@ -93,9 +93,9 @@ MonoBehaviour: x: 0 y: 0 width: 363 - height: 606 - m_MinSize: {x: 201, y: 221} - m_MaxSize: {x: 4001, y: 4021} + height: 547 + m_MinSize: {x: 200, y: 200} + m_MaxSize: {x: 4000, y: 4000} m_ActualView: {fileID: 14} m_Panes: - {fileID: 14} @@ -117,9 +117,9 @@ MonoBehaviour: m_Position: serializedVersion: 2 x: 0 - y: 606 + y: 547 width: 1466 - height: 333 + height: 392 m_MinSize: {x: 100, y: 100} m_MaxSize: {x: 4000, y: 4000} m_ActualView: {fileID: 17} @@ -223,7 +223,7 @@ MonoBehaviour: m_MinSize: {x: 200, y: 200} m_MaxSize: {x: 16192, y: 16192} vertical: 1 - controlID: 45 + controlID: 89 --- !u!114 &10 MonoBehaviour: m_ObjectHideFlags: 52 @@ -244,11 +244,11 @@ MonoBehaviour: x: 0 y: 0 width: 1466 - height: 606 + height: 547 m_MinSize: {x: 200, y: 100} m_MaxSize: {x: 16192, y: 8096} vertical: 0 - controlID: 46 + controlID: 90 --- !u!114 &11 MonoBehaviour: m_ObjectHideFlags: 52 @@ -267,7 +267,7 @@ MonoBehaviour: x: 363 y: 0 width: 1103 - height: 606 + height: 547 m_MinSize: {x: 202, y: 221} m_MaxSize: {x: 4002, y: 4021} m_ActualView: {fileID: 15} @@ -330,9 +330,9 @@ MonoBehaviour: m_IsLocked: 0 m_FolderTreeState: scrollPos: {x: 0, y: 0} - m_SelectedIDs: 5c790000 - m_LastClickedID: 31068 - m_ExpandedIDs: 00000000be5f0000 + m_SelectedIDs: d05f0000 + m_LastClickedID: 24528 + m_ExpandedIDs: 00000000be5f0000d45f000000ca9a3bffffff7f m_RenameOverlay: m_UserAcceptedRename: 0 m_Name: @@ -385,9 +385,9 @@ MonoBehaviour: m_Icon: {fileID: 0} m_ResourceFile: m_ListAreaState: - m_SelectedInstanceIDs: 60790000 - m_LastClickedInstanceID: 31072 - m_HadKeyboardFocusLastEvent: 1 + m_SelectedInstanceIDs: + m_LastClickedInstanceID: 0 + m_HadKeyboardFocusLastEvent: 0 m_ExpandedInstanceIDs: c6230000 m_RenameOverlay: m_UserAcceptedRename: 0 @@ -431,12 +431,12 @@ MonoBehaviour: m_MinSize: {x: 275, y: 50} m_MaxSize: {x: 4000, y: 4000} m_TitleContent: - m_Text: Debug + m_Text: Inspector m_Image: {fileID: -2667387946076563598, guid: 0000000000000000d000000000000000, type: 0} m_Tooltip: m_Pos: serializedVersion: 2 - x: 1466 + x: -454 y: 73 width: 453 height: 918 @@ -447,13 +447,13 @@ MonoBehaviour: m_ObjectsLockedBeforeSerialization: [] m_InstanceIDsLockedBeforeSerialization: m_PreviewResizer: - m_CachedPref: 160 + m_CachedPref: -160 m_ControlHash: -371814159 m_PrefName: Preview_InspectorPreview m_LastInspectedObjectInstanceID: -1 m_LastVerticalScrollValue: 0 m_GlobalObjectId: - m_InspectorMode: 1 + m_InspectorMode: 0 m_LockTracker: m_IsLocked: 0 m_PreviewWindow: {fileID: 0} @@ -477,10 +477,10 @@ MonoBehaviour: m_Tooltip: m_Pos: serializedVersion: 2 - x: 0 + x: -1920 y: 73 width: 362 - height: 585 + height: 526 m_ViewDataDictionary: {fileID: 0} m_OverlayCanvas: m_LastAppliedPresetName: Default @@ -490,7 +490,7 @@ MonoBehaviour: scrollPos: {x: 0, y: 0} m_SelectedIDs: m_LastClickedID: 0 - m_ExpandedIDs: 2afbffff745f0000 + m_ExpandedIDs: da50feff6e55feff7455feff365afeff3c5afeffd89afefffaa7feff00a8feff02b1feffacc4feff5013ffff5613ffff2215ffff2815ffffe415ffffea15ffff2816ffff2e16ffff4437ffff4a37ffff1c46ffff2c48ffff9479ffff7c7dffff428affff628affff9691ffff9c91ffffb691ffff28acfffff4adffff26cbffffead1ffff06f4ffff2afbffff745f0000 m_RenameOverlay: m_UserAcceptedRename: 0 m_Name: @@ -534,10 +534,10 @@ MonoBehaviour: m_Tooltip: m_Pos: serializedVersion: 2 - x: 363 + x: -1557 y: 73 width: 1101 - height: 585 + height: 526 m_ViewDataDictionary: {fileID: 0} m_OverlayCanvas: m_LastAppliedPresetName: Default @@ -591,7 +591,7 @@ MonoBehaviour: floating: 0 collapsed: 0 displayed: 1 - snapOffset: {x: 0, y: 0} + snapOffset: {x: 0, y: 25} snapOffsetDelta: {x: 0, y: 0} snapCorner: 0 id: unity-transform-toolbar @@ -794,9 +794,9 @@ MonoBehaviour: m_PlayAudio: 0 m_AudioPlay: 0 m_Position: - m_Target: {x: -2.5165825, y: 4.253081, z: 0.12071203} + m_Target: {x: 7.8933806, y: 2.0472279, z: -0.19834569} speed: 2 - m_Value: {x: -2.5165825, y: 4.253081, z: 0.12071203} + m_Value: {x: 7.8933806, y: 2.0472279, z: -0.19834569} m_RenderMode: 0 m_CameraMode: drawMode: 0 @@ -847,9 +847,9 @@ MonoBehaviour: speed: 2 m_Value: {x: 0, y: 0, z: 0, w: 1} m_Size: - m_Target: 9.641667 + m_Target: 9.554622 speed: 2 - m_Value: 9.641667 + m_Value: 9.554622 m_Ortho: m_Target: 1 speed: 2 @@ -894,10 +894,10 @@ MonoBehaviour: m_Tooltip: m_Pos: serializedVersion: 2 - x: 363 + x: -1557 y: 73 width: 1101 - height: 585 + height: 526 m_ViewDataDictionary: {fileID: 0} m_OverlayCanvas: m_LastAppliedPresetName: Default @@ -908,7 +908,7 @@ MonoBehaviour: m_ShowGizmos: 1 m_TargetDisplay: 0 m_ClearColor: {r: 0, g: 0, b: 0, a: 0} - m_TargetSize: {x: 1101, y: 564} + m_TargetSize: {x: 1101, y: 505} m_TextureFilterMode: 0 m_TextureHideFlags: 61 m_RenderIMGUI: 1 @@ -925,8 +925,8 @@ MonoBehaviour: vZoomLockedByDefault: 0 m_HBaseRangeMin: -550.5 m_HBaseRangeMax: 550.5 - m_VBaseRangeMin: -282 - m_VBaseRangeMax: 282 + m_VBaseRangeMin: -252.5 + m_VBaseRangeMax: 252.5 m_HAllowExceedBaseRangeMin: 1 m_HAllowExceedBaseRangeMax: 1 m_VAllowExceedBaseRangeMin: 1 @@ -945,22 +945,22 @@ MonoBehaviour: x: 0 y: 21 width: 1101 - height: 564 - m_Scale: {x: 1, y: 1} - m_Translation: {x: 550.5, y: 282} + height: 505 + m_Scale: {x: 1.7223669, y: 1.7223667} + m_Translation: {x: 550.5, y: 252.49997} m_MarginLeft: 0 m_MarginRight: 0 m_MarginTop: 0 m_MarginBottom: 0 m_LastShownAreaInsideMargins: serializedVersion: 2 - x: -550.5 - y: -282 - width: 1101 - height: 564 + x: -319.61832 + y: -146.60059 + width: 639.23663 + height: 293.2012 m_MinimalGUI: 1 m_defaultScale: 1 - m_LastWindowPixelSize: {x: 1101, y: 585} + m_LastWindowPixelSize: {x: 1101, y: 526} m_ClearInEditMode: 1 m_NoCameraWarning: 1 m_LowResolutionForAspectRatios: 01000000000000000000 @@ -986,10 +986,10 @@ MonoBehaviour: m_Tooltip: m_Pos: serializedVersion: 2 - x: 0 - y: 679 + x: -1920 + y: 620 width: 1465 - height: 312 + height: 371 m_ViewDataDictionary: {fileID: 0} m_OverlayCanvas: m_LastAppliedPresetName: Default