#[cfg(test)]
#[path = "tests/ambient_sounds.rs"]
mod tests;
use crate::entities::{Entity, EntityHandle};
use sdk_raw::ambient_sounds::AmbientGenericLayout;
use sdk_raw::util::cstr::copy_cstr;
use std::ffi::CString;
#[doc(alias("ambient_generic", "CAmbientGeneric"))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AmbientSound<'s> {
entity: Entity<'s>,
layout: AmbientGenericLayout,
}
impl<'s> AmbientSound<'s> {
pub fn new(entity: Entity<'s>) -> Option<Self> {
Some(Self {
entity,
layout: AmbientGenericLayout::find(entity.data_maps())?,
})
}
pub const fn entity(self) -> Entity<'s> {
self.entity
}
#[doc(alias("m_fLooping"))]
pub fn is_looping(self) -> bool {
unsafe { self.layout.looping(self.entity.as_ptr()).read() != 0 }
}
#[doc(alias("m_fActive"))]
pub fn is_playing(self) -> bool {
unsafe { self.layout.active(self.entity.as_ptr()).read() != 0 }
}
#[doc(alias("m_hSoundSource"))]
pub fn set_source(self, source: Option<Entity<'_>>) {
let handle = source.map_or(EntityHandle::INVALID, Entity::handle);
unsafe {
(&raw mut (*self.layout.sound_source(self.entity.as_ptr())).m_Index)
.write(handle.to_raw())
};
}
#[doc(alias("m_iszSound"))]
pub fn sound(self) -> Option<CString> {
unsafe { copy_cstr(self.layout.sound(self.entity.as_ptr()).read().pszValue) }
}
#[doc(alias("m_hSoundSource"))]
pub fn source(self) -> Option<EntityHandle> {
let handle = EntityHandle::from_raw(unsafe {
(&raw const (*self.layout.sound_source(self.entity.as_ptr())).m_Index).read()
});
handle.is_valid().then_some(handle)
}
}