1
Fork 0
mirror of https://github.com/thegeneralist01/Scene-Manager-DevRepo synced 2026-01-11 23:50:29 +01:00

Refactored menu item initializations, updated Game.LogTrivial calls to Logger.Log, refactored SetDriveSpeedForWaypoint as local function

This commit is contained in:
Rich Dunne 2020-09-14 14:47:05 -06:00
parent 4dbe037cdd
commit de3aade534

View file

@ -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 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" }; private static string[] waypointTypes = new string[] { "Drive To (Normal)", "Drive To (Direct)", "Stop" };
public static UIMenu pathCreationMenu { get; private set; } internal static UIMenu pathCreationMenu = new UIMenu("Scene Manager", "~o~Path Creation Menu");
private static UIMenuItem trafficAddWaypoint, trafficRemoveWaypoint, trafficEndPath; private static UIMenuItem trafficAddWaypoint = new UIMenuItem("Add waypoint"), trafficRemoveWaypoint = new UIMenuItem("Remove last waypoint"), trafficEndPath = new UIMenuItem("End path creation");
public static UIMenuListScrollerItem<string> waypointType = new UIMenuListScrollerItem<string>("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); internal static UIMenuListScrollerItem<string> waypointType = new UIMenuListScrollerItem<string>("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<int> waypointSpeed; private static UIMenuNumericScrollerItem<int> waypointSpeed = new UIMenuNumericScrollerItem<int>("Waypoint Speed", $"How fast the AI will drive to the waypoint in ~b~{SettingsMenu.speedUnits.SelectedItem}", 5, 80, 5);
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."); 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.");
public static UIMenuNumericScrollerItem<int> collectorRadius = new UIMenuNumericScrollerItem<int>("Collection Radius", "The distance from this waypoint (in meters) vehicles will be collected", 1, 50, 1); internal static UIMenuNumericScrollerItem<int> collectorRadius = new UIMenuNumericScrollerItem<int>("Collection Radius", "The distance from this waypoint (in meters) vehicles will be collected", 1, 50, 1);
public static UIMenuNumericScrollerItem<int> speedZoneRadius = new UIMenuNumericScrollerItem<int>("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 UIMenuNumericScrollerItem<int> speedZoneRadius = new UIMenuNumericScrollerItem<int>("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() internal static void InstantiateMenu()
{ {
pathCreationMenu = new UIMenu("Scene Manager", "~o~Path Creation Menu");
pathCreationMenu.ParentMenu = PathMainMenu.pathMainMenu; pathCreationMenu.ParentMenu = PathMainMenu.pathMainMenu;
MenuManager.menuPool.Add(pathCreationMenu); MenuManager.menuPool.Add(pathCreationMenu);
} }
public static void BuildPathCreationMenu() internal static void BuildPathCreationMenu()
{ {
pathCreationMenu.AddItem(waypointType); pathCreationMenu.AddItem(waypointType);
pathCreationMenu.AddItem(waypointSpeed = new UIMenuNumericScrollerItem<int>("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; waypointSpeed.Index = 0;
pathCreationMenu.AddItem(collectorWaypoint); pathCreationMenu.AddItem(collectorWaypoint);
collectorWaypoint.Enabled = false; collectorWaypoint.Enabled = false;
collectorWaypoint.Checked = true; collectorWaypoint.Checked = true;
pathCreationMenu.AddItem(collectorRadius); pathCreationMenu.AddItem(collectorRadius);
collectorRadius.Index = 0; collectorRadius.Index = 0;
collectorRadius.Enabled = true; collectorRadius.Enabled = true;
pathCreationMenu.AddItem(speedZoneRadius); pathCreationMenu.AddItem(speedZoneRadius);
speedZoneRadius.Index = 0; speedZoneRadius.Index = 0;
speedZoneRadius.Enabled = true; speedZoneRadius.Enabled = true;
pathCreationMenu.AddItem(trafficAddWaypoint = new UIMenuItem("Add waypoint"));
pathCreationMenu.AddItem(trafficAddWaypoint);
trafficAddWaypoint.ForeColor = Color.Gold; trafficAddWaypoint.ForeColor = Color.Gold;
pathCreationMenu.AddItem(trafficRemoveWaypoint = new UIMenuItem("Remove last waypoint"));
pathCreationMenu.AddItem(trafficRemoveWaypoint);
trafficRemoveWaypoint.ForeColor = Color.Gold; trafficRemoveWaypoint.ForeColor = Color.Gold;
trafficRemoveWaypoint.Enabled = false; trafficRemoveWaypoint.Enabled = false;
pathCreationMenu.AddItem(trafficEndPath = new UIMenuItem("End path creation"));
pathCreationMenu.AddItem(trafficEndPath);
trafficEndPath.ForeColor = Color.Gold; trafficEndPath.ForeColor = Color.Gold;
trafficEndPath.Enabled = false; trafficEndPath.Enabled = false;
@ -68,12 +74,10 @@ namespace SceneManager
private static void PathCreation_OnItemSelected(UIMenu sender, UIMenuItem selectedItem, int index) 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) if (selectedItem == trafficAddWaypoint)
{ {
var anyPathsExist = PathMainMenu.GetPaths().Count > 0; 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) if (!anyPathsExist)
{ {
AddNewPathToPathsCollection(PathMainMenu.GetPaths(), 0); 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]))); 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); ToggleTrafficEndPathMenuItem(pathIndex);
collectorWaypoint.Enabled = true; collectorWaypoint.Enabled = true;
trafficRemoveWaypoint.Enabled = true; trafficRemoveWaypoint.Enabled = true;
PathMainMenu.createNewPath.Text = $"Continue Creating Path {pathNumber}"; 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) if (selectedItem == trafficRemoveWaypoint)
@ -111,7 +134,7 @@ namespace SceneManager
{ {
if (PathMainMenu.GetPaths().ElementAtOrDefault(i) != null && PathMainMenu.GetPaths()[i].State == State.Creating) 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().Blip.Delete();
PathMainMenu.GetPaths()[i].Waypoints.Last().RemoveSpeedZone(); PathMainMenu.GetPaths()[i].Waypoints.Last().RemoveSpeedZone();
@ -145,23 +168,19 @@ namespace SceneManager
var currentPath = PathMainMenu.GetPaths()[i]; var currentPath = PathMainMenu.GetPaths()[i];
if (PathMainMenu.GetPaths().ElementAtOrDefault(i) != null && currentPath.State == State.Creating) 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."); Game.DisplayNotification($"~o~Scene Manager\n~g~[Success]~w~ Path {i + 1} complete.");
currentPath.State = State.Finished; currentPath.State = State.Finished;
currentPath.IsEnabled = true; currentPath.IsEnabled = true;
currentPath.Number = i + 1; 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) foreach (Waypoint waypoint in PathMainMenu.GetPaths()[i].Waypoints)
{ {
GameFiber WaypointVehicleCollectorFiber = new GameFiber(() => VehicleCollector.StartCollectingAtWaypoint(PathMainMenu.GetPaths(), PathMainMenu.GetPaths()[i], waypoint)); GameFiber WaypointVehicleCollectorFiber = new GameFiber(() => VehicleCollector.StartCollectingAtWaypoint(PathMainMenu.GetPaths(), PathMainMenu.GetPaths()[i], waypoint));
WaypointVehicleCollectorFiber.Start(); 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.createNewPath.Text = "Create New Path";
PathMainMenu.pathMainMenu.Clear();
PathMainMenu.BuildPathMenu(); PathMainMenu.BuildPathMenu();
collectorWaypoint.Enabled = false; collectorWaypoint.Enabled = false;
collectorWaypoint.Checked = true; collectorWaypoint.Checked = true;
@ -207,26 +226,7 @@ namespace SceneManager
} }
} }
private static float SetDriveSpeedForWaypoint() internal static Blip CreateWaypointBlip(int pathIndex, VehicleDrivingFlags drivingFlag)
{
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)
{ {
var spriteNumericalEnum = pathIndex + 17; // 17 because the numerical value of these sprites are always 17 more than the path index 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) var blip = new Blip(Game.LocalPlayer.Character.Position)
@ -256,10 +256,10 @@ namespace SceneManager
return blip; return blip;
} }
public static void AddNewPathToPathsCollection(List<Path> paths, int pathIndex) internal static void AddNewPathToPathsCollection(List<Path> paths, int pathIndex)
{ {
var pathNum = pathIndex + 1; 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."); Game.DisplayNotification($"~o~Scene Manager\n~y~[Creating]~w~ Path {pathNum} started.");
paths.Insert(pathIndex, new Path(pathNum, State.Creating)); paths.Insert(pathIndex, new Path(pathNum, State.Creating));
trafficRemoveWaypoint.Enabled = false; trafficRemoveWaypoint.Enabled = false;