diff --git a/__init__.py b/__init__.py index 16a4b4c..901d315 100755 --- a/__init__.py +++ b/__init__.py @@ -173,42 +173,47 @@ def cut_scene(sounds, soundName): pygame.mixer.stop() pygame.event.pump() -def obj_play(sounds, sound_name, player_pos, obj_pos): - distance = player_pos - obj_pos - if abs(distance) > 9: +def obj_play(sounds, soundName, playerPos, objPos): + distance = playerPos - objPos + if distance > 9 or 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: - # 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) + 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) # Apply the position information to the channel - 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: + 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: left = 0 right = 0 + elif distance == 0: + left = 0.9 + right = 0.9 else: - # 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) + 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 - channel.set_volume(left, right) - # Return the channel - return channel - -soundData + x.set_volume(left, right) + # return the channel + return x + def obj_stop(x): # Tries to stop a playing object channel try: