sprites.rs 1.43 KB
Newer Older
Simon Goller's avatar
Simon Goller committed
1
use crate::resources;
Simon Goller's avatar
Simon Goller committed
2
3
4
5
use crate::svg_loader;

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Sprite {
Simon Goller's avatar
Simon Goller committed
6
    Player,
7
8
    BottleDropPointFull,
    BottleDropPointEmpty,
Simon Goller's avatar
Simon Goller committed
9
10
11
12
13
14
}

impl Sprite {
    pub fn filename(&self) -> &'static str {
        match *self {
            Sprite::Player => "player.svg",
15
16
            Sprite::BottleDropPointFull => "droppoint-full.svg",
            Sprite::BottleDropPointEmpty => "droppoint-empty.svg",
Simon Goller's avatar
Simon Goller committed
17
18
19
20
        }
    }

    pub async fn load_and_register(&self, renderer: &mut resources::Rendering) {
21
22
23
24
        let image_handle: resources::ImageHandle = (*self).into();
        if renderer.has_image(&image_handle) {
            return;
        }
Simon Goller's avatar
Simon Goller committed
25
        let filename = self.filename();
Rahix's avatar
Rahix committed
26
        let image_url = format!("resources/sprites/{}", filename);
Simon Goller's avatar
Simon Goller committed
27
        let image = svg_loader::load_svg_as_image(&image_url).await;
28
29
30
        renderer
            .register_image_on_handle(image, &image_handle)
            .expect("ImageHandle was registered twice - this should not happen");
Simon Goller's avatar
Simon Goller committed
31
    }
32
33

    pub async fn load_and_register_all(renderer: &mut resources::Rendering) {
34
35
36
37
38
        for sprite in &[
            Sprite::Player,
            Sprite::BottleDropPointFull,
            Sprite::BottleDropPointEmpty,
        ] {
39
40
41
            sprite.load_and_register(renderer).await;
        }
    }
Simon Goller's avatar
Simon Goller committed
42
43
44
45
}

impl Into<resources::ImageHandle> for Sprite {
    fn into(self) -> resources::ImageHandle {
46
        resources::ImageHandle(self as usize)
Simon Goller's avatar
Simon Goller committed
47
48
    }
}