Add the ability for obj_play to play a sound once.
This commit is contained in:
parent
5fa90f9e84
commit
2df86c9c76
31
__init__.py
31
__init__.py
@ -310,28 +310,31 @@ def calculate_volume_and_pan(player_pos, obj_pos):
|
||||
left = right = 1
|
||||
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)
|
||||
if volume == 0:
|
||||
return None # Don't play if out of range
|
||||
# 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
|
||||
x.set_volume(volume * left, volume * right)
|
||||
if x:
|
||||
x.set_volume(volume * left, volume * right)
|
||||
return x
|
||||
|
||||
def obj_stop(x):
|
||||
# Tries to stop a playing object channel
|
||||
"""Tries to stop a playing object channel"""
|
||||
try:
|
||||
x.stop()
|
||||
return None
|
||||
|
Loading…
x
Reference in New Issue
Block a user