diff --git a/assets/tiles-spaced.png b/assets/tiles-spaced.png new file mode 100644 index 00000000..96c15d07 Binary files /dev/null and b/assets/tiles-spaced.png differ diff --git a/examples/spacing.rs b/examples/spacing.rs new file mode 100644 index 00000000..670a270f --- /dev/null +++ b/examples/spacing.rs @@ -0,0 +1,80 @@ +use bevy::{prelude::*, render::texture::ImageSettings}; +use bevy_ecs_tilemap::prelude::*; +mod helpers; + +fn startup(mut commands: Commands, asset_server: Res) { + commands.spawn_bundle(Camera2dBundle::default()); + + let texture_handle: Handle = asset_server.load("tiles-spaced.png"); + + let tilemap_size = TilemapSize { x: 32, y: 32 }; + + // Layer 1 + let mut tile_storage = TileStorage::empty(tilemap_size); + let tilemap_entity = commands.spawn().id(); + + fill_tilemap( + TileTexture(0), + tilemap_size, + TilemapId(tilemap_entity), + &mut commands, + &mut tile_storage, + ); + + let tile_size = TilemapTileSize { x: 16.0, y: 16.0 }; + let grid_size = tile_size.into(); + + commands + .entity(tilemap_entity) + .insert_bundle(TilemapBundle { + grid_size, + size: tilemap_size, + storage: tile_storage, + texture: TilemapTexture::Single(texture_handle.clone()), + tile_size, + spacing: TilemapSpacing { x: 8.0, y: 8.0 }, + transform: get_tilemap_center_transform(&tilemap_size, &grid_size, 0.0), + ..Default::default() + }); + + // Layer 2 + let mut tile_storage = TileStorage::empty(tilemap_size); + let tilemap_entity = commands.spawn().id(); + + fill_tilemap( + TileTexture(2), + tilemap_size, + TilemapId(tilemap_entity), + &mut commands, + &mut tile_storage, + ); + + commands + .entity(tilemap_entity) + .insert_bundle(TilemapBundle { + grid_size: grid_size, + size: tilemap_size, + storage: tile_storage, + texture: TilemapTexture::Single(texture_handle), + tile_size: TilemapTileSize { x: 16.0, y: 16.0 }, + transform: get_tilemap_center_transform(&tilemap_size, &grid_size, 1.0) + * Transform::from_xyz(32.0, 32.0, 0.0), + ..Default::default() + }); +} + +fn main() { + App::new() + .insert_resource(WindowDescriptor { + width: 1270.0, + height: 720.0, + title: String::from("Spacing Example"), + ..Default::default() + }) + .insert_resource(ImageSettings::default_nearest()) + .add_plugins(DefaultPlugins) + .add_plugin(TilemapPlugin) + .add_startup_system(startup) + .add_system(helpers::camera::movement) + .run(); +} diff --git a/src/map/mod.rs b/src/map/mod.rs index 45f7295a..1e9a1f21 100644 --- a/src/map/mod.rs +++ b/src/map/mod.rs @@ -229,7 +229,8 @@ impl From<&Vec2> for TilemapGridSize { } } -/// Spacing between tiles inside of the texture atlas. +/// Spacing between tiles in pixels inside of the texture atlas. +/// Defaults to 0.0 #[derive(Component, Default, Clone, Copy, Debug)] pub struct TilemapSpacing { pub x: f32, diff --git a/src/render/extract.rs b/src/render/extract.rs index e44763d8..8dd6284b 100644 --- a/src/render/extract.rs +++ b/src/render/extract.rs @@ -96,10 +96,8 @@ impl ExtractedTilemapTexture { it is being extracted as a texture!", ); let texture_size: TilemapTextureSize = image.size().into(); - let tile_count_x = - (texture_size.x + tile_spacing.x) / (tile_size.x + tile_spacing.x); - let tile_count_y = - (texture_size.y + tile_spacing.y) / (tile_size.y + tile_spacing.y); + let tile_count_x = ((texture_size.x) / (tile_size.x + tile_spacing.x)).floor(); + let tile_count_y = ((texture_size.y) / (tile_size.y + tile_spacing.y)).floor(); ((tile_count_x * tile_count_y) as u32, texture_size) } #[cfg(not(feature = "atlas"))] diff --git a/src/render/texture_array_cache.rs b/src/render/texture_array_cache.rs index e5036a05..32d0b213 100644 --- a/src/render/texture_array_cache.rs +++ b/src/render/texture_array_cache.rs @@ -73,10 +73,8 @@ impl TextureArrayCache { it is being extracted as a texture!", ); let texture_size: TilemapTextureSize = image.size().into(); - let tile_count_x = - (texture_size.x + tile_spacing.x) / (tile_size.x + tile_spacing.x); - let tile_count_y = - (texture_size.y + tile_spacing.y) / (tile_size.y + tile_spacing.y); + let tile_count_x = ((texture_size.x) / (tile_size.x + tile_spacing.x)).floor(); + let tile_count_y = ((texture_size.y) / (tile_size.y + tile_spacing.y)).floor(); ((tile_count_x * tile_count_y) as u32, texture_size) } TilemapTexture::Vector(handles) => { @@ -206,12 +204,15 @@ impl TextureArrayCache { }); for i in 0..count { - let columns = (texture_size.x as f32 + spacing.x as f32) - / (tile_size.x as f32 + spacing.x as f32); - let sprite_sheet_x: f32 = - (i as f32 % columns).floor() * (tile_size.x + spacing.x) as f32; - let sprite_sheet_y: f32 = - (i as f32 / columns).floor() * (tile_size.y + spacing.y) as f32; + let columns = ((texture_size.x as f32) + / (tile_size.x as f32 + spacing.x as f32)) + .floor(); + let sprite_sheet_x: f32 = (i as f32 % columns).floor() + * (tile_size.x + spacing.x) as f32 + + spacing.x; + let sprite_sheet_y: f32 = (i as f32 / columns).floor() + * (tile_size.y + spacing.y) as f32 + + spacing.y; command_encoder.copy_texture_to_texture( ImageCopyTexture {