Compare commits
3 Commits
658709ebce
...
80fe2caff3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
80fe2caff3 | ||
|
|
2df86c9c76 | ||
|
|
5fa90f9e84 |
57
__init__.py
57
__init__.py
@@ -310,15 +310,29 @@ 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
|
# 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_update(x, player_pos, obj_pos):
|
def obj_update(x, player_pos, obj_pos):
|
||||||
if x is None:
|
if x is None:
|
||||||
return None
|
return None
|
||||||
@@ -395,8 +409,8 @@ def play_random_positional(sounds, soundName, playerX, objectX):
|
|||||||
channel.set_volume(volume * left, volume * right)
|
channel.set_volume(volume * left, volume * right)
|
||||||
return channel
|
return channel
|
||||||
|
|
||||||
def play_random_falling(sounds, soundName, playerX, objectX, startY, currentY=0, maxY=20):
|
def play_random_falling(sounds, soundName, playerX, objectX, startY, currentY=0, maxY=20, existingChannel=None):
|
||||||
"""Play a random sound with positional audio that increases in volume as it 'falls'.
|
"""Play or update a random sound with positional audio that increases in volume as it 'falls'.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
sounds: Dictionary of sound objects
|
sounds: Dictionary of sound objects
|
||||||
@@ -406,26 +420,41 @@ def play_random_falling(sounds, soundName, playerX, objectX, startY, currentY=0,
|
|||||||
startY: Starting Y position (0-20, higher = quieter start)
|
startY: Starting Y position (0-20, higher = quieter start)
|
||||||
currentY: Current Y position (0 = ground level)
|
currentY: Current Y position (0 = ground level)
|
||||||
maxY: Maximum Y value (default 20)
|
maxY: Maximum Y value (default 20)
|
||||||
|
existingChannel: Existing sound channel to update instead of creating new one (default None)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
The sound channel object for updating position/volume
|
The sound channel object for updating position/volume, or None if sound should stop
|
||||||
"""
|
"""
|
||||||
|
# Calculate horizontal positioning
|
||||||
|
volume, left, right = calculate_volume_and_pan(playerX, objectX)
|
||||||
|
|
||||||
|
# Calculate vertical fall volume multiplier (0 at maxY, 1 at y=0)
|
||||||
|
fallMultiplier = 1 - (currentY / maxY)
|
||||||
|
|
||||||
|
# Adjust final volumes
|
||||||
|
finalVolume = volume * fallMultiplier
|
||||||
|
finalLeft = left * finalVolume
|
||||||
|
finalRight = right * finalVolume
|
||||||
|
|
||||||
|
if existingChannel is not None:
|
||||||
|
if volume == 0: # Out of audible range
|
||||||
|
existingChannel.stop()
|
||||||
|
return None
|
||||||
|
existingChannel.set_volume(finalLeft, finalRight)
|
||||||
|
return existingChannel
|
||||||
|
else: # Need to create new channel
|
||||||
|
if volume == 0: # Don't start if out of range
|
||||||
|
return None
|
||||||
|
|
||||||
|
# Find matching sound files
|
||||||
keys = [k for k in sounds.keys() if k.startswith(soundName)]
|
keys = [k for k in sounds.keys() if k.startswith(soundName)]
|
||||||
if not keys:
|
if not keys:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
randomKey = random.choice(keys)
|
randomKey = random.choice(keys)
|
||||||
# Calculate horizontal positioning
|
|
||||||
volume, left, right = calculate_volume_and_pan(playerX, objectX)
|
|
||||||
if volume == 0:
|
|
||||||
return None
|
|
||||||
|
|
||||||
# Calculate vertical fall volume multiplier (0 at maxY, 1 at y=0)
|
|
||||||
fallMultiplier = 1 - (currentY / maxY)
|
|
||||||
|
|
||||||
channel = sounds[randomKey].play()
|
channel = sounds[randomKey].play()
|
||||||
if channel:
|
if channel:
|
||||||
channel.set_volume(volume * left * fallMultiplier, volume * right * fallMultiplier)
|
channel.set_volume(finalLeft, finalRight)
|
||||||
return channel
|
return channel
|
||||||
|
|
||||||
def instructions():
|
def instructions():
|
||||||
|
|||||||
Reference in New Issue
Block a user