use crate::palette::Rgb;
#[derive(Clone, Copy, Debug)]
pub struct PixelMap {
pub width: u32,
pub height: u32,
pub rows: &'static [&'static str],
pub colors: &'static [(char, Rgb)],
}
impl PixelMap {
pub fn pixel(&self, x: u32, y: u32) -> Option<Rgb> {
let ch = self.rows[y as usize].as_bytes()[x as usize] as char;
if ch == '.' {
return None;
}
self.colors
.iter()
.find(|(c, _)| *c == ch)
.map(|(_, rgb)| *rgb)
}
pub fn palette(&self) -> impl Iterator<Item = Rgb> + '_ {
self.colors.iter().map(|(_, rgb)| *rgb)
}
}
const CLAWD_BODY: Rgb = Rgb::hex(0xD9785C);
const CLAWD_EYES: Rgb = Rgb::hex(0x261A16);
pub const CLAWD_PLACEHOLDER: PixelMap = PixelMap {
width: 15,
height: 9,
rows: &[
"..XXXXXXXXXXX..",
"..XXXXXXXXXXX..",
"..XXEXXXXXEXX..",
"XXXXEXXXXXEXXXX",
"XXXXXXXXXXXXXXX",
"..XXXXXXXXXXX..",
"..XXXXXXXXXXX..",
"..X.X.....X.X..",
"..X.X.....X.X..",
],
colors: &[('X', CLAWD_BODY), ('E', CLAWD_EYES)],
};
const FIRE_ORANGE: Rgb = Rgb::hex(0xE86028);
const FIRE_YELLOW: Rgb = Rgb::hex(0xFFAA3C);
const FIRE_WHITE: Rgb = Rgb::hex(0xFFECAA);
const LOG: Rgb = Rgb::hex(0x784E30);
const LOG_DARK: Rgb = Rgb::hex(0x462E1E);
pub const CAMPFIRE: PixelMap = PixelMap {
width: 9,
height: 11,
rows: &[
"....Y....",
"...OY....",
"..OOYO...",
"..OYYO...",
".OOYYYO..",
".OYYWYYO.",
"OOYYWWYOO",
"OYYWWWYYO",
".OYYYYYO.",
"BBBBBBBBB",
"KKKKKKKKK",
],
colors: &[
('O', FIRE_ORANGE),
('Y', FIRE_YELLOW),
('W', FIRE_WHITE),
('B', LOG),
('K', LOG_DARK),
],
};
pub const LOG_SEAT: PixelMap = PixelMap {
width: 13,
height: 4,
rows: &[
".BBBBBBBBBBB.",
"KBBBBBBBBBBBB",
"BBBBBBBBBBBKB",
".KKKKKKKKKKK.",
],
colors: &[('B', LOG), ('K', LOG_DARK)],
};
const SLATE: Rgb = Rgb::hex(0x5E6B7A);
const SLATE_DARK: Rgb = Rgb::hex(0x3B4450);
const BRASS: Rgb = Rgb::hex(0xC9A45C);
const METAL: Rgb = Rgb::hex(0x3A3330);
pub const TELESCOPE: PixelMap = PixelMap {
width: 12,
height: 10,
rows: &[
".........NN.",
".......TTNN.",
".....TTTT...",
"...TTTT.....",
"..TTT.......",
"....H.......",
"...L.L......",
"..L...L.....",
".L.....L....",
"L.......L...",
],
colors: &[('T', SLATE), ('N', SLATE_DARK), ('H', BRASS), ('L', LOG)],
};
pub const LANTERN: PixelMap = PixelMap {
width: 5,
height: 7,
rows: &[
"..M..", ".M.M.", "MMMMM", "MYWYM", "MYWYM", "MYYYM", "MMMMM",
],
colors: &[('M', METAL), ('Y', FIRE_YELLOW), ('W', FIRE_WHITE)],
};
const SPARK: Rgb = Rgb::hex(0xFFE9A0);
const PAGE: Rgb = Rgb::hex(0xEEEAE0);
const PAGE_LINE: Rgb = Rgb::hex(0xC8C2B0);
const COVER: Rgb = Rgb::hex(0x7A2A4A);
const WOOD: Rgb = Rgb::hex(0x7A5234);
const WOOD_DARK: Rgb = Rgb::hex(0x4E3422);
const STEEL: Rgb = Rgb::hex(0x5E6B7A);
const STEEL_DARK: Rgb = Rgb::hex(0x3B4450);
const HAT: Rgb = Rgb::hex(0x5B4B8A);
const HAT_DARK: Rgb = Rgb::hex(0x413566);
pub const SPELLBOOK: PixelMap = PixelMap {
width: 11,
height: 9,
rows: &[
"..s.....s..",
"....s.s....",
"PPPPP.PPPPP",
"PlllPBPlllP",
"PlllPBPlllP",
"RRRRRBRRRRR",
"....WWW....",
"....WWW....",
"...WWWWW...",
],
colors: &[
('s', SPARK),
('P', PAGE),
('l', PAGE_LINE),
('B', WOOD_DARK),
('R', COVER),
('W', WOOD),
],
};
pub const CAMP_STOVE: PixelMap = PixelMap {
width: 9,
height: 8,
rows: &[
"...MM....",
"..M..M...",
".KKKKKKS.",
".KKKKKK.S",
".KKKKKK..",
"OYOYOYO..",
"GGGGGGG..",
"G.G.G.G..",
],
colors: &[
('M', STEEL_DARK),
('K', STEEL),
('S', STEEL),
('O', FIRE_ORANGE),
('Y', FIRE_YELLOW),
('G', METAL),
],
};
pub const COFFEE_MUG: PixelMap = PixelMap {
width: 5,
height: 4,
rows: &["WCCW.", "WWWWW", "WWWW.", "WWWW."],
colors: &[('W', PAGE), ('C', WOOD_DARK)],
};
pub const FALLEN_HAT: PixelMap = PixelMap {
width: 12,
height: 6,
rows: &[
"......PP....",
"....PPPPP...",
"..PPPPPPPPS.",
"DPPPPPPPPPP.",
"DDDDDDDDDDDD",
"www.wwwwwww.",
],
colors: &[('P', HAT), ('D', HAT_DARK), ('S', SPARK), ('w', WOOD)],
};
const INK_DARK: Rgb = Rgb::hex(0x2A2624);
pub const BOOK_CLOSED: PixelMap = PixelMap {
width: 7,
height: 3,
rows: &["RRRRRRR", "PPPPPPR", "RRRRRRR"],
colors: &[('R', COVER), ('P', PAGE)],
};
pub const GUITAR_REST: PixelMap = PixelMap {
width: 7,
height: 12,
rows: &[
"...KK..", "...d...", "...d...", "...d...", "...d...", "..WWW..", ".WWWWW.", ".WWKWW.",
"..WWW..", ".WWWWW.", ".WWWWW.", "..WWW..",
],
colors: &[('K', INK_DARK), ('d', WOOD_DARK), ('W', WOOD)],
};
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn maps_are_rectangular_and_fully_coloured() {
for map in [
CLAWD_PLACEHOLDER,
CAMPFIRE,
LOG_SEAT,
TELESCOPE,
LANTERN,
SPELLBOOK,
CAMP_STOVE,
COFFEE_MUG,
FALLEN_HAT,
] {
assert_eq!(map.rows.len() as u32, map.height);
for row in map.rows {
assert_eq!(row.len() as u32, map.width);
for ch in row.chars() {
assert!(
ch == '.' || map.colors.iter().any(|(c, _)| *c == ch),
"{ch}"
);
}
}
}
}
}