| 1 | //! Built-in art adapter over the shared avatar-pack contract. |
| 2 | //! No separate action vocabulary or animation clock lives here. |
| 3 | use crate::avatar::{Frame, Pack}; |
| 4 | use std::sync::OnceLock; |
| 5 | pub const TERMINAL: &[u8] = include_bytes!("terminal.rgba"); |
| 6 | pub const TILE: usize = 96; |
| 7 | pub const FRAMES: usize = 36; |
| 8 | pub fn pack() -> &'static Pack { |
| 9 | static PACK: OnceLock<Pack> = OnceLock::new(); |
| 10 | PACK.get_or_init(|| { |
| 11 | Pack::parse(include_bytes!("avatar.json")).expect("validated built-in avatar") |
| 12 | }) |
| 13 | } |
| 14 | pub fn sample( |
| 15 | act: &str, |
| 16 | frame: f64, |
| 17 | reduced: bool, |
| 18 | view: Option<&str>, |
| 19 | action: Option<&str>, |
| 20 | ) -> Frame { |
| 21 | pack().sample(act, frame, reduced, view, action) |
| 22 | } |
| 23 | pub fn pixel(index: usize, x: usize, y: usize) -> [u8; 4] { |
| 24 | if index >= FRAMES || x >= TILE || y >= TILE { |
| 25 | return [0; 4]; |
| 26 | } |
| 27 | let at = ((index * TILE + y) * TILE + x) * 4; |
| 28 | TERMINAL[at..at + 4].try_into().expect("four channels") |
| 29 | } |
| 30 | |
| 31 | #[cfg(test)] |
| 32 | mod tests { |
| 33 | use super::*; |
| 34 | #[test] |
| 35 | fn girl_has_distinct_frames_for_all_native_actions_and_authored_views() { |
| 36 | assert_eq!(TERMINAL.len(), FRAMES * TILE * TILE * 4); |
| 37 | for act in crate::avatar::ACTS { |
| 38 | let clip = &pack().actions[&pack().states[act]]; |
| 39 | assert_eq!(clip.frames.len(), 2, "{act} has two authored poses"); |
| 40 | let tile = |index: u16| { |
| 41 | &TERMINAL[index as usize * TILE * TILE * 4..(index as usize + 1) * TILE * TILE * 4] |
| 42 | }; |
| 43 | assert_ne!( |
| 44 | tile(clip.frames[0]), |
| 45 | tile(clip.frames[1]), |
| 46 | "{act} poses differ" |
| 47 | ); |
| 48 | for index in &clip.frames { |
| 49 | let pixels = tile(*index); |
| 50 | assert!( |
| 51 | pixels.as_chunks::<4>().0.iter().any(|p| p[3] > 200), |
| 52 | "{act} paints" |
| 53 | ); |
| 54 | assert!( |
| 55 | pixels.as_chunks::<4>().0.iter().any(|p| p[3] == 0), |
| 56 | "{act} is transparent" |
| 57 | ); |
| 58 | } |
| 59 | assert_eq!( |
| 60 | sample(act, 10000., true, None, None).index, |
| 61 | clip.poster as usize |
| 62 | ); |
| 63 | } |
| 64 | assert_ne!( |
| 65 | sample("rest", 0., false, Some("side"), None), |
| 66 | sample("rest", 0., false, Some("back"), None) |
| 67 | ); |
| 68 | } |
| 69 | } |
| 70 |