From de3aade534f04a1f46d10e0930773fe76c674b81 Mon Sep 17 00:00:00 2001 From: Rich Dunne Date: Mon, 14 Sep 2020 14:47:05 -0600 Subject: [PATCH] Refactored menu item initializations, updated Game.LogTrivial calls to Logger.Log, refactored SetDriveSpeedForWaypoint as local function --- SceneManager/Menus/PathCreationMenu.cs | 88 +++++++++++++------------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/SceneManager/Menus/PathCreationMenu.cs b/SceneManager/Menus/PathCreationMenu.cs index 7cf1bcd..9f7b1b8 100644 --- a/SceneManager/Menus/PathCreationMenu.cs +++ b/SceneManager/Menus/PathCreationMenu.cs @@ -13,41 +13,47 @@ namespace SceneManager private static VehicleDrivingFlags[] drivingFlags = new VehicleDrivingFlags[] { VehicleDrivingFlags.Normal, VehicleDrivingFlags.IgnorePathFinding, VehicleDrivingFlags.StopAtDestination }; // Implement custom driving flag for normal private static string[] waypointTypes = new string[] { "Drive To (Normal)", "Drive To (Direct)", "Stop" }; - public static UIMenu pathCreationMenu { get; private set; } - private static UIMenuItem trafficAddWaypoint, trafficRemoveWaypoint, trafficEndPath; - public static UIMenuListScrollerItem waypointType = new UIMenuListScrollerItem("Waypoint Type", $"~b~Drive To (Normal): ~w~AI obeys traffic as much as possible{Environment.NewLine}~b~Drive To (Direct): ~w~AI ignores pathfinding rules{Environment.NewLine}~b~Stop: ~w~AI stops at the waypoint until dismissed", waypointTypes); - private static UIMenuNumericScrollerItem waypointSpeed; - public static UIMenuCheckboxItem collectorWaypoint = new UIMenuCheckboxItem("Collector", true, "If this waypoint will collect vehicles to follow the path. Your path's first waypoint ~b~must~w~ be a collector."); - public static UIMenuNumericScrollerItem collectorRadius = new UIMenuNumericScrollerItem("Collection Radius", "The distance from this waypoint (in meters) vehicles will be collected", 1, 50, 1); - public static UIMenuNumericScrollerItem speedZoneRadius = new UIMenuNumericScrollerItem("Speed Zone Radius", "The distance from this collector waypoint (in meters) non-collected vehicles will drive at this waypoint's speed", 5, 200, 5); + internal static UIMenu pathCreationMenu = new UIMenu("Scene Manager", "~o~Path Creation Menu"); + private static UIMenuItem trafficAddWaypoint = new UIMenuItem("Add waypoint"), trafficRemoveWaypoint = new UIMenuItem("Remove last waypoint"), trafficEndPath = new UIMenuItem("End path creation"); + internal static UIMenuListScrollerItem waypointType = new UIMenuListScrollerItem("Waypoint Type", $"~b~Drive To (Normal): ~w~AI obeys traffic as much as possible{Environment.NewLine}~b~Drive To (Direct): ~w~AI ignores pathfinding rules{Environment.NewLine}~b~Stop: ~w~AI stops at the waypoint until dismissed", waypointTypes); + private static UIMenuNumericScrollerItem waypointSpeed = new UIMenuNumericScrollerItem("Waypoint Speed", $"How fast the AI will drive to the waypoint in ~b~{SettingsMenu.speedUnits.SelectedItem}", 5, 80, 5); + internal static UIMenuCheckboxItem collectorWaypoint = new UIMenuCheckboxItem("Collector", true, "If this waypoint will collect vehicles to follow the path. Your path's first waypoint ~b~must~w~ be a collector."); + internal static UIMenuNumericScrollerItem collectorRadius = new UIMenuNumericScrollerItem("Collection Radius", "The distance from this waypoint (in meters) vehicles will be collected", 1, 50, 1); + internal static UIMenuNumericScrollerItem speedZoneRadius = new UIMenuNumericScrollerItem("Speed Zone Radius", "The distance from this collector waypoint (in meters) non-collected vehicles will drive at this waypoint's speed", 5, 200, 5); internal static void InstantiateMenu() { - pathCreationMenu = new UIMenu("Scene Manager", "~o~Path Creation Menu"); pathCreationMenu.ParentMenu = PathMainMenu.pathMainMenu; MenuManager.menuPool.Add(pathCreationMenu); } - public static void BuildPathCreationMenu() + internal static void BuildPathCreationMenu() { pathCreationMenu.AddItem(waypointType); - pathCreationMenu.AddItem(waypointSpeed = new UIMenuNumericScrollerItem("Waypoint Speed", $"How fast the AI will drive to the waypoint in ~b~{SettingsMenu.speedUnits.SelectedItem}", 5, 80, 5)); + + pathCreationMenu.AddItem(waypointSpeed); waypointSpeed.Index = 0; + pathCreationMenu.AddItem(collectorWaypoint); collectorWaypoint.Enabled = false; collectorWaypoint.Checked = true; + pathCreationMenu.AddItem(collectorRadius); collectorRadius.Index = 0; collectorRadius.Enabled = true; + pathCreationMenu.AddItem(speedZoneRadius); speedZoneRadius.Index = 0; speedZoneRadius.Enabled = true; - pathCreationMenu.AddItem(trafficAddWaypoint = new UIMenuItem("Add waypoint")); + + pathCreationMenu.AddItem(trafficAddWaypoint); trafficAddWaypoint.ForeColor = Color.Gold; - pathCreationMenu.AddItem(trafficRemoveWaypoint = new UIMenuItem("Remove last waypoint")); + + pathCreationMenu.AddItem(trafficRemoveWaypoint); trafficRemoveWaypoint.ForeColor = Color.Gold; trafficRemoveWaypoint.Enabled = false; - pathCreationMenu.AddItem(trafficEndPath = new UIMenuItem("End path creation")); + + pathCreationMenu.AddItem(trafficEndPath); trafficEndPath.ForeColor = Color.Gold; trafficEndPath.Enabled = false; @@ -68,12 +74,10 @@ namespace SceneManager private static void PathCreation_OnItemSelected(UIMenu sender, UIMenuItem selectedItem, int index) { - // Do I need to implement a distance restriction? Idiots place waypoints unnecessarily close, possibly causing AI to drive in circles if (selectedItem == trafficAddWaypoint) { var anyPathsExist = PathMainMenu.GetPaths().Count > 0; - // If no paths exist, then add a new path to the collection at index 0. If paths do exist, then we want to add a new path at the first null index if there are no non-null paths where pathFinished = false if (!anyPathsExist) { AddNewPathToPathsCollection(PathMainMenu.GetPaths(), 0); @@ -96,12 +100,31 @@ namespace SceneManager { PathMainMenu.GetPaths()[pathIndex].Waypoints.Add(new Waypoint(firstNonNullPath, waypointNumber, Game.LocalPlayer.Character.Position, SetDriveSpeedForWaypoint(), drivingFlags[waypointType.Index], CreateWaypointBlip(pathIndex, drivingFlags[waypointType.Index]))); } - Game.LogTrivial($"[Path {pathNumber}] Waypoint {waypointNumber} ({drivingFlags[waypointType.Index].ToString()}) added"); + Logger.Log($"[Path {pathNumber}] Waypoint {waypointNumber} ({drivingFlags[waypointType.Index].ToString()}) added"); ToggleTrafficEndPathMenuItem(pathIndex); collectorWaypoint.Enabled = true; trafficRemoveWaypoint.Enabled = true; PathMainMenu.createNewPath.Text = $"Continue Creating Path {pathNumber}"; + + float SetDriveSpeedForWaypoint() + { + float convertedSpeed; + if (SettingsMenu.speedUnits.SelectedItem == SpeedUnits.MPH) + { + //Logger.Log($"Original speed: {waypointSpeeds[waypointSpeed.Index]}{SettingsMenu.speedUnits.SelectedItem}"); + convertedSpeed = MathHelper.ConvertMilesPerHourToMetersPerSecond(waypointSpeed.Value); + //Logger.Log($"Converted speed: {convertedSpeed}m/s"); + } + else + { + //Logger.Log($"Original speed: {waypointSpeeds[waypointSpeed.Index]}{SettingsMenu.speedUnits.SelectedItem}"); + convertedSpeed = MathHelper.ConvertKilometersPerHourToMetersPerSecond(waypointSpeed.Value); + //Logger.Log($"Converted speed: {convertedSpeed}m/s"); + } + + return convertedSpeed; + } } if (selectedItem == trafficRemoveWaypoint) @@ -111,7 +134,7 @@ namespace SceneManager { if (PathMainMenu.GetPaths().ElementAtOrDefault(i) != null && PathMainMenu.GetPaths()[i].State == State.Creating) { - Game.LogTrivial($"[Path {i + 1}] {PathMainMenu.GetPaths()[i].Waypoints.Last().DrivingFlag.ToString()} waypoint removed"); + Logger.Log($"[Path {i + 1}] {PathMainMenu.GetPaths()[i].Waypoints.Last().DrivingFlag.ToString()} waypoint removed"); PathMainMenu.GetPaths()[i].Waypoints.Last().Blip.Delete(); PathMainMenu.GetPaths()[i].Waypoints.Last().RemoveSpeedZone(); @@ -145,23 +168,19 @@ namespace SceneManager var currentPath = PathMainMenu.GetPaths()[i]; if (PathMainMenu.GetPaths().ElementAtOrDefault(i) != null && currentPath.State == State.Creating) { - Game.LogTrivial($"[Path Creation] Path {currentPath.Number} finished with {currentPath.Waypoints.Count} waypoints."); + Logger.Log($"[Path Creation] Path {currentPath.Number} finished with {currentPath.Waypoints.Count} waypoints."); Game.DisplayNotification($"~o~Scene Manager\n~g~[Success]~w~ Path {i + 1} complete."); currentPath.State = State.Finished; currentPath.IsEnabled = true; currentPath.Number = i + 1; - // For each waypoint in the path's WaypointData, start a collector game fiber and loop while the path and waypoint exist, and while the path is enabled foreach (Waypoint waypoint in PathMainMenu.GetPaths()[i].Waypoints) { GameFiber WaypointVehicleCollectorFiber = new GameFiber(() => VehicleCollector.StartCollectingAtWaypoint(PathMainMenu.GetPaths(), PathMainMenu.GetPaths()[i], waypoint)); WaypointVehicleCollectorFiber.Start(); } - MenuManager.menuPool.CloseAllMenus(); - //pathCreationMenu.Reset(true, true); // Trying to see if we can get away with resetting the menu instead of rebuilding it PathMainMenu.createNewPath.Text = "Create New Path"; - PathMainMenu.pathMainMenu.Clear(); PathMainMenu.BuildPathMenu(); collectorWaypoint.Enabled = false; collectorWaypoint.Checked = true; @@ -207,26 +226,7 @@ namespace SceneManager } } - private static float SetDriveSpeedForWaypoint() - { - float convertedSpeed; - if (SettingsMenu.speedUnits.SelectedItem == SpeedUnits.MPH) - { - //Game.LogTrivial($"Original speed: {waypointSpeeds[waypointSpeed.Index]}{SettingsMenu.speedUnits.SelectedItem}"); - convertedSpeed = MathHelper.ConvertMilesPerHourToMetersPerSecond(waypointSpeed.Value); - //Game.LogTrivial($"Converted speed: {convertedSpeed}m/s"); - } - else - { - //Game.LogTrivial($"Original speed: {waypointSpeeds[waypointSpeed.Index]}{SettingsMenu.speedUnits.SelectedItem}"); - convertedSpeed = MathHelper.ConvertKilometersPerHourToMetersPerSecond(waypointSpeed.Value); - //Game.LogTrivial($"Converted speed: {convertedSpeed}m/s"); - } - - return convertedSpeed; - } - - public static Blip CreateWaypointBlip(int pathIndex, VehicleDrivingFlags drivingFlag) + internal static Blip CreateWaypointBlip(int pathIndex, VehicleDrivingFlags drivingFlag) { var spriteNumericalEnum = pathIndex + 17; // 17 because the numerical value of these sprites are always 17 more than the path index var blip = new Blip(Game.LocalPlayer.Character.Position) @@ -256,10 +256,10 @@ namespace SceneManager return blip; } - public static void AddNewPathToPathsCollection(List paths, int pathIndex) + internal static void AddNewPathToPathsCollection(List paths, int pathIndex) { var pathNum = pathIndex + 1; - Game.LogTrivial($"Creating path {pathNum}"); + Logger.Log($"Creating path {pathNum}"); Game.DisplayNotification($"~o~Scene Manager\n~y~[Creating]~w~ Path {pathNum} started."); paths.Insert(pathIndex, new Path(pathNum, State.Creating)); trafficRemoveWaypoint.Enabled = false;