pub struct HexLayout {
pub orientation: HexOrientation,
pub origin: Vec2,
pub scale: Vec2,
}Expand description
Hexagonal layout. This type is the bridge between your world/pixel coordinate system and the hexagonal coordinate system.
§Axis
By default, the Hex y axis is pointing up and the x axis is
pointing right but you have the option to invert them using invert_x and
invert_y This may be useful depending on the coordinate system of your
display.
§Example
let layout = HexLayout {
// We want flat topped hexagons
orientation: HexOrientation::Flat,
// We define the world space origin equivalent of `Hex::ZERO` in hex space
origin: Vec2::new(1.0, 2.0),
// We define the world space scale of the hexagons
scale: Vec2::new(1.0, 1.0),
};
// You can now find the world positon (center) of any given hexagon
let world_pos = layout.hex_to_world_pos(Hex::ZERO);
// You can also find which hexagon is at a given world/screen position
let hex_pos = layout.world_pos_to_hex(Vec2::new(1.23, 45.678));§Builder
HexLayout provides a builder pattern:
let mut layout = HexLayout::flat()
.with_scale(Vec2::new(2.0, 3.0)) // Individual Hexagon size
.with_origin(Vec2::new(-1.0, 0.0)); // World origin
// Invert the x axis, which will now go left. Will change `scale.x` to `-2.0`
layout.invert_x();
// Invert the y axis, which will now go down. Will change `scale.y` to `-3.0`
layout.invert_y();§Working with Sprites
If you intend to use the hexagonal grid to place images/sprites you may use
HexLayout::with_rect_size to make the hexagon scale fit the your sprite
dimensions.
You can also retrieve the matching rect size from any layout using
HexLayout::rect_size()
Fields§
§orientation: HexOrientationThe hexagonal orientation of the layout (usually “flat” or “pointy”)
origin: Vec2The origin of the hexagonal representation in world/pixel space, usually
Vec2::ZERO
scale: Vec2The size of individual hexagons in world/pixel space. The scale can be irregular or negative
Implementations§
Source§impl HexLayout
impl HexLayout
Sourcepub fn transform_vector(&self, vector: Vec2) -> Vec2
pub fn transform_vector(&self, vector: Vec2) -> Vec2
Transforms a local hex space vector to world space
by applying the layout scale but NOT the origin
Sourcepub fn transform_point(&self, point: Vec2) -> Vec2
pub fn transform_point(&self, point: Vec2) -> Vec2
Transforms a local hex point to world space
by applying the layout scale and origin
Sourcepub fn inverse_transform_vector(&self, vector: Vec2) -> Vec2
pub fn inverse_transform_vector(&self, vector: Vec2) -> Vec2
Transforms a world space vector to local hex space
by applying the layout scale but NOT the origin
Sourcepub fn inverse_transform_point(&self, point: Vec2) -> Vec2
pub fn inverse_transform_point(&self, point: Vec2) -> Vec2
Transforms a world pace point to local hex space
by applying the layout scale and origin
Source§impl HexLayout
impl HexLayout
Sourcepub fn hex_to_world_pos(&self, hex: Hex) -> Vec2
pub fn hex_to_world_pos(&self, hex: Hex) -> Vec2
Computes hexagonal coordinates hex into world/pixel coordinates
Sourcepub fn fract_hex_to_world_pos(&self, hex: Vec2) -> Vec2
pub fn fract_hex_to_world_pos(&self, hex: Vec2) -> Vec2
Computes fractional hexagonal coordinates hex into world/pixel
coordinates
Sourcepub fn world_pos_to_hex(&self, pos: Vec2) -> Hex
pub fn world_pos_to_hex(&self, pos: Vec2) -> Hex
Computes world/pixel coordinates pos into hexagonal coordinates
Sourcepub fn world_pos_to_fract_hex(&self, pos: Vec2) -> Vec2
pub fn world_pos_to_fract_hex(&self, pos: Vec2) -> Vec2
Computes world/pixel coordinates pos into fractional hexagonal
coordinates
Sourcepub fn hex_corners(&self, hex: Hex) -> [Vec2; 6]
pub fn hex_corners(&self, hex: Hex) -> [Vec2; 6]
Retrieves all 6 corner coordinates of the given hexagonal coordinates
hex
Sourcepub fn hex_edge_corners(&self, hex: Hex) -> [[Vec2; 2]; 6]
pub fn hex_edge_corners(&self, hex: Hex) -> [[Vec2; 2]; 6]
Retrieves all 6 edge corner pair coordinates of the given hexagonal
coordinates hex
Sourcepub fn center_aligned_hex_corners(&self) -> [Vec2; 6]
pub fn center_aligned_hex_corners(&self) -> [Vec2; 6]
Retrieves all 6 edge corner pair coordinates of the given hexagonal
coordinates hex without offsetting at the origin
Source§impl HexLayout
impl HexLayout
Sourcepub fn edge_coordinates(&self, edge: GridEdge) -> [Vec2; 2]
pub fn edge_coordinates(&self, edge: GridEdge) -> [Vec2; 2]
Returns the world coordinate of the two edge vertices in clockwise order
Sourcepub fn all_edge_coordinates(&self, coord: Hex) -> [[Vec2; 2]; 6]
pub fn all_edge_coordinates(&self, coord: Hex) -> [[Vec2; 2]; 6]
Returns the world coordinate of all edge vertex pairs in clockwise order
Sourcepub fn vertex_coordinates(&self, vertex: GridVertex) -> Vec2
pub fn vertex_coordinates(&self, vertex: GridVertex) -> Vec2
Returns the world coordinate of the vertex
Source§impl HexLayout
impl HexLayout
Sourcepub const fn new(orientation: HexOrientation) -> Self
pub const fn new(orientation: HexOrientation) -> Self
Constructs a new layout with the given orientation and default
values
Sourcepub const fn with_origin(self, origin: Vec2) -> Self
pub const fn with_origin(self, origin: Vec2) -> Self
Specifies the world/pixel origin of the layout
Sourcepub const fn with_hex_size(self, size: f32) -> Self
pub const fn with_hex_size(self, size: f32) -> Self
Specifies the world/pixel regular size of individual hexagons
Sourcepub fn with_rect_size(self, rect_size: Vec2) -> Self
pub fn with_rect_size(self, rect_size: Vec2) -> Self
Specifies the world/pixel size of individual hexagons to match
the given rect_size. This is useful if you want hexagons
to match a sprite size
Sourcepub const fn with_scale(self, scale: Vec2) -> Self
pub const fn with_scale(self, scale: Vec2) -> Self
Specifies the world/pixel scale of individual hexagons.
§Note
For most use cases prefer Self::with_hex_size instead.
Trait Implementations§
Source§impl Component for HexLayout
impl Component for HexLayout
Source§const STORAGE_TYPE: StorageType = bevy_ecs::component::StorageType::Table
const STORAGE_TYPE: StorageType = bevy_ecs::component::StorageType::Table
Source§type Mutability = Mutable
type Mutability = Mutable
Component<Mutability = Mutable>,
while immutable components will instead have Component<Mutability = Immutable>. Read moreSource§fn register_required_components(
_requiree: ComponentId,
required_components: &mut RequiredComponentsRegistrator<'_, '_>,
)
fn register_required_components( _requiree: ComponentId, required_components: &mut RequiredComponentsRegistrator<'_, '_>, )
Source§fn clone_behavior() -> ComponentCloneBehavior
fn clone_behavior() -> ComponentCloneBehavior
Source§fn on_add() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
fn on_add() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
Source§fn on_insert() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
fn on_insert() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
Source§fn on_replace() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
fn on_replace() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
Source§fn on_remove() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
fn on_remove() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
Source§fn on_despawn() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
fn on_despawn() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
Source§fn map_entities<E>(_this: &mut Self, _mapper: &mut E)where
E: EntityMapper,
fn map_entities<E>(_this: &mut Self, _mapper: &mut E)where
E: EntityMapper,
EntityMapper. This is used to remap entities in contexts like scenes and entity cloning.
When deriving Component, this is populated by annotating fields containing entities with #[entities] Read moreSource§impl<'de> Deserialize<'de> for HexLayout
impl<'de> Deserialize<'de> for HexLayout
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl FromReflect for HexLayout
impl FromReflect for HexLayout
Source§fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self>
fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self>
Self from a reflected value.Source§fn take_from_reflect(
reflect: Box<dyn PartialReflect>,
) -> Result<Self, Box<dyn PartialReflect>>
fn take_from_reflect( reflect: Box<dyn PartialReflect>, ) -> Result<Self, Box<dyn PartialReflect>>
Self using,
constructing the value using from_reflect if that fails. Read moreSource§impl GetTypeRegistration for HexLayout
impl GetTypeRegistration for HexLayout
Source§fn get_type_registration() -> TypeRegistration
fn get_type_registration() -> TypeRegistration
TypeRegistration for this type.Source§fn register_type_dependencies(registry: &mut TypeRegistry)
fn register_type_dependencies(registry: &mut TypeRegistry)
Source§impl PartialReflect for HexLayout
impl PartialReflect for HexLayout
Source§fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
Source§fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError>
fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError>
Source§fn reflect_kind(&self) -> ReflectKind
fn reflect_kind(&self) -> ReflectKind
Source§fn reflect_ref(&self) -> ReflectRef<'_>
fn reflect_ref(&self) -> ReflectRef<'_>
Source§fn reflect_mut(&mut self) -> ReflectMut<'_>
fn reflect_mut(&mut self) -> ReflectMut<'_>
Source§fn reflect_owned(self: Box<Self>) -> ReflectOwned
fn reflect_owned(self: Box<Self>) -> ReflectOwned
Source§fn try_into_reflect(
self: Box<Self>,
) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>>
fn try_into_reflect( self: Box<Self>, ) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>>
Source§fn try_as_reflect(&self) -> Option<&dyn Reflect>
fn try_as_reflect(&self) -> Option<&dyn Reflect>
Source§fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect>
fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect>
Source§fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect>
fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect>
Source§fn as_partial_reflect(&self) -> &dyn PartialReflect
fn as_partial_reflect(&self) -> &dyn PartialReflect
Source§fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect
fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect
Source§fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool>
fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool>
Source§fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError>
fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError>
Self using reflection. Read moreSource§fn apply(&mut self, value: &(dyn PartialReflect + 'static))
fn apply(&mut self, value: &(dyn PartialReflect + 'static))
Source§fn to_dynamic(&self) -> Box<dyn PartialReflect>
fn to_dynamic(&self) -> Box<dyn PartialReflect>
Source§fn reflect_clone_and_take<T>(&self) -> Result<T, ReflectCloneError>
fn reflect_clone_and_take<T>(&self) -> Result<T, ReflectCloneError>
PartialReflect, combines reflect_clone and
take in a useful fashion, automatically constructing an appropriate
ReflectCloneError if the downcast fails. Read moreSource§fn reflect_hash(&self) -> Option<u64>
fn reflect_hash(&self) -> Option<u64>
Source§fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>
fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>
Source§fn is_dynamic(&self) -> bool
fn is_dynamic(&self) -> bool
Source§impl Reflect for HexLayout
impl Reflect for HexLayout
Source§fn as_any_mut(&mut self) -> &mut dyn Any
fn as_any_mut(&mut self) -> &mut dyn Any
&mut dyn Any. Read moreSource§fn into_reflect(self: Box<Self>) -> Box<dyn Reflect>
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect>
Source§fn as_reflect(&self) -> &dyn Reflect
fn as_reflect(&self) -> &dyn Reflect
Source§fn as_reflect_mut(&mut self) -> &mut dyn Reflect
fn as_reflect_mut(&mut self) -> &mut dyn Reflect
Source§impl Struct for HexLayout
impl Struct for HexLayout
Source§fn field(&self, name: &str) -> Option<&dyn PartialReflect>
fn field(&self, name: &str) -> Option<&dyn PartialReflect>
name as a &dyn PartialReflect.Source§fn field_mut(&mut self, name: &str) -> Option<&mut dyn PartialReflect>
fn field_mut(&mut self, name: &str) -> Option<&mut dyn PartialReflect>
name as a
&mut dyn PartialReflect.Source§fn field_at(&self, index: usize) -> Option<&dyn PartialReflect>
fn field_at(&self, index: usize) -> Option<&dyn PartialReflect>
index as a
&dyn PartialReflect.Source§fn field_at_mut(&mut self, index: usize) -> Option<&mut dyn PartialReflect>
fn field_at_mut(&mut self, index: usize) -> Option<&mut dyn PartialReflect>
index
as a &mut dyn PartialReflect.Source§fn name_at(&self, index: usize) -> Option<&str>
fn name_at(&self, index: usize) -> Option<&str>
index.Source§fn iter_fields(&self) -> FieldIter<'_>
fn iter_fields(&self) -> FieldIter<'_>
Source§fn to_dynamic_struct(&self) -> DynamicStruct
fn to_dynamic_struct(&self) -> DynamicStruct
DynamicStruct from this struct.Source§fn get_represented_struct_info(&self) -> Option<&'static StructInfo>
fn get_represented_struct_info(&self) -> Option<&'static StructInfo>
None if TypeInfo is not available.Source§impl TypePath for HexLayout
impl TypePath for HexLayout
Source§fn type_path() -> &'static str
fn type_path() -> &'static str
Source§fn short_type_path() -> &'static str
fn short_type_path() -> &'static str
Source§fn type_ident() -> Option<&'static str>
fn type_ident() -> Option<&'static str>
Source§fn crate_name() -> Option<&'static str>
fn crate_name() -> Option<&'static str>
impl Resource for HexLayout
Auto Trait Implementations§
impl Freeze for HexLayout
impl RefUnwindSafe for HexLayout
impl Send for HexLayout
impl Sync for HexLayout
impl Unpin for HexLayout
impl UnwindSafe for HexLayout
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<C> Bundle for Cwhere
C: Component,
impl<C> Bundle for Cwhere
C: Component,
fn component_ids( components: &mut ComponentsRegistrator<'_>, ids: &mut impl FnMut(ComponentId), )
Source§fn get_component_ids(
components: &Components,
ids: &mut impl FnMut(Option<ComponentId>),
)
fn get_component_ids( components: &Components, ids: &mut impl FnMut(Option<ComponentId>), )
Source§impl<C> BundleFromComponents for Cwhere
C: Component,
impl<C> BundleFromComponents for Cwhere
C: Component,
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<C> DynamicBundle for Cwhere
C: Component,
impl<C> DynamicBundle for Cwhere
C: Component,
Source§unsafe fn get_components(
ptr: MovingPtr<'_, C>,
func: &mut impl FnMut(StorageType, OwningPtr<'_>),
) -> <C as DynamicBundle>::Effect
unsafe fn get_components( ptr: MovingPtr<'_, C>, func: &mut impl FnMut(StorageType, OwningPtr<'_>), ) -> <C as DynamicBundle>::Effect
Source§unsafe fn apply_effect(
_ptr: MovingPtr<'_, MaybeUninit<C>>,
_entity: &mut EntityWorldMut<'_>,
)
unsafe fn apply_effect( _ptr: MovingPtr<'_, MaybeUninit<C>>, _entity: &mut EntityWorldMut<'_>, )
Source§impl<T> DynamicTypePath for Twhere
T: TypePath,
impl<T> DynamicTypePath for Twhere
T: TypePath,
Source§fn reflect_type_path(&self) -> &str
fn reflect_type_path(&self) -> &str
TypePath::type_path.Source§fn reflect_short_type_path(&self) -> &str
fn reflect_short_type_path(&self) -> &str
Source§fn reflect_type_ident(&self) -> Option<&str>
fn reflect_type_ident(&self) -> Option<&str>
TypePath::type_ident.Source§fn reflect_crate_name(&self) -> Option<&str>
fn reflect_crate_name(&self) -> Option<&str>
TypePath::crate_name.Source§fn reflect_module_path(&self) -> Option<&str>
fn reflect_module_path(&self) -> Option<&str>
Source§impl<T> DynamicTyped for Twhere
T: Typed,
impl<T> DynamicTyped for Twhere
T: Typed,
Source§fn reflect_type_info(&self) -> &'static TypeInfo
fn reflect_type_info(&self) -> &'static TypeInfo
Typed::type_info.Source§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
Source§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Creates Self using default().
Source§impl<S> GetField for Swhere
S: Struct,
impl<S> GetField for Swhere
S: Struct,
Source§impl<T> GetPath for T
impl<T> GetPath for T
Source§fn reflect_path<'p>(
&self,
path: impl ReflectPath<'p>,
) -> Result<&(dyn PartialReflect + 'static), ReflectPathError<'p>>
fn reflect_path<'p>( &self, path: impl ReflectPath<'p>, ) -> Result<&(dyn PartialReflect + 'static), ReflectPathError<'p>>
path. Read moreSource§fn reflect_path_mut<'p>(
&mut self,
path: impl ReflectPath<'p>,
) -> Result<&mut (dyn PartialReflect + 'static), ReflectPathError<'p>>
fn reflect_path_mut<'p>( &mut self, path: impl ReflectPath<'p>, ) -> Result<&mut (dyn PartialReflect + 'static), ReflectPathError<'p>>
path. Read moreSource§fn path<'p, T>(
&self,
path: impl ReflectPath<'p>,
) -> Result<&T, ReflectPathError<'p>>where
T: Reflect,
fn path<'p, T>(
&self,
path: impl ReflectPath<'p>,
) -> Result<&T, ReflectPathError<'p>>where
T: Reflect,
path. Read moreSource§fn path_mut<'p, T>(
&mut self,
path: impl ReflectPath<'p>,
) -> Result<&mut T, ReflectPathError<'p>>where
T: Reflect,
fn path_mut<'p, T>(
&mut self,
path: impl ReflectPath<'p>,
) -> Result<&mut T, ReflectPathError<'p>>where
T: Reflect,
path. Read more