load BGM from config paths after loading folders to give config precedence

This commit is contained in:
frank 2022-01-29 17:56:52 -05:00
parent 285181d23b
commit 6693ca4514
1 changed files with 23 additions and 17 deletions

View File

@ -152,27 +152,22 @@ class Audio(Animation):
def play_sfx(self, name, loops=None, maxtime=None, fade_ms=None, position=None, x=None):
return self.sfx[name].play(loops, maxtime, fade_ms, position, x)
#
# Loading BGM procedure
#
# - load config file name/path definitions
# - check project specific bgm paths, load any that don't conflict
# - other paths can replace loaded paths through the audio panel and get
# written to config file
#
def load_bgm(self):
for name, bgm_definition in self.get_configuration("bgm").items():
bgm_definition_members = bgm_definition.split(self.CONFIG_SEPARATOR)
path, volume = bgm_definition_members[0], 1.0
for ii, member in enumerate(bgm_definition_members[1:]):
if ii == 0:
volume = float(member)
self.set_bgm(path, name, volume=volume)
"""
Loading BGM procedure:
- Check project specific BGM paths and load files found in those paths.
- Load config file name/path definitions, overwriting existing. This means the config file
definitions have precedence over the automatic loading of files placed in folders.
Further editing of BGM while the game is running can be done through the AudioPanel object.
"""
# First load BGM files found in the BGM path set in the configuration
for root in self.get_configuration("audio", "bgm-project-path"):
# look for path in resource folders
root = self.get_resource(root)
if os.path.exists(root):
print("checking {} for music".format(root))
print("checking {} for background music".format(root))
if os.path.isfile(root):
self.set_bgm(root)
else:
@ -183,10 +178,20 @@ class Audio(Animation):
if prefix:
prefix = re.sub("/", "_", prefix) + "_"
self.set_bgm(os.path.join(node, leaf), prefix=prefix)
# Next load BGM paths defined in the configuration. If any of these have the same name as
# BGM loaded by the previous code block, they will be overwritten to give the config file
# precedence over automatic BGM detection.
print("checking configuration for background music".format(root))
for name, bgm_definition in self.get_configuration("bgm").items():
bgm_definition_members = bgm_definition.split(self.CONFIG_SEPARATOR)
path, volume = bgm_definition_members[0], 1.0
for ii, member in enumerate(bgm_definition_members[1:]):
if ii == 0:
volume = float(member)
self.set_bgm(path, name, volume=volume)
def set_bgm(self, path, name=None, prefix="", volume=1.0):
path = self.get_resource(path)
print("setting {} music to {}".format(name, path))
try:
pygame.mixer.music.load(path)
except:
@ -194,6 +199,7 @@ class Audio(Animation):
return False
if name is None:
name = os.path.basename(path).split(".")[0]
print("setting {} background music to {}".format(name, path))
self.bgm[prefix + name] = BGM(self, path, volume)
if self.current_bgm is None:
self.current_bgm = self.bgm[prefix + name]