diff --git a/Cargo.toml b/Cargo.toml index 944ad10..c5e3008 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,7 +40,7 @@ bevy_ecs_tilemap = "0.14.0" futures-lite = "2.3.0" log = "0.4.22" thiserror = "1.0.63" -tiled = "0.12.1" +tiled = { git = "https://github.com/mapeditor/rs-tiled.git", rev = "31783f07d421b5d3130d81d63d1b2081d8c0021f" } # Optional dependencies, enabled via features. bevy_rapier2d = { version = "0.27.0", optional = true } @@ -51,7 +51,7 @@ bevy_ecs_tiled_macros = { version = "0.1.0", optional = true, path = "macros" } bevy_rapier2d = { version = "0.27.0", features = [ "wasm-bindgen", ], optional = true } -tiled = { version = "0.12.1", features = ["wasm"] } +tiled = { git = "https://github.com/mapeditor/rs-tiled.git", rev = "31783f07d421b5d3130d81d63d1b2081d8c0021f", features = ["wasm"] } # docs.rs-specific configuration [package.metadata.docs.rs] @@ -129,4 +129,4 @@ name = "multiple_tilesets" [[example]] name = "custom_physics" -required-features = ["physics"] \ No newline at end of file +required-features = ["physics"] diff --git a/assets/drjamgo_hex_16x16.tsx b/assets/drjamgo_hex_16x16.tsx index 788ad93..c96b96b 100644 --- a/assets/drjamgo_hex_16x16.tsx +++ b/assets/drjamgo_hex_16x16.tsx @@ -1,15 +1,20 @@ - + - - - - - - + + + + + + + + + + + diff --git a/assets/kenney-sketch-desert.tsx b/assets/kenney-sketch-desert.tsx index 7c08416..5dd2f4b 100644 --- a/assets/kenney-sketch-desert.tsx +++ b/assets/kenney-sketch-desert.tsx @@ -1,19 +1,22 @@ - + - + - + - + - + - + + + + diff --git a/assets/simple hex flat top.tsx b/assets/simple hex flat top.tsx index 60d9763..90e8ac3 100644 --- a/assets/simple hex flat top.tsx +++ b/assets/simple hex flat top.tsx @@ -1,22 +1,32 @@ - + - + - + + + + + + - + + + + + + - + - + - + diff --git a/assets/tiles/tiledtest64x32.png b/assets/tiles/tiledtest64x32.png new file mode 100644 index 0000000..627369a Binary files /dev/null and b/assets/tiles/tiledtest64x32.png differ diff --git a/assets/tiles/tiledtest64x64.png b/assets/tiles/tiledtest64x64.png new file mode 100644 index 0000000..f672f4a Binary files /dev/null and b/assets/tiles/tiledtest64x64.png differ diff --git a/examples/hex_map.rs b/examples/hex_map.rs index d870911..e693bf6 100644 --- a/examples/hex_map.rs +++ b/examples/hex_map.rs @@ -6,17 +6,32 @@ use bevy_ecs_tilemap::prelude::*; mod helper; +#[cfg(feature = "avian")] +use avian2d::prelude::*; + +#[cfg(feature = "rapier")] +use bevy_rapier2d::prelude::*; + fn main() { - App::new() - .add_plugins(DefaultPlugins) + let mut app = App::new(); + app.add_plugins(DefaultPlugins) .add_plugins(TilemapPlugin) .add_plugins(TiledMapPlugin) // Enable debug informations about Tiled objects .add_plugins(TiledMapDebugPlugin::default()) .add_plugins(helper::HelperPlugin) .add_systems(Startup, startup) - .add_systems(Update, switch_map) - .run(); + .add_systems(Update, switch_map); + + #[cfg(feature = "avian")] + app.add_plugins(PhysicsPlugins::default().with_length_unit(100.0)) + .add_plugins(PhysicsDebugPlugin::default()); + + #[cfg(feature = "rapier")] + app.add_plugins(RapierPhysicsPlugin::::pixels_per_meter(100.0)) + .add_plugins(RapierDebugRenderPlugin::default()); + + app.run(); } fn startup(mut commands: Commands, asset_server: Res) { diff --git a/examples/isometric_map.rs b/examples/isometric_map.rs index f0f869b..951fc7f 100644 --- a/examples/isometric_map.rs +++ b/examples/isometric_map.rs @@ -6,16 +6,31 @@ use bevy_ecs_tilemap::prelude::*; mod helper; +#[cfg(feature = "avian")] +use avian2d::prelude::*; + +#[cfg(feature = "rapier")] +use bevy_rapier2d::prelude::*; + fn main() { - App::new() - .add_plugins(DefaultPlugins) + let mut app = App::new(); + app.add_plugins(DefaultPlugins) .add_plugins(TilemapPlugin) .add_plugins(TiledMapPlugin) .add_plugins(TiledMapDebugPlugin::default()) .add_plugins(helper::HelperPlugin) .add_systems(Startup, startup) - .add_systems(Update, switch_map) - .run(); + .add_systems(Update, switch_map); + + #[cfg(feature = "avian")] + app.add_plugins(PhysicsPlugins::default().with_length_unit(100.0)) + .add_plugins(PhysicsDebugPlugin::default()); + + #[cfg(feature = "rapier")] + app.add_plugins(RapierPhysicsPlugin::::pixels_per_meter(100.0)) + .add_plugins(RapierDebugRenderPlugin::default()); + + app.run(); } fn startup(mut commands: Commands, asset_server: Res) { diff --git a/src/debug.rs b/src/debug.rs index 2eaa817..ce07900 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -2,6 +2,7 @@ //! use crate::prelude::*; use bevy::{color::palettes::css::RED, prelude::*}; +use bevy_ecs_tilemap::tiles::TilePos; /// Debug [Gizmos] configuration /// @@ -45,7 +46,7 @@ pub struct TiledMapDebugPlugin { impl Plugin for TiledMapDebugPlugin { fn build(&self, app: &mut bevy::prelude::App) { app.insert_resource(self.gizmos_config.clone()) - .add_systems(Update, draw_debug_arrow); + .add_systems(Update, (draw_debug_arrow, draw_debug_pos)); } } @@ -59,3 +60,25 @@ fn draw_debug_arrow( gizmos.arrow_2d(pos + config.arrow_length, pos, config.color); } } + +#[derive(Component)] +struct DebugPos; +fn draw_debug_pos(mut commands: Commands, q_pos: Query<(Entity, &TilePos), Without>) { + for (e, tile_pos) in q_pos.iter() { + commands + .spawn(Text2dBundle { + text: Text::from_section( + format!("{0}, {1}", tile_pos.x, tile_pos.y), + TextStyle { + color: Color::srgb(0.25, 0.75, 0.25), + font_size: 12.0, + ..default() + }, + ), + transform: Transform::from_xyz(0., 0., 100.), // Set it to top + ..default() + }) + .set_parent(e); + commands.entity(e).insert(DebugPos); + } +} diff --git a/src/loader.rs b/src/loader.rs index 582be50..c8de14f 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -665,6 +665,59 @@ fn load_finite_tiles_layer( }; let tile_pos = TilePos { x, y }; + let (mut x, mut y) = ( + tile_pos.x as f32 * grid_size.x, + tile_pos.y as f32 * grid_size.y, + ); + // TODO: I don't even know when the value would not be there, maybe tiled versions below 1.7? + // So this fix only happened when map contain this value. + if let Some(hexsidelength) = tiled_map.map.hex_side_length { + let hexsidelength = hexsidelength as f32; + if let TilemapType::Hexagon(hex_coord_system) = _map_type { + match hex_coord_system { + HexCoordSystem::RowEven => { + // Y Odd + x = tile_pos.x as f32 * grid_size.x + - (tile_pos.y % 2) as f32 * grid_size.x / 2.; + y = tile_pos.y as f32 + * (grid_size.y - (grid_size.y - hexsidelength) / 2.); + } + HexCoordSystem::RowOdd => { + // Y Even + x = tile_pos.x as f32 * grid_size.x + + (tile_pos.y % 2) as f32 * grid_size.x / 2.; + y = tile_pos.y as f32 + * (grid_size.y - (grid_size.y - hexsidelength) / 2.); + } + HexCoordSystem::ColumnEven => { + x = tile_pos.x as f32 + * (grid_size.x - (grid_size.x - hexsidelength) / 2.); + y = tile_pos.y as f32 * grid_size.y + - (tile_pos.x % 2) as f32 * (grid_size.y / 2.); + } + HexCoordSystem::ColumnOdd => { + x = tile_pos.x as f32 + * (grid_size.y - (grid_size.y - hexsidelength) / 2.); + y = tile_pos.y as f32 * grid_size.y + + (tile_pos.x % 2) as f32 * (grid_size.y / 2.); + } + _ => {} // Do nothing + } + } + } else if let TilemapType::Isometric(iso_coord_system) = _map_type { + match iso_coord_system { + IsoCoordSystem::Diamond => { + x = tile_pos.x as f32 * (grid_size.x / 2.) + + tile_pos.y as f32 * (grid_size.x / 2.); + y = tile_pos.y as f32 * (grid_size.y / 2.) + - tile_pos.x as f32 * (grid_size.y / 2.); + } + IsoCoordSystem::Staggered => { + warn!("Isometric (Staggered) map is not supported"); + } + } + } + let tile_entity = commands .spawn(TileBundle { position: tile_pos, @@ -678,9 +731,7 @@ fn load_finite_tiles_layer( ..Default::default() }) .insert(SpatialBundle::from_transform(Transform::from_xyz( - tile_pos.x as f32 * grid_size.x, - tile_pos.y as f32 * grid_size.y, - 0.0, + x, y, 0.0, ))) .set_parent(layer_entity) .insert(Name::new(format!(