1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use super::*;
use crate::config::Config;
pub(crate) fn plugin(_app: &mut App) {}
#[derive(Clone, Asset, Debug, Reflect)]
pub struct Pico8Asset {
#[cfg(feature = "scripting")]
pub scripts: Vec<Handle<bevy_mod_scripting::asset::ScriptAsset>>,
// this palette is given away and not actually used here.
pub(crate) palettes: Palettes,
pub(crate) border: Handle<Image>,
pub(crate) sprite_sheets: Vec<Handle<SpriteSheet>>,
pub(crate) maps: Vec<SpriteMap>,
pub(crate) font: Vec<N9Font>,
pub(crate) audio_banks: Vec<Handle<AudioBank>>,
pub(crate) meshes: Vec<MeshHandle>,
pub(crate) config: Handle<Config>,
}
#[derive(Clone, Debug, Reflect)]
pub struct N9Font {
pub handle: Handle<Font>,
}
impl Pico8Asset {
pub fn sprite_map(&self, map_index: Option<usize>) -> Result<&SpriteMap, Error> {
let index = map_index.unwrap_or(0);
self.maps
.get(index)
.ok_or(Error::NoSuch(format!("map index {index}").into()))
}
// pub fn get_pal(&self, palette_index: usize) -> Result<&Palette, PalError> {
// self.palettes.get(palette_index).ok_or(PalError::NoSuchPalette {
// index: palette_index,
// count: self.palettes.len(),
// })
// }
// // Resolve a PColor into a Color.
// pub fn get_color(&self, c: PColor, palette_index: usize) -> Result<Color, PalError> {
// match c {
// PColor::Palette(n) => self
// .get_pal(palette_index)?
// .get_color(n)
// .map(|c| c.into())
// .map_err(|e| match e {
// PalError::NoSuchColor(c) => PalError::NoSuchPaletteColor {
// color: c,
// palette: palette_index,
// },
// x => x,
// }),
// PColor::Color(c) => Ok(c.into()),
// }
// }
// pub fn sprite_map_mut(&mut self, map_index: Option<usize>) -> Result<&mut SpriteMap, Error> {
// let index = map_index.unwrap_or(0);
// self.maps
// .get_mut(index)
// .ok_or(Error::NoSuch(format!("map index {index}").into()))
// }
}
impl FromWorld for Pico8Asset {
fn from_world(world: &mut World) -> Self {
let config_handle = {
let mut configs = world.resource_mut::<Assets<Config>>();
configs.add(Config::default())
};
let palettes = {
let mut images = world.resource_mut::<Assets<Image>>();
let strip = pico8::pal::strip_image_from_data(&pico8::PALETTE);
let palette_handle = images.add(strip);
vec![Palette {
image: palette_handle,
access: pico8::pal::PaletteAccess::default(),
}]
.into()
};
let asset_server = world.resource::<AssetServer>();
Pico8Asset {
#[cfg(feature = "scripting")]
scripts: vec![],
palettes,
border: asset_server
.load_with_settings(crate::config::pico8::BORDER, pixel_art_settings),
font: vec![N9Font {
handle: asset_server.load(crate::config::pico8::FONT),
}],
audio_banks: Vec::new(),
sprite_sheets: Vec::new(),
maps: Vec::new(),
meshes: Vec::new(),
config: config_handle,
}
}
}