-
Notifications
You must be signed in to change notification settings - Fork 309
Expand file tree
/
Copy pathDebugPathOutput.cs
More file actions
69 lines (60 loc) · 1.99 KB
/
DebugPathOutput.cs
File metadata and controls
69 lines (60 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using View;
namespace UnitBrains.Pathfinding
{
public class DebugPathOutput : MonoBehaviour
{
[SerializeField] private GameObject cellHighlightPrefab;
[SerializeField] private int maxHighlights = 5;
public BaseUnitPath Path { get; private set; }
private readonly List<GameObject> allHighlights = new();
private Coroutine highlightCoroutine;
public void HighlightPath(BaseUnitPath path)
{
Path = path;
while (allHighlights.Count > 0)
{
DestroyHighlight(0);
}
if (highlightCoroutine != null)
{
StopCoroutine(highlightCoroutine);
}
highlightCoroutine = StartCoroutine(HighlightCoroutine(path));
}
private IEnumerator HighlightCoroutine(BaseUnitPath path)
{
while (true)
{
foreach (var atCell in path.GetPath())
{
CreateHighlight(atCell);
if (allHighlights.Count > 5)
{
DestroyHighlight(0);
}
yield return new WaitForSeconds(0.1f);
}
while (allHighlights.Count > 0)
{
DestroyHighlight(0);
yield return new WaitForSeconds(0.1f);
}
}
}
private void CreateHighlight(Vector2Int atCell)
{
var pos = Gameplay3dView.ToWorldPosition(atCell, 1f);
var highlight = Instantiate(cellHighlightPrefab, pos, Quaternion.identity);
highlight.transform.SetParent(transform);
allHighlights.Add(highlight);
}
private void DestroyHighlight(int index)
{
Destroy(allHighlights[index]);
allHighlights.RemoveAt(index);
}
}
}