#specs #sprite

specs_sprite

sprite component for specs

17 releases

0.4.3 Nov 24, 2018
0.4.2 Jul 8, 2018
0.4.0 Jun 29, 2018
0.3.4 Jan 22, 2018
0.3.0 Nov 27, 2017

#17 in Rendering

Download history 32/week @ 2018-11-10 17/week @ 2018-11-17 142/week @ 2018-11-24 97/week @ 2018-12-01 34/week @ 2018-12-08 2/week @ 2018-12-15 51/week @ 2018-12-22 3/week @ 2018-12-29 35/week @ 2019-01-05 4/week @ 2019-01-12 1/week @ 2019-01-19 2/week @ 2019-01-26 17/week @ 2019-02-16

234 downloads per month
Used in 1 crate

MIT license

15KB
408 lines

specs_sprite

sprite component for specs

extern crate specs_sprite;
extern crate specs_guided_join;
extern crate specs_bundler;
extern crate specs;

use specs_sprite::{Sprite, SpriteGuide, SpriteBundle};
use specs_guided_join::GuidedJoin;
use specs_bundler::SpecsBundler;
use specs::{Builder, Join, World, DispatcherBuilder};

fn main() {
    let mut world = World::new();

    let mut dispatcher = SpecsBundler::new(&mut world, DispatcherBuilder::new())
        .bundle(SpriteBundle::<f32, usize>::default()).unwrap()
        .build();

    // just using an unsigned integer for the image
    let _ = world
        .create_entity()
        .with(Sprite::<f32, usize>::new(0).with_layer_and_z(1, 1))
        .build();

    let _ = world
        .create_entity()
        .with(Sprite::<f32, usize>::new(1).with_layer_and_z(0, 0))
        .build();

    let _ = world
        .create_entity()
        .with(Sprite::<f32, usize>::new(2).with_layer_and_z(1, 0))
        .build();

    dispatcher.dispatch(&mut world.res);

    let read_sprites = world.read::<Sprite<f32, usize>>();
    let read_guide = world.read_resource::<SpriteGuide<f32>>();

    let sprites: Vec<&Sprite<f32, usize>> = (&read_sprites).join().collect();
    let ordered_sprites: Vec<&Sprite<f32, usize>> = (&read_sprites).guided_join(read_guide.as_slice()).collect();

    assert_eq!(sprites.iter().map(|s| s.image).collect::<Vec<usize>>(), [0, 1, 2].to_vec());
    assert_eq!(ordered_sprites.iter().map(|s| s.image).collect::<Vec<usize>>(), [1, 2, 0].to_vec());
}

Dependencies

~4.5MB
~79K SLoC