| @@ -0,0 +1,62 @@ | ||
| using UnityEngine; | ||
| using System.Collections; | ||
|
|
||
| public class Node { | ||
|
|
||
| public readonly bool walkable; | ||
| public readonly Vector3 position; | ||
| public readonly Coords gridPos; | ||
| public readonly Coords[] neighbours; | ||
|
|
||
| #region Pathfinding | ||
|
|
||
| public int gCost; | ||
| public int hCost; | ||
| public int fCost | ||
| { | ||
| get | ||
| { | ||
| return gCost + hCost; | ||
| } | ||
| } | ||
| public Node parent; | ||
|
|
||
| #endregion | ||
|
|
||
| public Node (bool walkable, Vector3 position, int gridX, int gridY) | ||
| { | ||
| this.walkable = walkable; | ||
| this.position = position; | ||
| this.gridPos = new Coords(gridX, gridY); | ||
| this.neighbours = GetNeighbours(); | ||
| } | ||
|
|
||
| private Coords[] GetNeighbours () | ||
| { | ||
| Coords[] nb_holder = new Coords[8]; | ||
| int count = 0; | ||
|
|
||
| for (int x = -1; x < 2; x++) | ||
| { | ||
| for (int y = -1; y < 2; y++) | ||
| { | ||
| if (x == 0 && y == 0) continue; | ||
|
|
||
| Coords neighbour_coords = new Coords(gridPos.x + x, gridPos.y + y); | ||
|
|
||
| if (!Grid.NodeExist(neighbour_coords)) continue; | ||
|
|
||
| nb_holder[count] = neighbour_coords; | ||
| count++; | ||
| } | ||
| } | ||
|
|
||
| Coords[] output = new Coords[count + 1]; | ||
| for (int i = 0; i < count; i++) | ||
| { | ||
| output[i] = nb_holder[i]; | ||
| } | ||
|
|
||
| return output; | ||
| } | ||
| } |
| @@ -0,0 +1,32 @@ | ||
| public struct Coords | ||
| { | ||
| public int x; | ||
| public int y; | ||
|
|
||
| public Coords(int x, int y) | ||
| { | ||
| this.x = x; | ||
| this.y = y; | ||
| } | ||
| public static bool operator ==(Coords a, Coords b) | ||
| { | ||
| return a.x == b.x && a.y == b.y; | ||
| } | ||
| public static bool operator !=(Coords a, Coords b) | ||
| { | ||
| return a.x != b.x || a.y != b.y; | ||
| } | ||
| public static Coords operator +(Coords a, Coords b) | ||
| { | ||
| Coords out_ = new Coords(a.x + b.x, a.y + b.y); | ||
| return out_; | ||
| } | ||
| public override bool Equals(object obj) | ||
| { | ||
| return base.Equals(obj); | ||
| } | ||
| public override int GetHashCode() | ||
| { | ||
| return base.GetHashCode(); | ||
| } | ||
| } |
| @@ -0,0 +1,123 @@ | ||
| using UnityEngine; | ||
| using System.Collections; | ||
| using System; | ||
|
|
||
| public class Heap<T> where T : IHeapItem<T> { | ||
|
|
||
| T[] items; | ||
| int count; | ||
|
|
||
| public Heap(int maxHeapSize) | ||
| { | ||
| items = new T[maxHeapSize]; | ||
| } | ||
|
|
||
| public void Add(T item) | ||
| { | ||
| item.heapIndex = count; | ||
| items[count] = item; | ||
| SortUp(item); | ||
| count++; | ||
| } | ||
|
|
||
| public T RemoveFirst () | ||
| { | ||
| T firstItem = items[0]; | ||
| count--; | ||
| items[0] = items[count]; | ||
| items[0].heapIndex = 0; | ||
|
|
||
| SortDown(items[0]); | ||
| return firstItem; | ||
| } | ||
|
|
||
| public void UpdateItem (T item) | ||
| { | ||
| SortUp(item); | ||
| } | ||
|
|
||
| public bool Contains (T item) | ||
| { | ||
| return Equals(items[item.heapIndex], item); | ||
| } | ||
|
|
||
| public int Count | ||
| { | ||
| get | ||
| { | ||
| return count; | ||
| } | ||
| } | ||
|
|
||
| void SortDown (T item) | ||
| { | ||
| while (true) | ||
| { | ||
| int childIndexLeft = item.heapIndex * 2 + 1; | ||
| int childIndexRight = item.heapIndex * 2 + 2; | ||
| int swapIndex = 0; | ||
|
|
||
| if (childIndexLeft < count) | ||
| { | ||
| swapIndex = childIndexLeft; | ||
|
|
||
| if (childIndexRight < count) | ||
| { | ||
| if (items[childIndexLeft].CompareTo(items[childIndexRight]) < 0 ) | ||
| { | ||
| swapIndex = childIndexRight; | ||
| } | ||
| } | ||
|
|
||
| if (item.CompareTo(items[swapIndex]) < 0) | ||
| { | ||
| Swap(item, items[swapIndex]); | ||
| } | ||
| else | ||
| { | ||
| return; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void SortUp (T item) | ||
| { | ||
| int parentIndex = (item.heapIndex - 1)/ 2; | ||
|
|
||
| while (true) | ||
| { | ||
| T parentItem = items[parentIndex]; | ||
| if (item.CompareTo(parentItem) > 0) | ||
| { | ||
| Swap(item, parentItem); | ||
| } | ||
| else | ||
| { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void Swap (T itemA, T itemB) | ||
| { | ||
| items[itemA.heapIndex] = itemB; | ||
| items[itemB.heapIndex] = itemA; | ||
| int itemAIndex = itemA.heapIndex; | ||
| itemA.heapIndex = itemB.heapIndex; | ||
| itemB.heapIndex = itemAIndex; | ||
| } | ||
| } | ||
|
|
||
| public interface IHeapItem<T> : IComparable<T> | ||
| { | ||
| int heapIndex | ||
| { | ||
| get; | ||
| set; | ||
| } | ||
| } |
| @@ -0,0 +1,38 @@ | ||
| using UnityEngine; | ||
| using System.Collections; | ||
|
|
||
| public class test : MonoBehaviour { | ||
|
|
||
| Vector3[] path; | ||
| public Transform a; | ||
| public Transform b; | ||
| // Use this for initialization | ||
| void Start () { | ||
|
|
||
| } | ||
|
|
||
| // Update is called once per frame | ||
| void Update () { | ||
| path = Pathfinding.FindPath(a.position, b.position); | ||
| } | ||
|
|
||
| void OnDrawGizmos () | ||
| { | ||
| if (path == null) return; | ||
| for (int i = 1; i < path.Length; i++) | ||
| { | ||
| Gizmos.color = Color.black; | ||
| Gizmos.DrawLine(path[i-1], path[i]); | ||
| } | ||
| if (Grid.GetGrid() != null) | ||
| { | ||
| Node playerNode = Grid.GetClosestNode(a.transform.position); | ||
| foreach (Node n in Grid.GetGrid()) | ||
| { | ||
| Gizmos.color = (n.walkable) ? Color.black : Color.red; | ||
| if (n == playerNode) Gizmos.color = Color.cyan; | ||
| Gizmos.DrawCube(n.position, Vector3.one*0.2f); | ||
| } | ||
| } | ||
| } | ||
| } |
| @@ -1,15 +1,3 @@ | ||
| Base path: C:/Program Files/Unity/Editor/Data | ||
| Pipe name: \\.\pipe\UnityShaderCompiler-64bit-1-9156 | ||
| Cmd: getPlatforms | ||