Shows a simple graph type generating a polar graph.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Pathfinding;
using Pathfinding.Serialization.JsonFx;
[JsonOptIn]
public class PolarGraph : NavGraph {
[JsonMember]
public int circles = 10;
[JsonMember]
public int steps = 20;
[JsonMember]
public Vector3 center = Vector3.zero;
[JsonMember]
public float scale = 2;
public override void Scan () {
nodes = CreateNodes (circles*steps);
Matrix4x4 matrix = Matrix4x4.TRS (center,Quaternion.identity,Vector3.one*scale);
nodes[0].position = (Int3)matrix.MultiplyPoint (Vector3.zero);
float anglesPerStep = (2*Mathf.PI)/steps;
for (int i=1;i<circles;i++) {
for (int j=0;j<steps;j++) {
float angle = j * anglesPerStep;
Vector3 pos = new Vector3 (Mathf.Sin (angle),0, Mathf.Cos (angle));
pos *= i*scale;
pos = matrix.MultiplyPoint (pos);
Node node = nodes[(i-1)*steps + j + 1];
node.position = (Int3)pos;
}
}
for (int i=1;i<circles;i++) {
for (int j=0;j<steps;j++) {
Node node = nodes[(i-1)*steps + j + 1];
int numConnections = (i < circles-1) ? 4 : 3;
Node[] connections = new Node[numConnections];
int[] connectionCosts = new int[numConnections];
int connId = (i-1)*steps + (j < steps-1 ? j+1 : 0) + 1;
connections[0] = nodes[connId];
connId = (i-1)*steps + (j > 0 ? j-1 : steps-1) + 1;
connections[1] = nodes[connId];
if (i > 1) {
connId = (i-2)*steps + j + 1;
} else {
connId = 0;
}
connections[2] = nodes[connId];
if (numConnections == 4) {
connId = i*steps + j + 1;
connections[3] = nodes[connId];
}
for (int q=0;q<connections.Length;q++) {
connectionCosts[q] = (node.position-connections[q].position).costMagnitude;
}
node.connections = connections;
node.connectionCosts = connectionCosts;
}
}
Node centerNode = nodes[0];
centerNode.connections = new Node[steps];
centerNode.connectionCosts = new int[steps];
for (int j=0;j<steps;j++) {
centerNode.connections[j] = nodes[1+j];
centerNode.connectionCosts[j] = (centerNode.position-centerNode.connections[j].position).costMagnitude;
}
for (int i=0;i<nodes.Length;i++) {
nodes[i].walkable = true;
}
}
}