Skip to content

tilemap

FreeBodyEngine.core.tilemap #

Chunk(tilemap, position, size, data) #

A square block of size x size tiles within a Tilemap layer.

Tile data is stored flat in tiles, a numpy array of _NUM_TILE_VALS values per tile (row-major, see _tile_index), rather than as a grid of Tile objects - Tile instances are only created on demand by get_tile.

Args: tilemap: The Tilemap this chunk belongs to. position: The chunk's position in chunk-grid coordinates (see Tilemap.chunk_pos), not world/tile coordinates. size: The chunk's width/height in tiles. data: Flat per-tile value array backing this chunk, _NUM_TILE_VALS values per tile.

position = position instance-attribute #

size = size instance-attribute #

tilemap = tilemap instance-attribute #

tiles = data instance-attribute #

get_tile(position) #

Builds a Tile view onto the tile at position (local to this chunk).

Tile objects are not stored - this reads the raw values back out of tiles and wraps them fresh on every call.

get_tile_neighbors(position) #

Gets the tile's neighbors, reaching across chunk boundaries as needed.

remove_tile(position) #

Clears the tile at position (local to this chunk) back to empty.

set_tile(position, image_id, spritesheet_index) #

Writes a tile's image id and spritesheet index at position (local to this chunk).

Layer(name, chunks, visible) dataclass #

One named, independently-visible layer of a Tilemap, holding its own set of Chunks keyed by chunk position.

chunks instance-attribute #

name instance-attribute #

visible instance-attribute #

StaticSpritesheet(data, renderer) #

Bases: TilemapSpritesheet

A spritesheet where each tile's image is fixed by its image_id alone (no auto-tiling/animation), so its image index only needs computing once per tile (UpdateMode.ONCE).

data: Spritesheet definition; data["paths"] is a list of (key, path) pairs, keyed by image_id. renderer: The tilemap's renderer, whose texture stack the paths are added to.

data = data instance-attribute #

get_image_id(key) #

Looks up the image_id registered under key in data["paths"], or -1 if key isn't found.

get_image_index(tile, neighbors) #

Looks up tile's image index by its image_id alone; neighbors is unused.

get_name() staticmethod #

The type name spritesheet data uses to select this class (see Tilemap.add_spritesheet_type).

Tile(position, image_id, spritesheet, chunk) #

A single tile, backing onto its owning Chunk's underlying data array.

A Tile is a lightweight view rather than the source of truth - it holds no state of its own beyond what it was constructed with, and every property setter writes straight through to _chunk so the change is reflected in the chunk's tile data immediately.

Args: position: The tile's position, local to chunk. image_id: The id of the image drawn for this tile. spritesheet: The name of the spritesheet image_id is looked up in. chunk: The chunk this tile belongs to; writes made through this Tile are applied to chunk.

image_id property writable #

The id of the image drawn for this tile.

position property writable #

The tile's position, local to its chunk.

spritesheet property writable #

The name of the spritesheet image_id is looked up in.

destroy() #

Removes this tile from its chunk.

Tilemap(position=Vector(), rotation=0, scale=Vector(1, 1), chunk_size=16, tile_size=1) #

Bases: Node2D

A layered grid of tiles, split into fixed-size Chunks for storage and rendering. Tile positions are addressed in tilemap coordinates (see tilemap_pos); chunk_pos/tile_pos convert those down to the chunk a tile lives in and its local position within that chunk.

Args: position: World position of the tilemap node. rotation: World rotation of the tilemap node. scale: World scale of the tilemap node. chunk_size: Width/height of each chunk, in tiles. tile_size: Size of a single tile, in world units.

chunk_size = chunk_size instance-attribute #

layers = {} instance-attribute #

renderer = None instance-attribute #

spritesheets = {} instance-attribute #

tile_size = tile_size instance-attribute #

add_chunk(position, layer, data=None) #

Creates a chunk at chunk-grid position on layer, backed by data if given, otherwise a freshly allocated empty chunk.

add_layer(name, chunks={}, visible=False) #

Creates a new, empty (unless chunks is given) layer under name.

add_spritesheet(spritesheet_type, data) #

Instantiates a registered spritesheet type from data and stores it under data["name"]. Requires create_renderer to have been called first, since spritesheet construction needs the renderer to upload textures.

add_spritesheet_type(type) #

Registers a TilemapSpritesheet subclass so it can be created by create_spritesheet/add_spritesheet via its get_name().

chunk_exists(position, layer) #

Whether a chunk has been created at chunk-grid position on layer.

chunk_pos(position) #

Converts a tilemap position into a chunk position.

create_renderer() #

Creates and attaches this tilemap's TilemapRenderer, and registers the built-in StaticSpritesheet type. Must be called before any spritesheet is added (see add_spritesheet).

create_spritesheet(data) #

Creates a spritesheet and adds it the tilemaps spritesheets.

get_chunk(position, layer) #

Gets the chunk at chunk-grid position on layer, logging an error (and returning None) if no chunk exists there.

get_tile(position, layer) #

Gets the tile at tilemap position on layer, resolving it to the owning chunk first.

get_tile_neighbors() #

Not yet implemented.

set_tile(position, image_id, spritesheet, layer) #

Sets the tile at tilemap position on layer, resolving it to the owning chunk first.

tile_pos(position) #

Converts a tilemap position into the tile position in the chunk.

tilemap_pos(position) #

Converts a world position into a position in the tilemap.

TilemapSpritesheet(data, renderer, update_mode=UpdateMode.NEVER) #

Base class for a tilemap's image-lookup source: maps a tile's image_id (and its neighbors, for auto-tiling) to an index into the renderer's uploaded texture stack. Subclasses implement get_image_index, _extract_paths, and update for a particular lookup strategy (static, auto-tiled, animated).

Args: data: The spritesheet definition data (as passed to Tilemap.create_spritesheet). renderer: The tilemap's renderer; its texture stack is extended with the paths this spritesheet extracts from data. update_mode: How often get_image_index is re-run for a tile - see UpdateMode.

data = data instance-attribute #

path_map = renderer._add_textures(self._extract_paths(data)) instance-attribute #

update_mode = update_mode instance-attribute #

get_image_index(tile, neighbors) #

Standardized function to get the image_index for any given tile. The frequency this is run is determined by the tilemap's 'update_mode'.

:param tile: The tile that the image index is being gotten for. :type tile: Tile

:param neighbors: The 8 neighbors of the given tile, ordered in the clockwise direction stating in the top left. Includes neighbors in nearby chunks. :type neighbors: tuple[Tile, Tile, Tile, Tile, Tile, Tile, Tile, Tile]

:rtype: int

get_name() staticmethod #

The type name spritesheet data uses to select this class (see Tilemap.add_spritesheet_type).

update() #

Called when the tilemap node is updated.