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

Try to fix Hexagon(RowEven/RowOdd/ColumnEven/ColumnOdd) collider bugs. #39

Closed
wants to merge 9 commits into from
Closed
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
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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]
Expand Down Expand Up @@ -129,4 +129,4 @@ name = "multiple_tilesets"

[[example]]
name = "custom_physics"
required-features = ["physics"]
required-features = ["physics"]
19 changes: 12 additions & 7 deletions assets/drjamgo_hex_16x16.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.10" tiledversion="1.10.2" name="drjamgo_hex_16x16" tilewidth="16" tileheight="16" tilecount="20" columns="4">
<tileset version="1.10" tiledversion="1.11.0" name="drjamgo_hex_16x16" tilewidth="16" tileheight="16" tilecount="20" columns="4">
<image source="tiles/drjamgo_hex_16x16.png" width="64" height="80"/>
<tile id="2" type="TileBundle">
<properties>
<property name="BiomeInfos" type="class" propertytype="BiomeInfos">
<properties>
<property name="BlockLineOfSight" type="bool" value="true"/>
<property name="Type" propertytype="BiomeType" value="Mountain"/>
</properties>
</property>
<property name="BiomeInfos" type="class"/>
</properties>
<objectgroup draworder="index" id="2">
<object id="1" x="3.15789" y="3.52941" width="9.47368" height="8.35913"/>
</objectgroup>
</tile>
<tile id="10">
<objectgroup draworder="index" id="2">
<object id="1" x="5.387" y="4.27245" width="7.43034" height="7.80186">
<ellipse/>
</object>
</objectgroup>
</tile>
<tile id="19" type="TileComponent">
<properties>
Expand Down
15 changes: 9 additions & 6 deletions assets/kenney-sketch-desert.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.10" tiledversion="1.10.2" name="kenney-sketch-desert" tilewidth="256" tileheight="352" tilecount="5" columns="0">
<tileset version="1.10" tiledversion="1.11.0" name="kenney-sketch-desert" tilewidth="256" tileheight="352" tilecount="5" columns="0">
<grid orientation="isometric" width="1" height="1"/>
<tile id="0">
<image width="256" height="352" source="tiles/kenney-sketch-desert/building_dark_sides_windows_E.png"/>
<image source="tiles/kenney-sketch-desert/building_dark_sides_windows_E.png" width="256" height="352"/>
</tile>
<tile id="1">
<image width="256" height="352" source="tiles/kenney-sketch-desert/dirt_center_E.png"/>
<image source="tiles/kenney-sketch-desert/dirt_center_E.png" width="256" height="352"/>
</tile>
<tile id="2">
<image width="256" height="352" source="tiles/kenney-sketch-desert/grass_center_S.png"/>
<image source="tiles/kenney-sketch-desert/grass_center_S.png" width="256" height="352"/>
</tile>
<tile id="3">
<image width="256" height="352" source="tiles/kenney-sketch-desert/stairs_full_N.png"/>
<image source="tiles/kenney-sketch-desert/stairs_full_N.png" width="256" height="352"/>
</tile>
<tile id="4">
<image width="256" height="352" source="tiles/kenney-sketch-desert/trees_N.png"/>
<image source="tiles/kenney-sketch-desert/trees_N.png" width="256" height="352"/>
<objectgroup draworder="index" id="2">
<object id="1" x="-85.3333" y="-253.333" width="232" height="26.6667"/>
</objectgroup>
</tile>
</tileset>
24 changes: 17 additions & 7 deletions assets/simple hex flat top.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.10" tiledversion="1.10.2" name="simple hex flat top" tilewidth="60" tileheight="52" tilecount="6" columns="0">
<tileset version="1.10" tiledversion="1.11.0" name="simple hex flat top" tilewidth="60" tileheight="52" tilecount="6" columns="0">
<grid orientation="orthogonal" width="1" height="1"/>
<tile id="1">
<image width="60" height="52" source="tiles/simple hex flat top/boat.png"/>
<image source="tiles/simple hex flat top/boat.png" width="60" height="52"/>
</tile>
<tile id="2">
<image width="60" height="52" source="tiles/simple hex flat top/cave.png"/>
<image source="tiles/simple hex flat top/cave.png" width="60" height="52"/>
<objectgroup draworder="index" id="2">
<object id="1" x="9.10217" y="31.5789">
<polygon points="0,0 21.7337,-26.3777 43.2817,0.743034"/>
</object>
</objectgroup>
</tile>
<tile id="4">
<image width="60" height="52" source="tiles/simple hex flat top/moutain.png"/>
<image source="tiles/simple hex flat top/moutain.png" width="60" height="52"/>
<objectgroup draworder="index" id="2">
<object id="1" x="14.8607" y="20.0619">
<polygon points="0,0 13.7461,-18.5759 28.4211,0.185759"/>
</object>
</objectgroup>
</tile>
<tile id="5">
<image width="60" height="52" source="tiles/simple hex flat top/ocean.png"/>
<image source="tiles/simple hex flat top/ocean.png" width="60" height="52"/>
</tile>
<tile id="6">
<image width="60" height="52" source="tiles/simple hex flat top/plain.png"/>
<image source="tiles/simple hex flat top/plain.png" width="60" height="52"/>
</tile>
<tile id="7">
<image width="60" height="52" source="tiles/simple hex flat top/forest.png"/>
<image source="tiles/simple hex flat top/forest.png" width="60" height="52"/>
</tile>
</tileset>
Binary file added assets/tiles/tiledtest64x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/tiles/tiledtest64x64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 19 additions & 4 deletions examples/hex_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<NoUserData>::pixels_per_meter(100.0))
.add_plugins(RapierDebugRenderPlugin::default());

app.run();
}

fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
Expand Down
23 changes: 19 additions & 4 deletions examples/isometric_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<NoUserData>::pixels_per_meter(100.0))
.add_plugins(RapierDebugRenderPlugin::default());

app.run();
}

fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
Expand Down
25 changes: 24 additions & 1 deletion src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//!
use crate::prelude::*;
use bevy::{color::palettes::css::RED, prelude::*};
use bevy_ecs_tilemap::tiles::TilePos;

/// Debug [Gizmos] configuration
///
Expand Down Expand Up @@ -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));
}
}

Expand All @@ -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<DebugPos>>) {
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);
}
}
57 changes: 54 additions & 3 deletions src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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!(
Expand Down
Loading