A new try at sound panning with walking.

This commit is contained in:
Storm Dragon 2024-06-20 02:03:06 -04:00
parent 155ed6ec39
commit 58ab5aa854

View File

@ -173,47 +173,42 @@ def cut_scene(sounds, soundName):
pygame.mixer.stop()
pygame.event.pump()
def obj_play(sounds, soundName, playerPos, objPos):
distance = playerPos - objPos
if distance > 9 or distance < -9:
def obj_play(sounds, sound_name, player_pos, obj_pos):
distance = player_pos - obj_pos
if abs(distance) > 9:
# The item is out of range, so play it at 0
left = 0
right = 0
elif distance == 0:
left = 0.9
right = 0.9
else:
angle = math.radians(distance * 5)
left = math.sqrt(2)/2.0 * (math.cos(angle) + math.sin(angle))
right = math.sqrt(2)/2.0 * (math.cos(angle) - math.sin(angle))
if left < 0: left *= -1
if right < 0: right *= -1
# x is the channel for the sound
x = sounds[soundName].play(-1)
# Calculate the panning based on the distance
left = max(0, 1 - (distance / 9))
right = max(0, 1 + (distance / 9))
left = min(1, left)
right = min(1, right)
# Play the sound on a new channel
channel = sounds[sound_name].play(-1)
# Apply the position information to the channel
x.set_volume(left, right)
# return the channel so that it can be used in the update and stop functions.
return x
def obj_update(x, playerPos, objPos):
distance = playerPos - objPos
if distance > 9 or distance < -9:
channel.set_volume(left, right)
# Return the channel so that it can be used in the update and stop functions.
return channel
def obj_update(channel, player_pos, obj_pos):
distance = player_pos - obj_pos
if abs(distance) > 9:
left = 0
right = 0
elif distance == 0:
left = 0.9
right = 0.9
else:
angle = math.radians(distance * 5)
left = math.sqrt(2)/2.0 * (math.cos(angle) + math.sin(angle))
right = math.sqrt(2)/2.0 * (math.cos(angle) - math.sin(angle))
if left < 0: left *= -1
if right < 0: right *= -1
# Calculate the panning based on the distance
left = max(0, 1 - (distance / 9))
right = max(0, 1 + (distance / 9))
left = min(1, left)
right = min(1, right)
# Apply the position information to the channel
x.set_volume(left, right)
# return the channel
return x
channel.set_volume(left, right)
# Return the channel
return channel
soundData
def obj_stop(x):
# Tries to stop a playing object channel
try: