Pathfinding.LayerGridGraph.CalculateConnections C# (CSharp) Method

CalculateConnections() public method

public CalculateConnections ( GraphNode nodes, GraphNode node, int x, int z, int layerIndex ) : void
nodes GraphNode
node GraphNode
x int
z int
layerIndex int
return void
		public void CalculateConnections (GraphNode[] nodes, GraphNode node, int x, int z, int layerIndex) {
			
			if (node == null) return;
			
			LevelGridNode lgn = (LevelGridNode)node;
			lgn.ResetAllGridConnections ();
			
			if (!node.Walkable) {
				return;
			}
			
			float height = 0;
			if (layerIndex == layerCount-1 || nodes[lgn.NodeInGridIndex + width*depth*(layerIndex+1)] == null) {
				height = float.PositiveInfinity;
			} else {
				height = System.Math.Abs (lgn.position.y - nodes[lgn.NodeInGridIndex+width*depth*(layerIndex+1)].position.y)*Int3.PrecisionFactor;
			}
			
			for (int dir=0;dir<4;dir++) {
				
				int nx = x + neighbourXOffsets[dir];
				int nz = z + neighbourZOffsets[dir];
				
				//Check for out-of-bounds
				if (nx < 0 || nz < 0 || nx >= width || nz >= depth) {
					continue;
				}
				
				//Calculate new index
				int nIndex = nz*width+nx;
				int conn = LevelGridNode.NoConnection;
				
				for (int i=0;i<layerCount;i++) {
					GraphNode other = nodes[nIndex + width*depth*i];
					if (other != null && other.Walkable) {
						
						float otherHeight = 0;
						
						//Is there a node above this one
						if (i == layerCount-1 || nodes[nIndex+width*depth*(i+1)] == null) {
							otherHeight = float.PositiveInfinity;
						} else {
							otherHeight = System.Math.Abs (other.position.y - nodes[nIndex+width*depth*(i+1)].position.y)*Int3.PrecisionFactor;
						}
						
						float bottom = Mathf.Max (other.position.y*Int3.PrecisionFactor,lgn.position.y*Int3.PrecisionFactor);
						float top = Mathf.Min (other.position.y*Int3.PrecisionFactor+otherHeight,lgn.position.y*Int3.PrecisionFactor+height);
						
						float dist = top-bottom;
						
						if (dist >= characterHeight && Mathf.Abs (other.position.y-lgn.position.y)*Int3.PrecisionFactor <= maxClimb) {
							
						
							//Debug.DrawLine (lgn.position,other.position,new Color (0,1,0,0.5F));
							conn = i;
						}
					}
				}
				
				lgn.SetConnectionValue (dir,conn);
				
				/*LinkedLevelCell llc = linkedCells[nIndex];
				//LinkedLevelNode lln = llc.first;
				
				int conn = 0xF;
				//float minDist = Mathf.Infinity;
				
				for (int i=0;i<llc.count;i++) {
					LevelGridNode other = (LevelGridNode)nodes[llc.index+i];
					
					if (!other.walkable) {
						continue;
					}
					
					float bottom = Mathf.Max (other.position.y*Int3.PrecisionFactor,lgn.position.y*Int3.PrecisionFactor);
					float top = Mathf.Min (other.position.y*Int3.PrecisionFactor+other.height,lgn.position.y*Int3.PrecisionFactor+lgn.height);
					
					float dist = top-bottom;
					
					//if (z == 3) {
					//	Debug.DrawRay (lgn.position,Vector3.up*(dist >= characterHeight ? 2 : 0)*0.9F,Color.yellow);
					//}
					
					//Debug.DrawLine ((Vector3)lgn.position+Vector3.up,(Vector3)other.position+Vector3.up,new Color (1,0,0,0.5F));
					
					//if (Mathf.Abs (other.position.y-lgn.position.y)*Int3.PrecisionFactor > maxClimb) {
					//	Debug.DrawLine (lgn.position,other.position,new Color (1,0,0,0.5F));
					//}
					
					
					if (dist >= characterHeight && Mathf.Abs (other.position.y-lgn.position.y)*Int3.PrecisionFactor <= maxClimb) {
						
						if (i >= 0xF) {
							Debug.LogError ("Too many layers");
							continue;
						}
						
						//Debug.DrawLine (lgn.position,other.position,new Color (0,1,0,0.5F));
						conn = i;
					}
				}
				
				lgn.SetConnection (dir,conn);
				*/
				//Debug.Log ("Yey");
				//Debug.DrawLine (node.position,minNode.position,Color.yellow);
			}
			
		}