bevy/crates/bevy_sprite/src/sprite.rs

37 lines
1.3 KiB
Rust
Raw Normal View History

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();
*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
}
}
})
}