Coder Social home page Coder Social logo

patchwh / grids-and-pathfinding Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 154 KB

Useful classes for creating a grids that can exist in World Space or as Data Structures, contains Square and Hexagon Grids. Includes Pathfinding using AStar, BFS, GBFS/ or Dijkstra Algorithms. Easily switch between XZ and XY grids.

C# 100.00%
unity csharp grids hexagon pathfinding

grids-and-pathfinding's Introduction

!GRIDS AND PATHFINDING!

Demo Videos

The Square Grid with Pathfinding

uGr8VZmTU6.mp4

can be located at Grids&Pathfinding/Examples/SquareGrid.unity

The Hexagon Grid

fG8IgjLdJN.mp4

can be located at Grids&Pathfinding/Examples/HexagonGrid.unity

Example of Use

Explanation

This example will create a new HexagonGrid and generate a mesh for it.

All grids need 6 arguments (and 1 type) for their basic functions.

Grids are generic, so they require a Type. This is the Type that the grid will contain:

new HexagonGrid<int>( <- A Hexagon Grid which cells are integers

The first argument or creating a grid is the Axis it will exist on, this is passed as the GridAxis enum:

new HexagonGrid<int>(GridAxis.XY creates a grid on the X and Y axis,

replacing it with GridAxis.XZ will create a grid on the X and Z axis.

The second argument is the World Space offset of the grid, this effects any functions which reurn the world space position of a cell,

If new Vector3(0, 5, 0) is passed, then the [0,0] cell of the grid will be at the world position of (0, 5, 0).

Keep in mind the [0,0] cell of a grid is the bottom left.

The third and fourth arguments are the width and height, remember that a grid starts at [0,0], so if you give it a width and height of 6 and 6, the top right corner will be [5,5]

The fifth argument is a Func, which gets a Grid argument, an int for the x and an int for the y and returns an instance of T. This function is used to initialize your grid, it will take the return of it and use it to fill every spot in your grid.

The sixth argument is cellSize, which is another that only matters in the worldSpace operations.

Hexagon and Square grids have 3 additional Debug arguments: showDebug, debugFontSize and debugFontScale.

Final Code

using PWH.Grids;
HexagonGrid<int> hexGrid = 
  new HexagonGrid<int>
  (
    GridAxis.XY, // Grid Axis
    new Vector3(0, 10, 0), // Offset
    6, // Width
    6, // Height
    (source, x, y) => { return 1; }, // Cell Initialization Func
    10f, // Cell Size
    true, // Do Debug?
    40, // Debug Font Size
    0.075f// Debug Font Scale
); 

To generate a mesh with your newly created HexagonGrid, simply call: hexGrid.GenerateMesh(GetComponent<MeshFilter>(), GetComponent<MeshCollider>())

Pathfinding

See Grids&Pathfinding/Examples/PathGridObject on how to implement IPathfindingNode and setup your GridCell Object to work with pathfinding!

The PathFinder is the powerhouse of all pathfinding, it is a Singleton which simply needs to be placed in your scene, from there you can get a List from calling PathFinder.Instance.Init().

This List is the path from your specified start to your specified goal in order.

It takes a Grid as it's first argument, if you have a Grid of elements that inherit from IPathfinding Node, simply give it to the utility function: PathfinderUtils.ConvertGridToGraph(Your Grid), and it will return a Grid which you can give to the pathFinder.

From there you simply get your first node and goal node and pass them into the .Init() as the second and third argument respectively.

There is also a third argument which is a Debug option to show the path it generated in red.

The Pathfinder has 4 modes: AStar, GBFS, Dijkstra and BFS, this mode can be set in the inspector or by the PathFinder.Instance.mode = field.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.