Commit c12ef4fe authored by Rahix's avatar Rahix 🦀
Browse files

sprite: Add custom offsetting mechanism

In case some entity needs its sprite to be offset from its position, add
an `offset` attribute to the `Sprite` component which allows doing just
that.

The offset is a translation vector from the default position.  By
default, a sprite is placed such that its center is located at the
entities `Position`.
parent 1f573a2c
......@@ -2,12 +2,21 @@ use crate::resources;
pub struct Sprite {
pub image_handle: resources::ImageHandle,
pub offset: nalgebra::Vector2<f32>,
}
impl Sprite {
pub fn new<S: Into<resources::ImageHandle>>(handle: S) -> Self {
Self::with_offset(handle, nalgebra::Vector2::new(0.0, 0.0))
}
pub fn with_offset<S: Into<resources::ImageHandle>>(
handle: S,
offset: nalgebra::Vector2<f32>,
) -> Self {
Sprite {
image_handle: handle.into(),
offset,
}
}
}
......@@ -10,7 +10,7 @@ pub fn draw_sprites(
sprite: &components::Sprite,
) {
let spritesize = rendering.get_image_size(&sprite.image_handle).unwrap();
let upper_left = position.0 - spritesize / 2.0;
let upper_left = position.0 - spritesize / 2.0 + sprite.offset;
rendering
.draw_image(
&sprite.image_handle,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment