Add the ability for obj_play to play a sound once.

This commit is contained in:
Storm Dragon 2025-02-03 23:49:08 -05:00
parent 5fa90f9e84
commit 2df86c9c76

View File

@ -310,28 +310,31 @@ def calculate_volume_and_pan(player_pos, obj_pos):
left = right = 1 left = right = 1
return volume, left, right return volume, left, right
def obj_play(sounds, soundName, player_pos, obj_pos): def obj_play(sounds, soundName, player_pos, obj_pos, loop=True):
"""Play a sound with positional audio.
Args:
sounds: Dictionary of sound objects
soundName: Name of sound to play
player_pos: Player's position for audio panning
obj_pos: Object's position for audio panning
loop: Whether to loop the sound (default True)
Returns:
The sound channel object, or None if out of range
"""
volume, left, right = calculate_volume_and_pan(player_pos, obj_pos) volume, left, right = calculate_volume_and_pan(player_pos, obj_pos)
if volume == 0: if volume == 0:
return None # Don't play if out of range return None # Don't play if out of range
# Play the sound on a new channel # Play the sound on a new channel
x = sounds[soundName].play(-1) x = sounds[soundName].play(-1 if loop else 0) # -1 for loop, 0 for once
# Apply the volume and pan
x.set_volume(volume * left, volume * right)
return x
def obj_update(x, player_pos, obj_pos):
if x is None:
return None
volume, left, right = calculate_volume_and_pan(player_pos, obj_pos)
if volume == 0:
x.stop()
return None
# Apply the volume and pan # Apply the volume and pan
if x:
x.set_volume(volume * left, volume * right) x.set_volume(volume * left, volume * right)
return x return x
def obj_stop(x): def obj_stop(x):
# Tries to stop a playing object channel """Tries to stop a playing object channel"""
try: try:
x.stop() x.stop()
return None return None