fix: fix greedy visualization in allowed diagonal, and add some UI

This commit is contained in:
Bobby Rafael
2025-04-30 00:49:32 +07:00
parent df18c63766
commit 15ff52f683
4 changed files with 1993 additions and 8 deletions

File diff suppressed because it is too large Load Diff

View File

@ -23,7 +23,14 @@ public class GridMap : MonoBehaviour
// Mengizinkan atau melarang pergerakan diagonal pada pathfinding
[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
public Color COLOR_WALKABLE = new Color(0.4f, 0.4f, 0.8f, 1.0f); // Warna untuk node yang dapat dilalui

View File

@ -1065,7 +1065,7 @@ namespace PathFinding
// Flag untuk batch processing
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>
/// Implementasi spesifik algoritma Greedy untuk memproses node tetangga
@ -1079,6 +1079,7 @@ namespace PathFinding
// Hitung biaya G yang sebenarnya (jarak dari start)
float G = CurrentNode.GCost + NodeTraversalCost(CurrentNode.Location.Value, cell.Value);
float H;
// Periksa apakah node sudah ada di open list
PathFinderNode existingNode = null;
@ -1087,7 +1088,7 @@ namespace PathFinding
if (!nodeExists)
{
// Hitung heuristik dengan normal
float H;
if (EqualityComparer<T>.Default.Equals(cell.Value, Goal.Value))
{
// Langsung ke tujuan - prioritaskan dengan H = 0
@ -1101,10 +1102,11 @@ namespace PathFinding
// Buat node dan tambahkan ke open list - gunakan G yang benar
PathFinderNode n = new PathFinderNode(cell, CurrentNode, G, H);
openList.Enqueue(n);
onAddToOpenList?.Invoke(n);
openSet[cell.Value] = n;
if (!processingBatch && onAddToOpenList != null)
onAddToOpenList.Invoke(n);
//if (!processingBatch && onAddToOpenList != null)
// onAddToOpenList.Invoke(n);
}
else if (G < existingNode.GCost)
{
@ -1117,10 +1119,11 @@ namespace PathFinding
// Jika kita menggunakan G sebagai tie-breaker, maka perlu update prioritas
// karena G yang lebih rendah sekarang bisa mempengaruhi urutan priority queue
openList.UpdatePriority(existingNode, existingNode.HCost);
onAddToOpenList.Invoke(existingNode);
// Callback untuk UI
if (!processingBatch && onAddToOpenList != null)
onAddToOpenList.Invoke(existingNode);
//if (!processingBatch && onAddToOpenList != null)
}
}
@ -1161,7 +1164,7 @@ namespace PathFinding
neighborBatch.AddRange(CurrentNode.Location.GetNeighbours());
// 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)
{

View File

@ -20,6 +20,14 @@ public class PathfindingUIManager : MonoBehaviour
public TMP_Dropdown algorithmDropdown;
public Button runPathfindingButton;
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")]
public TMP_Text timeEstimateText;
@ -68,12 +76,28 @@ public class PathfindingUIManager : MonoBehaviour
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;
// Initialize performance metrics
ClearPerformanceMetrics();
// Initialize visualization controls
InitializeVisualizationControls();
// Perform algorithm warmup
if (performWarmup)
{
@ -190,6 +214,12 @@ public class PathfindingUIManager : MonoBehaviour
{
npc.OnPathfindingComplete -= UpdatePerformanceMetrics;
}
// Unsubscribe from UI events
if (allowDiagonalToggle != null)
{
allowDiagonalToggle.onValueChanged.RemoveListener(OnDiagonalMovementChanged);
}
}
private void InitializeUI()
@ -208,6 +238,13 @@ 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)
{
@ -488,4 +525,65 @@ public class PathfindingUIManager : MonoBehaviour
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();
}
}