-
Notifications
You must be signed in to change notification settings - Fork 201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spacing fixes #309
Spacing fixes #309
Changes from all commits
50fa7c6
c71b989
87d1d9a
5b95a4f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<AssetServer>) { | ||
commands.spawn_bundle(Camera2dBundle::default()); | ||
|
||
let texture_handle: Handle<Image> = 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(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to floor here because otherwise you can get tile counts that are larger than what's available in the atlas. |
||
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"))] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't divide by 2 here. This is because spacing is treated as the "space" between tiles. This is what tiled map editor does and I think it's something we should stick with. |
||
|
||
command_encoder.copy_texture_to_texture( | ||
ImageCopyTexture { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simple example based off of the layer example(just to show multiple tiles). The spacing is 8 pixels between each tile on both the X and Y axes.