2020-06-22 19:14:40 +00:00
|
|
|
use crate::ColorMaterial;
|
2020-05-13 23:42:27 +00:00
|
|
|
use bevy_asset::{Assets, Handle};
|
2020-06-22 19:14:40 +00:00
|
|
|
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::*;
|
2020-06-22 19:14:40 +00:00
|
|
|
use glam::Vec2;
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Default, RenderResources, RenderResource)]
|
|
|
|
#[render_resources(from_self)]
|
2020-05-04 08:22:25 +00:00
|
|
|
pub struct Sprite {
|
2020-06-22 19:14:40 +00:00
|
|
|
size: Vec2,
|
2020-05-04 08:22:25 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 19:14:40 +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
|
|
|
|
2020-06-22 19:14:40 +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>>()
|
2020-06-22 19:14:40 +00:00
|
|
|
.with_query(<(Write<Sprite>, Read<Handle<ColorMaterial>>)>::query())
|
2020-05-04 08:22:25 +00:00
|
|
|
.build(|_, world, (materials, textures), query| {
|
2020-06-22 19:14:40 +00:00
|
|
|
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) {
|
2020-06-22 19:14:40 +00:00
|
|
|
sprite.size = texture.size;
|
2020-05-16 02:30:02 +00:00
|
|
|
}
|
2020-05-04 08:22:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|