Skip to content

Commit 36d6cfc

Browse files
2 more sound effects
1 parent e8ef709 commit 36d6cfc

6 files changed

Lines changed: 55 additions & 22 deletions

File tree

docs/looks.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ All the valid effects that can be used are:
4242
- `"grayscale-averaged"`: Changes the grayscale effect of the sprite, using the average method.
4343
- `"grayscale-weighted"`: Changes the grayscale effect of the sprite, using the weighted method.
4444
- `"invert"`: Inverts the colors of the sprite.
45-
- `"multiply"`: Multiplies the colors of the sprite by the specified value.
45+
- `"multiply"`: Multiplies the RGB excluding transparency of the sprite by the specified value.
4646
- `"multiply-r"`: Multiplies the red channel of the sprite by the specified value.
4747
- `"multiply-g"`: Multiplies the green channel of the sprite by the specified value.
4848
- `"multiply-b"`: Multiplies the blue channel of the sprite by the specified value.
49-
- `"add"`: Adds the specified value to the colors of the sprite.
49+
- `"multiply-a"`: Multiplies the transparency channel of the sprite by the specified value.
50+
- `"add"`: Adds the specified value to the RGB excluding transparency of the sprite.
5051
- `"add-r"`: Adds the specified value to the red channel of the sprite.
5152
- `"add-g"`: Adds the specified value to the green channel of the sprite.
5253
- `"add-b"`: Adds the specified value to the blue channel of the sprite.
54+
- `"add-a"`: Adds the specified value to the transparency channel of the sprite.

docs/sound.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ The sound functions are used to play and control sounds in the project. They can
1111

1212
All the valid sound effects that can be used are:
1313

14-
- `"volume"`: Changes the volume of the sound.
14+
- `"volume"`: Changes the volume of the sound (0 - 100).
15+
- `"pitch"`: Changes the pitch of the sound (0 - 100).
16+
- `"pan"`: Changes the stereo panning of the sound (0 - 100, where 0 is left, 50 is center, and 100 is right).

src/utils/helpers.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,13 @@ pub fn draw_convex_polygon_lines(
421421
}
422422
}
423423

