Skip to content

Instantly share code, notes, and snippets.

@brunomikoski
Last active March 11, 2018 15:55
Show Gist options
  • Save brunomikoski/4c8d48a59172d0c8b4a82120ef945007 to your computer and use it in GitHub Desktop.
Save brunomikoski/4c8d48a59172d0c8b4a82120ef945007 to your computer and use it in GitHub Desktop.
using UnityEngine;
namespace BrunoMikoski.Pahtfinding.Grid
{
public sealed class GridController : MonoBehaviour
{
[SerializeField]
private int gridSizeX;
public int GridSizeX
{
get { return gridSizeX; }
}
[SerializeField]
private int gridSizeY;
public int GridSizeY
{
get { return gridSizeY; }
}
private Tile[] tiles;
public Tile[] Tiles
{
get
{
return tiles;
}
}
public int TilePosToIndex( int x, int y )
{
return x + y * gridSizeY;
}
public void IndexToTilePos( int index, out int x, out int y )
{
x = index % gridSizeX;
y = Mathf.FloorToInt( index / (float) gridSizeX );
}
public void SetTileType( int index, TileType type )
{
tiles[index].SetType( type );
}
public void SetTileType( int x, int y, TileType type )
{
SetTileType( TilePosToIndex( x, y ), type );
}
public void SetTileType( Vector2Int targetPosition, TileType type )
{
SetTileType( TilePosToIndex( targetPosition.x, targetPosition.y ), type );
}
public TileType GetTileType( int index )
{
return tiles[index].TileType;
}
public TileType GetTileType( int x, int y )
{
return GetTileType( TilePosToIndex( x, y ) );
}
public void SetTileBlocked( int index, bool blocked )
{
SetTileType( index, blocked ? TileType.BLOCK : TileType.EMPTY );
}
public void SetTileBlocked( int x, int y, bool blocked )
{
SetTileBlocked( TilePosToIndex( x, y ), blocked );
}
public bool IsTileBlocked( int index )
{
return tiles[index].TileType == TileType.BLOCK;
}
public bool IsTileBlocked( int x, int y )
{
return IsTileBlocked( TilePosToIndex( x, y ) );
}
public void GenerateTiles()
{
tiles = new Tile[gridSizeX * gridSizeY];
for ( int i = tiles.Length - 1; i >= 0; i-- )
{
int positionX;
int positionY;
IndexToTilePos( i, out positionX, out positionY );
tiles[i] = new Tile( i, new Vector2Int(positionX, positionY) );
}
}
public bool IsValidTilePosition( int targetPositionX, int targetPositionY )
{
if ( targetPositionX < 0 || targetPositionX > gridSizeX - 1 )
return false;
if ( targetPositionY < 0 || targetPositionY > gridSizeY - 1 )
return false;
int tilePosToIndex = TilePosToIndex( targetPositionX, targetPositionY );
if ( tiles[tilePosToIndex].TileType == TileType.BLOCK)
return false;
return true;
}
public bool IsValidTilePosition( Vector2Int targetPosition )
{
return IsValidTilePosition( targetPosition.x, targetPosition.y );
}
public void Clear()
{
for ( int i = tiles.Length - 1; i >= 0; i-- )
SetTileType( i, TileType.EMPTY );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment