ya2 · news · projects · code · about

fixed the restart of the music
[pmachines.git] / game / music.py
1 from os.path import dirname, exists
2 from platform import system
3 from glob import glob
4 from pathlib import Path
5 from random import choice
6 from logging import info
7 from panda3d.core import AudioSound, Filename
8
9
10 class MusicMgr:
11
12 def __init__(self, volume):
13 files = self.curr_path + 'assets/audio/music/*.ogg'
14 self._start_music(glob(files))
15 base.musicManager.setVolume(.8 * volume)
16 base.sfxManagerList[0].setVolume(volume)
17 taskMgr.add(self._on_frame, 'on frame music')
18
19 @property
20 def is_appimage(self):
21 par_path = str(Path(__file__).parent.absolute())
22 is_appimage = par_path.startswith('/tmp/.mount_Pmachi')
23 return is_appimage and par_path.endswith('/usr/bin')
24
25 @property
26 def curr_path(self):
27 if system() == 'Windows':
28 return ''
29 if exists('main.py'):
30 return ''
31 else:
32 par_path = str(Path(__file__).parent.absolute())
33 if self.is_appimage:
34 par_path = str(Path(par_path).absolute())
35 par_path += '/'
36 return par_path
37
38 def _start_music(self, files):
39 self._music = loader.load_music(choice(files))
40 info('playing music ' + self._music.get_name())
41 self._music.play()
42
43 def set_volume(self, volume):
44 base.musicManager.setVolume(.8 * volume)
45 base.sfxManagerList[0].setVolume(volume)
46
47 def _on_frame(self, task):
48 if self._music.status() == AudioSound.READY:
49 files = glob(self.curr_path + 'assets/audio/music/*.ogg')
50 files.remove(self.curr_path + 'assets/audio/music/' + self._music.get_name())
51 self._start_music(files)
52 return task.cont