use std::collections::HashMap;
use pixtuoid_core::sprite::{Frame, Rgb};
use pixtuoid_core::{AgentId, SceneState};
#[derive(Default)]
pub struct FrameCache {
entries: HashMap<(AgentId, &'static str, usize, bool, Option<Rgb>), Frame>,
}
impl FrameCache {
pub fn new() -> Self {
Self::default()
}
#[allow(clippy::too_many_arguments)]
pub fn get_or_make<F: FnOnce() -> Frame>(
&mut self,
agent_id: AgentId,
anim_name: &'static str,
frame_idx: usize,
flip_x: bool,
glow_tint: Option<Rgb>,
compute: F,
) -> &Frame {
self.entries
.entry((agent_id, anim_name, frame_idx, flip_x, glow_tint))
.or_insert_with(compute)
}
pub fn evict_missing(&mut self, scene: &SceneState) {
self.entries
.retain(|(id, _, _, _, _), _| scene.agents.contains_key(id));
}
pub fn len(&self) -> usize {
self.entries.len()
}
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
}