public void OnPostScan(AstarPath script)
{
AstarPath.OnPostScan -= new OnScanDelegate (OnPostScan);
if (!autoLinkGrids || autoLinkDistLimit <= 0) {
return;
}
//Link to other grids
int maxCost = Mathf.RoundToInt (autoLinkDistLimit * Int3.Precision);
//Loop through all GridGraphs
foreach (GridGraph gg in script.astarData.FindGraphsOfType (typeof (GridGraph))) {
if (gg == this || gg.nodes == null || nodes == null) {
continue;
}
//Int3 prevPos = gg.GetNearest (nodes[0]).position;
//Z = 0
for (int x = 0; x < width;x++) {
Node node1 = nodes[x];
Node node2 = gg.GetNearest ((Vector3)node1.position).node;
Vector3 pos = inverseMatrix.MultiplyPoint3x4 ((Vector3)node2.position);
if (pos.z > 0) {
continue;
}
int cost = (node1.position-node2.position).costMagnitude;
if (cost > maxCost) {
continue;
}
node1.AddConnection (node2,cost);
node2.AddConnection (node1,cost);
}
//X = 0
for (int z = 0; z < depth;z++) {
Node node1 = nodes[z*width];
Node node2 = gg.GetNearest ((Vector3)node1.position).node;
Vector3 pos = inverseMatrix.MultiplyPoint3x4 ((Vector3)node2.position);
if (pos.x > 0) {
continue;
}
int cost = (node1.position-node2.position).costMagnitude;
if (cost > maxCost) {
continue;
}
node1.AddConnection (node2,cost);
node2.AddConnection (node1,cost);
}
//Z = max
for (int x = 0; x < width;x++) {
Node node1 = nodes[(depth-1)*width+x];
Node node2 = gg.GetNearest ((Vector3)node1.position).node;
Vector3 pos = inverseMatrix.MultiplyPoint3x4 ((Vector3)node2.position);
if (pos.z < depth-1) {
continue;
}
//Debug.DrawLine (node1.position,node2.position,Color.red);
int cost = (node1.position-node2.position).costMagnitude;
if (cost > maxCost) {
continue;
}
node1.AddConnection (node2,cost);
node2.AddConnection (node1,cost);
}
//X = max
for (int z = 0; z < depth;z++) {
Node node1 = nodes[z*width+width-1];
Node node2 = gg.GetNearest ((Vector3)node1.position).node;
Vector3 pos = inverseMatrix.MultiplyPoint3x4 ((Vector3)node2.position);
if (pos.x < width-1) {
continue;
}
int cost = (node1.position-node2.position).costMagnitude;
if (cost > maxCost) {
continue;
}
node1.AddConnection (node2,cost);
node2.AddConnection (node1,cost);
}
}
}