Skip to main content

steel_registry/
mob_effect.rs

1use rustc_hash::FxHashMap;
2use steel_utils::Identifier;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub enum MobEffectCategory {
6    Beneficial,
7    Harmful,
8    Neutral,
9}
10
11#[derive(Debug, PartialEq, Eq, Hash)]
12pub struct MobEffect {
13    pub key: Identifier,
14    pub category: MobEffectCategory,
15    pub color: i32,
16}
17
18pub type MobEffectRef = &'static MobEffect;
19
20pub struct MobEffectRegistry {
21    effects_by_id: Vec<MobEffectRef>,
22    effects_by_key: FxHashMap<Identifier, usize>,
23    allows_registering: bool,
24}
25
26impl Default for MobEffectRegistry {
27    fn default() -> Self {
28        Self::new()
29    }
30}
31
32impl MobEffectRegistry {
33    #[must_use]
34    pub fn new() -> Self {
35        Self {
36            effects_by_id: Vec::new(),
37            effects_by_key: FxHashMap::default(),
38            allows_registering: true,
39        }
40    }
41
42    pub fn register(&mut self, effect: MobEffectRef) {
43        assert!(
44            self.allows_registering,
45            "Cannot register mob effects after the registry has been frozen"
46        );
47        let idx = self.effects_by_id.len();
48        self.effects_by_key.insert(effect.key.clone(), idx);
49        self.effects_by_id.push(effect);
50    }
51
52    pub fn iter(&self) -> impl Iterator<Item = (usize, MobEffectRef)> + '_ {
53        self.effects_by_id
54            .iter()
55            .enumerate()
56            .map(|(id, &effect)| (id, effect))
57    }
58}
59
60crate::impl_registry!(
61    MobEffectRegistry,
62    MobEffect,
63    effects_by_id,
64    effects_by_key,
65    mob_effects
66);