mirror of
https://github.com/BobbyRafael31/Unity-MazeRunner-Pathfinding-Visualizer.git
synced 2025-08-12 08:42:21 +00:00
fix: fix greedy visualization in allowed diagonal, and add some UI
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@ -23,7 +23,14 @@ public class GridMap : MonoBehaviour
|
|||||||
|
|
||||||
// Mengizinkan atau melarang pergerakan diagonal pada pathfinding
|
// Mengizinkan atau melarang pergerakan diagonal pada pathfinding
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
bool allowDiagonalMovement = true;
|
bool allowDiagonalMovement = false;
|
||||||
|
|
||||||
|
// Properti publik untuk mengakses allowDiagonalMovement dari luar
|
||||||
|
public bool AllowDiagonalMovement
|
||||||
|
{
|
||||||
|
get { return allowDiagonalMovement; }
|
||||||
|
set { allowDiagonalMovement = value; }
|
||||||
|
}
|
||||||
|
|
||||||
// Warna-warna untuk representasi visual berbagai status node
|
// Warna-warna untuk representasi visual berbagai status node
|
||||||
public Color COLOR_WALKABLE = new Color(0.4f, 0.4f, 0.8f, 1.0f); // Warna untuk node yang dapat dilalui
|
public Color COLOR_WALKABLE = new Color(0.4f, 0.4f, 0.8f, 1.0f); // Warna untuk node yang dapat dilalui
|
||||||
|
@ -1065,7 +1065,7 @@ namespace PathFinding
|
|||||||
|
|
||||||
// Flag untuk batch processing
|
// Flag untuk batch processing
|
||||||
private bool processingBatch = false;
|
private bool processingBatch = false;
|
||||||
private List<Node<T>> neighborBatch = new List<Node<T>>(8);
|
private List<Node<T>> neighborBatch = new List<Node<T>>(4);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Implementasi spesifik algoritma Greedy untuk memproses node tetangga
|
/// Implementasi spesifik algoritma Greedy untuk memproses node tetangga
|
||||||
@ -1079,6 +1079,7 @@ namespace PathFinding
|
|||||||
|
|
||||||
// Hitung biaya G yang sebenarnya (jarak dari start)
|
// Hitung biaya G yang sebenarnya (jarak dari start)
|
||||||
float G = CurrentNode.GCost + NodeTraversalCost(CurrentNode.Location.Value, cell.Value);
|
float G = CurrentNode.GCost + NodeTraversalCost(CurrentNode.Location.Value, cell.Value);
|
||||||
|
float H;
|
||||||
|
|
||||||
// Periksa apakah node sudah ada di open list
|
// Periksa apakah node sudah ada di open list
|
||||||
PathFinderNode existingNode = null;
|
PathFinderNode existingNode = null;
|
||||||
@ -1087,7 +1088,7 @@ namespace PathFinding
|
|||||||
if (!nodeExists)
|
if (!nodeExists)
|
||||||
{
|
{
|
||||||
// Hitung heuristik dengan normal
|
// Hitung heuristik dengan normal
|
||||||
float H;
|
|
||||||
if (EqualityComparer<T>.Default.Equals(cell.Value, Goal.Value))
|
if (EqualityComparer<T>.Default.Equals(cell.Value, Goal.Value))
|
||||||
{
|
{
|
||||||
// Langsung ke tujuan - prioritaskan dengan H = 0
|
// Langsung ke tujuan - prioritaskan dengan H = 0
|
||||||
@ -1101,10 +1102,11 @@ namespace PathFinding
|
|||||||
// Buat node dan tambahkan ke open list - gunakan G yang benar
|
// Buat node dan tambahkan ke open list - gunakan G yang benar
|
||||||
PathFinderNode n = new PathFinderNode(cell, CurrentNode, G, H);
|
PathFinderNode n = new PathFinderNode(cell, CurrentNode, G, H);
|
||||||
openList.Enqueue(n);
|
openList.Enqueue(n);
|
||||||
|
onAddToOpenList?.Invoke(n);
|
||||||
openSet[cell.Value] = n;
|
openSet[cell.Value] = n;
|
||||||
|
|
||||||
if (!processingBatch && onAddToOpenList != null)
|
//if (!processingBatch && onAddToOpenList != null)
|
||||||
onAddToOpenList.Invoke(n);
|
// onAddToOpenList.Invoke(n);
|
||||||
}
|
}
|
||||||
else if (G < existingNode.GCost)
|
else if (G < existingNode.GCost)
|
||||||
{
|
{
|
||||||
@ -1117,10 +1119,11 @@ namespace PathFinding
|
|||||||
// Jika kita menggunakan G sebagai tie-breaker, maka perlu update prioritas
|
// Jika kita menggunakan G sebagai tie-breaker, maka perlu update prioritas
|
||||||
// karena G yang lebih rendah sekarang bisa mempengaruhi urutan priority queue
|
// karena G yang lebih rendah sekarang bisa mempengaruhi urutan priority queue
|
||||||
openList.UpdatePriority(existingNode, existingNode.HCost);
|
openList.UpdatePriority(existingNode, existingNode.HCost);
|
||||||
|
onAddToOpenList.Invoke(existingNode);
|
||||||
|
|
||||||
// Callback untuk UI
|
// Callback untuk UI
|
||||||
if (!processingBatch && onAddToOpenList != null)
|
//if (!processingBatch && onAddToOpenList != null)
|
||||||
onAddToOpenList.Invoke(existingNode);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1161,7 +1164,7 @@ namespace PathFinding
|
|||||||
neighborBatch.AddRange(CurrentNode.Location.GetNeighbours());
|
neighborBatch.AddRange(CurrentNode.Location.GetNeighbours());
|
||||||
|
|
||||||
// Proses tetangga dalam batch untuk mengurangi callback overhead
|
// Proses tetangga dalam batch untuk mengurangi callback overhead
|
||||||
processingBatch = neighborBatch.Count > 5; // Gunakan batch hanya jika cukup banyak tetangga
|
// processingBatch = neighborBatch.Count > 5; // Gunakan batch hanya jika cukup banyak tetangga
|
||||||
|
|
||||||
foreach (Node<T> cell in neighborBatch)
|
foreach (Node<T> cell in neighborBatch)
|
||||||
{
|
{
|
||||||
|
@ -20,6 +20,14 @@ public class PathfindingUIManager : MonoBehaviour
|
|||||||
public TMP_Dropdown algorithmDropdown;
|
public TMP_Dropdown algorithmDropdown;
|
||||||
public Button runPathfindingButton;
|
public Button runPathfindingButton;
|
||||||
public Button resetButton;
|
public Button resetButton;
|
||||||
|
public Toggle allowDiagonalToggle; // Toggle untuk diagonal movement
|
||||||
|
|
||||||
|
[Header("Visualization Controls")]
|
||||||
|
public Slider visualizationSpeedSlider;
|
||||||
|
public Slider visualizationBatchSlider;
|
||||||
|
// public Toggle showVisualizationToggle;
|
||||||
|
public TMP_Text speedValueText;
|
||||||
|
public TMP_Text batchValueText;
|
||||||
|
|
||||||
[Header("Performance Metrics")]
|
[Header("Performance Metrics")]
|
||||||
public TMP_Text timeEstimateText;
|
public TMP_Text timeEstimateText;
|
||||||
@ -68,12 +76,28 @@ public class PathfindingUIManager : MonoBehaviour
|
|||||||
if (generateMazeButton != null)
|
if (generateMazeButton != null)
|
||||||
generateMazeButton.onClick.AddListener(OnGenerateMaze);
|
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
|
// Subscribe to NPC's pathfinding events
|
||||||
npc.OnPathfindingComplete += UpdatePerformanceMetrics;
|
npc.OnPathfindingComplete += UpdatePerformanceMetrics;
|
||||||
|
|
||||||
// Initialize performance metrics
|
// Initialize performance metrics
|
||||||
ClearPerformanceMetrics();
|
ClearPerformanceMetrics();
|
||||||
|
|
||||||
|
// Initialize visualization controls
|
||||||
|
InitializeVisualizationControls();
|
||||||
|
|
||||||
// Perform algorithm warmup
|
// Perform algorithm warmup
|
||||||
if (performWarmup)
|
if (performWarmup)
|
||||||
{
|
{
|
||||||
@ -190,6 +214,12 @@ public class PathfindingUIManager : MonoBehaviour
|
|||||||
{
|
{
|
||||||
npc.OnPathfindingComplete -= UpdatePerformanceMetrics;
|
npc.OnPathfindingComplete -= UpdatePerformanceMetrics;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Unsubscribe from UI events
|
||||||
|
if (allowDiagonalToggle != null)
|
||||||
|
{
|
||||||
|
allowDiagonalToggle.onValueChanged.RemoveListener(OnDiagonalMovementChanged);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void InitializeUI()
|
private void InitializeUI()
|
||||||
@ -208,6 +238,13 @@ public class PathfindingUIManager : MonoBehaviour
|
|||||||
"BFS"
|
"BFS"
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Initialize diagonal movement toggle
|
||||||
|
if (allowDiagonalToggle != null)
|
||||||
|
{
|
||||||
|
allowDiagonalToggle.isOn = gridMap.AllowDiagonalMovement;
|
||||||
|
allowDiagonalToggle.onValueChanged.AddListener(OnDiagonalMovementChanged);
|
||||||
|
}
|
||||||
|
|
||||||
// Setup maze size dropdown
|
// Setup maze size dropdown
|
||||||
if (mazeSizeDropdown != null)
|
if (mazeSizeDropdown != null)
|
||||||
{
|
{
|
||||||
@ -488,4 +525,65 @@ public class PathfindingUIManager : MonoBehaviour
|
|||||||
|
|
||||||
Debug.Log("Application exit requested");
|
Debug.Log("Application exit requested");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void InitializeVisualizationControls()
|
||||||
|
{
|
||||||
|
// Set initial slider values based on NPC settings
|
||||||
|
if (visualizationSpeedSlider != null)
|
||||||
|
{
|
||||||
|
visualizationSpeedSlider.value = npc.visualizationSpeed;
|
||||||
|
UpdateSpeedValueText(npc.visualizationSpeed);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (visualizationBatchSlider != null)
|
||||||
|
{
|
||||||
|
visualizationBatchSlider.value = npc.visualizationBatch;
|
||||||
|
UpdateBatchValueText(npc.visualizationBatch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnVisualizationSpeedChanged(float newValue)
|
||||||
|
{
|
||||||
|
npc.visualizationSpeed = newValue;
|
||||||
|
UpdateSpeedValueText(newValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnVisualizationBatchChanged(float newValue)
|
||||||
|
{
|
||||||
|
// Pastikan nilai batch adalah integer
|
||||||
|
int batchValue = Mathf.RoundToInt(newValue);
|
||||||
|
npc.visualizationBatch = batchValue;
|
||||||
|
UpdateBatchValueText(batchValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnShowVisualizationChanged(bool isOn)
|
||||||
|
{
|
||||||
|
npc.showVisualization = isOn;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdateSpeedValueText(float value)
|
||||||
|
{
|
||||||
|
if (speedValueText != null)
|
||||||
|
{
|
||||||
|
speedValueText.text = value.ToString("F2") + "s";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdateBatchValueText(int value)
|
||||||
|
{
|
||||||
|
if (batchValueText != null)
|
||||||
|
{
|
||||||
|
batchValueText.text = value.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Menangani perubahan toggle diagonal movement
|
||||||
|
/// </summary>
|
||||||
|
private void OnDiagonalMovementChanged(bool isOn)
|
||||||
|
{
|
||||||
|
gridMap.AllowDiagonalMovement = isOn;
|
||||||
|
// Reset performance metrics
|
||||||
|
ClearPerformanceMetrics();
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user