This commit is contained in:
Janis
2022-12-14 21:47:16 +01:00
parent 274f5f7cac
commit fe2da27074
20 changed files with 407 additions and 197 deletions

View File

@@ -3,23 +3,40 @@ using UnityEngine;
public class Street : MonoBehaviour
{
[SerializeField]
GameObject nodePrefab;
private List<TrafficNode> nodes = new List<TrafficNode>();
private void Start()
public void Place()
{
nodes.Add(Instantiate(PrefabDictionary.instance.nodePrefab, transform).GetComponent<TrafficNode>());
nodes.Add(Instantiate(PrefabDictionary.instance.nodePrefab, transform).GetComponent<TrafficNode>());
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<TrafficNode>());
nodes.Add(Instantiate(nodePrefab, transform).GetComponent<TrafficNode>());
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>();
TrafficNode newNode = Instantiate(nodePrefab, transform).GetComponent<TrafficNode>();
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;

View File

@@ -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:

View File

@@ -3,4 +3,9 @@ using UnityEngine;
public class StreetContainer : MonoBehaviour
{
public GameObject streetObject;
public void Place()
{
streetObject.GetComponent<Street>().Place();
}
}

View File

@@ -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<TrafficNode> 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)
{