forked from digiholic/universalSmashSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
musicManager.py
84 lines (73 loc) · 2.73 KB
/
musicManager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import pygame
import random
import settingsManager
import thread
"""
The Music Manager is an object that is meant to store the list of music
for a stage or menu with the chances and display name, as well as
rolling for those chances.
"""
music = None
def getMusicManager():
global music
if music == None:
music = musicManager()
return music
SONG_ENDED = pygame.USEREVENT + 616
pygame.mixer.music.set_endevent(SONG_ENDED)
class musicManager():
def __init__(self):
self.music_dict = {}
self.current_music = None
self.path_index = -1
def createMusicSet(self,setName,music_list):
self.music_dict[setName] = music_list
def getTotalChance(self,setName):
music_list = self.music_dict[setName]
total_chance = 0
for path, chance, name in music_list:
total_chance += chance
return total_chance
def rollMusic(self,setName):
music_list = self.music_dict[setName]
roll = random.randint(0,self.getTotalChance(setName))
print(roll, self.getTotalChance(setName))
for path, chance, name in music_list:
roll -= chance
if roll <= 0:
pygame.mixer.music.set_volume(settingsManager.getSetting('music_volume'))
self.current_music = (path,chance,name)
if isinstance(path, list):
self.path_index = 0
pygame.mixer.music.load(path[0])
pygame.mixer.music.play(0)
else:
self.path_index = -1
pygame.mixer.music.load(path)
pygame.mixer.music.play(-1)
return
def doMusicEvent(self):
if self.current_music != None and self.path_index > -1:
path = self.current_music[0]
if isinstance(path, list):
if pygame.event.peek(SONG_ENDED):
self.advanceSong()
def advanceSong(self):
path = self.current_music[0]
self.path_index = self.path_index + 1
if self.path_index == len(path):
if path[self.path_index] != None:
pygame.mixer.music.load(path[self.path_index])
pygame.mixer.music.play(-1)
else:
self.path_index = -1
else:
pygame.mixer.music.load(path[self.path_index])
pygame.mixer.music.play()
def stopMusic(self,_time=0):
if self.current_music != None:
pygame.mixer.music.fadeout(_time)
self.current_music = None
self.path_index = -1
def isPlaying(self):
return pygame.mixer.music.get_busy() or (self.current_music != None and self.path_index > -1)