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

feat: HexLayout has a single scale field for axis management #190

Merged
merged 4 commits into from
Dec 22, 2024
Merged
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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
take a new `HexOrientation` parameter (#189)
* (**BREAKING**) `HexLayout` Y axis is no longer inverted by default (#187)
* `HexLayout` builder patter (#187)
* (**BREAKING**) `HexLayout` removed the `invert_x` and `invert_y` fields (#190)
* (**BREAKING**) `HexLayout` `hex_size` field is now `scale` (#190)
* Added the following `HexLayout` methods: (#190)
- `transform_point`
- `transform_vector`
- `inverse_transform_point`
- `inverse_transform_vector`
- `invert_x`
- `invert_y`
* Added `world_unit_vector` methods to `EdgeDirection` and `VertexDirection` (#190)

## 0.19.1

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@

// Define your layout
let layout = HexLayout {
hex_size: Vec2::new(1.0, 1.0),
scale: Vec2::new(1.0, 1.0),
orientation: HexOrientation::Flat,
..Default::default()
};
Expand Down
2 changes: 1 addition & 1 deletion examples/3d_columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn setup_grid(
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let layout = HexLayout {
hex_size: HEX_SIZE,
scale: HEX_SIZE,
..default()
};
// materials
Expand Down
2 changes: 1 addition & 1 deletion examples/3d_picking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn setup_grid(
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let layout = HexLayout {
hex_size: HEX_SIZE,
scale: HEX_SIZE,
..default()
};
// materials
Expand Down
2 changes: 1 addition & 1 deletion examples/a_star.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn setup_grid(
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let layout = HexLayout {
hex_size: HEX_SIZE,
scale: HEX_SIZE,
..default()
};
let mesh = meshes.add(hexagonal_plane(&layout));
Expand Down
2 changes: 1 addition & 1 deletion examples/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn setup_grid(
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let layout = HexLayout {
hex_size: HEX_SIZE,
scale: HEX_SIZE,
..default()
};
// materials
Expand Down
2 changes: 1 addition & 1 deletion examples/field_of_movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn setup_grid(
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let layout = HexLayout {
hex_size: HEX_SIZE,
scale: HEX_SIZE,
..default()
};
let mesh = meshes.add(hexagonal_plane(&layout));
Expand Down
2 changes: 1 addition & 1 deletion examples/field_of_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn setup_grid(
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let layout = HexLayout {
hex_size: HEX_SIZE,
scale: HEX_SIZE,
..default()
};
let mesh = meshes.add(hexagonal_plane(&layout));
Expand Down
6 changes: 2 additions & 4 deletions examples/hex_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,14 @@ fn setup_grid(
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let flat_layout = HexLayout {
hex_size: HEX_SIZE,
scale: HEX_SIZE,
orientation: HexOrientation::Flat,
origin: Vec2::new(-480.0, 0.0),
..default()
};
let pointy_layout = HexLayout {
hex_size: HEX_SIZE,
scale: HEX_SIZE,
orientation: HexOrientation::Pointy,
origin: Vec2::new(480.0, 0.0),
..default()
};
// materials
let area_material = materials.add(Color::Srgba(GOLD));
Expand Down
2 changes: 1 addition & 1 deletion examples/hex_grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn setup_grid(
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let layout = HexLayout {
hex_size: HEX_SIZE,
scale: HEX_SIZE,
..default()
};
// materials
Expand Down
2 changes: 1 addition & 1 deletion examples/merged_columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn setup_grid(
commands.entity(map.0).despawn_recursive();
}
let layout = HexLayout {
hex_size: settings.hex_size,
scale: settings.hex_size,
..default()
};
// Materials shouldn't be added to assets every time, this is just to keep the
Expand Down
4 changes: 2 additions & 2 deletions examples/mesh_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ fn gizmos(
// Local axis
let mut transform = *transform;
transform.scale.y += params.height / 2.0;
transform.scale.x += info.layout.hex_size.x;
transform.scale.z += info.layout.hex_size.y;
transform.scale.x += info.layout.scale.x;
transform.scale.z += info.layout.scale.y;
transform.scale *= params.scale;
draw.axes(transform, 1.0);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/scroll_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn setup_grid(
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let layout = HexLayout {
hex_size: HEX_SIZE,
scale: HEX_SIZE,
..default()
};
let mesh = meshes.add(hexagonal_plane(&layout));
Expand Down
6 changes: 5 additions & 1 deletion examples/shapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl Shape {
pub fn setup(mut commands: Commands, mut mats: ResMut<Assets<ColorMaterial>>) {
commands.spawn(Camera2d);
let layout = HexLayout {
hex_size: HEX_SIZE,
scale: HEX_SIZE,
..default()
};
let mat = mats.add(Color::WHITE);
Expand Down Expand Up @@ -112,6 +112,10 @@ fn show_ui(world: &mut World) {
ui.label("Orientation");
bevy_inspector::ui_for_value(&mut map.layout.orientation, ui, world);
});
ui.horizontal(|ui| {
ui.label("scale");
bevy_inspector::ui_for_value(&mut map.layout.scale, ui, world);
});
});

world.resource_scope(|world, mut shape: Mut<Shape>| {
Expand Down
2 changes: 1 addition & 1 deletion examples/sprite_sheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn setup_grid(
let atlas_layout = atlas_layouts.add(atlas_layout);
let layout = HexLayout {
orientation: HexOrientation::Pointy,
hex_size: HEX_SIZE,
scale: HEX_SIZE,
..default()
};
let sprite_size = layout.rect_size();
Expand Down
2 changes: 1 addition & 1 deletion examples/wrap_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn setup_grid(
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let layout = HexLayout {
hex_size: HEX_SIZE,
scale: HEX_SIZE,
..default()
};
let mesh = meshes.add(hexagonal_plane(&layout));
Expand Down
19 changes: 17 additions & 2 deletions src/direction/edge_direction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
DIRECTION_ANGLE_DEGREES, DIRECTION_ANGLE_OFFSET_DEGREES, DIRECTION_ANGLE_OFFSET_RAD,
DIRECTION_ANGLE_RAD,
},
Hex, HexOrientation, VertexDirection,
Hex, HexLayout, HexOrientation, VertexDirection,
};
use glam::Vec2;
use std::{f32::consts::TAU, fmt::Debug};
Expand Down Expand Up @@ -384,12 +384,27 @@ impl EdgeDirection {

#[inline]
#[must_use]
/// Returns the unit vector of the direction in the given `orientation`
/// Returns the unit vector of the direction in the given `orientation`.
///
/// The vector is normalized and in local hex space. To use within a
/// [`HexLayout`] use [`HexLayout::transform_vector`] or
/// [`Self::world_unit_vector`]
pub fn unit_vector(self, orientation: HexOrientation) -> Vec2 {
let angle = self.angle(orientation);
Vec2::new(angle.cos(), angle.sin())
}

#[inline]
#[must_use]
/// Returns the unit vector of the direction in the given `layout`.
///
/// The vector is provided in pixel/workd space. To use in local hex
/// space use [`Self::unit_vector`]
pub fn world_unit_vector(self, layout: &HexLayout) -> Vec2 {
let vector = self.unit_vector(layout.orientation);
layout.transform_vector(vector)
}

#[inline]
#[must_use]
/// Returns the angle in degrees of the given direction for *pointy*
Expand Down
17 changes: 16 additions & 1 deletion src/direction/vertex_direction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
DIRECTION_ANGLE_DEGREES, DIRECTION_ANGLE_OFFSET_DEGREES, DIRECTION_ANGLE_OFFSET_RAD,
DIRECTION_ANGLE_RAD,
},
EdgeDirection, Hex, HexOrientation,
EdgeDirection, Hex, HexLayout, HexOrientation,
};
use glam::Vec2;
use std::{f32::consts::TAU, fmt::Debug};
Expand Down Expand Up @@ -386,11 +386,26 @@ impl VertexDirection {
#[inline]
#[must_use]
/// Returns the unit vector of the direction in the given `orientation`
///
/// The vector is normalized and in local hex space. To use within a
/// [`HexLayout`] use [`HexLayout::transform_vector`] or
/// [`Self::world_unit_vector`]
pub fn unit_vector(self, orientation: HexOrientation) -> Vec2 {
let angle = self.angle(orientation);
Vec2::new(angle.cos(), angle.sin())
}

#[inline]
#[must_use]
/// Returns the unit vector of the direction in the given `layout`.
///
/// The vector is provided in pixel/workd space. To use in local hex
/// space use [`Self::unit_vector`]
pub fn world_unit_vector(self, layout: &HexLayout) -> Vec2 {
let vector = self.unit_vector(layout.orientation);
layout.transform_vector(vector)
}

#[inline]
#[must_use]
/// Returns the angle in degrees of the given direction for *pointy*
Expand Down
Loading
Loading