Improved sound while walking on left/right only.
This commit is contained in:
parent
0ef11785ec
commit
0c73e98876
73
__init__.py
73
__init__.py
@ -173,45 +173,46 @@ def cut_scene(sounds, soundName):
|
|||||||
pygame.mixer.stop()
|
pygame.mixer.stop()
|
||||||
pygame.event.pump()
|
pygame.event.pump()
|
||||||
|
|
||||||
def obj_play(sounds, soundName, playerPos, objPos):
|
|
||||||
distance = playerPos - objPos
|
def calculate_volume_and_pan(player_pos, obj_pos):
|
||||||
if distance > 9 or distance < -9:
|
distance = abs(player_pos - obj_pos)
|
||||||
# The item is out of range, so play it at 0
|
max_distance = 12 # Maximum audible distance
|
||||||
left = 0
|
if distance > max_distance:
|
||||||
right = 0
|
return 0, 0, 0 # No sound if out of range
|
||||||
elif distance == 0:
|
# Calculate volume (non-linear scaling for more noticeable changes)
|
||||||
left = 0.9
|
volume = ((max_distance - distance) / max_distance) ** 1.5
|
||||||
right = 0.9
|
# Determine left/right based on relative position
|
||||||
|
if player_pos < obj_pos:
|
||||||
|
# Object is to the right
|
||||||
|
left = max(0, 1 - (obj_pos - player_pos) / max_distance)
|
||||||
|
right = 1
|
||||||
|
elif player_pos > obj_pos:
|
||||||
|
# Object is to the left
|
||||||
|
left = 1
|
||||||
|
right = max(0, 1 - (player_pos - obj_pos) / max_distance)
|
||||||
else:
|
else:
|
||||||
angle = math.radians(distance * 5)
|
# Player is on the object
|
||||||
left = math.sqrt(2)/2.0 * (math.cos(angle) + math.sin(angle))
|
left = right = 1
|
||||||
right = math.sqrt(2)/2.0 * (math.cos(angle) - math.sin(angle))
|
return volume, left, right
|
||||||
if left < 0: left *= -1
|
|
||||||
if right < 0: right *= -1
|
def obj_play(sounds, soundName, player_pos, obj_pos):
|
||||||
# x is the channel for the sound
|
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)
|
||||||
# Apply the position information to the channel
|
# Apply the volume and pan
|
||||||
x.set_volume(left, right)
|
x.set_volume(volume * left, volume * right)
|
||||||
# return the channel so that it can be used in the update and stop functions.
|
|
||||||
return x
|
return x
|
||||||
|
def obj_update(x, player_pos, obj_pos):
|
||||||
def obj_update(x, playerPos, objPos):
|
if x is None:
|
||||||
distance = playerPos - objPos
|
return None
|
||||||
if distance > 9 or distance < -9:
|
volume, left, right = calculate_volume_and_pan(player_pos, obj_pos)
|
||||||
left = 0
|
if volume == 0:
|
||||||
right = 0
|
x.stop()
|
||||||
elif distance == 0:
|
return None
|
||||||
left = 0.9
|
# Apply the volume and pan
|
||||||
right = 0.9
|
x.set_volume(volume * left, volume * right)
|
||||||
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
|
|
||||||
# Apply the position information to the channel
|
|
||||||
x.set_volume(left, right)
|
|
||||||
# return the channel
|
|
||||||
return x
|
return x
|
||||||
|
|
||||||
def obj_stop(x):
|
def obj_stop(x):
|
||||||
|
Loading…
Reference in New Issue
Block a user