#assets misplaced with bevy_ecs_tilemap

3 messages · Page 1 of 1 (latest)

glacial nymph
#

Hello, i'm using a little sprite for each tile, but they are misplaced because tilemap pos is relative to top left corner, but my sprites are centered in the middle of the map.

i want my sprites aligned to tiles.

#

here is the code i use to load sprites ```rust

fn setup_grid(mut cmd: Commands, asset_server: Res<AssetServer>) {
// camera for rendering
cmd.spawn(Camera2dBundle::default());

// load the asset
let texture_handle: Handle<Image> = asset_server.load("tile.png");

// set grid size ( unit is tile )
let map_size = TilemapSize { x: 16, y: 16 };

// create a entity for the grid and a collection of tiles
let tilemap_entity = cmd.spawn_empty().id();
let mut tile_storage = TileStorage::empty(map_size);

// place tiles on the map
for x in 0..map_size.x {
    for y in 0..map_size.y {
        let tile_pos = TilePos { x, y };
        let tile_entity = cmd
            .spawn(TileBundle {
                position: tile_pos,
                tilemap_id: TilemapId(tilemap_entity),
                ..Default::default()
            })
            .id();

        // register tile
        tile_storage.set(&tile_pos, tile_entity);
    }
}

let tile_size = TilemapTileSize { x: 32.0, y: 32.0 }; // px
let grid_size = tile_size.into(); // same size
let map_type = TilemapType::default(); // square tile

cmd.entity(tilemap_entity).insert(TilemapBundle {
    grid_size,
    map_type,
    size: map_size,
    storage: tile_storage,
    texture: TilemapTexture::Single(texture_handle),
    tile_size,
    transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
    ..Default::default()
});

// #[cfg(all(not(feature = "atlas"), feature = "render"))]
// {
//     array_texture_loader.add(TilemapArrayTexture {
//         texture: TilemapTexture::Single(asset_server.load("tiles.png")),
//         tile_size,
//         ..Default::default()
//     });
// }

}

#

and here is the code i use to test


pub fn handle_click(
    buttons: Res<ButtonInput<MouseButton>>,
    q_windows: Query<&Window, With<PrimaryWindow>>,
    q_tilemap: Query<(
        &TilemapSize,
        &TilemapGridSize,
        &TilemapType,
        &TileStorage,
        &Transform,
    )>,
) {
    //
    if buttons.just_pressed(MouseButton::Left) {
        for (map_size, grid_size, map_type, tile_storage, map_transform) in q_tilemap.iter() {
            if let Some(position) = q_windows.single().cursor_position() {
                if let Some(tile_pos) =
                    TilePos::from_world_pos(&position, map_size, grid_size, map_type)
                {
                    let center = tile_pos.center_in_world(grid_size, map_type);

                    println!("tile pos: ({}, {})", tile_pos.x, tile_pos.y);
                }
            }
        }
    }
}