steel_registry/
pig_sound_variant.rs1use rustc_hash::FxHashMap;
2use simdnbt::ToNbtTag;
3use simdnbt::owned::{NbtCompound, NbtTag};
4use steel_utils::Identifier;
5use steel_utils::random::Random;
6
7use crate::RegistryExt;
8use crate::sound_event::SoundEventRef;
9
10#[derive(Debug)]
12pub struct PigSoundVariant {
13 pub key: Identifier,
14 pub adult_sounds: PigAge,
15 pub baby_sounds: PigAge,
16}
17#[derive(Debug)]
18pub struct PigAge {
19 pub ambient_sound: SoundEventRef,
20 pub death_sound: SoundEventRef,
21 pub hurt_sound: SoundEventRef,
22 pub eat_sound: SoundEventRef,
23 pub step_sound: SoundEventRef,
24}
25impl ToNbtTag for &PigAge {
26 fn to_nbt_tag(self) -> NbtTag {
27 let mut component = NbtCompound::new();
28 let s = self.ambient_sound.key.to_string();
29 component.insert("ambient_sound", s.as_str());
30 let s = self.death_sound.key.to_string();
31 component.insert("death_sound", s.as_str());
32 let s = self.hurt_sound.key.to_string();
33 component.insert("hurt_sound", s.as_str());
34 let s = self.step_sound.key.to_string();
35 component.insert("step_sound", s.as_str());
36 let s = self.eat_sound.key.to_string();
37 component.insert("eat_sound", s.as_str());
38 NbtTag::Compound(component)
39 }
40}
41
42impl ToNbtTag for &PigSoundVariant {
43 fn to_nbt_tag(self) -> NbtTag {
44 use simdnbt::owned::{NbtCompound, NbtTag};
45 let mut compound = NbtCompound::new();
46 compound.insert("adult_sounds", self.adult_sounds.to_nbt_tag());
47 compound.insert("baby_sounds", self.baby_sounds.to_nbt_tag());
48 NbtTag::Compound(compound)
49 }
50}
51
52pub type PigSoundVariantRef = &'static PigSoundVariant;
53
54pub struct PigSoundVariantRegistry {
55 pig_sound_variants_by_id: Vec<PigSoundVariantRef>,
56 pig_sound_variants_by_key: FxHashMap<Identifier, usize>,
57 allows_registering: bool,
58}
59
60impl PigSoundVariantRegistry {
61 #[must_use]
62 pub fn new() -> Self {
63 Self {
64 pig_sound_variants_by_id: Vec::new(),
65 pig_sound_variants_by_key: FxHashMap::default(),
66 allows_registering: true,
67 }
68 }
69
70 #[must_use]
71 pub fn pick_random(&self, random: &mut impl Random) -> Option<PigSoundVariantRef> {
72 let bound = i32::try_from(self.len()).ok()?;
73 if bound == 0 {
74 return None;
75 }
76
77 self.by_id(random.next_i32_bounded(bound) as usize)
78 }
79}
80
81crate::impl_standard_methods!(
82 PigSoundVariantRegistry,
83 PigSoundVariantRef,
84 pig_sound_variants_by_id,
85 pig_sound_variants_by_key,
86 allows_registering
87);
88
89crate::impl_registry!(
90 PigSoundVariantRegistry,
91 PigSoundVariant,
92 pig_sound_variants_by_id,
93 pig_sound_variants_by_key,
94 pig_sound_variants
95);