melonJS
    Preparing search index...

    Class Grid

    A grid of cells a path can be found across, with A*.

    Each cell is open or solid and has a weight (1 or more) to enter. Build one from a Tiled tile layer (Grid.fromTileLayer), from a level's collision shapes (Grid.fromBodies), or from your own callbacks; change cells as the game goes on with Grid#setSolid and Grid#setWeight, and search it by cell with Grid#getIdPath and Grid#getPointPath, or by world position with Grid#findPath.

    Cells are Vector2ds, x the column and y the row. Points in world units are each cell's position: its corner plus Grid#offset, which is its centre by default.

    Cells are square (an orthogonal map) or diamonds (an isometric map), see CellShape. Staggered and hexagonal maps are not supported.

    const grid = new Grid({ cols: 20, rows: 15, tileWidth: 32, tileHeight: 32 });
    grid.diagonalMode = "onlyIfNoObstacles";
    grid.setSolid(5, 3);
    const cells = grid.getIdPath(new Vector2d(0, 0), new Vector2d(10, 8));
    const { path, found } = grid.findPath(enemy.pos, player.pos);
    Index
    cellShape: CellShape

    how cells sit in the world

    cols: number

    columns

    diagonalMode: DiagonalMode = "never"

    when a path may move diagonally

    "never"
    
    heuristic: Heuristic | undefined = undefined

    the estimate of the distance left

    "manhattan" when diagonalMode is "never", "octile" otherwise

    offset: Vector2d

    where a cell's position is, from its corner (the top left of a square, the top of a diamond): the points of a path are cell positions

    the centre: half a cell across and down for a square, half a
    cell down for a diamond
    rows: number

    rows

    tileHeight: number

    a cell's height, in world units

    tileWidth: number

    a cell's width, in world units

    x: number

    where the grid's top left corner is in the world

    y: number

    where the grid's top left corner is in the world

    • Find the cheapest path between two world positions, with what the search found: whether it reached the goal, the path as points and as cells, its cost and how much it looked at.

      Parameters

      • from: Readonly<{ x: number; y: number }>

        where to start, in world units: a renderable's pos, a Vector2d, any { x, y }. Clamped to the grid; the start may stand on a solid cell.

      • to: Readonly<{ x: number; y: number }>

        where to go, in world units. Clamped to the grid.

      • options: PathOptions = {}

        see PathOptions; avoid is in world units

      Returns PathResult

      the path, with whether it reaches the goal

    • The cheapest path between two cells, as cells, from the start to the goal. Empty when there is none, unless allowPartialPath.

      Parameters

      • from: Readonly<{ x: number; y: number }>

        the start cell (x the column, y the row; a fraction is rounded down). Clamped to the grid.

      • to: Readonly<{ x: number; y: number }>

        the goal cell. Clamped to the grid.

      • options: boolean | PathOptions = false

        allowPartialPath, or the search's options

      Returns Vector2d[]

      the cells, start and goal included

    • The cheapest path between two cells, as points in world units (each cell's position, see Grid#offset), from the start to the goal. Empty when there is none, unless allowPartialPath.

      Parameters

      • from: Readonly<{ x: number; y: number }>

        the start cell (x the column, y the row; a fraction is rounded down). Clamped to the grid.

      • to: Readonly<{ x: number; y: number }>

        the goal cell. Clamped to the grid.

      • options: boolean | PathOptions = false

        allowPartialPath, or the search's options

      Returns Vector2d[]

      the points, start and goal included

    • what entering a cell costs

      Parameters

      • col: number

        the cell's column

      • row: number

        the cell's row

      Returns number

      its weight, 1 or more

    • whether a cell is on the grid

      Parameters

      • col: number

        the cell's column

      • row: number

        the cell's row

      Returns boolean

      true if it is

    • whether a cell is solid (off the grid, it is)

      Parameters

      • col: number

        the cell's column

      • row: number

        the cell's row

      Returns boolean

      true if it is

    • Whether a straight line from the centre of one cell to the centre of another crosses only open cells, the two ends included, never cutting a corner between two solid ones.

      Parameters

      • from: Readonly<{ x: number; y: number }>

        one end (x the column, y the row)

      • to: Readonly<{ x: number; y: number }>

        the other end

      • maxWeight: number = Infinity

        only through cells this heavy or lighter

      Returns boolean

      true if the line is clear

    • make a cell solid or open: a door, a wall knocked down, a floor fallen away

      Parameters

      • col: number

        the cell's column

      • row: number

        the cell's row

      • solid: boolean = true

        false to open it

      Returns void

    • set what entering a cell costs: 1 or more, a finite number (mud 3, a road 1). To block it, use Grid#setSolid.

      Parameters

      • col: number

        the cell's column

      • row: number

        the cell's row

      • weight: number

        1 or more

      Returns void

    • A grid over a level's collision shapes: the walls drawn in a Tiled map's collision layer, or any static body. A cell is solid when a wall's shape covers any of nine points spread over it (a wall thinner than an eighth of a cell can slip between them). Cells are read from the shapes themselves, from the body or the bodyDef, and from each wall's bounding box when it has none.

      Build it once the level is loaded (the shapes are scanned then, not found by a physics query, so it works before the world first steps); Grid#refresh scans them again.

      Parameters

      • container: Container

        where to look for the walls, its children's children included (usually app.world)

      • options: BodiesOptions

        the grid's size, and which renderables are walls

      Returns Grid

      the grid

      await level.load("map", { container: app.world, async: true });
      const map = level.getCurrentLevel();
      const grid = Grid.fromBodies(app.world, {
      cols: map.cols, rows: map.rows,
      tileWidth: map.tilewidth, tileHeight: map.tileheight,
      });
    • A grid over a Tiled tile layer: one cell per tile, square on an orthogonal map and diamonds on an isometric one (staggered and hexagonal maps throw a RangeError). By default a cell holding a tile is solid and an empty one is open (a layer of walls); pass solid to decide from the tile instead (a tile property, its id). A tile's weight property sets what crossing it costs.

      The layer is read again on Grid#refresh, so after changing tiles with setTile / clearTile, refresh the grid (or just the changed cell, with Grid#refreshCell).

      Parameters

      Returns Grid

      a grid with one cell per tile

      const walls = app.world.getChildByName("walls")[0];
      const grid = Grid.fromTileLayer(walls);
      // a floor layer: solid where the tile is a wall, or missing
      const grid = Grid.fromTileLayer(floor, {
      solid: (tile) =>
      tile === null || tile.tileset.getTileProperties(tile.tileId)?.kind === "wall",
      });