2020-05-04 08:22:25 +00:00
|
|
|
use crate::{ColorMaterial, Rect};
|
2020-05-13 23:42:27 +00:00
|
|
|
use bevy_asset::{Assets, Handle};
|
2020-05-04 08:22:25 +00:00
|
|
|
use bevy_render::texture::Texture;
|
|
|
|
pub use legion::prelude::*;
|
|
|
|
pub struct Sprite {
|
|
|
|
pub scale: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Sprite {
|
|
|
|
fn default() -> Self {
|
2020-05-06 01:44:32 +00:00
|
|
|
Sprite { scale: 1.0 }
|
2020-05-04 08:22:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sprite_system() -> Box<dyn Schedulable> {
|
|
|
|
SystemBuilder::new("sprite_system")
|
2020-05-13 23:42:27 +00:00
|
|
|
.read_resource::<Assets<ColorMaterial>>()
|
|
|
|
.read_resource::<Assets<Texture>>()
|
2020-05-04 08:22:25 +00:00
|
|
|
.with_query(
|
|
|
|
<(Read<Sprite>, Read<Handle<ColorMaterial>>, Write<Rect>)>::query().filter(
|
|
|
|
changed::<Sprite>() | changed::<Rect>() | changed::<Handle<ColorMaterial>>(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.build(|_, world, (materials, textures), query| {
|
|
|
|
for (sprite, handle, mut rect) in query.iter_mut(world) {
|
|
|
|
let material = materials.get(&handle).unwrap();
|
|
|
|
if let Some(texture_handle) = material.texture {
|
2020-05-16 02:30:02 +00:00
|
|
|
if let Some(texture) = textures.get(&texture_handle) {
|
|
|
|
let aspect = texture.aspect();
|
|
|
|
*rect.size.x_mut() = texture.width as f32 * sprite.scale;
|
|
|
|
*rect.size.y_mut() = rect.size.x() * aspect;
|
|
|
|
}
|
2020-05-04 08:22:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|