Skip to content
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

Add in_world_tile_size field to determine how large each tile is rendered in-world #580

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/accessing_tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
map_type,
texture: TilemapTexture::Single(texture_handle),
tile_size,
in_world_tile_size: tile_size.into(),
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
},
Expand Down
2 changes: 2 additions & 0 deletions examples/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ fn create_background(mut commands: Commands, asset_server: Res<AssetServer>) {
grid_size,
map_type,
tile_size,
in_world_tile_size: tile_size.into(),
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
transform: get_tilemap_center_transform(&size, &grid_size, &map_type, 0.0),
Expand Down Expand Up @@ -115,6 +116,7 @@ fn create_animated_flowers(mut commands: Commands, asset_server: Res<AssetServer
grid_size,
map_type,
tile_size,
in_world_tile_size: tile_size.into(),
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 1.0),
Expand Down
1 change: 1 addition & 0 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ fn startup(
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
tile_size,
in_world_tile_size: tile_size.into(),
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
});
Expand Down
2 changes: 2 additions & 0 deletions examples/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {

let tile_size = TilemapTileSize { x: 16.0, y: 16.0 };
let grid_size = tile_size.into();

let map_type = TilemapType::default();

commands.entity(tilemap_entity).insert(TilemapBundle {
Expand All @@ -34,6 +35,7 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
tile_size,
in_world_tile_size: tile_size.into(),
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
render_settings: TilemapRenderSettings {
render_chunk_size: UVec2::new(256, 256),
Expand Down
1 change: 1 addition & 0 deletions examples/chunking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ fn spawn_chunk(commands: &mut Commands, asset_server: &AssetServer, chunk_pos: I
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
tile_size: TILE_SIZE,
in_world_tile_size: TILE_SIZE.into(),
transform,
render_settings: TilemapRenderSettings {
render_chunk_size: RENDER_CHUNK_SIZE,
Expand Down
1 change: 1 addition & 0 deletions examples/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
tile_size,
in_world_tile_size: tile_size.into(),
map_type: TilemapType::Square,
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
Expand Down
68 changes: 68 additions & 0 deletions examples/custom_tile_scaling.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::*,
};
use bevy_ecs_tilemap::prelude::*;

mod helpers;

fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default());

let texture_handle: Handle<Image> = asset_server.load("tiles.png");

let map_size = TilemapSize { x: 32, y: 32 };
let mut tile_storage = TileStorage::empty(map_size);
let tilemap_entity = commands.spawn_empty().id();

fill_tilemap(
TileTextureIndex(0),
map_size,
TilemapId(tilemap_entity),
&mut commands,
&mut tile_storage,
);

// `tile_size` specifies how large in pixels each tile is in the source atlas texture
let tile_size = TilemapTileSize { x: 16.0, y: 16.0 };

// `in_world_tile_size` specifies how large to render each tile in the world
let in_world_tile_size = TilemapInWorldTileSize { x: 4.0, y: 4.0 };
// The `grid_size` is essentially how far about each tile is placed from eachother
let grid_size = TilemapGridSize { x: 4.0, y: 4.0 };

let map_type = TilemapType::default();

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

fn main() {
App::new()
.add_plugins((
DefaultPlugins
.set(WindowPlugin {
primary_window: Option::Some(Window {
title: String::from("Benchmark Example"),
..Default::default()
}),
..default()
})
.set(ImagePlugin::default_nearest()),
LogDiagnosticsPlugin::default(),
FrameTimeDiagnosticsPlugin::default(),
TilemapPlugin,
))
.add_systems(Startup, startup)
.add_systems(Update, helpers::camera::movement)
.run();
}
1 change: 1 addition & 0 deletions examples/frustum_cull_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ fn spawn_tilemap(mut commands: Commands, tile_handle_square: Res<TileHandleSquar
storage: tile_storage,
texture: TilemapTexture::Single(tile_handle_square.clone()),
tile_size,
in_world_tile_size: tile_size.into(),
map_type: TilemapType::Square,
// The default behaviour is `FrustumCulling(true)`, but we supply this explicitly here
// for the purposes of the example.
Expand Down
1 change: 1 addition & 0 deletions examples/game_of_life.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
tile_size,
in_world_tile_size: tile_size.into(),
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
},
Expand Down
1 change: 1 addition & 0 deletions examples/helpers/tiled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ pub fn process_loaded_maps(
storage: tile_storage,
texture: tilemap_texture.clone(),
tile_size,
in_world_tile_size: tile_size.into(),
spacing: tile_spacing,
transform: get_tilemap_center_transform(
&map_size,
Expand Down
1 change: 1 addition & 0 deletions examples/hex_neighbors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ fn spawn_tilemap(mut commands: Commands, tile_handle_hex_row: Res<TileHandleHexR
storage: tile_storage,
texture: TilemapTexture::Single(tile_handle_hex_row.clone()),
tile_size,
in_world_tile_size: tile_size.into(),
map_type,
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
Expand Down
1 change: 1 addition & 0 deletions examples/hexagon_column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
tile_size,
in_world_tile_size: tile_size.into(),
map_type,
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
Expand Down
1 change: 1 addition & 0 deletions examples/hexagon_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ fn spawn_tilemap(mut commands: Commands, tile_handle_hex_row: Res<TileHandleHexR
storage: tile_storage,
texture: TilemapTexture::Single(tile_handle_hex_row.clone()),
tile_size,
in_world_tile_size: tile_size.into(),
map_type,
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
Expand Down
1 change: 1 addition & 0 deletions examples/hexagon_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
tile_size,
in_world_tile_size: tile_size.into(),
map_type,
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
Expand Down
1 change: 1 addition & 0 deletions examples/iso_diamond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
tile_size,
in_world_tile_size: tile_size.into(),
map_type,
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
Expand Down
1 change: 1 addition & 0 deletions examples/iso_staggered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
tile_size,
in_world_tile_size: tile_size.into(),
map_type,
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
Expand Down
2 changes: 2 additions & 0 deletions examples/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle.clone()),
tile_size,
in_world_tile_size: tile_size.into(),
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
});
Expand All @@ -55,6 +56,7 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
tile_size: TilemapTileSize { x: 16.0, y: 16.0 },
in_world_tile_size: TilemapInWorldTileSize { x: 16.0, y: 16.0 },
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 1.0)
* Transform::from_xyz(32.0, 32.0, 0.0),
..Default::default()
Expand Down
1 change: 1 addition & 0 deletions examples/mouse_to_tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ fn spawn_tilemap(mut commands: Commands, tile_handle_square: Res<TileHandleSquar
storage: tile_storage,
texture: TilemapTexture::Single(tile_handle_square.clone()),
tile_size,
in_world_tile_size: tile_size.into(),
map_type,
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
Expand Down
1 change: 1 addition & 0 deletions examples/move_tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
tile_size,
in_world_tile_size: tile_size.into(),
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
});
Expand Down
1 change: 1 addition & 0 deletions examples/random_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
tile_size,
in_world_tile_size: tile_size.into(),
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
});
Expand Down
1 change: 1 addition & 0 deletions examples/remove_tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
tile_size,
in_world_tile_size: tile_size.into(),
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
},
Expand Down
2 changes: 2 additions & 0 deletions examples/spacing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle.clone()),
tile_size,
in_world_tile_size: tile_size.into(),
spacing: TilemapSpacing { x: 8.0, y: 8.0 },
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
Expand All @@ -56,6 +57,7 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
tile_size: TilemapTileSize { x: 16.0, y: 16.0 },
in_world_tile_size: TilemapInWorldTileSize { x: 16.0, y: 16.0 },
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 1.0)
* Transform::from_xyz(32.0, 32.0, 0.0),
..Default::default()
Expand Down
1 change: 1 addition & 0 deletions examples/texture_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ mod no_atlas {
grid_size,
map_type,
tile_size,
in_world_tile_size: tile_size.into(),
size: map_size,
storage: tile_storage,
texture: texture_vec,
Expand Down
1 change: 1 addition & 0 deletions examples/texture_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ mod no_atlas {
grid_size,
map_type,
tile_size,
in_world_tile_size: tile_size.into(),
size: map_size,
storage: tile_storage,
texture: texture_vec,
Expand Down
1 change: 1 addition & 0 deletions examples/visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
tile_size,
in_world_tile_size: tile_size.into(),
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
},
Expand Down
7 changes: 4 additions & 3 deletions src/helpers/transform.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::prelude::TilemapInWorldTileSize;
use crate::tiles::TilePos;
use crate::{TilemapGridSize, TilemapTileSize, TilemapType};
use crate::{TilemapGridSize, TilemapType};
use bevy::math::{UVec2, Vec2, Vec3};
use bevy::render::primitives::Aabb;

Expand Down Expand Up @@ -29,11 +30,11 @@ pub fn chunk_index_to_world_space(
pub fn chunk_aabb(
chunk_size: UVec2,
grid_size: &TilemapGridSize,
tile_size: &TilemapTileSize,
in_world_tile_size: &TilemapInWorldTileSize,
map_type: &TilemapType,
) -> Aabb {
// The AABB minimum and maximum have to be modified by -border and +border respectively.
let border = Vec2::from(grid_size).max(tile_size.into());
let border = Vec2::from(grid_size).max(in_world_tile_size.into());

// For most map types, it would be sufficient to calculate c0 and c3. However, for some map
// types (right now, isometric diamond), this would not work, and for robustness (especially
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use map::{
TilemapGridSize, TilemapSize, TilemapSpacing, TilemapTexture, TilemapTextureSize,
TilemapTileSize, TilemapType,
};
use prelude::{TilemapId, TilemapRenderSettings};
use prelude::{TilemapId, TilemapInWorldTileSize, TilemapRenderSettings};
#[cfg(feature = "render")]
use render::material::{MaterialTilemap, StandardTilemapMaterial};
use tiles::{
Expand Down Expand Up @@ -120,6 +120,7 @@ pub struct MaterialTilemapBundle<M: MaterialTilemap> {
pub storage: TileStorage,
pub texture: TilemapTexture,
pub tile_size: TilemapTileSize,
pub in_world_tile_size: TilemapInWorldTileSize,
pub transform: Transform,
pub global_transform: GlobalTransform,
pub render_settings: TilemapRenderSettings,
Expand All @@ -145,6 +146,7 @@ pub struct StandardTilemapBundle {
pub storage: TileStorage,
pub texture: TilemapTexture,
pub tile_size: TilemapTileSize,
pub in_world_tile_size: TilemapInWorldTileSize,
pub transform: Transform,
pub global_transform: GlobalTransform,
pub render_settings: TilemapRenderSettings,
Expand Down
Loading