public void AddPortal(GridNode n1, GridNode n2, List<Vector3> left, List<Vector3> right)
{
if (n1 == n2) {
return;
}
int i1 = n1.GetIndex ();
int i2 = n2.GetIndex ();
int x1 = i1 % width;
int x2 = i2 % width;
int z1 = i1 / width;
int z2 = i2 / width;
Vector3 n1p = (Vector3)n1.position;
Vector3 n2p = (Vector3)n2.position;
int diffx = Mathf.Abs (x1-x2);
int diffz = Mathf.Abs (z1-z2);
if (diffx > 1 || diffz > 1) {
//If the nodes are not adjacent to each other
left.Add (n1p);
right.Add (n1p);
left.Add (n2p);
right.Add (n2p);
} else if ((diffx+diffz) <= 1){
//If it is not a diagonal move
Vector3 dir = n2p - n1p;
dir = dir.normalized * nodeSize * 0.5F;
Vector3 tangent = Vector3.Cross (dir, Vector3.up);
tangent = tangent.normalized * nodeSize * 0.5F;
left.Add (n1p + dir - tangent);
right.Add (n1p + dir + tangent);
} else {
//Diagonal move
Node t1 = nodes[z1 * width + x2];
Node t2 = nodes[z2 * width + x1];
Node target = null;
if (t1.walkable) {
target = t1;
} else if (t2.walkable) {
target = t2;
}
if (target == null) {
Vector3 avg = (n1p + n2p) * 0.5F;
left.Add (avg);
right.Add (avg);
} else {
AddPortal (n1,(GridNode)target,left,right);
AddPortal ((GridNode)target,n2,left,right);
}
}
}