use nightshade::prelude::*;
pub fn load_sound(world: &mut World, name: &str, bytes: &[u8]) {
match nightshade::ecs::audio::systems::load_sound_from_cursor(bytes.to_vec()) {
Ok(data) => audio_engine_load_sound(&mut world.resources.audio, name, data),
Err(error) => tracing::error!("Failed to load sound {name}: {error}"),
}
}
pub fn play_sound(world: &mut World, name: &str) -> Entity {
let entity = spawn_entities(world, AUDIO_SOURCE, 1)[0];
world
.core
.set_audio_source(entity, AudioSource::new(name).playing());
entity
}
pub fn play_sound_looping(world: &mut World, name: &str) -> Entity {
let entity = spawn_entities(world, AUDIO_SOURCE, 1)[0];
world
.core
.set_audio_source(entity, AudioSource::new(name).with_looping(true).playing());
entity
}
pub fn set_volume(world: &mut World, entity: Entity, volume: f32) {
if let Some(source) = world.core.get_audio_source_mut(entity) {
source.volume = volume;
}
}
pub fn play_sound_at(world: &mut World, name: &str, position: Vec3) -> Entity {
let entity = spawn_entities(
world,
AUDIO_SOURCE | LOCAL_TRANSFORM | LOCAL_TRANSFORM_DIRTY | GLOBAL_TRANSFORM,
1,
)[0];
assign_local_transform(
world,
entity,
LocalTransform {
translation: position,
..Default::default()
},
);
world
.core
.set_audio_source(entity, AudioSource::new(name).with_spatial(true).playing());
entity
}