mirror of
https://github.com/BobbyRafael31/Unity-MazeRunner-Pathfinding-Visualizer.git
synced 2025-08-13 08:52:21 +00:00
Fix: Clean up code
Removing unused comment, refactor and remove some unused script
This commit is contained in:
@ -6,6 +6,11 @@ using UnityEngine.UI;
|
||||
using TMPro;
|
||||
using System.IO;
|
||||
|
||||
/// <summary>
|
||||
/// PathfindingUIManager merupakan kelas yang mengelola antarmuka pengguna untuk sistem pathfinding.
|
||||
/// pengguna dapat mengatur ukuran grid, memilih algoritma pathfinding, menjalankan pathfinding.
|
||||
/// </summary>
|
||||
|
||||
public class PathfindingUIManager : MonoBehaviour
|
||||
{
|
||||
[Header("References")]
|
||||
@ -22,12 +27,11 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
public TMP_Dropdown algorithmDropdown;
|
||||
public Button runPathfindingButton;
|
||||
public Button resetButton;
|
||||
public Toggle allowDiagonalToggle; // Toggle untuk diagonal movement
|
||||
public Toggle allowDiagonalToggle;
|
||||
|
||||
[Header("Visualization Controls")]
|
||||
public Slider visualizationSpeedSlider;
|
||||
public Slider visualizationBatchSlider;
|
||||
// public Toggle showVisualizationToggle;
|
||||
public TMP_Text speedValueText;
|
||||
public TMP_Text batchValueText;
|
||||
|
||||
@ -36,7 +40,7 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
public TMP_Text pathLengthText;
|
||||
public TMP_Text nodesExploredText;
|
||||
public TMP_Text memoryUsageText;
|
||||
public TMP_Text cpuUsageText; // Text untuk menampilkan penggunaan CPU
|
||||
public TMP_Text cpuUsageText;
|
||||
|
||||
[Header("Map Save/Load")]
|
||||
public TMP_InputField mapNameInput;
|
||||
@ -44,7 +48,7 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
public Button loadButton;
|
||||
|
||||
[Header("Application Controls")]
|
||||
public Button exitButton; // Tombol untuk keluar aplikasi
|
||||
public Button exitButton;
|
||||
|
||||
[Header("Optimization")]
|
||||
[SerializeField] private bool performWarmup = true;
|
||||
@ -54,29 +58,16 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
public TMP_Dropdown mazeSizeDropdown;
|
||||
public TMP_Dropdown mazeDensityDropdown;
|
||||
|
||||
[Header("Automated Testing")]
|
||||
[SerializeField] private GameObject testerPanelPrefab;
|
||||
[SerializeField] private Transform testerPanelParent;
|
||||
private GameObject testerPanelInstance;
|
||||
|
||||
// Konstanta untuk perhitungan CPU usage
|
||||
private const float TARGET_FRAME_TIME_MS = 16.67f; // 60 FPS = 16.67ms per frame
|
||||
|
||||
// Add a flag to track if pathfinding is running
|
||||
private bool isPathfindingRunning = false;
|
||||
|
||||
// Add a flag to track if visualization is running
|
||||
private bool isVisualizationRunning = false;
|
||||
|
||||
// Add a flag to track if NPC is moving
|
||||
private bool isNpcMoving = false;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// Initialize UI elements
|
||||
InitializeUI();
|
||||
|
||||
// Add listeners
|
||||
applyGridSizeButton.onClick.AddListener(OnApplyGridSize);
|
||||
runPathfindingButton.onClick.AddListener(OnRunPathfinding);
|
||||
resetButton.onClick.AddListener(OnResetPathfinding);
|
||||
@ -84,56 +75,38 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
saveButton.onClick.AddListener(OnSaveMap);
|
||||
loadButton.onClick.AddListener(OnLoadMap);
|
||||
|
||||
// Add exit button listener if the button exists
|
||||
if (exitButton != null)
|
||||
exitButton.onClick.AddListener(OnExitApplication);
|
||||
|
||||
// Add listener for maze generator
|
||||
if (generateMazeButton != null)
|
||||
generateMazeButton.onClick.AddListener(OnGenerateMaze);
|
||||
|
||||
// Add visualization control listeners
|
||||
if (visualizationSpeedSlider != null)
|
||||
visualizationSpeedSlider.onValueChanged.AddListener(OnVisualizationSpeedChanged);
|
||||
|
||||
if (visualizationBatchSlider != null)
|
||||
visualizationBatchSlider.onValueChanged.AddListener(OnVisualizationBatchChanged);
|
||||
|
||||
//if (showVisualizationToggle != null)
|
||||
//{
|
||||
// showVisualizationToggle.isOn = npc.showVisualization;
|
||||
// showVisualizationToggle.onValueChanged.AddListener(OnShowVisualizationChanged);
|
||||
//}
|
||||
|
||||
// Subscribe to NPC's pathfinding events
|
||||
npc.OnPathfindingComplete += UpdatePerformanceMetrics;
|
||||
npc.OnPathfindingComplete += OnPathfindingCompleted;
|
||||
|
||||
// Subscribe to visualization completion event
|
||||
npc.OnVisualizationComplete += OnVisualizationCompleted;
|
||||
|
||||
// Subscribe to movement completion event
|
||||
npc.OnMovementComplete += OnMovementCompleted;
|
||||
|
||||
// Initialize performance metrics
|
||||
ClearPerformanceMetrics();
|
||||
|
||||
// Initialize visualization controls
|
||||
InitializeVisualizationControls();
|
||||
|
||||
// Perform algorithm warmup
|
||||
if (performWarmup)
|
||||
{
|
||||
StartCoroutine(WarmupPathfindingSystem());
|
||||
}
|
||||
|
||||
// Tampilkan lokasi penyimpanan
|
||||
ShowSaveLocation();
|
||||
}
|
||||
|
||||
private IEnumerator WarmupPathfindingSystem()
|
||||
{
|
||||
// Wait one frame to ensure everything is initialized
|
||||
yield return null;
|
||||
|
||||
if (showWarmupMessage)
|
||||
@ -141,20 +114,16 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
//Debug.Log("Performing pathfinding warmup...");
|
||||
}
|
||||
|
||||
// Get current NPC position
|
||||
Vector3 npcPos = npc.transform.position;
|
||||
int startX = (int)(npcPos.x / gridMap.GridNodeWidth);
|
||||
int startY = (int)(npcPos.y / gridMap.GridNodeHeight);
|
||||
|
||||
// Find destination node for warmup (try to use opposite corner)
|
||||
int destX = gridMap.NumX - 1;
|
||||
int destY = gridMap.NumY - 1;
|
||||
|
||||
// Ensure destination is walkable
|
||||
GridNode destNode = gridMap.GetGridNode(destX, destY);
|
||||
if (destNode == null || !destNode.IsWalkable)
|
||||
{
|
||||
// Find any walkable node for warmup
|
||||
for (int x = 0; x < gridMap.NumX; x++)
|
||||
{
|
||||
for (int y = 0; y < gridMap.NumY; y++)
|
||||
@ -173,66 +142,46 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
// Save current destination position
|
||||
Vector3 originalDestPos = gridMap.Destination.position;
|
||||
|
||||
// Set temporary destination for warmup
|
||||
gridMap.SetDestination(destX, destY);
|
||||
|
||||
// Run pathfinding quietly (without visualization)
|
||||
GridNode startNode = gridMap.GetGridNode(startX, startY);
|
||||
|
||||
if (startNode != null && destNode != null && destNode.IsWalkable)
|
||||
{
|
||||
// Temporarily disable visualization for warmup
|
||||
float originalVisualizationSpeed = npc.visualizationSpeed;
|
||||
npc.visualizationSpeed = 0f;
|
||||
bool originalShowVisualization = npc.showVisualization;
|
||||
npc.showVisualization = false;
|
||||
|
||||
// Do warmup for each algorithm type to JIT compile all code paths
|
||||
foreach (NPC.PathFinderType algoType in System.Enum.GetValues(typeof(NPC.PathFinderType)))
|
||||
{
|
||||
// Save current algorithm
|
||||
NPC.PathFinderType originalAlgorithm = npc.pathFinderType;
|
||||
|
||||
// Change to this algorithm
|
||||
npc.ChangeAlgorithm(algoType);
|
||||
|
||||
// Run silent pathfinding
|
||||
npc.MoveTo(destNode, true);
|
||||
|
||||
// Wait a bit to ensure completion
|
||||
yield return new WaitForSeconds(0.05f);
|
||||
|
||||
// Reset back to original algorithm
|
||||
npc.ChangeAlgorithm(originalAlgorithm);
|
||||
}
|
||||
|
||||
// Restore visualization settings
|
||||
npc.visualizationSpeed = originalVisualizationSpeed;
|
||||
npc.showVisualization = originalShowVisualization;
|
||||
}
|
||||
|
||||
// Restore original destination
|
||||
gridMap.Destination.position = originalDestPos;
|
||||
|
||||
// Clear metrics from warmup
|
||||
ClearPerformanceMetrics();
|
||||
|
||||
if (showWarmupMessage)
|
||||
{
|
||||
//Debug.Log("Pathfinding warmup complete");
|
||||
}
|
||||
|
||||
|
||||
// Reset grid colors
|
||||
gridMap.ResetGridNodeColours();
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
// Unsubscribe from events
|
||||
if (npc != null)
|
||||
{
|
||||
npc.OnPathfindingComplete -= UpdatePerformanceMetrics;
|
||||
@ -241,7 +190,6 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
npc.OnMovementComplete -= OnMovementCompleted;
|
||||
}
|
||||
|
||||
// Unsubscribe from UI events
|
||||
if (allowDiagonalToggle != null)
|
||||
{
|
||||
allowDiagonalToggle.onValueChanged.RemoveListener(OnDiagonalMovementChanged);
|
||||
@ -250,19 +198,15 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
|
||||
private void InitializeUI()
|
||||
{
|
||||
// Set initial values
|
||||
gridSizeXInput.text = gridMap.NumX.ToString();
|
||||
gridSizeYInput.text = gridMap.NumY.ToString();
|
||||
|
||||
// Set input fields to only accept integers
|
||||
gridSizeXInput.contentType = TMP_InputField.ContentType.IntegerNumber;
|
||||
gridSizeYInput.contentType = TMP_InputField.ContentType.IntegerNumber;
|
||||
|
||||
// Add input validation events
|
||||
gridSizeXInput.onValidateInput += ValidateNumberInput;
|
||||
gridSizeYInput.onValidateInput += ValidateNumberInput;
|
||||
|
||||
// Setup algorithm dropdown
|
||||
algorithmDropdown.ClearOptions();
|
||||
algorithmDropdown.AddOptions(new System.Collections.Generic.List<string> {
|
||||
"A*",
|
||||
@ -272,14 +216,12 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
"BFS"
|
||||
});
|
||||
|
||||
// Initialize diagonal movement toggle
|
||||
if (allowDiagonalToggle != null)
|
||||
{
|
||||
allowDiagonalToggle.isOn = gridMap.AllowDiagonalMovement;
|
||||
allowDiagonalToggle.onValueChanged.AddListener(OnDiagonalMovementChanged);
|
||||
}
|
||||
|
||||
// Setup maze size dropdown
|
||||
if (mazeSizeDropdown != null)
|
||||
{
|
||||
mazeSizeDropdown.ClearOptions();
|
||||
@ -292,7 +234,6 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
});
|
||||
}
|
||||
|
||||
// Setup maze density dropdown
|
||||
if (mazeDensityDropdown != null)
|
||||
{
|
||||
mazeDensityDropdown.ClearOptions();
|
||||
@ -306,17 +247,15 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
ClearPerformanceMetrics();
|
||||
}
|
||||
|
||||
// Validation function to only allow numeric input
|
||||
private char ValidateNumberInput(string text, int charIndex, char addedChar)
|
||||
{
|
||||
// Only allow digits
|
||||
if (char.IsDigit(addedChar))
|
||||
{
|
||||
return addedChar;
|
||||
}
|
||||
else
|
||||
{
|
||||
return '\0'; // Return null character to reject the input
|
||||
return '\0';
|
||||
}
|
||||
}
|
||||
|
||||
@ -336,7 +275,6 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
nodesExploredText.text = $"{metrics.nodesExplored} nodes";
|
||||
memoryUsageText.text = FormatBytes(metrics.memoryUsed);
|
||||
|
||||
// Hitung dan tampilkan CPU usage
|
||||
if (cpuUsageText != null)
|
||||
{
|
||||
float cpuUsagePercentage = (metrics.timeTaken / TARGET_FRAME_TIME_MS) * 100f;
|
||||
@ -362,38 +300,29 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
if (int.TryParse(gridSizeXInput.text, out int newSizeX) &&
|
||||
int.TryParse(gridSizeYInput.text, out int newSizeY))
|
||||
{
|
||||
// Validate grid size limits
|
||||
const int MAX_GRID_SIZE = 200;
|
||||
const int MIN_GRID_SIZE = 2;
|
||||
|
||||
if (newSizeX > MAX_GRID_SIZE || newSizeY > MAX_GRID_SIZE)
|
||||
{
|
||||
// Display an error message
|
||||
Debug.LogWarning($"Grid size cannot exceed {MAX_GRID_SIZE}x{MAX_GRID_SIZE}. Resize operation cancelled.");
|
||||
|
||||
// Revert input fields to current grid size
|
||||
gridSizeXInput.text = gridMap.NumX.ToString();
|
||||
gridSizeYInput.text = gridMap.NumY.ToString();
|
||||
|
||||
// Don't proceed with resize
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for minimum size
|
||||
if (newSizeX < MIN_GRID_SIZE || newSizeY < MIN_GRID_SIZE)
|
||||
{
|
||||
// Display an error message
|
||||
Debug.LogWarning($"Grid size cannot be less than {MIN_GRID_SIZE}x{MIN_GRID_SIZE}. Resize operation cancelled.");
|
||||
|
||||
// Revert input fields to current grid size
|
||||
gridSizeXInput.text = gridMap.NumX.ToString();
|
||||
gridSizeYInput.text = gridMap.NumY.ToString();
|
||||
|
||||
// Don't proceed with resize
|
||||
return;
|
||||
}
|
||||
|
||||
// Apply the grid size (only if within limits)
|
||||
if (gridMap.ResizeGrid(newSizeX, newSizeY))
|
||||
{
|
||||
ClearPerformanceMetrics();
|
||||
@ -403,17 +332,14 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
|
||||
private void OnRunPathfinding()
|
||||
{
|
||||
// Get current NPC position
|
||||
Vector3 npcPos = npc.transform.position;
|
||||
int startX = (int)(npcPos.x / gridMap.GridNodeWidth);
|
||||
int startY = (int)(npcPos.y / gridMap.GridNodeHeight);
|
||||
|
||||
// Get destination position
|
||||
Vector3 destPos = gridMap.Destination.position;
|
||||
int destX = (int)(destPos.x / gridMap.GridNodeWidth);
|
||||
int destY = (int)(destPos.y / gridMap.GridNodeHeight);
|
||||
|
||||
// Run pathfinding
|
||||
GridNode startNode = gridMap.GetGridNode(startX, startY);
|
||||
GridNode endNode = gridMap.GetGridNode(destX, destY);
|
||||
|
||||
@ -421,10 +347,9 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
{
|
||||
ClearPerformanceMetrics();
|
||||
|
||||
// Set flags that pathfinding, visualization, and movement will happen
|
||||
isPathfindingRunning = true;
|
||||
isVisualizationRunning = true;
|
||||
isNpcMoving = true; // assume movement will happen
|
||||
isNpcMoving = true;
|
||||
SetUIInteractivity(false);
|
||||
|
||||
npc.MoveTo(endNode);
|
||||
@ -433,20 +358,15 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
|
||||
private void OnResetPathfinding()
|
||||
{
|
||||
// Reset all flags to ensure UI will be enabled after reload
|
||||
isPathfindingRunning = false;
|
||||
isVisualizationRunning = false;
|
||||
isNpcMoving = false;
|
||||
|
||||
// Force enable UI - this ensures buttons will be enabled
|
||||
// after reset regardless of editor/build status
|
||||
SetUIInteractivity(true);
|
||||
|
||||
// Reload the current scene
|
||||
UnityEngine.SceneManagement.SceneManager.LoadScene(
|
||||
UnityEngine.SceneManagement.SceneManager.GetActiveScene().name);
|
||||
|
||||
//Debug.Log("Reloading scene...");
|
||||
}
|
||||
|
||||
private void OnAlgorithmChanged(int index)
|
||||
@ -460,7 +380,6 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
ClearPerformanceMetrics();
|
||||
}
|
||||
|
||||
// Helper method to get the readable name of the algorithm
|
||||
private string GetAlgorithmName(NPC.PathFinderType type)
|
||||
{
|
||||
switch (type)
|
||||
@ -488,7 +407,6 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
return;
|
||||
}
|
||||
|
||||
// Buat direktori jika belum ada
|
||||
string saveDirectory = Path.Combine(Application.persistentDataPath, "GridSaves");
|
||||
if (!Directory.Exists(saveDirectory))
|
||||
{
|
||||
@ -501,20 +419,13 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
Debug.Log($"Map saved to: {filePath} (includes grid state, NPC position, and destination position)");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Membuka folder penyimpanan di File Explorer
|
||||
/// </summary>
|
||||
public void OpenSaveFolder()
|
||||
{
|
||||
string saveDirectory = Path.Combine(Application.persistentDataPath, "GridSaves");
|
||||
|
||||
// Buat direktori jika belum ada
|
||||
if (!Directory.Exists(saveDirectory))
|
||||
{
|
||||
Directory.CreateDirectory(saveDirectory);
|
||||
}
|
||||
|
||||
// Buka folder di file explorer
|
||||
System.Diagnostics.Process.Start("explorer.exe", saveDirectory);
|
||||
}
|
||||
|
||||
@ -530,22 +441,15 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
//Debug.LogWarning($"Map file not found: {filePath}");
|
||||
return;
|
||||
}
|
||||
|
||||
gridMap.LoadGridState(filePath);
|
||||
ClearPerformanceMetrics();
|
||||
|
||||
//Debug.Log($"Map loaded from: {filePath}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a random maze with the selected size and density
|
||||
/// </summary>
|
||||
private void OnGenerateMaze()
|
||||
{
|
||||
// Get selected maze size
|
||||
int sizeX = 20;
|
||||
int sizeY = 20;
|
||||
bool isLargeGrid = false;
|
||||
@ -569,10 +473,8 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
sizeX = sizeY = 100;
|
||||
isLargeGrid = true;
|
||||
break;
|
||||
// If more options are added that exceed MAX_GRID_SIZE, they'll be rejected
|
||||
}
|
||||
|
||||
// Check if size exceeds maximum or is below minimum - reject if it does
|
||||
if (sizeX > MAX_GRID_SIZE || sizeY > MAX_GRID_SIZE)
|
||||
{
|
||||
Debug.LogWarning($"Maze size {sizeX}x{sizeY} exceeds maximum of {MAX_GRID_SIZE}x{MAX_GRID_SIZE}. Operation cancelled.");
|
||||
@ -585,22 +487,18 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
return;
|
||||
}
|
||||
|
||||
// Resize grid if needed
|
||||
if (gridMap.NumX != sizeX || gridMap.NumY != sizeY)
|
||||
{
|
||||
if (!gridMap.ResizeGrid(sizeX, sizeY))
|
||||
{
|
||||
// Resize failed, abort maze generation
|
||||
return;
|
||||
}
|
||||
|
||||
// Update grid size inputs
|
||||
gridSizeXInput.text = sizeX.ToString();
|
||||
gridSizeYInput.text = sizeY.ToString();
|
||||
}
|
||||
|
||||
// Get selected density
|
||||
float density = 30f; // Default medium
|
||||
float density = 30f;
|
||||
|
||||
switch (mazeDensityDropdown.value)
|
||||
{
|
||||
@ -615,61 +513,40 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
break;
|
||||
}
|
||||
|
||||
// Untuk grid besar, nonaktifkan visualisasi sementara untuk performa lebih baik
|
||||
bool originalShowVisualization = false;
|
||||
float originalVisualizationSpeed = 0f;
|
||||
|
||||
if (isLargeGrid && npc != null)
|
||||
{
|
||||
// Simpan nilai asli
|
||||
originalShowVisualization = npc.showVisualization;
|
||||
originalVisualizationSpeed = npc.visualizationSpeed;
|
||||
|
||||
// Nonaktifkan visualisasi untuk grid besar
|
||||
npc.showVisualization = false;
|
||||
}
|
||||
|
||||
// Generate the maze with selected density
|
||||
gridMap.GenerateRandomMaze(density);
|
||||
|
||||
// Kembalikan nilai visualisasi jika diubah
|
||||
if (isLargeGrid && npc != null)
|
||||
{
|
||||
npc.showVisualization = originalShowVisualization;
|
||||
npc.visualizationSpeed = originalVisualizationSpeed;
|
||||
}
|
||||
|
||||
// Clear performance metrics
|
||||
ClearPerformanceMetrics();
|
||||
|
||||
// Tampilkan pesan khusus untuk grid besar
|
||||
if (isLargeGrid)
|
||||
{
|
||||
//Debug.Log("Large maze generated. For best performance, consider disabling visualization during pathfinding.");
|
||||
}
|
||||
|
||||
//Debug.Log($"Generated maze with size {sizeX}x{sizeY} and density {density}%");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Menampilkan lokasi penyimpanan file di konsol
|
||||
/// </summary>
|
||||
private void ShowSaveLocation()
|
||||
{
|
||||
string saveDirectory = Path.Combine(Application.persistentDataPath, "GridSaves");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Menutup aplikasi saat tombol exit ditekan
|
||||
/// </summary>
|
||||
public void OnExitApplication()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
// Jika di Unity Editor
|
||||
UnityEditor.EditorApplication.isPlaying = false;
|
||||
#else
|
||||
// Jika di build
|
||||
Application.Quit();
|
||||
// Jika di build
|
||||
Application.Quit();
|
||||
#endif
|
||||
|
||||
Debug.Log("Application exit requested");
|
||||
@ -677,7 +554,6 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
|
||||
private void InitializeVisualizationControls()
|
||||
{
|
||||
// Set initial slider values based on NPC settings
|
||||
if (visualizationSpeedSlider != null)
|
||||
{
|
||||
visualizationSpeedSlider.value = npc.visualizationSpeed;
|
||||
@ -699,7 +575,6 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
|
||||
private void OnVisualizationBatchChanged(float newValue)
|
||||
{
|
||||
// Pastikan nilai batch adalah integer
|
||||
int batchValue = Mathf.RoundToInt(newValue);
|
||||
npc.visualizationBatch = batchValue;
|
||||
UpdateBatchValueText(batchValue);
|
||||
@ -725,31 +600,22 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
batchValueText.text = value.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Menangani perubahan toggle diagonal movement
|
||||
/// </summary>
|
||||
private void OnDiagonalMovementChanged(bool isOn)
|
||||
{
|
||||
gridMap.AllowDiagonalMovement = isOn;
|
||||
// Reset performance metrics
|
||||
ClearPerformanceMetrics();
|
||||
}
|
||||
|
||||
// New method to handle pathfinding completion
|
||||
private void OnPathfindingCompleted(PathfindingMetrics metrics)
|
||||
{
|
||||
// Pathfinding is completed, but visualization might still be running
|
||||
isPathfindingRunning = false;
|
||||
|
||||
// Check if pathfinding failed by looking at metrics or path length
|
||||
bool pathfindingFailed = (metrics.pathLength == 0) ||
|
||||
(npc.pathFinder != null &&
|
||||
npc.pathFinder.Status == PathFinding.PathFinderStatus.FAILURE);
|
||||
|
||||
if (pathfindingFailed)
|
||||
{
|
||||
// If pathfinding failed, there won't be any visualization or movement
|
||||
Debug.Log("Pathfinding failed - re-enabling UI controls immediately");
|
||||
isVisualizationRunning = false;
|
||||
isNpcMoving = false;
|
||||
@ -765,22 +631,15 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
return;
|
||||
}
|
||||
|
||||
// If pathfinding succeeded, continue with normal flow
|
||||
// Very important: Keep UI disabled regardless of visualization state to prevent
|
||||
// the brief window of interactivity between pathfinding completion and visualization start
|
||||
|
||||
if (npc.showVisualization)
|
||||
{
|
||||
// If visualization is enabled in settings, assume it will start soon
|
||||
// Keep UI disabled by keeping isVisualizationRunning true
|
||||
isVisualizationRunning = true;
|
||||
// Do NOT enable UI here - wait for visualization to complete
|
||||
}
|
||||
else
|
||||
{
|
||||
// Only if visualization is completely disabled in settings, enable UI
|
||||
isVisualizationRunning = false;
|
||||
|
||||
// Only re-enable in editor mode
|
||||
#if UNITY_EDITOR
|
||||
SetUIInteractivity(true);
|
||||
#else
|
||||
@ -790,25 +649,18 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
// New method to handle visualization completion
|
||||
private void OnVisualizationCompleted()
|
||||
{
|
||||
// Visualization is completed, but NPC may start moving
|
||||
isVisualizationRunning = false;
|
||||
|
||||
// Check if NPC is moving or will move
|
||||
if (npc.IsMoving || npc.wayPoints.Count > 0)
|
||||
{
|
||||
// Movement is starting or in progress
|
||||
isNpcMoving = true;
|
||||
// Leave UI disabled
|
||||
}
|
||||
else
|
||||
{
|
||||
// No movement expected
|
||||
isNpcMoving = false;
|
||||
|
||||
// Only re-enable in editor mode
|
||||
#if UNITY_EDITOR
|
||||
SetUIInteractivity(true);
|
||||
#else
|
||||
@ -818,13 +670,10 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
// New method to handle movement completion
|
||||
private void OnMovementCompleted()
|
||||
{
|
||||
// Movement is completed, re-enable UI buttons only in editor
|
||||
isNpcMoving = false;
|
||||
|
||||
// Only re-enable in editor mode
|
||||
#if UNITY_EDITOR
|
||||
SetUIInteractivity(true);
|
||||
#else
|
||||
@ -834,13 +683,10 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
#endif
|
||||
}
|
||||
|
||||
// Method to enable/disable UI elements based on pathfinding, visualization, and movement state
|
||||
private void SetUIInteractivity(bool enabled)
|
||||
{
|
||||
// If any process is running, disable controls
|
||||
bool shouldEnable = enabled && !isPathfindingRunning && !isVisualizationRunning && !isNpcMoving;
|
||||
|
||||
// In builds (not editor), once disabled, buttons stay disabled until reset
|
||||
#if !UNITY_EDITOR
|
||||
if (shouldEnable && (isPathfindingRunning || isVisualizationRunning || isNpcMoving))
|
||||
{
|
||||
@ -850,13 +696,10 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
}
|
||||
#endif
|
||||
|
||||
// Add debug logging
|
||||
Debug.Log($"SetUIInteractivity called with enabled={enabled}, pathfinding={isPathfindingRunning}, " +
|
||||
$"visualization={isVisualizationRunning}, movement={isNpcMoving}, shouldEnable={shouldEnable}, " +
|
||||
$"inEditor={Application.isEditor}");
|
||||
|
||||
// Keep reset and exit buttons always enabled
|
||||
// Disable all other buttons when processes are running
|
||||
|
||||
if (applyGridSizeButton != null)
|
||||
applyGridSizeButton.interactable = shouldEnable;
|
||||
@ -899,17 +742,12 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
|
||||
if (mapNameInput != null)
|
||||
mapNameInput.interactable = shouldEnable;
|
||||
|
||||
// Reset and exit buttons remain enabled
|
||||
// resetButton and exitButton stay interactable
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// Continuously check for various states - this ensures buttons stay disabled
|
||||
if (npc != null)
|
||||
{
|
||||
// Check visualization
|
||||
if (npc.IsVisualizingPath && !isVisualizationRunning)
|
||||
{
|
||||
Debug.Log("Detected active visualization - updating UI state");
|
||||
@ -917,7 +755,6 @@ public class PathfindingUIManager : MonoBehaviour
|
||||
SetUIInteractivity(false);
|
||||
}
|
||||
|
||||
// Check movement
|
||||
if (npc.IsMoving && !isNpcMoving)
|
||||
{
|
||||
Debug.Log("Detected active NPC movement - updating UI state");
|
||||
|
Reference in New Issue
Block a user