diff --git a/Assets/Scripts/Roads/Node/CustomNode.cs b/Assets/Scripts/Roads/Node/CustomNode.cs index 17dd703..f4a1eff 100644 --- a/Assets/Scripts/Roads/Node/CustomNode.cs +++ b/Assets/Scripts/Roads/Node/CustomNode.cs @@ -17,8 +17,6 @@ override public void pull(Vector3 position) { this.position = position; gameObject.transform.position = position; - foreach (Road road in roads) { - road.update(true); - } + update(); } } diff --git a/Assets/Scripts/Roads/Node/CustomNode.cs b/Assets/Scripts/Roads/Node/CustomNode.cs index 17dd703..f4a1eff 100644 --- a/Assets/Scripts/Roads/Node/CustomNode.cs +++ b/Assets/Scripts/Roads/Node/CustomNode.cs @@ -17,8 +17,6 @@ override public void pull(Vector3 position) { this.position = position; gameObject.transform.position = position; - foreach (Road road in roads) { - road.update(true); - } + update(); } } diff --git a/Assets/Scripts/Roads/Node/Node.cs b/Assets/Scripts/Roads/Node/Node.cs index e5fbd48..dd01e5c 100644 --- a/Assets/Scripts/Roads/Node/Node.cs +++ b/Assets/Scripts/Roads/Node/Node.cs @@ -9,6 +9,7 @@ public Vector3 position; public List roads = new List(); public GameObject gameObject; + public Vector3 direction; public Node(Vector3 position, Transform parent, Config config) { gameObject = new GameObject(); @@ -30,6 +31,7 @@ nodeRoad.transform.parent = gameObject.transform; nodeRoad.transform.localPosition = Vector3.zero; this.position = position; + update(); } public Node getOther(Node caller) { @@ -60,7 +62,14 @@ abstract public void pull(Vector3 position); + private void updateDirection() { + Vector3 inDirection = getAverageDirection(roads.FindAll(it => it.nodes[0] == this), 0); + Vector3 outDirection = getAverageDirection(roads.FindAll(it => it.nodes[1] == this), 1); + direction = (inDirection - outDirection) * 0.25f; + } + public void lateUpdate(Road caller) { + updateDirection(); foreach (Road road in roads) { if (!road.Equals(caller)) { road.update(false); @@ -69,10 +78,22 @@ } public void update() { + updateDirection(); foreach (Road road in roads) { road.update(true); } } + private Vector3 getAverageDirection(List roads, int index) { + Vector3 sum = new Vector3(0f, 0f, 0f); + if (roads.Count == 0) { + return sum; + } + foreach (Road road in roads) { + sum += road.nodes[1-index].position - position; + } + return sum / roads.Count; + } + public abstract void delete(); } diff --git a/Assets/Scripts/Roads/Node/CustomNode.cs b/Assets/Scripts/Roads/Node/CustomNode.cs index 17dd703..f4a1eff 100644 --- a/Assets/Scripts/Roads/Node/CustomNode.cs +++ b/Assets/Scripts/Roads/Node/CustomNode.cs @@ -17,8 +17,6 @@ override public void pull(Vector3 position) { this.position = position; gameObject.transform.position = position; - foreach (Road road in roads) { - road.update(true); - } + update(); } } diff --git a/Assets/Scripts/Roads/Node/Node.cs b/Assets/Scripts/Roads/Node/Node.cs index e5fbd48..dd01e5c 100644 --- a/Assets/Scripts/Roads/Node/Node.cs +++ b/Assets/Scripts/Roads/Node/Node.cs @@ -9,6 +9,7 @@ public Vector3 position; public List roads = new List(); public GameObject gameObject; + public Vector3 direction; public Node(Vector3 position, Transform parent, Config config) { gameObject = new GameObject(); @@ -30,6 +31,7 @@ nodeRoad.transform.parent = gameObject.transform; nodeRoad.transform.localPosition = Vector3.zero; this.position = position; + update(); } public Node getOther(Node caller) { @@ -60,7 +62,14 @@ abstract public void pull(Vector3 position); + private void updateDirection() { + Vector3 inDirection = getAverageDirection(roads.FindAll(it => it.nodes[0] == this), 0); + Vector3 outDirection = getAverageDirection(roads.FindAll(it => it.nodes[1] == this), 1); + direction = (inDirection - outDirection) * 0.25f; + } + public void lateUpdate(Road caller) { + updateDirection(); foreach (Road road in roads) { if (!road.Equals(caller)) { road.update(false); @@ -69,10 +78,22 @@ } public void update() { + updateDirection(); foreach (Road road in roads) { road.update(true); } } + private Vector3 getAverageDirection(List roads, int index) { + Vector3 sum = new Vector3(0f, 0f, 0f); + if (roads.Count == 0) { + return sum; + } + foreach (Road road in roads) { + sum += road.nodes[1-index].position - position; + } + return sum / roads.Count; + } + public abstract void delete(); } diff --git a/Assets/Scripts/Roads/Road.cs b/Assets/Scripts/Roads/Road.cs index 08be76d..d36ccca 100644 --- a/Assets/Scripts/Roads/Road.cs +++ b/Assets/Scripts/Roads/Road.cs @@ -63,26 +63,14 @@ public void update(bool updateOthers) { path.A = nodes[0].position; + path.B = nodes[0].position + nodes[0].direction; + path.C = nodes[1].position - nodes[1].direction; path.D = nodes[1].position; - if (nodes[0].roads.Count == 2) { - path.B = nodes[0].position + - 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); - } else { - path.B = 0.5f * (nodes[0].position + nodes[1].position); - } - if (nodes[1].roads.Count == 2) { - path.C = nodes[1].position + - 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); - } else { - path.C = 0.5f * (nodes[0].position + nodes[1].position); - } if (updateOthers) { foreach (Node node in nodes) { node.lateUpdate(this); } } - pathLine.update(); - roadBody.update(); collider.sharedMesh = roadBody.mesh; Vector3 midPosition = path.getPosition(0.5f); @@ -90,19 +78,21 @@ midDirection.Normalize(); Vector3 midNormal = new Vector3(-midDirection.z, 0, midDirection.x); midNormal.Normalize(); - arrow1.A = midPosition; - arrow2.A = midPosition; - arrow1.B = midPosition - midDirection * 0.5f + midNormal * 0.5f; - arrow2.B = arrow1.B - midNormal; - arrow1.C = midPosition; - arrow2.C = midPosition; - arrow1.D = arrow1.B; - arrow2.D = arrow2.B; - arrow1Renderer.update(); - arrow2Renderer.update(); + updateArrow(arrow1, midPosition, midDirection, midNormal, 1.0f, arrow1Renderer); + updateArrow(arrow2, midPosition, midDirection, midNormal, -1.0f, arrow2Renderer); + pathLine.update(); + roadBody.update(); path.updateLength(); } + private void updateArrow(Bezier arrow, Vector3 position, Vector3 direction, Vector3 normal, float offset, FlatBezierRenderer renderer) { + arrow.A = position; + arrow.B = position - direction * 0.5f + offset * normal * 0.5f; + arrow.C = position; + arrow.D = arrow.B; + renderer.update(); + } + override public bool Equals(object other) { if (other == null || !(other is Road)) { return false;