2020-06-01 06:39:20 +00:00
|
|
|
use crate::{ColorMaterial, Quad};
|
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(
|
2020-06-01 06:39:20 +00:00
|
|
|
<(Read<Sprite>, Read<Handle<ColorMaterial>>, Write<Quad>)>::query().filter(
|
|
|
|
changed::<Sprite>() | changed::<Quad>() | changed::<Handle<ColorMaterial>>(),
|
2020-05-04 08:22:25 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.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();
|
2020-06-04 02:00:19 +00:00
|
|
|
*rect.size.x_mut() = texture.size.x() * sprite.scale;
|
2020-05-16 02:30:02 +00:00
|
|
|
*rect.size.y_mut() = rect.size.x() * aspect;
|
|
|
|
}
|
2020-05-04 08:22:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|