I have asked quite a few things recently I hope that's fine :). I'm just trying to get a hang of bevy and experiment with it as much as I can. (I have tried my best to condense the code to its most important aspects without loosing the complexity that troubles me)
I have two more questions about ergonomics that are somewhat related.
- How do I handle metadata for assets? (I only want to specify it once)
- How do I use assets to spawn entities without having to fetch them all the time.
I have included my current ideas but I'm not 100% satisfied. I'd love any input on how you solve these problems.
Note that I have removed most generics (such as impl Into<IVec2>) etc. to make the code more concise.
- Metadata for assets:
I use a new asset (Spritesheet) to store my metadata.
#[derive(Asset, Reflect, Clone)]
pub struct Spritesheet {
pub texture: Handle<Image>,
pub layout: Handle<TextureAtlasLayout>,
}
#[derive(Resource, Clone, Debug, Deref)]
struct CreaturesSpritesheet(Handle<Spritesheet>);
// Setting up the assets in the FromWorld implementation
impl FromWorld for CreaturesSpritesheet {
fn from_world(world: &mut World) -> Self {
let asset_server = world.resource::<AssetServer>();
let texture = asset_server.load(
"path",
);
let layout = TextureAtlasLayout::from_grid(
UVec2::splat(10),
14,
18,
Some(UVec2::new(2, 2)), // padding
Some(UVec2::new(2, 2)), // offset
);
let mut texture_atlas_layouts = world.resource_mut::<Assets<TextureAtlasLayout>>();
let layout_handle = texture_atlas_layouts.add(layout);
let mut spritesheet_assets = world.resource_mut::<Assets<Spritesheet>>();
let spritesheet = spritesheet_assets.add(Spritesheet {
texture: texture.clone(),
layout: layout_handle.clone(),
});
CreaturesSpritesheet(spritesheet)
}
}