This file was deleted.

@@ -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;
}
}
@@ -2,7 +2,117 @@
using System.Collections;
using System.Collections.Generic;

public class Pathfinding : System.Object
{

public static class Pathfinding {

public static Vector3[] FindPath (Vector3 from, Vector3 target)
{
#region Defintion

Node startNode = Grid.GetClosestNode(from);
Node targetNode = Grid.GetClosestNode(target);

List<Node> openSet = new List<Node>();
HashSet<Node> closedSet = new HashSet<Node>();

#endregion

#region Start

openSet.Add(startNode);

#endregion

#region Loop

while (openSet.Count > 0)
{
#region Find lowest cost node

Node currentNode = openSet[0];
for (int i = 1; i > openSet.Count; i++)
{
if (openSet[i].fCost < currentNode.fCost || openSet[i].fCost == currentNode.fCost && openSet[i].hCost < currentNode.hCost)
{
currentNode = openSet[i];
}
}

#endregion

#region Tweak sets

openSet.Remove(currentNode);
closedSet.Add(currentNode);

#endregion

#region Check if arrived to goal

if (currentNode == targetNode)
{
return RetracePath(startNode, targetNode);
}

#endregion

#region Add neighbours of current node to openSet or update them

foreach (Coords neighbour in currentNode.neighbours)
{
Node nb_node = Grid.GetNode(neighbour);
bool openSetContains = openSet.Contains(nb_node);
if (!nb_node.walkable || closedSet.Contains(nb_node)) continue;

int distance2Neighbour = currentNode.gCost + Grid.GetDistance(currentNode, nb_node);
if (distance2Neighbour < nb_node.gCost || !openSetContains)
{
nb_node.gCost = distance2Neighbour;
nb_node.hCost = Grid.GetDistance(nb_node, targetNode);
nb_node.parent = currentNode;

if (!openSetContains)
openSet.Add(nb_node);
}
}

#endregion

#region Check if couldnt reach goal

if (openSet.Count == 0)
{
break;
}

#endregion
}

#endregion

#region Default

return new Vector3[] { startNode.position };

#endregion
}

private static Vector3[] RetracePath (Node startNode, Node endNode)
{
List<Node> path = new List<Node>();
Node currentNode = endNode;

while (currentNode != startNode)
{
path.Add(currentNode);
currentNode = currentNode.parent;
}

Vector3[] output = new Vector3[path.Count];
for (int i = path.Count-1; i >= 0; i--)
{
output[i] = path[i].position;
}

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);
}
}
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,5 +1,5 @@
Base path: C:/Program Files/Unity/Editor/Data
Pipe name: \\.\pipe\UnityShaderCompiler-32bit-1-7832
Pipe name: \\.\pipe\UnityShaderCompiler-32bit-1-9156
Cmd: getPlatforms
Unhandled exception: Readfile from pipe failed. GLE=Ha terminado la canalización.

@@ -1,15 +1,3 @@
Base path: C:/Program Files/Unity/Editor/Data
Pipe name: \\.\pipe\UnityShaderCompiler-64bit-1-7832
Pipe name: \\.\pipe\UnityShaderCompiler-64bit-1-9156
Cmd: getPlatforms
Cmd: compileSnippet
api=4 type=0 insize=530 outsize=2224 kw= ok=1
Cmd: compileSnippet
api=4 type=1 insize=530 outsize=1899 kw= ok=1
Cmd: compileSnippet
api=4 type=0 insize=982 outsize=4711 kw=DIRECTIONAL SHADOWS_SCREEN LIGHTMAP_OFF DIRLIGHTMAP_COMBINED DYNAMICLIGHTMAP_ON ok=1
Cmd: compileSnippet
api=4 type=1 insize=982 outsize=11389 kw=DIRECTIONAL SHADOWS_SCREEN LIGHTMAP_OFF DIRLIGHTMAP_COMBINED DYNAMICLIGHTMAP_ON ok=1
Cmd: compileSnippet
api=4 type=0 insize=938 outsize=4727 kw=POINT SHADOWS_OFF ok=1
Cmd: compileSnippet
api=4 type=1 insize=938 outsize=5962 kw=POINT SHADOWS_OFF ok=1
Binary file not shown.