diff --git a/Cargo.toml b/Cargo.toml index 0fb27974..751643ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,25 +16,26 @@ render = [] serde = ["dep:serde"] [dependencies] -bevy = { version = "0.14.0", default-features = false, features = [ +bevy = { version = "0.15", default-features = false, features = [ "bevy_core_pipeline", "bevy_render", "bevy_asset", "bevy_sprite", ] } +# See Bevy#16563 +bevy_internal = { version = "0.15", features = ["bevy_image"] } log = "0.4" serde = { version = "1", features = ["derive"], optional = true } [dev-dependencies] ldtk_rust = { version = "0.6" } rand = "0.8" -env_logger = "0.10" serde_json = { version = "1.0" } tiled = { version = "0.11.0", default-features = false } thiserror = { version = "1.0" } [dev-dependencies.bevy] -version = "0.14.0" +version = "0.15" default-features = false features = [ "bevy_core_pipeline", @@ -43,6 +44,7 @@ features = [ "png", "ktx2", "bevy_winit", + "bevy_window", "bevy_text", "bevy_sprite", #"file_watcher", @@ -51,7 +53,7 @@ features = [ ] [target.'cfg(unix)'.dev-dependencies.bevy] -version = "0.14.0" +version = "0.15" default-features = false features = [ "bevy_core_pipeline", @@ -60,6 +62,7 @@ features = [ "png", "ktx2", "bevy_winit", + "bevy_window", "wayland", "x11", "bevy_text", diff --git a/examples/3d_iso.rs b/examples/3d_iso.rs index b8219cd3..cdf0301d 100644 --- a/examples/3d_iso.rs +++ b/examples/3d_iso.rs @@ -4,9 +4,9 @@ use bevy_ecs_tilemap::prelude::*; mod helpers; fn startup(mut commands: Commands, asset_server: Res) { - commands.spawn(Camera2dBundle::default()); + commands.spawn(Camera2d); - let map_handle: Handle = asset_server.load("iso_map.tmx"); + let map_handle = helpers::tiled::TiledMapHandle(asset_server.load("iso_map.tmx")); commands.spawn(helpers::tiled::TiledMapBundle { tiled_map: map_handle, diff --git a/examples/accessing_tiles.rs b/examples/accessing_tiles.rs index 16929c40..a44acf5e 100644 --- a/examples/accessing_tiles.rs +++ b/examples/accessing_tiles.rs @@ -11,7 +11,7 @@ struct CurrentColor(u16); struct LastUpdate(f64); fn startup(mut commands: Commands, asset_server: Res) { - commands.spawn(Camera2dBundle::default()); + commands.spawn(Camera2d); let texture_handle: Handle = asset_server.load("tiles.png"); @@ -109,7 +109,7 @@ fn update_map( )>, mut tile_query: Query<&mut TileTextureIndex>, ) { - let current_time = time.elapsed_seconds_f64(); + let current_time = time.elapsed_secs_f64(); for (mut current_color, mut last_update, tile_storage, map_size) in tilemap_query.iter_mut() { if current_time - last_update.0 > 0.1 { current_color.0 += 1; diff --git a/examples/animation.rs b/examples/animation.rs index 59ff5c20..59765714 100644 --- a/examples/animation.rs +++ b/examples/animation.rs @@ -123,7 +123,7 @@ fn create_animated_flowers(mut commands: Commands, asset_server: Res, ) { - commands.spawn(Camera2dBundle::default()); + commands.spawn(Camera2d); let texture_handle: Handle = asset_server.load("tiles.png"); diff --git a/examples/bench.rs b/examples/bench.rs index 3b3f9ffe..e056d565 100644 --- a/examples/bench.rs +++ b/examples/bench.rs @@ -1,13 +1,14 @@ use bevy::{ diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, prelude::*, + window::PresentMode, }; use bevy_ecs_tilemap::prelude::*; mod helpers; fn startup(mut commands: Commands, asset_server: Res) { - commands.spawn(Camera2dBundle::default()); + commands.spawn(Camera2d); let texture_handle: Handle = asset_server.load("tiles.png"); @@ -50,6 +51,7 @@ fn main() { .set(WindowPlugin { primary_window: Some(Window { title: String::from("Benchmark Example"), + present_mode: PresentMode::AutoNoVsync, ..Default::default() }), ..default() diff --git a/examples/chunking.rs b/examples/chunking.rs index c289ffc6..3818cfaf 100644 --- a/examples/chunking.rs +++ b/examples/chunking.rs @@ -2,7 +2,7 @@ use bevy::{math::Vec3Swizzles, prelude::*, utils::HashSet}; use bevy_ecs_tilemap::prelude::*; mod helpers; -/// Press WASD to move the camera around, and watch as chunks spawn/despawn in response. +// Press WASD to move the camera around, and watch as chunks spawn/despawn in response. const TILE_SIZE: TilemapTileSize = TilemapTileSize { x: 16.0, y: 16.0 }; // For this example, don't choose too large a chunk size. @@ -54,7 +54,7 @@ fn spawn_chunk(commands: &mut Commands, asset_server: &AssetServer, chunk_pos: I } fn startup(mut commands: Commands) { - commands.spawn(Camera2dBundle::default()); + commands.spawn(Camera2d); } fn camera_pos_to_chunk_pos(camera_pos: &Vec2) -> IVec2 { diff --git a/examples/colors.rs b/examples/colors.rs index 6b33e878..14a94dfe 100644 --- a/examples/colors.rs +++ b/examples/colors.rs @@ -7,7 +7,7 @@ mod helpers; const QUADRANT_SIDE_LENGTH: u32 = 64; fn startup(mut commands: Commands, asset_server: Res) { - commands.spawn(Camera2dBundle::default()); + commands.spawn(Camera2d); let texture_handle: Handle = asset_server.load("tiles.png"); diff --git a/examples/custom_shader.rs b/examples/custom_shader.rs index 3c37a7d6..1e5ff477 100644 --- a/examples/custom_shader.rs +++ b/examples/custom_shader.rs @@ -22,12 +22,12 @@ fn startup( asset_server: Res, mut materials: ResMut>, ) { - commands.spawn(Camera2dBundle::default()); + commands.spawn(Camera2d); - let my_material_handle = materials.add(MyMaterial { + let my_material_handle = MaterialTilemapHandle::from(materials.add(MyMaterial { brightness: 0.5, ..default() - }); + })); let texture_handle: Handle = asset_server.load("tiles.png"); diff --git a/examples/frustum_cull_test.rs b/examples/frustum_cull_test.rs index ee15f082..83354ed5 100644 --- a/examples/frustum_cull_test.rs +++ b/examples/frustum_cull_test.rs @@ -75,7 +75,7 @@ impl FromWorld for FontHandle { // Generates the initial tilemap, which is a square grid. fn spawn_tilemap(mut commands: Commands, tile_handle_square: Res) { - commands.spawn(Camera2dBundle::default()); + commands.spawn(Camera2d); let map_size = TilemapSize { // Render chunks are of size 64, so let's create two render chunks @@ -122,13 +122,6 @@ fn spawn_map_type_label( windows: Query<&Window>, map_type_q: Query<&TilemapType>, ) { - let text_style = TextStyle { - font: font_handle.clone(), - font_size: 20.0, - color: Color::BLACK, - }; - let text_justify = JustifyText::Center; - for window in windows.iter() { for map_type in map_type_q.iter() { // Place the map type label somewhere in the top left side of the screen @@ -138,12 +131,15 @@ fn spawn_map_type_label( ..Default::default() }; commands.spawn(( - Text2dBundle { - text: Text::from_section(format!("{map_type:?}"), text_style.clone()) - .with_justify(text_justify), - transform, + Text2d::new(format!("{map_type:?}")), + TextFont { + font: font_handle.clone(), + font_size: 20.0, ..default() }, + TextColor(Color::BLACK), + TextLayout::new_with_justify(JustifyText::Center), + transform, MapTypeLabel, )); } @@ -160,7 +156,7 @@ fn swap_map_type( &mut TilemapTileSize, )>, keyboard_input: Res>, - mut map_type_label_q: Query<&mut Text, With>, + mut map_type_label_q: Query<&mut Text2d, With>, tile_handle_square: Res, tile_handle_hex_row: Res, tile_handle_hex_col: Res, @@ -216,7 +212,7 @@ fn swap_map_type( } for mut label_text in map_type_label_q.iter_mut() { - label_text.sections[0].value = format!("{:?}", map_type.as_ref()); + label_text.0 = format!("{:?}", map_type.as_ref()); } } } diff --git a/examples/game_of_life.rs b/examples/game_of_life.rs index 6b11cacc..65415b1a 100644 --- a/examples/game_of_life.rs +++ b/examples/game_of_life.rs @@ -5,7 +5,7 @@ use bevy_ecs_tilemap::prelude::*; mod helpers; fn startup(mut commands: Commands, asset_server: Res) { - commands.spawn(Camera2dBundle::default()); + commands.spawn(Camera2d); let texture_handle: Handle = asset_server.load("tiles.png"); @@ -58,7 +58,7 @@ fn update( mut tile_storage_query: Query<(&TileStorage, &TilemapSize, &mut LastUpdate)>, tile_query: Query<(Entity, &TilePos, &TileVisible)>, ) { - let current_time = time.elapsed_seconds_f64(); + let current_time = time.elapsed_secs_f64(); let (tile_storage, map_size, mut last_update) = tile_storage_query.single_mut(); if current_time - last_update.0 > 0.1 { for (entity, position, visibility) in tile_query.iter() { diff --git a/examples/helpers/camera.rs b/examples/helpers/camera.rs index e6155f5c..0511a56b 100644 --- a/examples/helpers/camera.rs +++ b/examples/helpers/camera.rs @@ -39,7 +39,7 @@ pub fn movement( } let z = transform.translation.z; - transform.translation += time.delta_seconds() * direction * 500.; + transform.translation += time.delta_secs() * direction * 500.; // Important! We need to restore the Z values when moving the camera around. // Bevy has a specific camera setup and this can mess with how our layers are shown. transform.translation.z = z; diff --git a/examples/helpers/ldtk.rs b/examples/helpers/ldtk.rs index e515ed01..f6e26b5e 100644 --- a/examples/helpers/ldtk.rs +++ b/examples/helpers/ldtk.rs @@ -7,10 +7,7 @@ use bevy_ecs_tilemap::{ use std::{collections::HashMap, io::ErrorKind}; use thiserror::Error; -use bevy::{ - asset::{io::Reader, AsyncReadExt}, - reflect::TypePath, -}; +use bevy::{asset::io::Reader, reflect::TypePath}; use bevy::{ asset::{AssetLoader, AssetPath, LoadContext}, prelude::*, @@ -39,9 +36,12 @@ pub struct LdtkMapConfig { pub selected_level: usize, } +#[derive(Default, Component)] +pub struct LdtkMapHandle(pub Handle); + #[derive(Default, Bundle)] pub struct LdtkMapBundle { - pub ldtk_map: Handle, + pub ldtk_map: LdtkMapHandle, pub ldtk_map_config: LdtkMapConfig, pub transform: Transform, pub global_transform: GlobalTransform, @@ -61,11 +61,11 @@ impl AssetLoader for LdtkLoader { type Settings = (); type Error = LdtkAssetLoaderError; - async fn load<'a>( - &'a self, - reader: &'a mut Reader<'_>, - _settings: &'a Self::Settings, - load_context: &'a mut LoadContext<'_>, + async fn load( + &self, + reader: &mut dyn Reader, + _settings: &Self::Settings, + load_context: &mut LoadContext<'_>, ) -> Result { let mut bytes = Vec::new(); reader.read_to_end(&mut bytes).await?; @@ -110,8 +110,8 @@ pub fn process_loaded_tile_maps( mut commands: Commands, mut map_events: EventReader>, maps: Res>, - mut query: Query<(Entity, &Handle, &LdtkMapConfig)>, - new_maps: Query<&Handle, Added>>, + mut query: Query<(Entity, &LdtkMapHandle, &LdtkMapConfig)>, + new_maps: Query<&LdtkMapHandle, Added>, ) { let mut changed_maps = Vec::>::default(); for event in map_events.read() { @@ -136,16 +136,16 @@ pub fn process_loaded_tile_maps( // If we have new map entities, add them to the changed_maps list for new_map_handle in new_maps.iter() { - changed_maps.push(new_map_handle.id()); + changed_maps.push(new_map_handle.0.id()); } for changed_map in changed_maps.iter() { for (entity, map_handle, map_config) in query.iter_mut() { // only deal with currently changed map - if map_handle.id() != *changed_map { + if map_handle.0.id() != *changed_map { continue; } - if let Some(ldtk_map) = maps.get(map_handle) { + if let Some(ldtk_map) = maps.get(&map_handle.0) { // Despawn all existing tilemaps for this LdtkMap commands.entity(entity).despawn_descendants(); diff --git a/examples/helpers/tiled.rs b/examples/helpers/tiled.rs index 4e5172ff..810f8ee0 100644 --- a/examples/helpers/tiled.rs +++ b/examples/helpers/tiled.rs @@ -17,7 +17,7 @@ use std::path::Path; use std::sync::Arc; use bevy::{ - asset::{io::Reader, AssetLoader, AssetPath, AsyncReadExt}, + asset::{io::Reader, AssetLoader, AssetPath}, log, prelude::{ Added, Asset, AssetApp, AssetEvent, AssetId, Assets, Bundle, Commands, Component, @@ -59,9 +59,12 @@ pub struct TiledLayersStorage { pub storage: HashMap, } +#[derive(Component, Default)] +pub struct TiledMapHandle(pub Handle); + #[derive(Default, Bundle)] pub struct TiledMapBundle { - pub tiled_map: Handle, + pub tiled_map: TiledMapHandle, pub storage: TiledLayersStorage, pub transform: Transform, pub global_transform: GlobalTransform, @@ -104,11 +107,11 @@ impl AssetLoader for TiledLoader { type Settings = (); type Error = TiledAssetLoaderError; - async fn load<'a>( - &'a self, - reader: &'a mut Reader<'_>, - _settings: &'a Self::Settings, - load_context: &'a mut bevy::asset::LoadContext<'_>, + async fn load( + &self, + reader: &mut dyn Reader, + _settings: &Self::Settings, + load_context: &mut bevy::asset::LoadContext<'_>, ) -> Result { let mut bytes = Vec::new(); reader.read_to_end(&mut bytes).await?; @@ -199,11 +202,11 @@ pub fn process_loaded_maps( maps: Res>, tile_storage_query: Query<(Entity, &TileStorage)>, mut map_query: Query<( - &Handle, + &TiledMapHandle, &mut TiledLayersStorage, &TilemapRenderSettings, )>, - new_maps: Query<&Handle, Added>>, + new_maps: Query<&TiledMapHandle, Added>, ) { let mut changed_maps = Vec::>::default(); for event in map_events.read() { @@ -228,16 +231,16 @@ pub fn process_loaded_maps( // If we have new map entities add them to the changed_maps list. for new_map_handle in new_maps.iter() { - changed_maps.push(new_map_handle.id()); + changed_maps.push(new_map_handle.0.id()); } for changed_map in changed_maps.iter() { for (map_handle, mut layer_storage, render_settings) in map_query.iter_mut() { // only deal with currently changed map - if map_handle.id() != *changed_map { + if map_handle.0.id() != *changed_map { continue; } - if let Some(tiled_map) = maps.get(map_handle) { + if let Some(tiled_map) = maps.get(&map_handle.0) { // TODO: Create a RemoveMap component.. for layer_entity in layer_storage.storage.values() { if let Ok((_, layer_tile_storage)) = tile_storage_query.get(*layer_entity) { diff --git a/examples/hex_neighbors.rs b/examples/hex_neighbors.rs index 9aa2f1bd..e56c0471 100644 --- a/examples/hex_neighbors.rs +++ b/examples/hex_neighbors.rs @@ -48,7 +48,7 @@ impl FromWorld for FontHandle { // Generates the initial tilemap, which is a hex grid. fn spawn_tilemap(mut commands: Commands, tile_handle_hex_row: Res) { - commands.spawn(Camera2dBundle::default()); + commands.spawn(Camera2d); let map_size = TilemapSize { x: MAP_SIDE_LENGTH_X, @@ -93,12 +93,6 @@ fn spawn_tile_labels( tile_q: Query<&mut TilePos>, font_handle: Res, ) { - let text_style = TextStyle { - font: font_handle.clone(), - font_size: 20.0, - color: Color::BLACK, - }; - let text_justify = JustifyText::Center; for (map_transform, map_type, grid_size, tilemap_storage) in tilemap_q.iter() { for tile_entity in tilemap_storage.iter().flatten() { let tile_pos = tile_q.get(*tile_entity).unwrap(); @@ -106,15 +100,17 @@ fn spawn_tile_labels( let transform = *map_transform * Transform::from_translation(tile_center); let label_entity = commands - .spawn(Text2dBundle { - text: Text::from_section( - format!("{}, {}", tile_pos.x, tile_pos.y), - text_style.clone(), - ) - .with_justify(text_justify), + .spawn(( + Text2d::new(format!("{}, {}", tile_pos.x, tile_pos.y)), + TextFont { + font: font_handle.clone(), + font_size: 20.0, + ..default() + }, + TextColor(Color::BLACK), + TextLayout::new_with_justify(JustifyText::Center), transform, - ..default() - }) + )) .id(); commands .entity(*tile_entity) @@ -133,13 +129,6 @@ fn spawn_map_type_label( windows: Query<&Window>, map_type_q: Query<&TilemapType>, ) { - let text_style = TextStyle { - font: font_handle.clone(), - font_size: 20.0, - color: Color::BLACK, - }; - let text_justify = JustifyText::Center; - for window in windows.iter() { for map_type in map_type_q.iter() { // Place the map type label somewhere in the top left side of the screen @@ -149,12 +138,15 @@ fn spawn_map_type_label( ..Default::default() }; commands.spawn(( - Text2dBundle { - text: Text::from_section(format!("{map_type:?}"), text_style.clone()) - .with_justify(text_justify), - transform, + Text2d::new(format!("{map_type:?}")), + TextFont { + font: font_handle.clone(), + font_size: 20.0, ..default() }, + TextColor(Color::BLACK), + TextLayout::new_with_justify(JustifyText::Center), + transform, MapTypeLabel, )); } @@ -177,7 +169,7 @@ fn swap_map_type( (&TileLabel, &TilePos), (With, Without, Without), >, - mut map_type_label_q: Query<&mut Text, With>, + mut map_type_label_q: Query<&mut Text2d, With>, mut transform_q: Query<&mut Transform, Without>, tile_handle_hex_row: Res, tile_handle_hex_col: Res, @@ -231,7 +223,7 @@ fn swap_map_type( } for mut label_text in map_type_label_q.iter_mut() { - label_text.sections[0].value = format!("{:?}", map_type.as_ref()); + label_text.0 = format!("{:?}", map_type.as_ref()); } } } @@ -261,7 +253,7 @@ pub fn update_cursor_pos( // any transforms on the camera. This is done by projecting the cursor position into // camera space (world space). for (cam_t, cam) in camera_q.iter() { - if let Some(pos) = cam.viewport_to_world_2d(cam_t, cursor_moved.position) { + if let Ok(pos) = cam.viewport_to_world_2d(cam_t, cursor_moved.position) { *cursor_pos = CursorPos(pos); } } @@ -281,15 +273,13 @@ fn hover_highlight_tile_label( )>, highlighted_tiles_q: Query>, tile_label_q: Query<&TileLabel>, - mut text_q: Query<&mut Text>, + mut text_q: Query<&mut TextColor>, ) { // Un-highlight any previously highlighted tile labels. for highlighted_tile_entity in highlighted_tiles_q.iter() { if let Ok(label) = tile_label_q.get(highlighted_tile_entity) { - if let Ok(mut tile_text) = text_q.get_mut(label.0) { - for section in tile_text.sections.iter_mut() { - section.style.color = Color::BLACK; - } + if let Ok(mut text_color) = text_q.get_mut(label.0) { + text_color.0 = Color::BLACK; commands.entity(highlighted_tile_entity).remove::(); } } @@ -313,10 +303,8 @@ fn hover_highlight_tile_label( // Highlight the relevant tile's label if let Some(tile_entity) = tile_storage.get(&tile_pos) { if let Ok(label) = tile_label_q.get(tile_entity) { - if let Ok(mut tile_text) = text_q.get_mut(label.0) { - for section in tile_text.sections.iter_mut() { - section.style.color = palettes::tailwind::RED_600.into(); - } + if let Ok(mut text_color) = text_q.get_mut(label.0) { + text_color.0 = palettes::tailwind::RED_600.into(); commands.entity(tile_entity).insert(Hovered); } } @@ -337,15 +325,13 @@ fn highlight_neighbor_label( highlighted_tiles_q: Query>, hovered_tiles_q: Query<&TilePos, With>, tile_label_q: Query<&TileLabel>, - mut text_q: Query<&mut Text>, + mut text_q: Query<&mut TextColor>, ) { // Un-highlight any previously highlighted tile labels. for highlighted_tile_entity in highlighted_tiles_q.iter() { if let Ok(label) = tile_label_q.get(highlighted_tile_entity) { - if let Ok(mut tile_text) = text_q.get_mut(label.0) { - for section in tile_text.sections.iter_mut() { - section.style.color = Color::BLACK; - } + if let Ok(mut text_color) = text_q.get_mut(label.0) { + text_color.0 = Color::BLACK; commands .entity(highlighted_tile_entity) .remove::(); @@ -369,10 +355,8 @@ fn highlight_neighbor_label( // `checked_get`. if let Some(tile_entity) = tile_storage.checked_get(neighbor_pos) { if let Ok(label) = tile_label_q.get(tile_entity) { - if let Ok(mut tile_text) = text_q.get_mut(label.0) { - for section in tile_text.sections.iter_mut() { - section.style.color = palettes::tailwind::BLUE_600.into(); - } + if let Ok(mut text_color) = text_q.get_mut(label.0) { + text_color.0 = palettes::tailwind::BLUE_600.into(); commands.entity(tile_entity).insert(NeighborHighlight); } } @@ -410,10 +394,8 @@ fn highlight_neighbor_label( // `checked_get`. if let Some(tile_entity) = tile_storage.checked_get(&tile_pos) { if let Ok(label) = tile_label_q.get(tile_entity) { - if let Ok(mut tile_text) = text_q.get_mut(label.0) { - for section in tile_text.sections.iter_mut() { - section.style.color = palettes::tailwind::GREEN_600.into(); - } + if let Ok(mut text_color) = text_q.get_mut(label.0) { + text_color.0 = palettes::tailwind::GREEN_600.into(); commands.entity(tile_entity).insert(NeighborHighlight); } } diff --git a/examples/hex_neighbors_radius_chunks.rs b/examples/hex_neighbors_radius_chunks.rs index 9f3f9593..2f64f3a2 100644 --- a/examples/hex_neighbors_radius_chunks.rs +++ b/examples/hex_neighbors_radius_chunks.rs @@ -188,7 +188,7 @@ fn hex_neighbors_radius_from_tile_pos( } fn spawn_chunks(mut commands: Commands, tile_handle_hex_row: Res) { - commands.spawn(Camera2dBundle::default()); + commands.spawn(Camera2d); let map_size = TilemapSize { x: CHUNK_MAP_SIDE_LENGTH_X, @@ -252,7 +252,7 @@ fn swap_map_type( )>, keyboard_input: Res>, tile_label_q: Query<(Entity, &TileLabel, &TilePos), Without>, - mut transform_q: Query<(&mut Transform, &mut Text), Without>, + mut transform_q: Query<(&mut Transform, &mut Text2d), Without>, tile_handle_hex_row: Res, tile_handle_hex_col: Res, ) { @@ -308,8 +308,7 @@ fn swap_map_type( &map_type, &map_transform, ); - tile_label_text.sections.get_mut(0).unwrap().value = - format!("{}, {}", hex_pos.x, hex_pos.y); + tile_label_text.0 = format!("{}, {}", hex_pos.x, hex_pos.y); } } } @@ -327,12 +326,6 @@ fn spawn_tile_labels( tile_q: Query<&TilePos>, font_handle: Res, ) { - let text_style = TextStyle { - font: font_handle.clone(), - font_size: 20.0, - color: Color::BLACK, - }; - let text_justify = JustifyText::Center; for (map_transform, map_type, grid_size, tilemap_storage) in tilemap_q.iter() { for tile_entity in tilemap_storage.iter().flatten() { let tile_pos = tile_q.get(*tile_entity).unwrap(); @@ -342,15 +335,17 @@ fn spawn_tile_labels( let hex_pos = hex_pos_from_tile_pos(tile_pos, grid_size, map_type, map_transform); let label_entity = commands - .spawn(Text2dBundle { - text: Text::from_section( - format!("{}, {}", hex_pos.x, hex_pos.y), - text_style.clone(), - ) - .with_justify(text_justify), + .spawn(( + Text2d(format!("{}, {}", hex_pos.x, hex_pos.y)), + TextFont { + font: font_handle.clone(), + font_size: 20.0, + ..default() + }, + TextColor(Color::BLACK), + TextLayout::new_with_justify(JustifyText::Center), transform, - ..default() - }) + )) .id(); commands .entity(*tile_entity) @@ -382,7 +377,7 @@ pub fn update_cursor_pos( // any transforms on the camera. This is done by projecting the cursor position into // camera space (world space). for (cam_t, cam) in camera_q.iter() { - if let Some(pos) = cam.viewport_to_world_2d(cam_t, cursor_moved.position) { + if let Ok(pos) = cam.viewport_to_world_2d(cam_t, cursor_moved.position) { *cursor_pos = CursorPos(pos); } } @@ -402,15 +397,13 @@ fn hover_highlight_tile_label( )>, highlighted_tiles_q: Query>, tile_label_q: Query<&TileLabel>, - mut text_q: Query<&mut Text>, + mut text_q: Query<&mut TextColor>, ) { // Un-highlight any previously highlighted tile labels. for highlighted_tile_entity in highlighted_tiles_q.iter() { if let Ok(label) = tile_label_q.get(highlighted_tile_entity) { - if let Ok(mut tile_text) = text_q.get_mut(label.0) { - for section in tile_text.sections.iter_mut() { - section.style.color = Color::BLACK; - } + if let Ok(mut text_color) = text_q.get_mut(label.0) { + text_color.0 = Color::BLACK; commands.entity(highlighted_tile_entity).remove::(); } } @@ -428,10 +421,8 @@ fn hover_highlight_tile_label( { if let Some(tile_entity) = tile_storage.get(&tile_pos) { if let Ok(label) = tile_label_q.get(tile_entity) { - if let Ok(mut tile_text) = text_q.get_mut(label.0) { - for section in tile_text.sections.iter_mut() { - section.style.color = palettes::tailwind::RED_600.into(); - } + if let Ok(mut text_color) = text_q.get_mut(label.0) { + text_color.0 = palettes::tailwind::RED_600.into(); commands.entity(tile_entity).insert(Hovered); } } @@ -468,15 +459,13 @@ fn highlight_neighbor_labels( hovered_tiles_q: Query<(Entity, &TilePos), With>, tiles_q: Query<&TilePos, Without>, tile_label_q: Query<&TileLabel>, - mut text_q: Query<&mut Text>, + mut text_q: Query<&mut TextColor>, radius: Res, ) { for highlighted_tile_entity in highlighted_tiles_q.iter() { if let Ok(label) = tile_label_q.get(highlighted_tile_entity) { - if let Ok(mut tile_text) = text_q.get_mut(label.0) { - for section in tile_text.sections.iter_mut() { - section.style.color = Color::BLACK; - } + if let Ok(mut text_color) = text_q.get_mut(label.0) { + text_color.0 = Color::BLACK; commands .entity(highlighted_tile_entity) .remove::(); @@ -509,10 +498,8 @@ fn highlight_neighbor_labels( let tile_hex_pos = hex_pos_from_tile_pos(tile_pos, grid_size, map_type, map_t); if neighbors.contains(&tile_hex_pos) { if let Ok(label) = tile_label_q.get(*tile_entity) { - if let Ok(mut tile_text) = text_q.get_mut(label.0) { - for section in tile_text.sections.iter_mut() { - section.style.color = palettes::tailwind::BLUE_600.into(); - } + if let Ok(mut text_color) = text_q.get_mut(label.0) { + text_color.0 = palettes::tailwind::BLUE_600.into(); commands.entity(*tile_entity).insert(NeighborHighlight); } } diff --git a/examples/hexagon_column.rs b/examples/hexagon_column.rs index 597f83e2..2740e0f3 100644 --- a/examples/hexagon_column.rs +++ b/examples/hexagon_column.rs @@ -7,7 +7,7 @@ mod helpers; const QUADRANT_SIDE_LENGTH: u32 = 80; fn startup(mut commands: Commands, asset_server: Res) { - commands.spawn(Camera2dBundle::default()); + commands.spawn(Camera2d); let texture_handle: Handle = asset_server.load("flat_hex_tiles.png"); diff --git a/examples/hexagon_generation.rs b/examples/hexagon_generation.rs index 57b9f895..15d20b1e 100644 --- a/examples/hexagon_generation.rs +++ b/examples/hexagon_generation.rs @@ -46,7 +46,7 @@ impl FromWorld for FontHandle { // Generates the initial tilemap, which is a square grid. fn spawn_tilemap(mut commands: Commands, tile_handle_hex_row: Res) { - commands.spawn(Camera2dBundle::default()); + commands.spawn(Camera2d); let map_size = TilemapSize { x: MAP_SIDE_LENGTH, @@ -98,13 +98,6 @@ fn spawn_map_type_label( windows: Query<&Window>, map_type_q: Query<&TilemapType>, ) { - let text_style = TextStyle { - font: font_handle.clone(), - font_size: 20.0, - color: Color::BLACK, - }; - let text_justify = JustifyText::Center; - for window in windows.iter() { for map_type in map_type_q.iter() { // Place the map type label somewhere in the top left side of the screen @@ -114,12 +107,15 @@ fn spawn_map_type_label( ..Default::default() }; commands.spawn(( - Text2dBundle { - text: Text::from_section(format!("{map_type:?}"), text_style.clone()) - .with_justify(text_justify), - transform, + Text2d::new(format!("{map_type:?}")), + TextFont { + font: font_handle.clone(), + font_size: 20.0, ..default() }, + TextColor(Color::BLACK), + TextLayout::new_with_justify(JustifyText::Center), + transform, MapTypeLabel, )); } @@ -141,7 +137,7 @@ fn swap_map_type( &mut TileStorage, )>, keyboard_input: Res>, - mut map_type_label_q: Query<&mut Text, With>, + mut map_type_label_q: Query<&mut Text2d, With>, tile_handle_hex_row: Res, tile_handle_hex_col: Res, ) { @@ -201,7 +197,7 @@ fn swap_map_type( ); for mut label_text in map_type_label_q.iter_mut() { - label_text.sections[0].value = format!("{:?}", map_type.as_ref()); + label_text.0 = format!("{:?}", map_type.as_ref()); } } } diff --git a/examples/hexagon_row.rs b/examples/hexagon_row.rs index e99cd66e..2e34121d 100644 --- a/examples/hexagon_row.rs +++ b/examples/hexagon_row.rs @@ -6,7 +6,7 @@ mod helpers; const QUADRANT_SIDE_LENGTH: u32 = 80; fn startup(mut commands: Commands, asset_server: Res) { - commands.spawn(Camera2dBundle::default()); + commands.spawn(Camera2d); let texture_handle: Handle = asset_server.load("pointy_hex_tiles.png"); diff --git a/examples/iso_diamond.rs b/examples/iso_diamond.rs index 89ebc966..81fc9be9 100644 --- a/examples/iso_diamond.rs +++ b/examples/iso_diamond.rs @@ -8,7 +8,7 @@ mod helpers; const QUADRANT_SIDE_LENGTH: u32 = 80; fn startup(mut commands: Commands, asset_server: Res) { - commands.spawn(Camera2dBundle::default()); + commands.spawn(Camera2d); let texture_handle: Handle = asset_server.load("iso_color.png"); diff --git a/examples/iso_staggered.rs b/examples/iso_staggered.rs index 3070d852..6d0baedf 100644 --- a/examples/iso_staggered.rs +++ b/examples/iso_staggered.rs @@ -10,7 +10,7 @@ mod helpers; const QUADRANT_SIDE_LENGTH: u32 = 80; fn startup(mut commands: Commands, asset_server: Res) { - commands.spawn(Camera2dBundle::default()); + commands.spawn(Camera2d); let texture_handle: Handle = asset_server.load("iso_color.png"); diff --git a/examples/layers.rs b/examples/layers.rs index 85e488b7..fca24438 100644 --- a/examples/layers.rs +++ b/examples/layers.rs @@ -3,7 +3,7 @@ use bevy_ecs_tilemap::prelude::*; mod helpers; fn startup(mut commands: Commands, asset_server: Res) { - commands.spawn(Camera2dBundle::default()); + commands.spawn(Camera2d); let texture_handle: Handle = asset_server.load("tiles.png"); diff --git a/examples/ldtk.rs b/examples/ldtk.rs index 8e68a22c..2b0e56ff 100644 --- a/examples/ldtk.rs +++ b/examples/ldtk.rs @@ -14,9 +14,9 @@ use bevy_ecs_tilemap::*; mod helpers; fn startup(mut commands: Commands, asset_server: Res) { - commands.spawn(Camera2dBundle::default()); + commands.spawn(Camera2d); - let handle: Handle = asset_server.load("map.ldtk"); + let handle = helpers::ldtk::LdtkMapHandle(asset_server.load("map.ldtk")); commands.spawn(helpers::ldtk::LdtkMapBundle { ldtk_map: handle, diff --git a/examples/mouse_to_tile.rs b/examples/mouse_to_tile.rs index 9ea13ec2..f79f88ac 100644 --- a/examples/mouse_to_tile.rs +++ b/examples/mouse_to_tile.rs @@ -72,7 +72,7 @@ impl FromWorld for FontHandle { // Generates the initial tilemap, which is a square grid. fn spawn_tilemap(mut commands: Commands, tile_handle_square: Res) { - commands.spawn(Camera2dBundle::default()); + commands.spawn(Camera2d); let map_size = TilemapSize { x: MAP_SIDE_LENGTH_X, @@ -117,12 +117,6 @@ fn spawn_tile_labels( tile_q: Query<&mut TilePos>, font_handle: Res, ) { - let text_style = TextStyle { - font: font_handle.clone(), - font_size: 20.0, - color: Color::BLACK, - }; - let text_justify = JustifyText::Center; for (map_transform, map_type, grid_size, tilemap_storage) in tilemap_q.iter() { for tile_entity in tilemap_storage.iter().flatten() { let tile_pos = tile_q.get(*tile_entity).unwrap(); @@ -130,15 +124,17 @@ fn spawn_tile_labels( let transform = *map_transform * Transform::from_translation(tile_center); let label_entity = commands - .spawn(Text2dBundle { - text: Text::from_section( - format!("{}, {}", tile_pos.x, tile_pos.y), - text_style.clone(), - ) - .with_justify(text_justify), + .spawn(( + Text2d::new(format!("{}, {}", tile_pos.x, tile_pos.y)), + TextFont { + font: font_handle.clone(), + font_size: 20.0, + ..default() + }, + TextColor(Color::BLACK), + TextLayout::new_with_justify(JustifyText::Center), transform, - ..default() - }) + )) .id(); commands .entity(*tile_entity) @@ -157,13 +153,6 @@ fn spawn_map_type_label( windows: Query<&Window>, map_type_q: Query<&TilemapType>, ) { - let text_style = TextStyle { - font: font_handle.clone(), - font_size: 20.0, - color: Color::BLACK, - }; - let text_alignment = JustifyText::Center; - for window in windows.iter() { for map_type in map_type_q.iter() { // Place the map type label somewhere in the top left side of the screen @@ -173,12 +162,15 @@ fn spawn_map_type_label( ..Default::default() }; commands.spawn(( - Text2dBundle { - text: Text::from_section(format!("{map_type:?}"), text_style.clone()) - .with_justify(text_alignment), - transform, + Text2d::new(format!("{map_type:?}")), + TextFont { + font: font_handle.clone(), + font_size: 20.0, ..default() }, + TextColor(Color::BLACK), + TextLayout::new_with_justify(JustifyText::Center), + transform, MapTypeLabel, )); } @@ -201,7 +193,7 @@ fn swap_map_type( (&TileLabel, &TilePos), (With, Without, Without), >, - mut map_type_label_q: Query<&mut Text, With>, + mut map_type_label_q: Query<&mut Text2d, With>, mut transform_q: Query<&mut Transform, Without>, tile_handle_square: Res, tile_handle_hex_row: Res, @@ -274,7 +266,7 @@ fn swap_map_type( } for mut label_text in map_type_label_q.iter_mut() { - label_text.sections[0].value = format!("{:?}", map_type.as_ref()); + label_text.0 = format!("{:?}", map_type.as_ref()); } } } @@ -304,7 +296,7 @@ pub fn update_cursor_pos( // any transforms on the camera. This is done by projecting the cursor position into // camera space (world space). for (cam_t, cam) in camera_q.iter() { - if let Some(pos) = cam.viewport_to_world_2d(cam_t, cursor_moved.position) { + if let Ok(pos) = cam.viewport_to_world_2d(cam_t, cursor_moved.position) { *cursor_pos = CursorPos(pos); } } @@ -324,15 +316,13 @@ fn highlight_tile_labels( )>, highlighted_tiles_q: Query>, tile_label_q: Query<&TileLabel>, - mut text_q: Query<&mut Text>, + mut text_q: Query<&mut TextColor>, ) { // Un-highlight any previously highlighted tile labels. for highlighted_tile_entity in highlighted_tiles_q.iter() { if let Ok(label) = tile_label_q.get(highlighted_tile_entity) { - if let Ok(mut tile_text) = text_q.get_mut(label.0) { - for section in tile_text.sections.iter_mut() { - section.style.color = Color::BLACK; - } + if let Ok(mut text_color) = text_q.get_mut(label.0) { + text_color.0 = Color::BLACK; commands .entity(highlighted_tile_entity) .remove::(); @@ -358,10 +348,8 @@ fn highlight_tile_labels( // Highlight the relevant tile's label if let Some(tile_entity) = tile_storage.get(&tile_pos) { if let Ok(label) = tile_label_q.get(tile_entity) { - if let Ok(mut tile_text) = text_q.get_mut(label.0) { - for section in tile_text.sections.iter_mut() { - section.style.color = palettes::tailwind::RED_600.into(); - } + if let Ok(mut text_color) = text_q.get_mut(label.0) { + text_color.0 = palettes::tailwind::RED_600.into(); commands.entity(tile_entity).insert(HighlightedLabel); } } diff --git a/examples/move_tile.rs b/examples/move_tile.rs index d88e2933..436d6988 100644 --- a/examples/move_tile.rs +++ b/examples/move_tile.rs @@ -3,7 +3,7 @@ use bevy_ecs_tilemap::prelude::*; mod helpers; fn startup(mut commands: Commands, asset_server: Res) { - commands.spawn(Camera2dBundle::default()); + commands.spawn(Camera2d); let texture_handle: Handle = asset_server.load("tiles.png"); diff --git a/examples/random_map.rs b/examples/random_map.rs index fe32b727..592311ef 100644 --- a/examples/random_map.rs +++ b/examples/random_map.rs @@ -8,7 +8,7 @@ use rand::{thread_rng, Rng}; mod helpers; fn startup(mut commands: Commands, asset_server: Res) { - commands.spawn(Camera2dBundle::default()); + commands.spawn(Camera2d); let texture_handle: Handle = asset_server.load("tiles.png"); @@ -57,7 +57,7 @@ struct LastUpdate { // In this example it's better not to use the default `MapQuery` SystemParam as // it's faster to do it this way: fn random(time: ResMut