From 8722a9920c1f6119bf6e769cba270e63097f8e25 Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Thu, 23 May 2024 10:08:29 +0800 Subject: + astar project --- .../com.arongranberg.astar/Pathfinders/FleePath.cs | 59 ++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Pathfinders/FleePath.cs (limited to 'Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Pathfinders/FleePath.cs') diff --git a/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Pathfinders/FleePath.cs b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Pathfinders/FleePath.cs new file mode 100644 index 0000000..7c3d62c --- /dev/null +++ b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Pathfinders/FleePath.cs @@ -0,0 +1,59 @@ +using UnityEngine; + +namespace Pathfinding { + /// + /// Returns a path heading away from a specified point to avoid. + /// The search will terminate when G \> length (passed to the constructor) + FleePath.spread. + /// + /// Can be used to make an AI to flee from an enemy (cannot guarantee that it will not be forced into corners though :D ) + /// + /// + /// // Call a FleePath call like this, assumes that a Seeker is attached to the GameObject + /// Vector3 thePointToFleeFrom = Vector3.zero; + /// + /// // The path will be returned when the path is over a specified length (or more accurately when the traversal cost is greater than a specified value). + /// // A score of 1000 is approximately equal to the cost of moving one world unit. + /// int theGScoreToStopAt = 10000; + /// + /// // Create a path object + /// FleePath path = FleePath.Construct (transform.position, thePointToFleeFrom, theGScoreToStopAt); + /// // This is how strongly it will try to flee, if you set it to 0 it will behave like a RandomPath + /// path.aimStrength = 1; + /// // Determines the variation in path length that is allowed + /// path.spread = 4000; + /// + /// // Get the Seeker component which must be attached to this GameObject + /// Seeker seeker = GetComponent(); + /// + /// // Start the path and return the result to MyCompleteFunction (which is a function you have to define, the name can of course be changed) + /// seeker.StartPath(path, MyCompleteFunction); + /// + /// + /// + public class FleePath : RandomPath { + /// + /// Default constructor. + /// Do not use this. Instead use the static Construct method which can handle path pooling. + /// + public FleePath () {} + + /// + /// Constructs a new FleePath. + /// The FleePath will be taken from a pool. + /// + public static FleePath Construct (Vector3 start, Vector3 avoid, int searchLength, OnPathDelegate callback = null) { + var p = PathPool.GetPath(); + + p.Setup(start, avoid, searchLength, callback); + return p; + } + + protected void Setup (Vector3 start, Vector3 avoid, int searchLength, OnPathDelegate callback) { + Setup(start, searchLength, callback); + // Set the aim to a point in the opposite direction from the point we to avoid + // TODO: Why is this multiplication by 10 here? + // Might want to remove it + aim = start - (avoid-start)*10; + } + } +} -- cgit v1.1-26-g67d0