Sfoglia il codice sorgente

calculating route length

Marcel Zickler 3 anni fa
parent
commit
8ae0afe72a

+ 36 - 10
Assets/Editor/RouteEditor.cs

@@ -21,6 +21,10 @@ public class RoutesEditor : Editor
     private SceneView sceneView;
     private readonly List<Turn> selectedTurns = new List<Turn>();
 
+    private Route route;
+
+    private static float length = 0;
+
     private static readonly Dictionary<RoadDirection, int> roadDirectionIndexes = new Dictionary<RoadDirection, int>
     {
         {RoadDirection.West, 0},
@@ -36,10 +40,16 @@ public class RoutesEditor : Editor
         EditorGUILayout.PropertyField(serializedObject.FindProperty("start"));
         EditorGUILayout.PropertyField(serializedObject.FindProperty("finish"));
         RoutesList(serializedObject.FindProperty("items"));
+        EditorGUILayout.LabelField($"Current length: {length}");
         //EditorGUILayout.PropertyField(routes.FindPropertyRelative("arraySize"), true);true);
         serializedObject.ApplyModifiedProperties();
     }
 
+    private void OnEnable()
+    {
+        route = (Route) target;
+    }
+
     private void OnSceneGUI()
     {
         if (inSelectionMode)
@@ -205,32 +215,48 @@ public class RoutesEditor : Editor
 
         return roadDirectionIndexes[direction];
     }
-    
-    
+
 
     [DrawGizmo(GizmoType.Selected)]
     private static void DrawRoutePreview(Route route, GizmoType gizmoType)
     {
+        length = 0f; // a bit hacky but who cares
         if (route.items == null) return;
         Gizmos.color = Color.red;
         Turn previousTurn = null;
+
+        Vector3 from;
+        Vector3 to;
+
+        if (route.start != null && route.items.Count > 0)
+        {
+            from = route.start.transform.position;
+            to = route.items[0].turn.transform.position;
+            length += (to - from).magnitude;
+            Helpers.DrawLine(from, to, 3);
+        }
+
         foreach (var item in route.items)
         {
             var turn = item.turn;
 
             if (previousTurn != null)
             {
-                //Gizmos.DrawCube(previousTurn.transform.position + turn.transform.position /2f, turn.transform.position - previousTurn.transform.position + Vector3.one * 0.1f);
-                //Gizmos.DrawLine(previousTurn.transform.position, turn.transform.position);
-                //var p1 = previousTurn.transform.position;
-                //var p2 = turn.transform.position;
-                //var thickness = 3;
-                //Handles.DrawBezier(p1,p2,p1,p2, Color.red,null,thickness);
-                Helpers.DrawLine(previousTurn.transform.position, turn.transform.position, 3);
+                from = previousTurn.transform.position;
+                to = turn.transform.position;
+                length += (to - from).magnitude;
+                Helpers.DrawLine(from, to, 3);
             }
-            
+
             previousTurn = turn;
         }
 
+        if (route.finish != null && route.items.Count > 0)
+        {
+            from = route.items.Last().turn.transform.position;
+            to = route.finish.transform.position;
+            length += (to - from).magnitude;
+            Helpers.DrawLine(from, to, 3);
+        }
     }
 }

File diff suppressed because it is too large
+ 918 - 1
Assets/Scenes/MainScene.unity


BIN
obj/Debug/Assembly-CSharp.csprojAssemblyReference.cache


Some files were not shown because too many files changed in this diff