123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- using UnityEngine;
- using System.Collections.Generic;
- namespace Pathfinding {
- [System.Serializable]
-
-
-
-
-
-
-
-
-
-
- public class StartEndModifier : PathModifier {
- public override int Order { get { return 0; } }
-
-
-
-
-
-
-
-
-
-
- public bool addPoints;
-
-
-
-
- public Exactness exactStartPoint = Exactness.ClosestOnNode;
-
-
-
-
- public Exactness exactEndPoint = Exactness.ClosestOnNode;
-
-
-
-
-
-
- public System.Func<Vector3> adjustStartPoint;
-
-
-
-
-
-
-
- public enum Exactness {
-
-
-
-
-
-
-
-
- SnapToNode,
-
-
-
-
-
-
-
- Original,
-
-
-
-
-
-
-
- Interpolate,
-
-
-
-
-
- ClosestOnNode,
-
-
-
-
-
-
-
-
- NodeConnection,
- }
-
-
-
-
-
-
-
- public bool useRaycasting;
- public LayerMask mask = -1;
-
-
-
-
-
-
- public bool useGraphRaycasting;
- List<GraphNode> connectionBuffer;
- System.Action<GraphNode> connectionBufferAddDelegate;
- public override void Apply (Path _p) {
- var p = _p as ABPath;
-
- if (p == null || p.vectorPath.Count == 0) return;
- bool singleNode = false;
- if (p.vectorPath.Count == 1 && !addPoints) {
-
- p.vectorPath.Add(p.vectorPath[0]);
- singleNode = true;
- }
-
- bool forceAddStartPoint, forceAddEndPoint;
-
- int closestStartConnection, closestEndConnection;
- Vector3 pStart = Snap(p, exactStartPoint, true, out forceAddStartPoint, out closestStartConnection);
- Vector3 pEnd = Snap(p, exactEndPoint, false, out forceAddEndPoint, out closestEndConnection);
-
-
-
-
-
-
-
-
-
-
-
- if (singleNode) {
- if (closestStartConnection == closestEndConnection) {
- forceAddStartPoint = false;
- forceAddEndPoint = false;
- } else {
- forceAddStartPoint = false;
- }
- }
-
-
-
-
- if ((forceAddStartPoint || addPoints) && exactStartPoint != Exactness.SnapToNode) {
- p.vectorPath.Insert(0, pStart);
- } else {
- p.vectorPath[0] = pStart;
- }
- if ((forceAddEndPoint || addPoints) && exactEndPoint != Exactness.SnapToNode) {
- p.vectorPath.Add(pEnd);
- } else {
- p.vectorPath[p.vectorPath.Count-1] = pEnd;
- }
- }
- Vector3 Snap (ABPath path, Exactness mode, bool start, out bool forceAddPoint, out int closestConnectionIndex) {
- var index = start ? 0 : path.path.Count - 1;
- var node = path.path[index];
- var nodePos = (Vector3)node.position;
- closestConnectionIndex = 0;
- forceAddPoint = false;
- switch (mode) {
- case Exactness.ClosestOnNode:
- return start ? path.startPoint : path.endPoint;
- case Exactness.SnapToNode:
- return nodePos;
- case Exactness.Original:
- case Exactness.Interpolate:
- case Exactness.NodeConnection:
- Vector3 relevantPoint;
- if (start) {
- relevantPoint = adjustStartPoint != null? adjustStartPoint() : path.originalStartPoint;
- } else {
- relevantPoint = path.originalEndPoint;
- }
- switch (mode) {
- case Exactness.Original:
- return GetClampedPoint(nodePos, relevantPoint, node);
- case Exactness.Interpolate:
-
- var adjacentNode = path.path[Mathf.Clamp(index + (start ? 1 : -1), 0, path.path.Count-1)];
- return VectorMath.ClosestPointOnSegment(nodePos, (Vector3)adjacentNode.position, relevantPoint);
- case Exactness.NodeConnection:
-
-
-
-
- connectionBuffer = connectionBuffer ?? new List<GraphNode>();
- connectionBufferAddDelegate = connectionBufferAddDelegate ?? (System.Action<GraphNode>)connectionBuffer.Add;
-
- adjacentNode = path.path[Mathf.Clamp(index + (start ? 1 : -1), 0, path.path.Count-1)];
-
- node.GetConnections(connectionBufferAddDelegate);
- var bestPos = nodePos;
- var bestDist = float.PositiveInfinity;
-
-
-
- for (int i = connectionBuffer.Count - 1; i >= 0; i--) {
- var neighbour = connectionBuffer[i];
- if (!path.CanTraverse(neighbour)) continue;
-
-
- var closest = VectorMath.ClosestPointOnSegment(nodePos, (Vector3)neighbour.position, relevantPoint);
- var dist = (closest - relevantPoint).sqrMagnitude;
- if (dist < bestDist) {
- bestPos = closest;
- bestDist = dist;
- closestConnectionIndex = i;
-
-
- forceAddPoint = neighbour != adjacentNode;
- }
- }
- connectionBuffer.Clear();
- return bestPos;
- default:
- throw new System.ArgumentException("Cannot reach this point, but the compiler is not smart enough to realize that.");
- }
- default:
- throw new System.ArgumentException("Invalid mode");
- }
- }
- protected Vector3 GetClampedPoint (Vector3 from, Vector3 to, GraphNode hint) {
- Vector3 point = to;
- RaycastHit hit;
- if (useRaycasting && Physics.Linecast(from, to, out hit, mask)) {
- point = hit.point;
- }
- if (useGraphRaycasting && hint != null) {
- var rayGraph = AstarData.GetGraph(hint) as IRaycastableGraph;
- if (rayGraph != null) {
- GraphHitInfo graphHit;
- if (rayGraph.Linecast(from, point, out graphHit)) {
- point = graphHit.point;
- }
- }
- }
- return point;
- }
- }
- }
|