123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- using UnityEngine;
- using Pathfinding.Serialization;
- namespace Pathfinding {
-
-
-
-
-
-
-
- public class PointNode : GraphNode {
-
-
-
-
-
-
-
-
-
- public Connection[] connections;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public GameObject gameObject;
- public void SetPosition (Int3 value) {
- position = value;
- }
- public PointNode (AstarPath astar) : base(astar) {
- }
-
-
-
-
-
- public override Vector3 ClosestPointOnNode (Vector3 p) {
- return (Vector3)this.position;
- }
- public override void GetConnections (System.Action<GraphNode> action) {
- if (connections == null) return;
- for (int i = 0; i < connections.Length; i++) action(connections[i].node);
- }
- public override void ClearConnections (bool alsoReverse) {
- if (alsoReverse && connections != null) {
- for (int i = 0; i < connections.Length; i++) {
- connections[i].node.RemoveConnection(this);
- }
- }
- connections = null;
- AstarPath.active.hierarchicalGraph.AddDirtyNode(this);
- }
- public override void UpdateRecursiveG (Path path, PathNode pathNode, PathHandler handler) {
- pathNode.UpdateG(path);
- handler.heap.Add(pathNode);
- for (int i = 0; i < connections.Length; i++) {
- GraphNode other = connections[i].node;
- PathNode otherPN = handler.GetPathNode(other);
- if (otherPN.parent == pathNode && otherPN.pathID == handler.PathID) {
- other.UpdateRecursiveG(path, otherPN, handler);
- }
- }
- }
- public override bool ContainsConnection (GraphNode node) {
- if (connections == null) return false;
- for (int i = 0; i < connections.Length; i++) if (connections[i].node == node) return true;
- return false;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public override void AddConnection (GraphNode node, uint cost) {
- if (node == null) throw new System.ArgumentNullException();
- if (connections != null) {
- for (int i = 0; i < connections.Length; i++) {
- if (connections[i].node == node) {
- connections[i].cost = cost;
- return;
- }
- }
- }
- int connLength = connections != null ? connections.Length : 0;
- var newconns = new Connection[connLength+1];
- for (int i = 0; i < connLength; i++) {
- newconns[i] = connections[i];
- }
- newconns[connLength] = new Connection(node, cost);
- connections = newconns;
- AstarPath.active.hierarchicalGraph.AddDirtyNode(this);
-
- (this.Graph as PointGraph).RegisterConnectionLength((node.position - position).sqrMagnitudeLong);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public override void RemoveConnection (GraphNode node) {
- if (connections == null) return;
- for (int i = 0; i < connections.Length; i++) {
- if (connections[i].node == node) {
- int connLength = connections.Length;
- var newconns = new Connection[connLength-1];
- for (int j = 0; j < i; j++) {
- newconns[j] = connections[j];
- }
- for (int j = i+1; j < connLength; j++) {
- newconns[j-1] = connections[j];
- }
- connections = newconns;
- AstarPath.active.hierarchicalGraph.AddDirtyNode(this);
- return;
- }
- }
- }
- public override void Open (Path path, PathNode pathNode, PathHandler handler) {
- if (connections == null) return;
- for (int i = 0; i < connections.Length; i++) {
- GraphNode other = connections[i].node;
- if (path.CanTraverse(other)) {
- PathNode pathOther = handler.GetPathNode(other);
- if (pathOther.pathID != handler.PathID) {
- pathOther.parent = pathNode;
- pathOther.pathID = handler.PathID;
- pathOther.cost = connections[i].cost;
- pathOther.H = path.CalculateHScore(other);
- pathOther.UpdateG(path);
- handler.heap.Add(pathOther);
- } else {
-
- uint tmpCost = connections[i].cost;
- if (pathNode.G + tmpCost + path.GetTraversalCost(other) < pathOther.G) {
- pathOther.cost = tmpCost;
- pathOther.parent = pathNode;
- other.UpdateRecursiveG(path, pathOther, handler);
- }
- }
- }
- }
- }
- public override int GetGizmoHashCode () {
- var hash = base.GetGizmoHashCode();
- if (connections != null) {
- for (int i = 0; i < connections.Length; i++) {
- hash ^= 17 * connections[i].GetHashCode();
- }
- }
- return hash;
- }
- public override void SerializeNode (GraphSerializationContext ctx) {
- base.SerializeNode(ctx);
- ctx.SerializeInt3(position);
- }
- public override void DeserializeNode (GraphSerializationContext ctx) {
- base.DeserializeNode(ctx);
- position = ctx.DeserializeInt3();
- }
- public override void SerializeReferences (GraphSerializationContext ctx) {
- if (connections == null) {
- ctx.writer.Write(-1);
- } else {
- ctx.writer.Write(connections.Length);
- for (int i = 0; i < connections.Length; i++) {
- ctx.SerializeNodeReference(connections[i].node);
- ctx.writer.Write(connections[i].cost);
- }
- }
- }
- public override void DeserializeReferences (GraphSerializationContext ctx) {
- int count = ctx.reader.ReadInt32();
- if (count == -1) {
- connections = null;
- } else {
- connections = new Connection[count];
- for (int i = 0; i < count; i++) {
- connections[i] = new Connection(ctx.DeserializeNodeReference(), ctx.reader.ReadUInt32());
- }
- }
- }
- }
- }
|