Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
neicureuil authored Nov 9, 2020
1 parent 971a911 commit 0036dfc
Show file tree
Hide file tree
Showing 7 changed files with 441 additions and 0 deletions.
104 changes: 104 additions & 0 deletions Grid.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace NPathfinding {

public class Grid : MonoBehaviour {

public Vector2Int gridSize;
private Node[,] grid;
private int gridSizeX, gridSizeY;

void Awake() {
gridSizeX = gridSize.x;
gridSizeY = gridSize.y;
CreateGrid();
}

void CreateGrid() {
grid = new Node[gridSizeX, gridSizeY];

for (int x = 0; x < gridSizeX; x++) {
for (int y = 0; y < gridSizeY; y++) {
grid[x, y] = new Node(true);
grid[x, y].SetPos(x, y);
}
}
}

public List<Node> GetNeighbours(Node node, PathfindingConfig config) {
List<Node> neighbours = new List<Node>();

for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {

if (config.AllowDiagonal) {
if (x == 0 && y == 0) continue;
} else {
if (x == 0 && y == 0 || x == -1 && y == -1 || x == 1 && y == -1 || x == -1 && y == 1 || x == 1 && y == 1) continue;
}

int checkX = node.gridX + x;
int checkY = node.gridY + y;

if (isInGrid(checkX, checkY)) {
neighbours.Add(grid[checkX, checkY]);
}

}
}

return neighbours;
}

public void SetWalkable(int x, int y, bool walk) {
if (isInGrid(x, y)) {
grid[x, y].walkable = walk;
}
}

public void SetWalkable(Vector2 pos, bool walk) {
SetWalkable((int)pos.x, (int)pos.y, walk);
}

public bool isInGrid(int x, int y) {
if (x >= 0 && x < gridSizeX && y >= 0 && y < gridSizeY) {
return true;
}
return false;
}

public bool isInGrid(Vector2 pos) {
return isInGrid((int)pos.x, (int)pos.y);
}

public void SetNode(int x, int y, Node node) {
if (isInGrid(x, y)) {
grid[x, y] = node;
}
}

public void SetNode(Vector2 pos, Node node) {
SetNode((int)pos.x, (int)pos.y, node);
}

public Node GetNode(int x, int y) {
if (isInGrid(x, y)) {
return grid[x, y];
}
return null;
}

public Node GetNode(Vector2 pos) {
return GetNode((int)pos.x, (int)pos.y);
}

public int MaxSize {
get {
return gridSizeX * gridSizeY;
}
}
}

}
105 changes: 105 additions & 0 deletions Heap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using UnityEngine;
using System.Collections;
using System;

namespace NPathfinding {

public class Heap<T> where T : IHeapItem<T> {

T[] items;
int currentItemCount;

public Heap(int maxHeapSize) {
items = new T[maxHeapSize];
}

public void Add(T item) {
item.HeapIndex = currentItemCount;
items[currentItemCount] = item;
SortUp(item);
currentItemCount++;
}

public T RemoveFirst() {
T firstItem = items[0];
currentItemCount--;
items[0] = items[currentItemCount];
items[0].HeapIndex = 0;
SortDown(items[0]);
return firstItem;
}

public void UpdateItem(T item) {
SortUp(item);
}

public int Count {
get {
return currentItemCount;
}
}

public bool Contains(T item) {
return Equals(items[item.HeapIndex], item);
}

void SortDown(T item) {
while (true) {
int childIndexLeft = item.HeapIndex * 2 + 1;
int childIndexRight = item.HeapIndex * 2 + 2;
int swapIndex = 0;

if (childIndexLeft < currentItemCount) {
swapIndex = childIndexLeft;

if (childIndexRight < currentItemCount) {
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;
}

parentIndex = (item.HeapIndex - 1) / 2;
}
}

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;
}
}

}
55 changes: 55 additions & 0 deletions NPathfinding.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{E2060040-6D45-43D8-8877-37BC3FF96812}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>NPathfinding</RootNamespace>
<AssemblyName>NPathfinding</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="UnityEngine">
<HintPath>..\UnityEngine.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Grid.cs" />
<Compile Include="Heap.cs" />
<Compile Include="Node.cs" />
<Compile Include="Pathfinding.cs" />
<Compile Include="PathfindingConfig.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
25 changes: 25 additions & 0 deletions NPathfinding.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29215.179
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NPathfinding", "NPathfinding.csproj", "{E2060040-6D45-43D8-8877-37BC3FF96812}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E2060040-6D45-43D8-8877-37BC3FF96812}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E2060040-6D45-43D8-8877-37BC3FF96812}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E2060040-6D45-43D8-8877-37BC3FF96812}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E2060040-6D45-43D8-8877-37BC3FF96812}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6D58CDE9-73AC-42B5-BA0A-C7A21F38990D}
EndGlobalSection
EndGlobal
54 changes: 54 additions & 0 deletions Node.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using UnityEngine;
using System.Collections;
using System;

namespace NPathfinding {

public class Node : IHeapItem<Node> {

//Node params
public bool walkable;

//Pathfinding Stuff
public int gCost;
public int hCost;
public int gridX;
public int gridY;
public Node parent;
private int heapIndex;

public Node(bool _walkable) {
this.walkable = _walkable;
}

public void SetPos(int x, int y) {
this.gridX = x;
this.gridY = y;
}

public int fCost {
get {
return gCost + hCost;
}
}

public int HeapIndex {
get {
return heapIndex;
}
set {
heapIndex = value;
}
}

public int CompareTo(Node nodeToCompare) {
int compare = fCost.CompareTo(nodeToCompare.fCost);
if (compare == 0) {
compare = hCost.CompareTo(nodeToCompare.hCost);
}
return -compare;
}

}

}
Loading

0 comments on commit 0036dfc

Please sign in to comment.