From 8e9b80d9b2377f3db7444129d80940469fa915ba Mon Sep 17 00:00:00 2001 From: patrickariel <161032380+patrickariel@users.noreply.github.com> Date: Mon, 14 Apr 2025 03:10:52 +0700 Subject: [PATCH] feature: migrate to required components --- Cargo.toml | 2 +- examples/accessing_tiles.rs | 27 +++++-------- examples/anchor.rs | 15 +++---- examples/animation.rs | 42 +++++++++---------- examples/basic.rs | 23 +++++------ examples/bench.rs | 19 ++++----- examples/chunking.rs | 25 +++++------- examples/colors.rs | 19 ++++----- examples/custom_shader.rs | 54 ++++++++++++------------- examples/frustum_cull_test.rs | 21 +++++----- examples/game_of_life.rs | 33 ++++++++------- examples/helpers/ldtk.rs | 39 +++++++++--------- examples/helpers/tiled.rs | 43 ++++++++++---------- examples/hex_neighbors.rs | 15 +++---- examples/hex_neighbors_radius_chunks.rs | 17 ++++---- examples/hexagon_column.rs | 15 +++---- examples/hexagon_generation.rs | 15 +++---- examples/hexagon_row.rs | 15 +++---- examples/iso_diamond.rs | 17 ++++---- examples/iso_staggered.rs | 17 ++++---- examples/layers.rs | 36 +++++++++-------- examples/mouse_to_tile.rs | 15 +++---- examples/move_tile.rs | 23 +++++------ examples/random_map.rs | 25 ++++++------ examples/remove_tiles.rs | 27 +++++-------- examples/spacing.rs | 38 ++++++++--------- examples/spawn_despawn_tilemap.rs | 33 +++++++-------- examples/texture_container.rs | 26 +++++------- examples/texture_vec.rs | 26 +++++------- examples/visibility.rs | 27 +++++-------- src/helpers/filling.rs | 31 +++----------- src/lib.rs | 45 +++++++++++++++++++-- src/render/draw.rs | 4 +- src/render/material.rs | 39 +++++++++++++----- src/tiles/mod.rs | 18 +++++++++ 35 files changed, 455 insertions(+), 431 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 869df516..fd33c57f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ exclude = ["assets/*", "screenshots/*"] default = ["render"] atlas = [] render = [] -serde = ["dep:serde"] +serde = ["dep:serde", "bevy/serialize"] [dependencies] bevy = { version = "0.16.0-rc.5", default-features = false, features = [ diff --git a/examples/accessing_tiles.rs b/examples/accessing_tiles.rs index 99fa50db..67d863f1 100644 --- a/examples/accessing_tiles.rs +++ b/examples/accessing_tiles.rs @@ -38,11 +38,7 @@ fn startup(mut commands: Commands, asset_server: Res) { for y in 0..map_size.y { let tile_pos = TilePos { x, y }; let tile_entity = commands - .spawn(TileBundle { - position: tile_pos, - tilemap_id: TilemapId(tilemap_entity), - ..Default::default() - }) + .spawn((Tile, tile_pos, TilemapId(tilemap_entity))) .id(); // Here we let the tile storage component know what tiles we have. tile_storage.set(&tile_pos, tile_entity); @@ -78,21 +74,20 @@ fn startup(mut commands: Commands, asset_server: Res) { // This is the size of each individual tiles in pixels. let tile_size = TilemapTileSize { x: 16.0, y: 16.0 }; - let grid_size = tile_size.into(); + let grid_size = TilemapGridSize::from(tile_size); // Spawns a tilemap. // Once the tile storage is inserted onto the tilemap entity it can no longer be accessed. commands.entity(tilemap_entity).insert(( - TilemapBundle { - grid_size, - size: map_size, - storage: tile_storage, - map_type, - texture: TilemapTexture::Single(texture_handle), - tile_size, - anchor: TilemapAnchor::Center, - ..Default::default() - }, + Tilemap, + grid_size, + map_size, + tile_storage, + map_type, + TilemapTexture::Single(texture_handle), + TilemapMaterial::standard(), + tile_size, + TilemapAnchor::Center, LastUpdate(0.0), CurrentColor(1), )); diff --git a/examples/anchor.rs b/examples/anchor.rs index a79355f6..c5165962 100644 --- a/examples/anchor.rs +++ b/examples/anchor.rs @@ -130,16 +130,17 @@ fn spawn_tilemap(mut commands: Commands, tile_handle_square: Res) { let map_type = TilemapType::default(); - commands.entity(tilemap_entity).insert(TilemapBundle { + commands.entity(tilemap_entity).insert(( + Tilemap, size, grid_size, map_type, tile_size, - storage: tile_storage, - texture: TilemapTexture::Single(texture_handle), - anchor: TilemapAnchor::Center, - ..Default::default() - }); + tile_storage, + TilemapTexture::Single(texture_handle), + TilemapMaterial::standard(), + TilemapAnchor::Center, + )); } fn create_animated_flowers(mut commands: Commands, asset_server: Res) { @@ -90,12 +91,10 @@ fn create_animated_flowers(mut commands: Commands, asset_server: Res) { ); let tile_size = TilemapTileSize { x: 16.0, y: 16.0 }; - let grid_size = tile_size.into(); + let grid_size = TilemapGridSize::from(tile_size); let map_type = TilemapType::default(); - commands.entity(tilemap_entity).insert(TilemapBundle { + commands.entity(tilemap_entity).insert(( + Tilemap, grid_size, map_type, - size: map_size, - storage: tile_storage, - texture: TilemapTexture::Single(texture_handle), + map_size, + tile_storage, + TilemapTexture::Single(texture_handle), + TilemapMaterial::standard(), tile_size, - anchor: TilemapAnchor::Center, - render_settings: TilemapRenderSettings { + TilemapAnchor::Center, + TilemapRenderSettings { render_chunk_size: UVec2::new(256, 256), ..Default::default() }, - ..Default::default() - }); + )); } fn main() { diff --git a/examples/chunking.rs b/examples/chunking.rs index 224e8ffb..20bd0f89 100644 --- a/examples/chunking.rs +++ b/examples/chunking.rs @@ -21,11 +21,7 @@ fn spawn_chunk(commands: &mut Commands, asset_server: &AssetServer, chunk_pos: I for y in 0..CHUNK_SIZE.y { let tile_pos = TilePos { x, y }; let tile_entity = commands - .spawn(TileBundle { - position: tile_pos, - tilemap_id: TilemapId(tilemap_entity), - ..Default::default() - }) + .spawn((Tile, tile_pos, TilemapId(tilemap_entity))) .id(); commands.entity(tilemap_entity).add_child(tile_entity); tile_storage.set(&tile_pos, tile_entity); @@ -38,19 +34,20 @@ fn spawn_chunk(commands: &mut Commands, asset_server: &AssetServer, chunk_pos: I 0.0, )); let texture_handle: Handle = asset_server.load("tiles.png"); - commands.entity(tilemap_entity).insert(TilemapBundle { - grid_size: TILE_SIZE.into(), - size: CHUNK_SIZE.into(), - storage: tile_storage, - texture: TilemapTexture::Single(texture_handle), - tile_size: TILE_SIZE, + commands.entity(tilemap_entity).insert(( + Tilemap, + TilemapGridSize::from(TILE_SIZE), + TilemapSize::from(CHUNK_SIZE), + tile_storage, + TilemapTexture::Single(texture_handle), + TilemapMaterial::standard(), + TILE_SIZE, transform, - render_settings: TilemapRenderSettings { + TilemapRenderSettings { render_chunk_size: RENDER_CHUNK_SIZE, ..Default::default() }, - ..Default::default() - }); + )); } fn startup(mut commands: Commands) { diff --git a/examples/colors.rs b/examples/colors.rs index 47921191..18d30f3a 100644 --- a/examples/colors.rs +++ b/examples/colors.rs @@ -75,18 +75,19 @@ fn startup(mut commands: Commands, asset_server: Res) { ); let tile_size = TilemapTileSize { x: 16.0, y: 16.0 }; - let grid_size = tile_size.into(); + let grid_size = TilemapGridSize::from(tile_size); - commands.entity(tilemap_entity).insert(TilemapBundle { + commands.entity(tilemap_entity).insert(( + Tilemap, grid_size, - size: map_size, - storage: tile_storage, - texture: TilemapTexture::Single(texture_handle), + map_size, + tile_storage, + TilemapTexture::Single(texture_handle), + TilemapMaterial::standard(), tile_size, - map_type: TilemapType::Square, - anchor: TilemapAnchor::Center, - ..Default::default() - }); + TilemapType::Square, + TilemapAnchor::Center, + )); } fn main() { diff --git a/examples/custom_shader.rs b/examples/custom_shader.rs index 7a963b7e..075ee2f4 100644 --- a/examples/custom_shader.rs +++ b/examples/custom_shader.rs @@ -24,7 +24,7 @@ fn startup( ) { commands.spawn(Camera2d); - let my_material_handle = MaterialTilemapHandle::from(materials.add(MyMaterial { + let my_material = TilemapMaterial(materials.add(MyMaterial { brightness: 0.5, ..default() })); @@ -46,22 +46,20 @@ fn startup( ); let tile_size = TilemapTileSize { x: 16.0, y: 16.0 }; - let grid_size = tile_size.into(); + let grid_size = TilemapGridSize::from(tile_size); let map_type = TilemapType::default(); - commands - .entity(tilemap_entity) - .insert(MaterialTilemapBundle { - grid_size, - map_type, - size: map_size, - storage: tile_storage, - texture: TilemapTexture::Single(texture_handle.clone()), - tile_size, - anchor: TilemapAnchor::Center, - material: my_material_handle.clone(), - ..Default::default() - }); + commands.entity(tilemap_entity).insert(( + Tilemap, + grid_size, + map_type, + map_size, + tile_storage, + TilemapTexture::Single(texture_handle.clone()), + tile_size, + TilemapAnchor::Center, + my_material.clone(), + )); // Layer 2 let mut tile_storage = TileStorage::empty(map_size); @@ -75,20 +73,18 @@ fn startup( &mut tile_storage, ); - commands - .entity(tilemap_entity) - .insert(MaterialTilemapBundle { - grid_size, - map_type, - size: map_size, - storage: tile_storage, - texture: TilemapTexture::Single(texture_handle), - tile_size: TilemapTileSize { x: 16.0, y: 16.0 }, - anchor: TilemapAnchor::Center, - transform: Transform::from_xyz(32.0, 32.0, 1.0), - material: my_material_handle, - ..Default::default() - }); + commands.entity(tilemap_entity).insert(( + Tilemap, + grid_size, + map_type, + map_size, + tile_storage, + TilemapTexture::Single(texture_handle), + TilemapTileSize { x: 16.0, y: 16.0 }, + TilemapAnchor::Center, + Transform::from_xyz(32.0, 32.0, 1.0), + my_material, + )); } fn main() { diff --git a/examples/frustum_cull_test.rs b/examples/frustum_cull_test.rs index 7127a091..5dd3f9b0 100644 --- a/examples/frustum_cull_test.rs +++ b/examples/frustum_cull_test.rs @@ -61,18 +61,19 @@ fn spawn_scene(mut commands: Commands, texture_handles: Res) { &mut tile_storage, ); - commands.entity(tilemap_entity).insert(TilemapBundle { - grid_size: GRID_SIZE_SQUARE, - size: map_size, - storage: tile_storage, - texture: TilemapTexture::Single(texture_handles.square.clone()), - tile_size: TILE_SIZE_SQUARE, - map_type: TilemapType::Square, + commands.entity(tilemap_entity).insert(( + Tilemap, + GRID_SIZE_SQUARE, + map_size, + tile_storage, + TilemapTexture::Single(texture_handles.square.clone()), + TilemapMaterial::standard(), + TILE_SIZE_SQUARE, + TilemapType::Square, // This is the default value, but we provide it explicitly for demonstration // purposes. - frustum_culling: FrustumCulling(true), - ..default() - }); + FrustumCulling(true), + )); } fn spawn_ui(mut commands: Commands) { diff --git a/examples/game_of_life.rs b/examples/game_of_life.rs index 26c7a350..b270086b 100644 --- a/examples/game_of_life.rs +++ b/examples/game_of_life.rs @@ -18,12 +18,12 @@ fn startup(mut commands: Commands, asset_server: Res) { for y in 0..map_size.y { let tile_pos = TilePos { x, y }; let tile_entity = commands - .spawn(TileBundle { - position: tile_pos, - tilemap_id: TilemapId(tilemap_entity), - visible: TileVisible(i % 2 == 0 || i % 7 == 0), - ..Default::default() - }) + .spawn(( + Tile, + tile_pos, + TilemapId(tilemap_entity), + TileVisible(i % 2 == 0 || i % 7 == 0), + )) .id(); tile_storage.set(&tile_pos, tile_entity); i += 1; @@ -31,20 +31,19 @@ fn startup(mut commands: Commands, asset_server: Res) { } let tile_size = TilemapTileSize { x: 16.0, y: 16.0 }; - let grid_size = tile_size.into(); + let grid_size = TilemapGridSize::from(tile_size); let map_type = TilemapType::Square; commands.entity(tilemap_entity).insert(( - TilemapBundle { - grid_size, - map_type, - size: map_size, - storage: tile_storage, - texture: TilemapTexture::Single(texture_handle), - tile_size, - anchor: TilemapAnchor::Center, - ..Default::default() - }, + Tilemap, + grid_size, + map_type, + map_size, + tile_storage, + TilemapTexture::Single(texture_handle), + TilemapMaterial::standard(), + tile_size, + TilemapAnchor::Center, LastUpdate(0.0), )); } diff --git a/examples/helpers/ldtk.rs b/examples/helpers/ldtk.rs index bea87496..3f2fe07a 100644 --- a/examples/helpers/ldtk.rs +++ b/examples/helpers/ldtk.rs @@ -1,10 +1,11 @@ use bevy_ecs_tilemap::{ - TilemapBundle, + Tilemap, anchor::TilemapAnchor, - map::{TilemapId, TilemapSize, TilemapTexture, TilemapTileSize}, - tiles::{TileBundle, TilePos, TileStorage, TileTextureIndex}, + map::{TilemapGridSize, TilemapId, TilemapSize, TilemapTexture, TilemapTileSize}, + prelude::TilemapMaterial, + tiles::{Tile, TilePos, TileStorage, TileTextureIndex}, }; -use std::{collections::HashMap, io::ErrorKind}; +use std::collections::HashMap; use thiserror::Error; use bevy::{asset::io::Reader, reflect::TypePath}; @@ -71,10 +72,7 @@ impl AssetLoader for LdtkLoader { reader.read_to_end(&mut bytes).await?; let project: ldtk_rust::Project = serde_json::from_slice(&bytes).map_err(|e| { - std::io::Error::new( - ErrorKind::Other, - format!("Could not read contents of Ldtk map: {e}"), - ) + std::io::Error::other(format!("Could not read contents of Ldtk map: {e}")) })?; let dependencies: Vec<(i64, AssetPath)> = project .defs @@ -205,32 +203,33 @@ pub fn process_loaded_tile_maps( position.y = map_tile_count_y - position.y - 1; let tile_entity = commands - .spawn(TileBundle { + .spawn(( + Tile, position, - tilemap_id: TilemapId(map_entity), - texture_index: TileTextureIndex(tile.t as u32), - ..default() - }) + TilemapId(map_entity), + TileTextureIndex(tile.t as u32), + )) .id(); storage.set(&position, tile_entity); } - let grid_size = tile_size.into(); + let grid_size = TilemapGridSize::from(tile_size); let map_type = TilemapType::default(); // Create the tilemap - commands.entity(map_entity).insert(TilemapBundle { + commands.entity(map_entity).insert(( + Tilemap, grid_size, map_type, size, storage, - texture: TilemapTexture::Single(texture), + TilemapTexture::Single(texture), + TilemapMaterial::standard(), tile_size, - anchor: TilemapAnchor::Center, - transform: Transform::from_xyz(0.0, 0.0, layer_id as f32), - ..default() - }); + TilemapAnchor::Center, + Transform::from_xyz(0.0, 0.0, layer_id as f32), + )); } } } diff --git a/examples/helpers/tiled.rs b/examples/helpers/tiled.rs index 50ee6f5d..b46221f3 100644 --- a/examples/helpers/tiled.rs +++ b/examples/helpers/tiled.rs @@ -12,7 +12,7 @@ // * When the 'atlas' feature is enabled tilesets using a collection of images will be skipped. // * Only finite tile layers are loaded. Infinite tile layers and object layers will be skipped. -use std::io::{Cursor, ErrorKind}; +use std::io::Cursor; use std::path::Path; use std::sync::Arc; @@ -118,9 +118,9 @@ impl AssetLoader for TiledLoader { tiled::DefaultResourceCache::new(), BytesResourceReader::new(&bytes), ); - let map = loader.load_tmx_map(load_context.path()).map_err(|e| { - std::io::Error::new(ErrorKind::Other, format!("Could not load TMX map: {e}")) - })?; + let map = loader + .load_tmx_map(load_context.path()) + .map_err(|e| std::io::Error::other(format!("Could not load TMX map: {e}")))?; let mut tilemap_textures = HashMap::default(); #[cfg(not(feature = "atlas"))] @@ -360,35 +360,36 @@ pub fn process_loaded_maps( let tile_pos = TilePos { x, y }; let tile_entity = commands - .spawn(TileBundle { - position: tile_pos, - tilemap_id: TilemapId(layer_entity), - texture_index: TileTextureIndex(texture_index), - flip: TileFlip { + .spawn(( + Tile, + tile_pos, + TilemapId(layer_entity), + TileTextureIndex(texture_index), + TileFlip { x: layer_tile_data.flip_h, y: layer_tile_data.flip_v, d: layer_tile_data.flip_d, }, - ..Default::default() - }) + )) .id(); tile_storage.set(&tile_pos, tile_entity); } } - commands.entity(layer_entity).insert(TilemapBundle { + commands.entity(layer_entity).insert(( + Tilemap, grid_size, - size: map_size, - storage: tile_storage, - texture: tilemap_texture.clone(), + map_size, + tile_storage, + tilemap_texture.clone(), + TilemapMaterial::standard(), tile_size, - spacing: tile_spacing, - anchor: TilemapAnchor::Center, - transform: Transform::from_xyz(offset_x, -offset_y, layer_index as f32), + tile_spacing, + TilemapAnchor::Center, + Transform::from_xyz(offset_x, -offset_y, layer_index as f32), map_type, - render_settings: *render_settings, - ..Default::default() - }); + *render_settings, + )); layer_storage .storage diff --git a/examples/hex_neighbors.rs b/examples/hex_neighbors.rs index d8954c94..14f6dedd 100644 --- a/examples/hex_neighbors.rs +++ b/examples/hex_neighbors.rs @@ -63,16 +63,17 @@ fn spawn_tilemap(mut commands: Commands, tile_handle_hex_row: Res) { let grid_size = TilemapGridSize { x: 17.0, y: 15.0 }; let map_type = TilemapType::Hexagon(HexCoordSystem::Column); - commands.entity(tilemap_entity).insert(TilemapBundle { + commands.entity(tilemap_entity).insert(( + Tilemap, grid_size, - size: map_size, - storage: tile_storage, - texture: TilemapTexture::Single(texture_handle), + map_size, + tile_storage, + TilemapTexture::Single(texture_handle), + TilemapMaterial::standard(), tile_size, map_type, - anchor: TilemapAnchor::Center, - ..Default::default() - }); + TilemapAnchor::Center, + )); } fn swap_mesh_type(mut query: Query<&mut TilemapType>, keyboard_input: Res>) { diff --git a/examples/hexagon_generation.rs b/examples/hexagon_generation.rs index 19eab9b6..f8688298 100644 --- a/examples/hexagon_generation.rs +++ b/examples/hexagon_generation.rs @@ -67,16 +67,17 @@ fn spawn_tilemap(mut commands: Commands, tile_handle_hex_row: Res) { let grid_size = TilemapGridSize { x: 15.0, y: 17.0 }; let map_type = TilemapType::Hexagon(HexCoordSystem::Row); - commands.entity(tilemap_entity).insert(TilemapBundle { + commands.entity(tilemap_entity).insert(( + Tilemap, grid_size, - size: map_size, - storage: tile_storage, - texture: TilemapTexture::Single(texture_handle), + map_size, + tile_storage, + TilemapTexture::Single(texture_handle), + TilemapMaterial::standard(), tile_size, map_type, - anchor: TilemapAnchor::Center, - ..Default::default() - }); + TilemapAnchor::Center, + )); } fn swap_mesh_type(mut query: Query<&mut TilemapType>, keyboard_input: Res>) { diff --git a/examples/iso_diamond.rs b/examples/iso_diamond.rs index b76d1057..dcba6a98 100644 --- a/examples/iso_diamond.rs +++ b/examples/iso_diamond.rs @@ -71,19 +71,20 @@ fn startup(mut commands: Commands, asset_server: Res) { ); let tile_size = TilemapTileSize { x: 64.0, y: 32.0 }; - let grid_size = tile_size.into(); + let grid_size = TilemapGridSize::from(tile_size); let map_type = TilemapType::Isometric(IsoCoordSystem::Diamond); - commands.entity(tilemap_entity).insert(TilemapBundle { + commands.entity(tilemap_entity).insert(( + Tilemap, grid_size, - size: map_size, - storage: tile_storage, - texture: TilemapTexture::Single(texture_handle), + map_size, + tile_storage, + TilemapTexture::Single(texture_handle), + TilemapMaterial::standard(), tile_size, map_type, - anchor: TilemapAnchor::Center, - ..Default::default() - }); + TilemapAnchor::Center, + )); } fn main() { diff --git a/examples/iso_staggered.rs b/examples/iso_staggered.rs index be6a93c8..772f3064 100644 --- a/examples/iso_staggered.rs +++ b/examples/iso_staggered.rs @@ -73,19 +73,20 @@ fn startup(mut commands: Commands, asset_server: Res) { ); let tile_size = TilemapTileSize { x: 64.0, y: 32.0 }; - let grid_size = tile_size.into(); + let grid_size = TilemapGridSize::from(tile_size); let map_type = TilemapType::Isometric(IsoCoordSystem::Staggered); - commands.entity(tilemap_entity).insert(TilemapBundle { + commands.entity(tilemap_entity).insert(( + Tilemap, grid_size, - size: map_size, - storage: tile_storage, - texture: TilemapTexture::Single(texture_handle), + map_size, + tile_storage, + TilemapTexture::Single(texture_handle), + TilemapMaterial::standard(), tile_size, map_type, - anchor: TilemapAnchor::Center, - ..Default::default() - }); + TilemapAnchor::Center, + )); } fn main() { diff --git a/examples/layers.rs b/examples/layers.rs index 1faffb34..25a6a3f1 100644 --- a/examples/layers.rs +++ b/examples/layers.rs @@ -22,19 +22,20 @@ fn startup(mut commands: Commands, asset_server: Res) { ); let tile_size = TilemapTileSize { x: 16.0, y: 16.0 }; - let grid_size = tile_size.into(); + let grid_size = TilemapGridSize::from(tile_size); let map_type = TilemapType::default(); - commands.entity(tilemap_entity).insert(TilemapBundle { + commands.entity(tilemap_entity).insert(( + Tilemap, grid_size, map_type, - size: map_size, - storage: tile_storage, - texture: TilemapTexture::Single(texture_handle.clone()), + map_size, + tile_storage, + TilemapTexture::Single(texture_handle.clone()), + TilemapMaterial::standard(), tile_size, - anchor: TilemapAnchor::Center, - ..Default::default() - }); + TilemapAnchor::Center, + )); // Layer 2 let mut tile_storage = TileStorage::empty(map_size); @@ -48,17 +49,18 @@ fn startup(mut commands: Commands, asset_server: Res) { &mut tile_storage, ); - commands.entity(tilemap_entity).insert(TilemapBundle { + commands.entity(tilemap_entity).insert(( + Tilemap, grid_size, map_type, - size: map_size, - storage: tile_storage, - texture: TilemapTexture::Single(texture_handle), - tile_size: TilemapTileSize { x: 16.0, y: 16.0 }, - anchor: TilemapAnchor::Center, - transform: Transform::from_xyz(32.0, 32.0, 1.0), - ..Default::default() - }); + map_size, + tile_storage, + TilemapTexture::Single(texture_handle), + TilemapMaterial::standard(), + TilemapTileSize { x: 16.0, y: 16.0 }, + TilemapAnchor::Center, + Transform::from_xyz(32.0, 32.0, 1.0), + )); } fn main() { diff --git a/examples/mouse_to_tile.rs b/examples/mouse_to_tile.rs index b55b8ef0..7409d664 100644 --- a/examples/mouse_to_tile.rs +++ b/examples/mouse_to_tile.rs @@ -85,16 +85,17 @@ fn spawn_tilemap(mut commands: Commands, tile_handle_square: Res) { let tile_pos = TilePos { x: 0, y: 0 }; let tile_entity = commands - .spawn(TileBundle { - position: tile_pos, - tilemap_id: TilemapId(tilemap_entity), - ..Default::default() - }) + .spawn((Tile, tile_pos, TilemapId(tilemap_entity))) .id(); tile_storage.set(&tile_pos, tile_entity); let tile_size = TilemapTileSize { x: 16.0, y: 16.0 }; - let grid_size = tile_size.into(); + let grid_size = TilemapGridSize::from(tile_size); let map_type = TilemapType::default(); - commands.entity(tilemap_entity).insert(TilemapBundle { + commands.entity(tilemap_entity).insert(( + Tilemap, grid_size, map_type, - size: map_size, - storage: tile_storage, - texture: TilemapTexture::Single(texture_handle), + map_size, + tile_storage, + TilemapTexture::Single(texture_handle), + TilemapMaterial::standard(), tile_size, - anchor: TilemapAnchor::Center, - ..Default::default() - }); + TilemapAnchor::Center, + )); } fn swap_pos(keyboard_input: Res>, mut query: Query<&mut TilePos>) { diff --git a/examples/random_map.rs b/examples/random_map.rs index 1c0f5425..a5a64f3a 100644 --- a/examples/random_map.rs +++ b/examples/random_map.rs @@ -21,11 +21,9 @@ fn startup(mut commands: Commands, asset_server: Res) { let tile_pos = TilePos { x, y }; let tile_entity = commands .spawn(( - TileBundle { - position: tile_pos, - tilemap_id: TilemapId(tilemap_entity), - ..Default::default() - }, + Tile, + tile_pos, + TilemapId(tilemap_entity), LastUpdate::default(), )) .id(); @@ -34,19 +32,20 @@ fn startup(mut commands: Commands, asset_server: Res) { } let tile_size = TilemapTileSize { x: 16.0, y: 16.0 }; - let grid_size = tile_size.into(); + let grid_size = TilemapGridSize::from(tile_size); let map_type = TilemapType::default(); - commands.entity(tilemap_entity).insert(TilemapBundle { + commands.entity(tilemap_entity).insert(( + Tilemap, grid_size, map_type, - size: map_size, - storage: tile_storage, - texture: TilemapTexture::Single(texture_handle), + map_size, + tile_storage, + TilemapTexture::Single(texture_handle), + TilemapMaterial::standard(), tile_size, - anchor: TilemapAnchor::Center, - ..Default::default() - }); + TilemapAnchor::Center, + )); } #[derive(Default, Component)] diff --git a/examples/remove_tiles.rs b/examples/remove_tiles.rs index a174ed97..54670a02 100644 --- a/examples/remove_tiles.rs +++ b/examples/remove_tiles.rs @@ -17,31 +17,26 @@ fn startup(mut commands: Commands, asset_server: Res) { for y in 0..32u32 { let tile_pos = TilePos { x, y }; let tile_entity = commands - .spawn(TileBundle { - position: tile_pos, - tilemap_id: TilemapId(tilemap_entity), - ..Default::default() - }) + .spawn((Tile, tile_pos, TilemapId(tilemap_entity))) .id(); tile_storage.set(&tile_pos, tile_entity); } } let tile_size = TilemapTileSize { x: 16.0, y: 16.0 }; - let grid_size = tile_size.into(); + let grid_size = TilemapGridSize::from(tile_size); 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, - anchor: TilemapAnchor::Center, - ..Default::default() - }, + Tilemap, + grid_size, + map_type, + map_size, + tile_storage, + TilemapTexture::Single(texture_handle), + TilemapMaterial::standard(), + tile_size, + TilemapAnchor::Center, LastUpdate::default(), )); } diff --git a/examples/spacing.rs b/examples/spacing.rs index 3415a154..ec300776 100644 --- a/examples/spacing.rs +++ b/examples/spacing.rs @@ -22,20 +22,21 @@ fn startup(mut commands: Commands, asset_server: Res) { ); let tile_size = TilemapTileSize { x: 16.0, y: 16.0 }; - let grid_size = tile_size.into(); + let grid_size = TilemapGridSize::from(tile_size); let map_type = TilemapType::default(); - commands.entity(tilemap_entity).insert(TilemapBundle { + commands.entity(tilemap_entity).insert(( + Tilemap, grid_size, map_type, - size: map_size, - storage: tile_storage, - texture: TilemapTexture::Single(texture_handle.clone()), + map_size, + tile_storage, + TilemapTexture::Single(texture_handle.clone()), + TilemapMaterial::standard(), tile_size, - spacing: TilemapSpacing { x: 8.0, y: 8.0 }, - anchor: TilemapAnchor::Center, - ..Default::default() - }); + TilemapSpacing { x: 8.0, y: 8.0 }, + TilemapAnchor::Center, + )); // Layer 2 let mut tile_storage = TileStorage::empty(map_size); @@ -49,17 +50,18 @@ fn startup(mut commands: Commands, asset_server: Res) { &mut tile_storage, ); - commands.entity(tilemap_entity).insert(TilemapBundle { + commands.entity(tilemap_entity).insert(( + Tilemap, grid_size, map_type, - size: map_size, - storage: tile_storage, - texture: TilemapTexture::Single(texture_handle), - tile_size: TilemapTileSize { x: 16.0, y: 16.0 }, - anchor: TilemapAnchor::Center, - transform: Transform::from_xyz(32.0, 32.0, 1.0), - ..Default::default() - }); + map_size, + tile_storage, + TilemapTexture::Single(texture_handle), + TilemapMaterial::standard(), + TilemapTileSize { x: 16.0, y: 16.0 }, + TilemapAnchor::Center, + Transform::from_xyz(32.0, 32.0, 1.0), + )); } fn main() { diff --git a/examples/spawn_despawn_tilemap.rs b/examples/spawn_despawn_tilemap.rs index 81f2e63f..3a3442e1 100644 --- a/examples/spawn_despawn_tilemap.rs +++ b/examples/spawn_despawn_tilemap.rs @@ -38,39 +38,40 @@ fn spawn_map( for y in 0..map_size.y { let tile_pos = TilePos { x, y }; let tile_entity = commands - .spawn(TileBundle { - position: tile_pos, - tilemap_id: TilemapId(tilemap_entity), - color: TileColor( + .spawn(( + Tile, + tile_pos, + TilemapId(tilemap_entity), + TileColor( Hsla::hsl(0., 0.9, 0.8) .rotate_hue(num_maps as f32 * 19.) .into(), ), - texture_index: TileTextureIndex(5), - ..default() - }) + TileTextureIndex(5), + )) .id(); tile_storage.set(&tile_pos, tile_entity); } } let tile_size = TilemapTileSize { x: 16.0, y: 16.0 }; - let grid_size = tile_size.into(); + let grid_size = TilemapGridSize::from(tile_size); let map_type = TilemapType::default(); let offset = Vec3::splat(num_maps as f32 * tile_size.x / 2.); - commands.entity(tilemap_entity).insert(TilemapBundle { + commands.entity(tilemap_entity).insert(( + Tilemap, grid_size, map_type, - size: map_size, - storage: tile_storage, - texture: TilemapTexture::Single(texture_handle), + map_size, + tile_storage, + TilemapTexture::Single(texture_handle), + TilemapMaterial::standard(), tile_size, - anchor: TilemapAnchor::Center, - transform: Transform::from_translation(offset), - ..default() - }); + TilemapAnchor::Center, + Transform::from_translation(offset), + )); } fn despawn_map(mut commands: Commands, mut maps: Query<(Entity, &mut TileStorage, &Transform)>) { diff --git a/examples/texture_container.rs b/examples/texture_container.rs index 0dbea9a1..033069a2 100644 --- a/examples/texture_container.rs +++ b/examples/texture_container.rs @@ -61,31 +61,25 @@ mod no_atlas { .choose_weighted(&mut rng, |choice| choice.1) .unwrap() .0; - let tile_entity = commands - .spawn(TileBundle { - position, - tilemap_id, - texture_index: texture, - ..Default::default() - }) - .id(); + let tile_entity = commands.spawn((Tile, position, tilemap_id, texture)).id(); tile_storage.set(&position, tile_entity); } let tile_size = TILE_SIZE; - let grid_size = TILE_SIZE.into(); + let grid_size = TilemapGridSize::from(TILE_SIZE); let map_type = TilemapType::Hexagon(COORD_SYS); - commands.entity(tilemap_entity).insert(TilemapBundle { + commands.entity(tilemap_entity).insert(( + Tilemap, grid_size, map_type, tile_size, - size: map_size, - storage: tile_storage, - texture: texture_vec, - anchor: TilemapAnchor::Center, - ..Default::default() - }); + map_size, + tile_storage, + texture_vec, + TilemapMaterial::standard(), + TilemapAnchor::Center, + )); } pub fn main() { diff --git a/examples/texture_vec.rs b/examples/texture_vec.rs index 38b39e9d..76eccc10 100644 --- a/examples/texture_vec.rs +++ b/examples/texture_vec.rs @@ -55,31 +55,25 @@ mod no_atlas { .choose_weighted(&mut rng, |choice| choice.1) .unwrap() .0; - let tile_entity = commands - .spawn(TileBundle { - position, - tilemap_id, - texture_index: texture, - ..Default::default() - }) - .id(); + let tile_entity = commands.spawn((Tile, position, tilemap_id, texture)).id(); tile_storage.set(&position, tile_entity); } let tile_size = TILE_SIZE; - let grid_size = TILE_SIZE.into(); + let grid_size = TilemapGridSize::from(TILE_SIZE); let map_type = TilemapType::Hexagon(COORD_SYS); - commands.entity(tilemap_entity).insert(TilemapBundle { + commands.entity(tilemap_entity).insert(( + Tilemap, grid_size, map_type, tile_size, - size: map_size, - storage: tile_storage, - texture: texture_vec, - anchor: TilemapAnchor::Center, - ..Default::default() - }); + map_size, + tile_storage, + texture_vec, + TilemapMaterial::standard(), + TilemapAnchor::Center, + )); } pub fn main() { diff --git a/examples/visibility.rs b/examples/visibility.rs index ae353f7f..0e3ec7e7 100644 --- a/examples/visibility.rs +++ b/examples/visibility.rs @@ -17,31 +17,26 @@ fn startup(mut commands: Commands, asset_server: Res) { for y in 0..32u32 { let tile_pos = TilePos { x, y }; let tile_entity = commands - .spawn(TileBundle { - position: tile_pos, - tilemap_id: TilemapId(tilemap_entity), - ..Default::default() - }) + .spawn((Tile, tile_pos, TilemapId(tilemap_entity))) .id(); tile_storage.set(&tile_pos, tile_entity); } } let tile_size = TilemapTileSize { x: 16.0, y: 16.0 }; - let grid_size = tile_size.into(); + let grid_size = TilemapGridSize::from(tile_size); 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, - anchor: TilemapAnchor::Center, - ..Default::default() - }, + Tilemap, + grid_size, + map_type, + map_size, + tile_storage, + TilemapTexture::Single(texture_handle), + TilemapMaterial::standard(), + tile_size, + TilemapAnchor::Center, LastUpdate::default(), )); } diff --git a/src/helpers/filling.rs b/src/helpers/filling.rs index f2c59415..61fc0a20 100644 --- a/src/helpers/filling.rs +++ b/src/helpers/filling.rs @@ -2,7 +2,7 @@ use crate::helpers::hex_grid::axial::AxialPos; use crate::helpers::hex_grid::neighbors::{HEX_DIRECTIONS, HexDirection}; use crate::map::TilemapId; use crate::prelude::HexCoordSystem; -use crate::tiles::{TileBundle, TileColor, TilePos, TileTextureIndex}; +use crate::tiles::{Tile, TileColor, TilePos, TileTextureIndex}; use crate::{TileStorage, TilemapSize}; use bevy::prelude::{Color, Commands}; @@ -20,12 +20,7 @@ pub fn fill_tilemap( for y in 0..size.y { let tile_pos = TilePos { x, y }; let tile_entity = parent - .spawn(TileBundle { - position: tile_pos, - tilemap_id, - texture_index, - ..Default::default() - }) + .spawn((Tile, tile_pos, tilemap_id, texture_index)) .id(); tile_storage.set(&tile_pos, tile_entity); } @@ -54,12 +49,7 @@ pub fn fill_tilemap_rect( }; let tile_entity = parent - .spawn(TileBundle { - position: tile_pos, - tilemap_id, - texture_index, - ..Default::default() - }) + .spawn((Tile, tile_pos, tilemap_id, texture_index)) .id(); tile_storage.set(&tile_pos, tile_entity); } @@ -89,13 +79,7 @@ pub fn fill_tilemap_rect_color( }; let tile_entity = parent - .spawn(TileBundle { - position: tile_pos, - tilemap_id, - texture_index, - color: TileColor(color), - ..Default::default() - }) + .spawn((Tile, tile_pos, tilemap_id, texture_index, TileColor(color))) .id(); tile_storage.set(&tile_pos, tile_entity); } @@ -167,12 +151,7 @@ pub fn fill_tilemap_hexagon( commands.entity(tilemap_id.0).with_children(|parent| { for tile_pos in tile_positions { let tile_entity = parent - .spawn(TileBundle { - position: tile_pos, - tilemap_id, - texture_index, - ..Default::default() - }) + .spawn((Tile, tile_pos, tilemap_id, texture_index)) .id(); tile_storage.checked_set(&tile_pos, tile_entity) } diff --git a/src/lib.rs b/src/lib.rs index 576c61a5..a547ac0f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,7 +26,7 @@ use bevy::{ }; #[cfg(feature = "render")] -use render::material::MaterialTilemapHandle; +use render::material::TilemapMaterial; use anchor::TilemapAnchor; use map::{ @@ -112,11 +112,22 @@ impl Default for FrustumCulling { } #[cfg(feature = "render")] +#[expect(deprecated)] +#[deprecated( + since = "0.16.0", + note = "Use the `Tilemap` and `TilemapMaterial::standard()` components instead. + Inserting `Tilemap` will now also insert all necessary components automatically." +)] pub type TilemapBundle = MaterialTilemapBundle; #[cfg(feature = "render")] /// The default tilemap bundle. All of the components within are required. #[derive(Bundle, Debug, Default, Clone)] +#[deprecated( + since = "0.16.0", + note = "Use the `Tilemap` and `TilemapMaterial` components instead. + Inserting `Tilemap` will now also insert all necessary components automatically." +)] pub struct MaterialTilemapBundle { pub grid_size: TilemapGridSize, pub map_type: TilemapType, @@ -136,14 +147,37 @@ pub struct MaterialTilemapBundle { pub view_visibility: ViewVisibility, /// User indication of whether tilemap should be frustum culled. pub frustum_culling: FrustumCulling, - pub material: MaterialTilemapHandle, + pub material: TilemapMaterial, pub sync: SyncToRenderWorld, pub anchor: TilemapAnchor, } +#[derive(Component, Debug, Default, Clone)] +#[require( + TilemapGridSize, + TilemapType, + TilemapSize, + TilemapSpacing, + TileStorage, + TilemapTexture, + TilemapTileSize, + Transform, + TilemapRenderSettings, + TilemapAnchor, + Visibility, + FrustumCulling, + SyncToRenderWorld +)] +pub struct Tilemap; + #[cfg(not(feature = "render"))] /// The default tilemap bundle. All of the components within are required. #[derive(Bundle, Debug, Default, Clone)] +#[deprecated( + since = "0.16.0", + note = "Use the `Tilemap` component instead. + Inserting `Tilemap` will now also insert all necessary components automatically." +)] pub struct StandardTilemapBundle { pub grid_size: TilemapGridSize, pub map_type: TilemapType, @@ -169,8 +203,11 @@ pub struct StandardTilemapBundle { /// A module which exports commonly used dependencies. pub mod prelude { #[cfg(feature = "render")] + #[expect(deprecated)] pub use crate::MaterialTilemapBundle; + pub use crate::Tilemap; #[cfg(feature = "render")] + #[expect(deprecated)] pub use crate::TilemapBundle; pub use crate::TilemapPlugin; pub use crate::anchor::TilemapAnchor; @@ -184,13 +221,13 @@ pub mod prelude { #[cfg(feature = "render")] pub use crate::render::material::MaterialTilemap; #[cfg(feature = "render")] - pub use crate::render::material::MaterialTilemapHandle; - #[cfg(feature = "render")] pub use crate::render::material::MaterialTilemapKey; #[cfg(feature = "render")] pub use crate::render::material::MaterialTilemapPlugin; #[cfg(feature = "render")] pub use crate::render::material::StandardTilemapMaterial; + #[cfg(feature = "render")] + pub use crate::render::material::TilemapMaterial; pub use crate::tiles::*; } diff --git a/src/render/draw.rs b/src/render/draw.rs index a31bf9f3..beaa45be 100644 --- a/src/render/draw.rs +++ b/src/render/draw.rs @@ -21,7 +21,7 @@ use crate::map::TilemapId; use super::{ DynamicUniformIndex, chunk::{ChunkId, RenderChunk2dStorage, TilemapUniformData}, - material::{MaterialTilemap, MaterialTilemapHandle, RenderMaterialsTilemap}, + material::{MaterialTilemap, TilemapMaterial, RenderMaterialsTilemap}, prepare::MeshUniform, queue::{ImageBindGroups, TilemapViewBindGroup, TransformBindGroup}, }; @@ -150,7 +150,7 @@ impl RenderCommand { type Param = ( SRes>, - SQuery<&'static MaterialTilemapHandle>, + SQuery<&'static TilemapMaterial>, ); type ViewQuery = (); type ItemQuery = Read; diff --git a/src/render/material.rs b/src/render/material.rs index 38808e35..19581fc3 100644 --- a/src/render/material.rs +++ b/src/render/material.rs @@ -1,4 +1,8 @@ -use crate::prelude::{TilemapId, TilemapRenderSettings}; +use crate::{ + Tilemap, + anchor::TilemapAnchor, + prelude::{TilemapId, TilemapRenderSettings}, +}; use bevy::log::error; #[cfg(not(feature = "atlas"))] @@ -102,28 +106,41 @@ where #[derive(Component, Clone, Debug, Deref, DerefMut, Reflect, PartialEq, Eq, ExtractComponent)] #[reflect(Component, Default)] -pub struct MaterialTilemapHandle(pub Handle); +#[require(Tilemap, TilemapAnchor)] +pub struct TilemapMaterial(pub Handle); + +impl TilemapMaterial { + pub fn standard() -> Self { + Self(default()) + } +} + +#[deprecated( + since = "0.16.0", + note = "Use the `TilemapMaterial` component instead." +)] +pub type MaterialTilemapHandle = TilemapMaterial; -impl Default for MaterialTilemapHandle { +impl Default for TilemapMaterial { fn default() -> Self { Self(Handle::default()) } } -impl From> for MaterialTilemapHandle { +impl From> for TilemapMaterial { fn from(handle: Handle) -> Self { Self(handle) } } -impl From> for AssetId { - fn from(tilemap: MaterialTilemapHandle) -> Self { +impl From> for AssetId { + fn from(tilemap: TilemapMaterial) -> Self { tilemap.id() } } -impl From<&MaterialTilemapHandle> for AssetId { - fn from(tilemap: &MaterialTilemapHandle) -> Self { +impl From<&TilemapMaterial> for AssetId { + fn from(tilemap: &TilemapMaterial) -> Self { tilemap.id() } } @@ -142,7 +159,7 @@ where { fn build(&self, app: &mut App) { app.init_asset::() - .add_plugins(ExtractComponentPlugin::>::extract_visible()); + .add_plugins(ExtractComponentPlugin::>::extract_visible()); } fn finish(&self, app: &mut App) { @@ -413,7 +430,7 @@ pub fn queue_material_tilemap_meshes( globals_buffer: Res, (standard_tilemap_meshes, materials): ( Query<(Entity, &ChunkId, &Transform, &TilemapId)>, - Query<&MaterialTilemapHandle>, + Query<&TilemapMaterial>, ), mut views: Query<(&ExtractedView, &Msaa, &RenderVisibleEntities)>, render_materials: Res>, @@ -529,7 +546,7 @@ pub fn bind_material_tilemap_meshes( mut image_bind_groups: ResMut, (standard_tilemap_meshes, materials): ( Query<(&ChunkId, &TilemapId)>, - Query<&MaterialTilemapHandle>, + Query<&TilemapMaterial>, ), mut views: Query<(Entity, &RenderVisibleEntities)>, render_materials: Res>, diff --git a/src/tiles/mod.rs b/src/tiles/mod.rs index 891c8fb1..650434cb 100644 --- a/src/tiles/mod.rs +++ b/src/tiles/mod.rs @@ -108,9 +108,27 @@ pub struct TileFlip { pub d: bool, // anti } +#[derive(Component, Default, Clone, Copy, Debug)] +#[require( + TilePos, + TileTextureIndex, + TilemapId, + TileVisible, + TileFlip, + TileColor, + TilePosOld, + SyncToRenderWorld +)] +pub struct Tile; + /// This an optional tile bundle with default components. #[derive(Bundle, Default, Clone, Copy, Debug)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[deprecated( + since = "0.16.0", + note = "Use the `Tile` component instead. + Inserting `Tile` will now also insert all necessary components automatically." +)] pub struct TileBundle { pub position: TilePos, pub texture_index: TileTextureIndex,