Skip to Content
defold-typescript libraries
selimanac

selimanac/defold-tile-raycast/tile_raycast

Ray Casting in tiled worlds using DDA algorithm.

  • GitHub: selimanac/defold-tile-raycast — pinned to v2.0.1
    1. Pick a release from selimanac/defold-tile-raycast releases and add its Source code (zip) URL (or a packaged .zip asset, if the library ships one) to game.project under [project] dependencies, then Fetch Libraries in the Defold editor.
    2. Run bunx @defold-typescript/cli resolve to materialize its types.
    3. Import it under a namespace alias of your choice:
      import * as tile_raycast from "tile_raycast"

Functions

setup(tile_width: number, tile_height: number, tilemap_width: number, tilemap_height: number, tiles: number[], target_tiles: number[])

Sets up the tile map to cast rays through, replacing any previous one.

Parameters

  • tile_width: number — Width of a single tile.
  • tile_height: number — Height of a single tile.
  • tilemap_width: number — Number of tiles horizontally in the tile map.
  • tilemap_height: number — Number of tiles vertically in the tile map.
  • tiles: number[] — One-dimensional table of tile IDs, row by row.
  • target_tiles: number[] — IDs of the tiles a ray stops at, such as walls.

cast(from_x: number, from_y: number, to_x: number, to_y: number): LuaMultiReturn<[ false ] | [ true, number, number, number, number, number, number, number ]>

Casts a ray through the tile map and reports the first target tile it hits. A miss returns false alone, so check hit before reading the other values. Before setup, it logs an error and returns nothing.

Parameters

  • from_x: number — X coordinate of the ray start.
  • from_y: number — Y coordinate of the ray start.
  • to_x: number — X coordinate of the ray end.
  • to_y: number — Y coordinate of the ray end.

Returns

  • LuaMultiReturn<[ false ] | [ true, number, number, number, number, number, number, number ]> — hit, then on a hit: tile_x, tile_y, array_id (index in the tile table), tile_id, intersection_x, intersection_y and side (LEFT, RIGHT, TOP or BOTTOM). ⚠️ tile_x is 1-based; passed to tile_raycast unchanged. ⚠️ tile_y is 1-based; passed to tile_raycast unchanged. ⚠️ array_id is 1-based; passed to tile_raycast unchanged.

set_at(tile_x: number, tile_y: number): number

Returns the tile ID at a tile coordinate. The extension registers its reader under this name and its writer as {@link tile_raycast.get_at}, the reverse of what its documentation describes.

Parameters

  • tile_x: number — Tile X coordinate. ⚠️ 1-based; passed to tile_raycast unchanged.
  • tile_y: number — Tile Y coordinate. ⚠️ 1-based; passed to tile_raycast unchanged.

Returns

  • number — The tile ID at that coordinate.

get_at(tile_x: number, tile_y: number, tile_id: number)

Sets the tile ID at a tile coordinate. The extension registers its writer under this name and its reader as {@link tile_raycast.set_at}, the reverse of what its documentation describes.

Parameters

  • tile_x: number — Tile X coordinate. ⚠️ 1-based; passed to tile_raycast unchanged.
  • tile_y: number — Tile Y coordinate. ⚠️ 1-based; passed to tile_raycast unchanged.
  • tile_id: number — Tile ID to set.

reset()

Clears all tile and tile map data.

Variables

LEFT: number

The ray hit the left side of the tile.

RIGHT: number

The ray hit the right side of the tile.

TOP: number

The ray hit the top side of the tile.

BOTTOM: number

The ray hit the bottom side of the tile.

On this page