bevy/crates/bevy_sprite/src/sprite.rs

38 lines
1.2 KiB
Rust
Raw Normal View History

use crate::ColorMaterial;
2020-05-13 23:42:27 +00:00
use bevy_asset::{Assets, Handle};
use bevy_core::bytes::Byteable;
use bevy_render::{
render_resource::{RenderResource, RenderResources},
texture::Texture,
};
2020-05-04 08:22:25 +00:00
pub use legion::prelude::*;
use glam::Vec2;
#[repr(C)]
#[derive(Default, RenderResources, RenderResource)]
#[render_resources(from_self)]
2020-05-04 08:22:25 +00:00
pub struct Sprite {
size: Vec2,
2020-05-04 08:22:25 +00:00
}
// SAFE: sprite is repr(C) and only consists of byteables
unsafe impl Byteable for Sprite {}
2020-05-04 08:22:25 +00:00
// TODO: port to system fn
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>>()
.with_query(<(Write<Sprite>, Read<Handle<ColorMaterial>>)>::query())
2020-05-04 08:22:25 +00:00
.build(|_, world, (materials, textures), query| {
for (mut sprite, handle) in query.iter_mut(world) {
2020-05-04 08:22:25 +00:00
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) {
sprite.size = texture.size;
2020-05-16 02:30:02 +00:00
}
2020-05-04 08:22:25 +00:00
}
}
})
}