public void GetGrid() { // Pathfinding BoundsInt tilemapBounds = floor.cellBounds; bool[,] tilesmap = new bool[tilemapBounds.xMax, tilemapBounds.yMax]; for (int x = 0; x < tilemapBounds.xMax - 1; x++) { for (int y = 0; y < tilemapBounds.yMax - 1; y++) { Vector3Int pos = new Vector3Int(x, y, 0); // If the cell isnt a floor tile remove if (floor.GetTile(pos) == null) { continue; } // If the cell is occupied by a floor prop remove if (floorProps.GetTile(pos) != null) { continue; } // Otherwise set tile to true tilesmap[x, y] = true; } } grid = new PathFind.Grid(tilesmap); }
//find the nearest path to targeted cell in the map, downloaded scripts used for that, check 2dTleBasedPathFinding folder void FindPath(bool findPac) { tileArray = tilesCounter.GetTileArrays(); grid = new NesScripts.Controls.PathFind.Grid(tileMapWidth, tileMapHeight, tileArray); cellPositionGhost = tileMap.WorldToCell(transform.position); if (findPac == true) { cellPositionTarget = tileMap.WorldToCell(Pac.transform.position); } else { if (randomCounterBreak == 1000) { randomIndex = Random.Range(0, tilesCounter.availablePlaces.Count); } cellPositionTarget = tileMap.WorldToCell(tilesCounter.availablePlaces[randomIndex]); } path = Pathfinding.FindPath(grid, tilesCounter.LocalGridToPathGrid(cellPositionGhost), tilesCounter.LocalGridToPathGrid(cellPositionTarget)); __tempPathWorldPos.Clear(); foreach (var cell in path) { var localCell = tilesCounter.PathGridToLocalGrid(cell); __tempPathWorldPos.Add(tileMap.GetCellCenterWorld(localCell)); } pathWorldPos = __tempPathWorldPos; }
public void SetPathingCosts(PathingCost[,] pathingCostArray) { // Create an Array of Floats from the Array of Pathing Costs float[,] tmpArray = GenerateFloatGridFromPathingCosts(pathingCostArray); // Convert that Array to a Grid in order to use our Pathing Library pathingCosts = new Grid(tmpArray.GetLength(0), tmpArray.GetLength(1), tmpArray); }
private void FindPath() { if (Path.Count == 0) { PathFind.Grid grid = new PathFind.Grid(ValueMap); PathFind.Point _from = new PathFind.Point(StartBlock.x, StartBlock.y); PathFind.Point _to = new PathFind.Point(EndBlock.x, EndBlock.y); Path = PathFind.Pathfinding.FindPath(grid, _from, _to, PathFind.Pathfinding.DistanceType.Manhattan); GetNextWayPoint(); } }
/// <summary> /// Finds a path of tile coordinates between the given starting (exclusive) and /// destination (inclusive) tile coordinates. /// </summary> /// <param name="boundaryMap"> /// A two-dimentional bool array created from <see cref="CreateBoundaryMap"/>. /// </param> /// <param name="startingTileCoordinate"> /// The tile coordinate of where to start the pathfinding. /// </param> /// <param name="destinationTileCoordinate"> /// The tile coordinate of where to end the pathfinding. /// </param> /// <returns> /// A list of tile coordinates representing the path to the destination tile coordinate. /// </returns> public static List <Vector3Int> GetTileCoordinatePath(bool[,] boundaryMap, Vector3Int startingTileCoordinate, Vector3Int destinationTileCoordinate) { // We have to negate the y value because SuperTiled2Unity uses negative y tile // coordinates but the y index in the boundary map needs to be positive if (boundaryMap[destinationTileCoordinate.x, -destinationTileCoordinate.y] == false) { return(new List <Vector3Int>()); } NesScripts.Controls.PathFind.Grid boundaryGrid = CreatePathFindGrid(boundaryMap); Point startingPoint = new Point(startingTileCoordinate.x, startingTileCoordinate.y); Point destinationPoint = new Point(destinationTileCoordinate.x, destinationTileCoordinate.y); List <Point> pathPoints = Pathfinding.FindPath(boundaryGrid, startingPoint, destinationPoint, Pathfinding.DistanceType.Manhattan); return(ConvertToTileCoordinates(pathPoints)); }
public void Blocktile(Tile tile, bool isBlock) { int value = 0; if (!isBlock) { tile.SetAsWalkable(); value = 1; } else { tile.SetAsUnwalkable(); value = 0; } tilesmap [(int)(tile.pos.x), (int)(tile.pos.y)] = value; grid = new NesScripts.Controls.PathFind.Grid(tilesWidth, tilesWHeight, tilesmap); }
private void AddEnemies(int count) { //generate a* map _map = _grid.GenerateMap(); //generate grid NesScripts.Controls.PathFind.Grid grid = new NesScripts.Controls.PathFind.Grid(_map); //path tests NesScripts.Controls.PathFind.Point _from = new NesScripts.Controls.PathFind.Point(_player.x, _player.y); NesScripts.Controls.PathFind.Point _to = new NesScripts.Controls.PathFind.Point(_target.x, _target.y); int _c = count; List <Tile> _uTiles = _grid.GetUnoccupiedTiles(null, null, new GridObject[] { _player.current, _target.current }); for (int i = 0; i < _uTiles.Count; i++) { Tile tile = _uTiles[i]; _map[tile.x, tile.y] = false; grid.UpdateGrid(_map); if (NesScripts.Controls.PathFind.Pathfinding.FindPath(grid, _from, _to, Pathfinding.DistanceType.Manhattan).Count > 0) { //occupy tile tile.Occupy(); //add and move enemy Enemy e = Instantiate(_enemyPrefab, _enemyContainer.transform); e.Move(tile); _c--; if (_c == 0) { break; } } else { _map[tile.x, tile.y] = true; } } }
void Start() { tilesmap = new bool[width, height]; for (int x = 0; x < tilesmap.GetLength(0); x += 1) { for (int y = 0; y < tilesmap.GetLength(1); y += 1) { if (Physics2D.BoxCast(new Vector2(x, y), new Vector2(.95f, .95f), 0f, Vector2.up, 1f, obstacleMask)) { tilesmap[x, y] = false; } else { tilesmap[x, y] = true; } } } Grid = new NesScripts.Controls.PathFind.Grid(tilesmap); // Camera.main.transform.position = Vector3.zero + new Vector3((width - 1) / 2, (height - 1) / 2f, -100f); }
private void Awake() { Instance = this; Grid = new NesScripts.Controls.PathFind.Grid(GridSquares, GridSquares); GridWithMovables = new NesScripts.Controls.PathFind.Grid(GridSquares, GridSquares); // Add default impasses (e.g. house etc) foreach (var area in DefaultImpasseAreas) { for (int x = (int)area.x; x < area.x + area.width; x++) { for (int y = (int)area.y; y < area.y + area.height; y++) { Grid.nodes[x, y].Type = NodeContent.Impasse; Grid.nodes[x, y].walkable = false; Grid.nodes[x, y].price = 0; } } } }
public void Init(int backgorundID) { Events.Blocktile += Blocktile; tilesmap = new float[tilesWidth, tilesWHeight]; int id = 0; for (int a = 0; a < tilesWidth; a++) { for (int b = 0; b < tilesWHeight; b++) { bool isWalkable = false; tilesmap [a, b] = 1; isWalkable = true; Tile newTile = Instantiate(tile); newTile.transform.SetParent(sceneInGame.tilesContainer); newTile.Init(isWalkable, new Vector3(a, 0, b)); tiles.Add(newTile); } } grid = new NesScripts.Controls.PathFind.Grid(tilesWidth, tilesWHeight, tilesmap); AddRandomObjects(backgorundID); }
// Start is called before the first frame update void Start() { currentHealth = maxHealth; for (int x = 0; x < 30; x++) { for (int y = 0; y < 30; y++) { bool isBlock = Random.Range(1, 10) == 1; tilesmap[x, y] = !isBlock; myGrid[x, y] = Instantiate(isBlock ? tileBlock : tile) as GameObject; myGrid[x, y].transform.position = new Vector3(x, y, 0); } } grid = new NesScripts.Controls.PathFind.Grid(tilesmap); _from = new NesScripts.Controls.PathFind.Point(1, 1); _to = new NesScripts.Controls.PathFind.Point(15, 15); // List<NesScripts.Controls.PathFind.Point> path = NesScripts.Controls.PathFind.Pathfinding.FindPath(grid, _from, _to); }
//Generate a random level public void GenerateLevel(int level) { level = 1; currentMap = new GameObject("Map"); //Get a random dungeon file and read it int rand = (int)(UnityEngine.Random.value * (numberOfDungeonsPerLevel[level])); string path = "Assets\\Dungeons\\level" + level + "\\d" + rand + ".json"; using (StreamReader r = new StreamReader(path)) { string json = r.ReadToEnd(); JSONMap = JsonUtility.FromJson <GeneratedMapJSONContent>(json); } //Add the rectangles tiles foreach (TileRect tile in JSONMap.rects) { for (int i = 0; i < tile.w; i++) { for (int j = 0; j < tile.h; j++) { AddTileFloor((tile.x + i), (tile.y + j)); } } } ///Add the doors tiles foreach (Tile tile in JSONMap.doors) { if (UnityEngine.Random.value < 0.5f) { AddTileDoor(tile.x, tile.y); } } //Add walls tiles : a base layer and then two more AddTilesWallsToMap(tilesFloors); for (int i = 0; i < 2; i++) { AddTilesWallsToMap(tilesWalls); } //Add trapdoor tile AddTileTrapDoor(); //Add player AddTilePlayer(); //Add the key AddTileKey(); //Add mobs //Add items //Make bool tilemap, convert negative numbers tiles positions to an array //Calculate the size of the map and the negative offset //The offset is the minimal number currentGridOffsetX = currentGridOffsetY = 0; int maxX = 0, maxY = 0; foreach (Tile t in tilesWalls) { if (t.x < currentGridOffsetX) { currentGridOffsetX = t.x; } if (t.y < currentGridOffsetY) { currentGridOffsetY = t.y; } if (t.x > maxX) { maxX = t.x; } if (t.y > maxY) { maxY = t.y; } } currentGridOffsetX = Math.Abs(currentGridOffsetX); currentGridOffsetY = Math.Abs(currentGridOffsetY); //Create tilemap with size tilesmap = new bool[(maxX + currentGridOffsetX) + 1, (maxY + currentGridOffsetY) + 1]; //Assign values foreach (Tile t in tilesFloors) { tilesmap[(t.x + currentGridOffsetX), (t.y + currentGridOffsetY)] = true; } foreach (Tile t in tilesWalls) { tilesmap[(t.x + currentGridOffsetX), (t.y + currentGridOffsetY)] = false; } //Create grid pathFindGrid = new NesScripts.Controls.PathFind.Grid(tilesmap); }