public PathFinding2D(int width, int height) { m_instance = this; m_grid = new AStarGrid <PathNode>(width, height, 1f, Vector3.zero, (AStarGrid <PathNode> grid, int x, int y) => new PathNode(grid, x, y)); m_grid.showDebug = true; }
/** * simulate path finding in grid tilemaps */ public void simPathFinding4() { StopAllCoroutines(); //init map var map = mapToDict4(generateMapArray(width, height)); float xScale = scale; float yScale = scale; renderMap(map, xScale, yScale); //init player and goal var playerPos = new Vector2Int(0, 0); map[playerPos] = (int)TileType.none; setTransformPosition(player.transform, playerPos, xScale, yScale); var goalPos = new Vector2Int(width - 1, height - 1); map[goalPos] = (int)TileType.none; setTransformPosition(goal.transform, goalPos, xScale, yScale); //finding var path = PathFinding2D.find4(playerPos, goalPos, map, passableValues); if (path.Count == 0) { message = "oops! cant find goal"; } else { StartCoroutine(movePlayer(path, xScale, yScale, .2f)); } }
/** * simulate path finding in hexagonal grid tilemaps */ public void simPathFinding6(bool staggerByRow = true) { StopAllCoroutines(); //init map var map = mapToDict6(generateMapArray(width, height), staggerByRow); var hexScale = scale + 4f; //addtional 4f makes tiles seperated float xScale = staggerByRow ? hexScale / 2 : hexScale; float yScale = staggerByRow ? hexScale : hexScale / 2; renderMap(map, xScale, yScale); //init player and goal var mapPoses = map.Keys.ToList(); mapPoses.Sort((a, b) => a.x + a.y - b.x - b.y); var playerPos = mapPoses.First(); map[playerPos] = (int)TileType.none; setTransformPosition(player.transform, playerPos, xScale, yScale); var goalPos = mapPoses.Last(); map[goalPos] = (int)TileType.none; setTransformPosition(goal.transform, goalPos, xScale, yScale); //find List <Vector2Int> path; if (staggerByRow) { path = PathFinding2D.find6X(playerPos, goalPos, map, passableValues); } else { path = PathFinding2D.find6Y(playerPos, goalPos, map, passableValues); } if (path.Count == 0) { message = "oops! cant find goal"; } else { StartCoroutine(movePlayer(path, xScale, yScale, .2f)); } }
private List <Vector2Int> FindPath(Vector2Int endPosition) { Vector2Int startPosition = Main.gridUnitsObjecToData[currentPersonObject]; HashSet <Vector2Int> enemyRc = new HashSet <Vector2Int>(); foreach (var id in GlobalData.EnemyQueue) { enemyRc.Add(GlobalData.Persons[id].FightStartRowCol); } HashSet <Vector2Int> grids = new HashSet <Vector2Int>(); foreach (var key in Main.gridUnitsDataToObject.Keys) { grids.Add(key); } var path = PathFinding2D.Astar(startPosition, endPosition, grids, enemyRc); path.Remove(startPosition); return(path); }
// Start is called before the first frame update void Start() { m_pathFinding2D = new PathFinding2D(m_gridWidth, m_gridHeight); m_visualPathFinding.SetGrid(m_pathFinding2D.GetGrid()); }
protected override void Awake() { base.Awake(); this.pathFindingAlgorithm = this.GetComponent <PathFinding2D>(); }