bevy_ecs_tilemap
repository·main·Indexed 22 days ago
https://github.com/stararawn/bevy_ecs_tilemapA high-performance tilemap rendering plugin for the Bevy engine (v0.19.0). It treats every tile as an individual ECS entity for easy manipulation and animation while utilizing a chunked mesh approach for GPU efficiency. The library includes comprehensive helpers for filling tilemaps and extensive support for hexagonal grids, including axial and cube coordinate systems, world-space projections, and neighbor management.
What's inside bevy_ecs_tilemap
- The plugin uses an ECS-centric approach where each tile is its own entity. To maintain high performance, tiles are automatically split into chunks behind the scenes. Each chunk has its own mesh that is sent to the GPU in an optimized manner, allowing for fast rendering even with many tiles.
Editing tiles via ECS queries
mainBecause every tile is an entity, you can manipulate them using standard Bevy queries. This makes it easy to implement features like tile-based damage, animations, or environmental changes by querying for specific tile components and updating them.
struct Damage { amount: u32, } fn update_damage( mut query: Query<(&mut TileTexture, &Damage), Changed<Damage>>, ) { for (mut tile_texture, damage) in query.iter_mut() { tile_texture.0 = TILE_DAMAGE_OFFSET + damage.amount; } }Run examples on the web
mainYou can run examples in a web browser using
wasm-server-runner.For WebGL2:
cargo run --target wasm32-unknown-unknown --example animationFor WebGPU:
cargo run --example animation --target=wasm32-unknown-unknown --features=bevy/webgpuUse DiamondPos for isometric diamond coordinate systems
mainDiamondPosis a vector-like coordinate system used for tiles arranged in an isometric diamond grid. It supports standard arithmetic operations like addition, subtraction, and scalar multiplication by ani32.Key capabilities include:
- Mapping
DiamondPosto world space (center, corners, or fractional positions). - Mapping world space positions back to a
DiamondPos. - Converting from other coordinate systems like
TilePos,StaggeredPos, orSquarePos. - Calculating neighbor offsets using
SquareDirectionvia theoffsetmethod ordiamond_offsetonTilePos.
- Mapping
Use `HexRowDirection` and `HexColDirection` for compass directions
mainDepending on your coordinate system, you can use semantic compass directions instead of raw mathematical directions.
HexRowDirection: Used for row-oriented systems (Row,RowEven,RowOdd). Variants:North,NorthWest,SouthWest,South,SouthEast,NorthEast.HexColDirection: Used for column-oriented systems (Column,ColumnEven,ColumnOdd). Variants:East,NorthEast,NorthWest,West,SouthWest,SouthEast.
Both enums can be converted to/from
HexDirectionand provide an.offset()method to find the neighboringTilePosgiven a starting position and aHexCoordSystem.Install and initialize the TilemapPlugin
mainTo use
bevy_ecs_tilemapin your Bevy application, you must add theTilemapPluginto your app. This plugin is required for rendering tilemaps. If you are running a headless application without a renderer, the plugin will still function but the rendering components will be omitted.Note that if the
renderfeature is enabled, the plugin automatically adds theTilemapRenderingPluginand handles internal scheduling for tile position updates.Run the basic example
mainTo run the simplest example of creating a tilemap, use the following command:
cargo run --release --example basicUse CubePos for hexagonal grid coordinates
mainThe
CubePosstruct represents a coordinate in a hexagonal grid using the cube coordinate system. It consists of three componentsq,r, andsthat always satisfy the identityq + r + s = 0.CubePosbehaves like a vector: you can add or subtract twoCubePosinstances, and you can multiply aCubePosby ani32oru32scalar. It can be converted fromAxialPosorTilePos.Use FractionalAxialPos for sub-tile precision
mainFractionalAxialPosrepresents a point that lies inside a hexagon, typically resulting from mapping a world position into hexagonal space. It usesf32components forqandr.It can be converted into a discrete
AxialPosusing the.round()method, which uses cube rounding to find the nearest hex center.Fill a rectangular region of a tilemap
mainUse
fill_tilemap_rectto spawn tiles within a rectangular area. The region is defined by anorigin(TilePos) and asize(TilemapSize).fill_tilemap_rect( texture_index, origin, size, tilemap_id, &mut commands, &mut tile_storage, );Clear all entities from TileStorage using drain()
mainTo remove all stored entities from the map and retrieve them (e.g., to despawn them), usedrain(). This returns an iterator over allEntityvalues currently held in the storage, leaving the storage empty (Noneat all positions).Get neighbors using SquareDirection
mainTo find adjacent tiles in a square grid, use
SquareDirectionin conjunction withSquarePosorTilePos.- From
SquarePos: Usepos.offset(direction)to get a newSquarePosrepresenting the neighbor. - From
TilePos: Usetile_pos.square_offset(direction, map_size)to get anOption<TilePos>. This method automatically handles bounds checking against the providedmap_size, returningNoneif the neighbor is outside the map.
This is specifically for standard (non-isometric) square coordinate systems.
- From