public override void Initialize(System.IO.BinaryReader reader, RecordHeader header) { while (reader.BaseStream.Position < header.DataEndPos) { var type = (SubRecordType)reader.ReadInt32(); var size = reader.ReadInt32(); switch (type) { case SubRecordType.Id: Cellname = reader.ReadString(size); break; case SubRecordType.Data: data = new PathgridData(reader); break; case SubRecordType.PathgridPoint: points = new PathgridPoint[data.PointCount]; for (var i = 0; i < points.Length; i++) { points[i] = new PathgridPoint(reader); } break; case SubRecordType.PathgridConnection: for (var i = 0; i < size / 4; i++) { pathgridConnections.Add(reader.ReadInt32()); } break; } } GenerateConnections(); //pathGrids.Add(id, this); if (data.x != 0 && data.y != 0) { records.Add(new Tuple <int, int>(data.x, data.y), this); } }
/// <summary> /// Given a current position, finds the closest point in this path grid /// </summary> /// <param name="position"> The position (In world space) </param> /// <returns> The pathgrid point closest to the position </returns> public PathgridPoint GetNearestPoint(Vector3 position) { float distance = float.MaxValue; PathgridPoint targetPoint = null; // Convert position to world space var offset = new Vector3(8192 * data.x, 0, 8192 * data.y); // Loop through EVERY path grid point and see which one is closest // Uses square magnitude for speed foreach (var point in points) { var worldPosition = point.Position + offset; var sqrDistance = (worldPosition - position).sqrMagnitude; if (sqrDistance < distance) { targetPoint = point; distance = sqrDistance; } } return(targetPoint); }
public void AddConnection(PathgridPoint connection) { connectedPoints.Add(connection); }