private PathFind.Grid BuildGrid(Grid grid) { Tilemap ground = grid.transform.Find("Ground").GetComponent <Tilemap>(); Tilemap collision = grid.transform.Find("Collision").GetComponent <Tilemap>(); bool[,] movableTiles = new bool[MAX_WIDTH_OF_ROOM, MAX_HEIGHT_OF_ROOM]; for (int x = ground.cellBounds.min.x; x < ground.cellBounds.max.x; ++x) { for (int y = ground.cellBounds.min.y; y < ground.cellBounds.max.y; ++y) { int r = x - ground.cellBounds.min.x; int c = y - ground.cellBounds.min.y; // All cell should be unreachable by default. movableTiles[r, c] = false; if (ground.HasTile(new Vector3Int(x, y, 0))) { movableTiles[r, c] = true; } if (collision.HasTile(new Vector3Int(x, y, 0))) { movableTiles[r, c] = false; } } } int width = ground.cellBounds.size.x; int height = ground.cellBounds.size.y; PathFind.Grid pathGrid = new PathFind.Grid(width, height, movableTiles); return(pathGrid); }
void PreparePath() { bool[,] tilesmap = new bool[100, 100]; //prepare obstaclemap preparedEnemyPos = this.transform.position; for (int x = 0; x < 100; x++) { for (int y = 0; y < 100; y++) { tilesmap [x, y] = !Physics2D.OverlapBox(new Vector2(x, y) + preparedEnemyPos, new Vector2(1, 1), 0); //Debug.Log (tilesmap [x, y]); } } PathFind.Grid grid = new PathFind.Grid(100, 100, tilesmap); // create source and target points PathFind.Point _from = new PathFind.Point(50, 50); PathFind.Point _to = new PathFind.Point((int)(GameObject.Find("Player").transform.position.x - preparedEnemyPos.x), (int)(GameObject.Find("Player").transform.position.y - preparedEnemyPos.y)); // get path // path will either be a list of Points (x, y), or an empty list if no path is found. path = PathFind.Pathfinding.FindPath(grid, _from, _to); Debug.Log(path.ToArray().Length); foreach (PathFind.Point point in path) { Debug.Log("path x: " + point.x + " y: " + point.y); } }
// ====================================================================================== // public static Position GetNextPosition(Position origin, Position destination) { // if pointing on creature - use the my stupid path finding if (destination.DungeonTile.IsBlockPath) { return(getNextStupidTile(origin, destination)); } // else, using A* of 3rd party library from https://github.com/RonenNess/Unity-2d-pathfinding // create a grid PathFind.Grid grid = new PathFind.Grid(Dungeon.Instance.Width, Dungeon.Instance.Height, Dungeon.Instance.GetWalkingMap()); // create source and target points PathFind.Point _from = new PathFind.Point(origin.X, origin.Y); PathFind.Point _to = new PathFind.Point(destination.X, destination.Y); // get path // path will either be a list of Points (x, y), or an empty list if no path is found. List <PathFind.Point> path = PathFind.Pathfinding.FindPath(grid, _from, _to); // if no path return null position if (path.Count <= 0) { return(Position.NullPosition); } // otherwise, return the first position in path return(new Position(path[0].x, path[0].y)); }
public List <PathFind.Point> Astar(int startX, int startY, int targetX, int targetY) { PathFind.Grid grid = new PathFind.Grid(mapSizeX, mapSizeY, navMesh); PathFind.Point _from = new PathFind.Point(startX, startY); PathFind.Point _to = new PathFind.Point(targetX, targetY); return(PathFind.Pathfinding.FindPath(grid, _from, _to)); }
void Update() { // The X and Y positions of both the player and the enemy are either rounded up or down. // These are necessary in making the AI's path to the player's location in the grid as accurate as possible. float playerX, playerY, enemyX, enemyY; if (player.transform.position.x > (int)player.transform.position.x + .49) { playerX = Mathf.Ceil(player.transform.position.x); } else { playerX = Mathf.Floor(player.transform.position.x); } if (player.transform.position.y > (int)player.transform.position.y + .49) { playerY = Mathf.Ceil(player.transform.position.y); } else { playerY = Mathf.Floor(player.transform.position.y); } if (transform.position.y > (int)transform.position.y + .49) { enemyY = Mathf.Ceil(transform.position.y); } else { enemyY = Mathf.Floor(transform.position.y); } if (transform.position.x > (int)transform.position.x + .49) { enemyX = Mathf.Ceil(transform.position.x); } else { enemyX = Mathf.Floor(transform.position.x); } // The reason why int casting is done here instead of in the if/else block above // is because I had some other code that did a different thing and didn't change this. PathFind.Point playerPos = new PathFind.Point((int)playerX, (int)playerY); PathFind.Point enemyPos = new PathFind.Point((int)enemyX, (int)enemyY); // Have to make a new tilemap, which is a 2D array of floats, where every index corresponds to // the penalty an enemy path would incur, should the enemy cross that way. makeNewTileMap(10, 10); grid = new PathFind.Grid(10, 10, tilesmap); // Find the shortest path to the player. If the count isn't 0, move towards player. enemyPath = PathFind.Pathfinding.FindPath(grid, enemyPos, playerPos); if (enemyPath.Count != 0) { enemyRB.MovePosition(Vector2.LerpUnclamped(new Vector2(transform.position.x, transform.position.y), new Vector2(enemyPath[0].x, enemyPath[0].y), .08f)); } }
protected override void Start() { base.Start(); path = new List <PathFind.Point>(); _to = new PathFind.Point(X, Y); _from = new PathFind.Point(X, Y); grid = new PathFind.Grid(_LevelController.instance.knightsTilemap.GetLength(0), _LevelController.instance.knightsTilemap.GetLength(1), _LevelController.instance.knightsTilemap); path = PathFind.Pathfinding.FindPath(grid, _from, _to); ExtrapolatePath(path); }
// Start is called before the first frame update void Start() { //for (int i = 0; i < length; i++) //{ // for (int j = 0; j < width; j++) // { // baseMap.SetTile(new Vector3Int(i, j, 0), baseTile); // mapData[i, j] = true; // } //} Walker(ref mapData, length, width, 100); for (int i = 0; i < length; i++) { baseMap.SetTile(new Vector3Int(i, 0, 0), baseTile); mapData[i, 0] = true; baseMap.SetTile(new Vector3Int(i, width - 1, 0), baseTile); mapData[i, width - 1] = true; } for (int i = 0; i < width; i++) { baseMap.SetTile(new Vector3Int(length - 1, i, 0), baseTile); mapData[length - 1, i] = true; baseMap.SetTile(new Vector3Int(0, i, 0), baseTile); mapData[0, i] = true; } for (int i = 14; i < 18; i++) { for (int j = 0; j < width; j++) { baseMap.SetTile(new Vector3Int(i, j, 0), baseTile); mapData[i, j] = true; baseMap.SetTile(new Vector3Int(j, i, 0), baseTile); mapData[j, i] = true; } } for (int i = 0; i < width; i++) { baseMap.SetTile(new Vector3Int(min, i, 0), baseTile); mapData[min, i] = true; baseMap.SetTile(new Vector3Int(max, i, 0), baseTile); mapData[max, i] = true; baseMap.SetTile(new Vector3Int(i, min, 0), baseTile); mapData[i, min] = true; baseMap.SetTile(new Vector3Int(i, max, 0), baseTile); mapData[i, max] = true; } grid = new PathFind.Grid(mapData); }
// Use this for initialization void Start() { actor = gameObject; walkableMap = GameManager.instance.walkableMap; gameGrid = new PathFind.Grid(walkableMap.GetLength(0), walkableMap.GetLength(1), walkableMap); SetCurrentPoint(transform.position); targetPoint = new PathFind.Point(currentPoint.x, currentPoint.y); avoidedPedestrian = false; waitingForPedToPass = false; unvisitedShops = new List <Vector2Int>(GameManager.instance.shops); unvisitedShops.Remove(new Vector2Int(currentPoint.x, currentPoint.y)); }
void Start() { enemyRB = gameObject.GetComponent <Rigidbody2D>(); // This for-loop is meant to set all cells in tilesmap to 1f, meaning // that the enemy can walk through it. This is a necessary primer for the 'makeNewTileMap' function. for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { tilesmap[i, j] = 1f; } } grid = new PathFind.Grid(10, 10, tilesmap); }
// Start is called before the first frame update void Start() { for (int i = 0; i < length; i++) { for (int j = 0; j < width; j++) { baseMap.SetTile(new Vector3Int(i, j, 0), baseTile); mapData[i, j] = true; } } //Debug.Log(baseMap.HasTile(new Vector3Int(0, 0, 0))); //Debug.Log(baseMap.HasTile(new Vector3Int(-1, -1, 0))); grid = new PathFind.Grid(mapData); }
private void Initialize() { buildings = new Building[Width, Length]; UnitsPerTile = new int[Width, Length]; var costs = new float[Width, Length]; for (int x = 0; x < Length; x++) { for (int z = 0; z < Length; z++) { costs[x, z] = 1; } } pathFinding = new Grid(Width, Length, costs); }
// Start is called before the first frame update void Start() { mapData = new bool[colmapData.maxLength, colmapData.maxWidth]; Vector2Int mapOrigin = colmapData.minOrigin; //Debug.Log(levelEnemyColMap.HasTile(new Vector3Int(9, 11, 0))); for (int i = mapOrigin.x; i < colmapData.maxLength; i++) { for (int j = mapOrigin.y; j < colmapData.maxWidth; j++) { if (!levelEnemyColMap.HasTile(new Vector3Int(i, j, 0))) { mapData[i, j] = true; } } } grid = new PathFind.Grid(mapData); }
void setupPathfinding() { float[,] pathsValuesGrid = mapManager.getPathsValuesGrid(); int columns = mapManager.getColumns(); int rows = mapManager.getRows(); // every float in the array represent the cost of passing the tile at that position. // use 0.0f for blocking tiles. // create a grid PathFind.Grid grid = new PathFind.Grid(columns, rows, pathsValuesGrid); // get path // path will either be a list of Points (x, y), or an empty list if no path is found. path = PathFind.Pathfinding.FindPath(grid, self, target); /*foreach(PathFind.Point point in path){ * Debug.Log ("x:"+point.x+ " y:"+point.y); * }*/ }
public PathFind.Grid GetGridDefault() { bool[,] tilesmap = new bool[width, height]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { tilesmap[x, y] = false; cellPosition = gridLayout.WorldToCell(new Vector2(x, y)); //finds position of a grid cell if (path.GetSprite(cellPosition) != null) { tilesmap[x, y] = true; } } } PathFind.Grid grid = new PathFind.Grid(width, height, tilesmap); return(grid); }
public override void OnUpdate(float deltaTime) { foreach (var entity in tilemapFilter) { tilemap = entity.GetComponent <TilemapComponent>().Tilemap; break; } var size = tilemap.size; PathFind.Grid grid = GetPathFindGrid(); foreach (var entity in filter) { var pathFinder = entity.GetComponent <PathFinderComponent>(); var transform = entity.GetComponent <TransformComponent>(); var start = tilemap.WorldToCell(transform.Transform.position); var target = tilemap.WorldToCell(pathFinder.Target.position); start = ToPathSystem(size, start); target = ToPathSystem(size, target); PathFind.Point _from = new PathFind.Point(start.x, start.y); PathFind.Point _to = new PathFind.Point(target.x, target.y); List <PathFind.Point> path = PathFind.Pathfinding.FindPath(grid, _from, _to); ref var pathComponent = ref entity.GetComponent <PathComponent>(); pathComponent.CurrentPoint = (Vector2Int)start; pathComponent.Distance = path.Count; if (path.Count > 0) { pathComponent.NextPoint = new Vector2Int(path[0].x, path[0].y); } else { pathComponent.NextPoint = (Vector2Int)target; } }
void AlternativeCatchPlayer() { Vector2 pos = transform.position; desiredMomentum = new Vector2(0, 0); PathFind.Point _from = new PathFind.Point(50, 50); PathFind.Point _to = new PathFind.Point(Random.Range(0, 100), Random.Range(0, 100)); if (Time.frameCount % 20 == 0) { bool[,] tilesmap = new bool[100, 100]; for (int x = 0; x <= 99; x++) { for (int y = 0; y <= 99; y++) { tilesmap [x, y] = !Physics2D.OverlapPoint(new Vector2(x, y) + pos); if (Input.GetKey(KeyCode.I) && !tilesmap[x, y]) { Debug.Log(tilesmap [x, y] + "checking x:" + (x + pos.x) + " y:" + (y + pos.y)); } } } PathFind.Grid grid = new PathFind.Grid(100, 100, tilesmap); path = PathFind.Pathfinding.FindPath(grid, _from, _to); } if (path != null && Time.frameCount % 20 < path.ToArray().Length) { desiredMomentum.x = (path [Time.frameCount % 20].x - 50) * speed; desiredMomentum.y = (path [Time.frameCount % 20].y - 50) * speed; } GetComponent <Rigidbody2D>().AddForce(desiredMomentum); }
void GenerateNavMesh() { float[,] tileMap = new float[width, height]; RaycastHit2D hit; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { hit = Physics2D.Raycast(new Vector3(x + 0.5f, y + 0.5f, 0), Vector2.zero, 3, nonWalkableLayerMask); if (hit) { tileMap[x, y] = 0; } else { tileMap[x, y] = 1; walkablePoints.Add(new Vector2(x, y)); } } } grid = new PathFind.Grid(width, height, tileMap); }
bool[,] tilesmap; // tiles in grid used for pathfinding // Use this for initialization void Start() { // Set up the pathfinding grid GameObject navGrid = GameObject.Find("NavGrid"); navGridScript = navGrid.GetComponent <NavGrid> (); // get the script attached gridRows = navGridScript.gridRows; gridCols = navGridScript.gridCols; tilesmap = new bool[gridCols, gridRows]; // # of tiles in grid used for pathfinding // get all grid squares and read if they are navigable var gridUnits = navGrid.GetComponentsInChildren <GridUnit> (true); foreach (GridUnit unit in gridUnits) { var r = unit.row; var c = unit.col; tilesmap [c, r] = unit.canNavigateTo; } // create a grid object for pathfinding grid = new PathFind.Grid(gridCols, gridRows, tilesmap); }
// Update is called once per frame void Update() { if (pause_game_control.paused) { return; } CleanUpActionLine(); if (unit_action_counter < unit_action_speed) { unit_action_counter += 1; return; } else { unit_action_counter = 0; } // If we have a job do something about it if (job != null) { // If we don't have a path we are either at the job // or we need to compute an new path if (!IsPathing()) { if (IsByJob()) { // we are at the target, do work! path = null; WorkOnJob(); } else { // We arent't near the target, path there var pos = GetPos(); var tgt = job.GetTarget(); bool[,] d_grid_mask = dispatcher.GetGridMask(); bool[,] grid_mask = new bool[d_grid_mask.GetLength(0), d_grid_mask.GetLength(1)]; for (int i = 0; i < d_grid_mask.GetLength(0); i++) { for (int j = 0; j < d_grid_mask.GetLength(1); j++) { grid_mask[i, j] = d_grid_mask[i, j]; } } grid_mask[tgt.x, tgt.y] = true; if (pathfinding_grid == null) { pathfinding_grid = new PathFind.Grid(grid_mask); } else { pathfinding_grid.UpdateGrid(grid_mask); } var _from = new PathFind.Point((int)pos.x, (int)pos.y); var _to = new PathFind.Point(tgt.x, tgt.y); path = PathFind.Pathfinding.FindPath(pathfinding_grid, _from, _to); if (path.Count == 0) { // we were not able to path to location or. Abort useful tasks, cancel idle tasks; if (job is IdleJob) { job.Cancel(); } else { job.Abort("Could not find path to job."); } } else if (path.Count > 1) { // pop off the last action so we don't move onto the tile. path.RemoveAt(path.Count - 1); } } } // explicitly separate if so that we can move this turn // if we needed to compute a new path if (IsPathing()) { // get the next point on the path and move there. var move_to = path[0]; path.RemoveAt(0); var new_vec = new Vector3((float)move_to.x, (float)move_to.y, transform.position.z); transform.position = new_vec; var pos = GetPos(); dispatcher.AcknowledgeUnitMove(pos, new_vec); } } else { // if we have no job, we can clear our pathing path = null; } }