xaguzman / pathfinding

Java pathfinding framework.
Apache License 2.0
96 stars 28 forks source link

GridCell reuse #17

Closed tootoll closed 6 years ago

tootoll commented 6 years ago

Hi, im creating GridCell on touchDown() as

        GridCell[][] cells = new GridCell[mapHeight][mapWidth];
        for (int x = 0 ; x < mapHeight; x++){
            for (int y = 0 ; y < mapWidth; y++){
                cells[x][y] = new GridCell(x, y, gridObj.getBoolean(x,y));
            }
        }
        GridFinderOptions opt = new GridFinderOptions();
        opt.allowDiagonal = false;
        AStarGridFinder<GridCell> finder = new AStarGridFinder(GridCell.class, opt);

        NavigationGrid<GridCell> navGrid = new NavigationGrid(cells);

        pathToEnd = finder.findPath(playerClass.getPositionX()/32, playerClass.getPositionY()/32, (int) coords.y/32, (int) coords.x/32, navGrid);

Problem with it is that i cant reuse GridCell[][] cells, so i am creating new GridCell[][] cells every time on new search. So am i missing something or is there way to fill GridCell[][] cells on show() and the use old data every time new search is needed on touchDown()?

tootoll commented 6 years ago

Strange how you find your own mistakes as soon as you post question.

I messed up declaring new instance of map every time in code and removing that line after 1 day of searching because of my stupidity realized that it works perfectly

    private List<GridCell> pathToEnd;
    private AStarGridFinder<GridCell> finder;
    private GridFinderOptions opt;
    private NavigationGrid<GridCell> navGrid;
    @Override
    public void show() {
        GridCell[][] cells = new GridCell[mapHeight][mapWidth];
        for (int x = 0 ; x < mapHeight; x++){
            for (int y = 0 ; y < mapWidth; y++){
                if (((TiledMapTileLayer) map.getLayers().get(0)).getCell(y, x).getTile().getProperties().get("Blocked") == null){
                    cells[x][y] = new GridCell(x, y, true);
                }else {
                    cells[x][y] = new GridCell(x, y, false);
                }
            }
        }
        opt = new GridFinderOptions();
        opt.allowDiagonal = false;
        finder = new AStarGridFinder(GridCell.class, opt);
        navGrid = new NavigationGrid(cells);
    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        pathToEnd = finder.findPath(playerClass.getPositionX()/32, playerClass.getPositionY()/32, (int) coords.y/32, (int) coords.x/32,navGrid);

you have my thanks, work perfectly in every way on android studio.