-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavigationActionGrid.cs
140 lines (114 loc) · 4.31 KB
/
NavigationActionGrid.cs
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System.Collections.Generic;
using UnityEngine;
namespace AStar.ActionGrid
{
/// <summary>
/// Auto refreshable walkable grid
/// </summary>
public class NavigationActionGrid : MonoBehaviour, INavigationActionGrid
{
public IGraph Graph { get; set; }
public IGraphNeighborsService NeighborsService { get; set; }
public float NotWalkableCost = 1;
public float WalkableCost = 1;
private readonly List<Vector2Int> neighborsBuffer = new List<Vector2Int>();
public float Width
{
get { return _Width; }
set { _Width = value; }
}
public float Height
{
get { return _Height; }
set { _Height = value; }
}
[SerializeField]
private float _Width;
[SerializeField]
private float _Height;
/// <summary>
/// Hitted layers by raycast when update transtions
/// </summary>
public LayerMask RaycastLayers;
/// <summary>
/// if true, transition will update auto at start event
/// </summary>
public bool RefreshOnStart;
public void Start()
{
if (!RefreshOnStart)
{
return;
}
RefreshTransitions();
}
/// <summary>
/// Refresh transitions for current graph configuration in plane
/// </summary>
[ContextMenu("Refresh transitions")]
public void RefreshTransitions()
{
for (int x = 0; x < Graph.Width; x++)
{
for (int y = 0; y < Graph.Height; y++)
{
var point = new Vector2Int(x, y);
var position = GetCellPoint(point);
NeighborsService.GetNeighbors(Graph, point, neighborsBuffer);
foreach (var i in neighborsBuffer)
{
var neighbor = GetCellPoint(i);
var dir = (neighbor - position).normalized;
var distance = Vector3.Distance(neighbor, position);
var transitionCost = WalkableCost;
if (Physics.Raycast(position, dir, distance, RaycastLayers.value) ||
Physics.Raycast(neighbor, -dir, distance, RaycastLayers.value))
{
transitionCost = NotWalkableCost;
}
Graph.SetTransition(point, i, transitionCost);
}
}
}
}
/// <summary>
/// Calculate point in world space relative for graph
/// </summary>
public Vector3 GetCellPoint(Vector2Int node)
{
return transform.TransformPoint(new Vector3(node.x * (Width / Graph.Width), 0,
node.y * (Height / Graph.Height)));
}
private void OnDrawGizmos()
{
if (Graph == null || NeighborsService == null)
{
return;
}
if (!Application.isPlaying)
{
RefreshTransitions();
}
for (int x = 0; x < Graph.Width; x++)
{
for (int y = 0; y < Graph.Height; y++)
{
var point = new Vector2Int(x, y);
var position = GetCellPoint(point);
NeighborsService.GetNeighbors(Graph, point, neighborsBuffer);
foreach (var i in neighborsBuffer)
{
var neighbor = GetCellPoint(i);
var dir = (neighbor - position).normalized;
var distance = Vector3.Distance(neighbor, position);
var transition = Graph.GetTransition(point, i);
Gizmos.color = Color.Lerp(Color.green, Color.red, transition);
Gizmos.DrawLine(position, neighbor);
}
Gizmos.color = Color.cyan;
Gizmos.DrawSphere(position, 0.8f);
}
}
}
}
}