424+
pub fn percentage_to_decibels(percentage: f32) -> f32 {
425+
if percentage <= 0.0 {
426+
return f32::NEG_INFINITY;
427+
}
428+
20.0 * percentage.log10()
429+
}
430+
424431
// Helper functions that help other helper functions!!
425432
fn cubic_bezier(t: f32, p0: f32, p1: f32, p2: f32, p3: f32) -> f32 {
426433
let u = 1.0 - t;

src/utils/sprite/builtins/drawing.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub fn set_color(state: &mut State, args: &[Value]) -> Result {
1919

2020
pub fn change_r(state: &mut State, args: &[Value]) -> Result {
2121
if let [Value::Number(amount)] = args {
22-
state.sprite.draw_color.x = (state.sprite.draw_color.x + *amount / 255.0).clamp(0.0, 1.0);
22+
state.sprite.draw_color.x = (state.sprite.draw_color.x + *amount / 255.0).clamp(0.0, 1.0);
2323
Ok(Value::Null)
2424
} else {
2525
Err("change_r() requires a single number argument".to_string())
@@ -360,13 +360,16 @@ pub fn textured_tri(state: &State, args: &[Value]) -> Result {
360360
Value::List(ys),
361361
Value::List(us),
362362
Value::List(vs),
363-
] = args {
363+
] = args
364+
{
364365
if let [
365366
Value::Number(width),
366367
Value::Number(height),
367368
Value::List(pixels),
368-
] = parse_image_result.as_slice() {
369-
if xs.len() != ys.len() || xs.len() != us.len() || xs.len() != vs.len() || xs.len() != 3 {
369+
] = parse_image_result.as_slice()
370+
{
371+
if xs.len() != ys.len() || xs.len() != us.len() || xs.len() != vs.len() || xs.len() != 3
372+
{
370373
return Err(
371374
"textured_tri() requires three lists of equal length: x, y, u, v coordinates"
372375
.to_string(),
@@ -377,7 +380,12 @@ pub fn textured_tri(state: &State, args: &[Value]) -> Result {
377380
.chunks(4)
378381
.map(|c| {
379382
if c.len() == 4 {
380-
U8Vec4::new(c[0].to_number() as u8, c[1].to_number() as u8, c[2].to_number() as u8, c[3].to_number() as u8)
383+
U8Vec4::new(
384+
c[0].to_number() as u8,
385+
c[1].to_number() as u8,
386+
c[2].to_number() as u8,
387+
c[3].to_number() as u8,
388+
)
381389
} else {
382390
U8Vec4::new(0, 0, 0, 0)
383391
}
@@ -415,10 +423,14 @@ pub fn textured_tri(state: &State, args: &[Value]) -> Result {
415423
mesh.draw();
416424
Ok(Value::Null)
417425
} else {
418-
return Err("textured_tri() requires an image with width, height, and pixel data".to_string());
426+
return Err(
427+
"textured_tri() requires an image with width, height, and pixel data".to_string(),
428+
);
419429
}
420430
} else {
421-
return Err("textured_tri() requires an image and lists of x, y, u, v coordinates".to_string());
431+
return Err(
432+
"textured_tri() requires an image and lists of x, y, u, v coordinates".to_string(),
433+
);
422434
}
423435
}
424436

src/utils/sprite/builtins/sounds.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
1-
use kira::Tween;
1+
use indexmap::IndexMap;
2+
use kira::{Tween, sound::static_sound::StaticSoundHandle};
23

34
use crate::utils::{sprite::function::Result, *};
45

6+
fn update_sound_handle(sound_effects: &IndexMap<String, f32>, handle: &mut StaticSoundHandle) {
7+
for (effect, value) in sound_effects {
8+
match effect.as_str() {
9+
"volume" => handle.set_volume(percentage_to_decibels(*value / 100.0), Tween::default()),
10+
"pitch" => handle.set_playback_rate(*value as f64 / 100.0, Tween::default()),
11+
"pan" => handle.set_panning((*value / 100.0) * 2.0 - 1.0, Tween::default()),
12+
_ => continue,
13+
}
14+
}
15+
}
16+
517
pub fn play_sound(state: &mut State, args: &[Value]) -> Result {
618
fn play_sound_inner(state: &mut State, name: &str) -> Result {
719
if let Some(sound) = state.sprite.sounds.get(name) {
820
let mut handle = state
921
.audio_manager
1022
.play(sound.clone())
1123
.map_err(|e| e.to_string())?;
12-
handle.set_volume(
13-
state
14-
.sprite
15-
.sound_effects
16-
.get("volume")
17-
.cloned()
18-
.unwrap_or(1.0),
19-
Tween::default(),
20-
);
24+
update_sound_handle(&state.sprite.sound_effects, &mut handle);
2125
state.sprite.sound_handles.insert(name.to_string(), handle);
2226
Ok(Value::Null)
2327
} else {
@@ -64,6 +68,9 @@ pub fn change_sound_effect(state: &mut State, args: &[Value]) -> Result {
6468
.entry(effect.clone())
6569
.and_modify(|v| *v += *value)
6670
.or_insert(*value);
71+
for sound_handle in state.sprite.sound_handles.values_mut() {
72+
update_sound_handle(&state.sprite.sound_effects, sound_handle);
73+
}
6774
Ok(Value::Null)
6875
} else {
6976
Err("change_sound_effect() requires a string and a numeric argument".to_string())
@@ -73,6 +80,9 @@ pub fn change_sound_effect(state: &mut State, args: &[Value]) -> Result {
7380
pub fn set_sound_effect(state: &mut State, args: &[Value]) -> Result {
7481
if let [Value::String(effect), Value::Number(value)] = args {
7582
state.sprite.sound_effects.insert(effect.clone(), *value);
83+
for sound_handle in state.sprite.sound_handles.values_mut() {
84+
update_sound_handle(&state.sprite.sound_effects, sound_handle);
85+
}
7686
Ok(Value::Null)
7787
} else {
7888
Err("set_sound_effect() requires a string and a numeric argument".to_string())

src/utils/sprite/sprite.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub struct Sprite {
137137
pub layer: isize,
138138
pub variables: HashMap<String, Value>,
139139
pub effects: IndexMap<String, f32>,
140-
pub sound_effects: HashMap<String, f32>,
140+
pub sound_effects: IndexMap<String, f32>,
141141
pub draw_color: Vec4,
142142
pub functions: HashMap<String, Callable>,
143143
pub clone_id: Option<usize>,
@@ -345,7 +345,7 @@ impl Sprite {
345345
rotation_style: RotationStyle::AllAround,
346346
variables: HashMap::new(),
347347
effects: IndexMap::new(),
348-
sound_effects: HashMap::new(),
348+
sound_effects: IndexMap::new(),
349349
time_waiting: 0,
350350
dialogue: None,
351351
glide: None,

0 commit comments

Comments
 (0)