### Run Basic Example Source: https://github.com/stararawn/bevy_ecs_tilemap/blob/main/README.md Execute the basic tilemap example using cargo. This is the simplest way to get started. ```bash cargo run --release --example basic ``` -------------------------------- ### Run Animation Example (WebGL2) Source: https://github.com/stararawn/bevy_ecs_tilemap/blob/main/README.md Compile and run the animation example for WebGL2 target using wasm32-unknown-unknown. ```bash cargo run --target wasm32-unknown-unknown --example animation ``` -------------------------------- ### Run Animation Example (WebGPU) Source: https://github.com/stararawn/bevy_ecs_tilemap/blob/main/README.md Compile and run the animation example for WebGPU target, enabling the bevy/webgpu feature. ```bash cargo run --example animation --target=wasm32-unknown-unknown --features=bevy/webgpu ``` -------------------------------- ### Accessing and Modifying Tiles with TileStorage Source: https://context7.com/stararawn/bevy_ecs_tilemap/llms.txt Demonstrates how to use TileStorage for efficient tile entity lookup by position. Includes safe direct gets, bounds-checked gets, and iteration over tile slots. Ensure TilePos is within map bounds for direct access. ```rust use bevy::prelude::* use bevy_ecs_tilemap::prelude::* fn access_tiles( tilemap_q: Query<&TileStorage>, mut tile_q: Query<&mut TileTextureIndex>, ) { for tile_storage in tilemap_q.iter() { // Safe direct get — panics if pos is out of bounds if let Some(entity) = tile_storage.get(&TilePos { x: 0, y: 0 }) { if let Ok(mut idx) = tile_q.get_mut(entity) { idx.0 = 3; } } // Bounds-checked get — returns None for out-of-bounds positions let oob = TilePos { x: 9999, y: 9999 }; assert!(tile_storage.checked_get(&oob).is_none()); // Iterate over all tile slots (Some = occupied, None = empty) let occupied: usize = tile_storage.iter().filter(|s| s.is_some()).count(); println!("Occupied tiles: {occupied}"); } } fn remove_tile( mut tilemap_q: Query<&mut TileStorage>, mut commands: Commands, ) { for mut tile_storage in tilemap_q.iter_mut() { if let Some(entity) = tile_storage.remove(&TilePos { x: 2, y: 2 }) { commands.entity(entity).despawn(); } } } ``` -------------------------------- ### Spawning Isometric Tilemaps Source: https://context7.com/stararawn/bevy_ecs_tilemap/llms.txt Example of spawning an isometric tilemap using the Diamond coordinate system. Requires a specific texture file for isometric tiles. ```rust use bevy::prelude::* use bevy_ecs_tilemap::prelude::* fn spawn_iso_map(mut commands: Commands, asset_server: Res) { let map_size = TilemapSize { x: 8, y: 8 }; let tilemap_entity = commands.spawn_empty().id(); let mut tile_storage = TileStorage::empty(map_size); fill_tilemap( TileTextureIndex(0), map_size, TilemapId(tilemap_entity), &mut commands, &mut tile_storage, ); commands.entity(tilemap_entity).insert(TilemapBundle { grid_size: TilemapGridSize { x: 100.0, y: 50.0 }, map_type: TilemapType::Isometric(IsoCoordSystem::Diamond), size: map_size, storage: tile_storage, texture: TilemapTexture::Single(asset_server.load("bw-tile-iso.png")), tile_size: TilemapTileSize { x: 100.0, y: 50.0 }, anchor: TilemapAnchor::Center, ..Default::default() }); } ``` -------------------------------- ### Spawning Hexagonal Tilemaps Source: https://context7.com/stararawn/bevy_ecs_tilemap/llms.txt Example of spawning a hexagonal tilemap with pointy-top tiles and row-based coordinates. Requires a specific texture file for hex tiles. ```rust use bevy::prelude::* use bevy_ecs_tilemap::prelude::* fn spawn_hex_map(mut commands: Commands, asset_server: Res) { let map_size = TilemapSize { x: 10, y: 10 }; let tilemap_entity = commands.spawn_empty().id(); let mut tile_storage = TileStorage::empty(map_size); fill_tilemap( TileTextureIndex(0), map_size, TilemapId(tilemap_entity), &mut commands, &mut tile_storage, ); // Pointy-top hex tiles with row-based coordinates commands.entity(tilemap_entity).insert(TilemapBundle { grid_size: TilemapGridSize { x: 50.0, y: 58.0 }, map_type: TilemapType::Hexagon(HexCoordSystem::Row), size: map_size, storage: tile_storage, texture: TilemapTexture::Single(asset_server.load("bw-tile-hex-row.png")), tile_size: TilemapTileSize { x: 50.0, y: 58.0 }, anchor: TilemapAnchor::Center, ..Default::default() }); } ``` -------------------------------- ### Spawning an Animated Tile Source: https://context7.com/stararawn/bevy_ecs_tilemap/llms.txt Adds an individual tile entity with the AnimatedTile component to enable GPU-driven animation. The animation range is defined by `start` (inclusive) and `end` (exclusive) atlas frame indices, and `speed` controls the animation rate. ```rust use bevy::prelude::* use bevy_ecs_tilemap::prelude::* use bevy_ecs_tilemap::tiles::{AnimatedTile, TileBundle, TileTextureIndex}; fn spawn_animated_tile( mut commands: Commands, tilemap_entity: Entity, tile_storage: &mut TileStorage, ) { let tile_pos = TilePos { x: 5, y: 5 }; commands.spawn(( TileBundle { position: tile_pos, tilemap_id: TilemapId(tilemap_entity), texture_index: TileTextureIndex(0), ..Default::default() }, AnimatedTile { start: 0, // first frame index in atlas (inclusive) end: 13, // last frame index in atlas (exclusive) speed: 0.95, }, )); } ``` -------------------------------- ### Get Hex Grid Neighbors Source: https://context7.com/stararawn/bevy_ecs_tilemap/llms.txt Retrieves up to six neighbors for a tile in a hexagonal grid. Use specific `_row_even`, `_row_odd`, etc. variants for performance if the coordinate system is known. `.entities()` converts positions to entities. ```rust use bevy::prelude::*; use bevy_ecs_tilemap::helpers::hex_grid::neighbors::{HexNeighbors, HexDirection}; use bevy_ecs_tilemap::prelude::*; fn paint_hex_neighbors( tilemap_q: Query<(&TileStorage, &TilemapSize, &TilemapType)>, mut tile_q: Query<&mut TileTextureIndex>, ) { for (tile_storage, map_size, map_type) in tilemap_q.iter() { if let TilemapType::Hexagon(coord_sys) = map_type { let center = TilePos { x: 4, y: 4 }; // Generic dispatch across all hex coord systems let neighbors = HexNeighbors::get_neighboring_positions(¢er, map_size, coord_sys); for entity in neighbors.entities(tile_storage).iter() { if let Ok(mut idx) = tile_q.get_mut(*entity) { idx.0 = 1; } } // Direction-specific access if let Some(pos) = neighbors.get(HexDirection::Zero) { println!("Hex direction 0 neighbor: {:?}", pos); } } } } ``` -------------------------------- ### Get Square Grid Neighbors Source: https://context7.com/stararawn/bevy_ecs_tilemap/llms.txt Retrieves up to eight directional neighbors for a tile in a square grid. Pass `include_diagonals: false` for cardinal directions only. Use `.entities()` to convert positions to entities. ```rust use bevy::prelude::*; use bevy_ecs_tilemap::helpers::square_grid::neighbors::{Neighbors, SquareDirection}; use bevy_ecs_tilemap::prelude::*; fn highlight_neighbors( tilemap_q: Query<(&TileStorage, &TilemapSize)>, mut tile_q: Query<&mut TileTextureIndex>, ) { for (tile_storage, map_size) in tilemap_q.iter() { let center = TilePos { x: 5, y: 5 }; // Get all 8 neighbors (including diagonals) let neighbors = Neighbors::get_square_neighboring_positions(¢er, map_size, true); for neighbor_entity in neighbors.entities(tile_storage).iter() { if let Ok(mut idx) = tile_q.get_mut(*neighbor_entity) { idx.0 = 3; // highlight neighbor tile } } // Access a specific direction if let Some(north_pos) = neighbors.get(SquareDirection::North) { println!("North neighbor: {:?}", north_pos); } // Cardinal-only neighbors (4-connected) let cardinal = Neighbors::get_square_neighboring_positions(¢er, map_size, false); assert!(cardinal.north_east.is_none()); // diagonal excluded } } ``` -------------------------------- ### Dynamically Update Tile Textures and Positions Source: https://context7.com/stararawn/bevy_ecs_tilemap/llms.txt Demonstrates updating tile properties like texture index and position at runtime using ECS queries. Change detection ensures efficient GPU updates. ```rust use bevy::prelude::* use bevy_ecs_tilemap::prelude::* use bevy_ecs_tilemap::tiles::AnimatedTile; // Example: randomly cycle tile textures at 5 Hz fn randomize_tiles( time: Res