Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
MusicManager.cs
浏览该文件的文档.
1using Engine;
2using Engine.Audio;
3using Engine.Media;
4
5namespace Game {
6 public static class MusicManager {
7 public enum Mix {
8 None, //没有正在播放的音乐,停止音乐
9 Menu, //主菜单音乐
10 InGame, //游戏游玩时的音乐,由模组自定义(API1.72新增)
11 Other //其他,由模组自己来定义(API1.72新增)
12 }
13
14 public static float m_fadeSpeed = 0.33f;
15
16 public static float m_fadeWait = 2f;
17
19
20 public static StreamingSound m_sound;
21
22 public static double m_fadeStartTime;
23
24 public static Mix m_currentMix;
25
26 public static double m_nextSongTime;
27
28 public static Random m_random = new();
29
30 public static float? m_volume;
31
32 public static Mix CurrentMix {
33 get => m_currentMix;
34 set {
35 if (value != m_currentMix) {
36 m_currentMix = value;
37 m_nextSongTime = 0.0;
38 }
39 }
40 }
41
42 public static bool IsPlaying {
43 get {
44 if (m_sound != null) {
45 return m_sound.State != SoundState.Stopped;
46 }
47 return false;
48 }
49 }
50
51 public static float Volume {
52 get {
53 if (m_volume.HasValue) {
54 return m_volume.Value;
55 }
56 return SettingsManager.MusicVolume * 0.6f;
57 }
58 set => m_volume = value;
59 }
60
61 public static void ChangeMenuMusic() {
62 float startPercentage = IsPlaying ? m_random.Float(0f, 0.75f) : 0f;
63 string ContentMusicPath = string.Empty;
65 "MenuPlayMusic",
66 loader => {
67 loader.MenuPlayMusic(out ContentMusicPath);
68 return false;
69 }
70 );
71 if (!string.IsNullOrEmpty(ContentMusicPath)) {
72 PlayMusic(ContentMusicPath, startPercentage);
74 return;
75 }
76 switch (m_random.Int(0, 5)) {
77 case 0: PlayMusic("Music/NativeAmericanFluteSpirit", startPercentage); break;
78 case 1: PlayMusic("Music/AloneForever", startPercentage); break;
79 case 2: PlayMusic("Music/NativeAmerican", startPercentage); break;
80 case 3: PlayMusic("Music/NativeAmericanHeart", startPercentage); break;
81 case 4: PlayMusic("Music/NativeAmericanPeaceFlute", startPercentage); break;
82 case 5: PlayMusic("Music/NativeIndianChant", startPercentage); break;
83 }
85 }
86
87 public static void Update() {
88 if (m_fadeSound != null) {
89 m_fadeSound.Volume = MathUtils.Min(m_fadeSound.Volume - m_fadeSpeed * Volume * Time.FrameDuration, Volume);
90 if (m_fadeSound.Volume <= 0f) {
91 m_fadeSound.Dispose();
92 m_fadeSound = null;
93 }
94 }
95 if (m_sound != null
97 m_sound.Volume = MathUtils.Min(m_sound.Volume + m_fadeSpeed * Volume * Time.FrameDuration, Volume);
98 }
99 if (m_currentMix == Mix.None
100 || Volume == 0f) {
101 StopMusic();
102 }
103 else if (m_currentMix == Mix.Menu
106 }
107 else if (m_currentMix == Mix.InGame) {
109 "PlayInGameMusic",
110 loader => {
111 loader.PlayInGameMusic();
112 return false;
113 }
114 );
115 }
116 else if (m_currentMix == Mix.Other) { }
117 }
118
119 public static void Initialize() {
120 Window.Closed += delegate {
121 try {
122 Utilities.Dispose(ref m_sound);
123 Utilities.Dispose(ref m_fadeSound);
124 }
125 catch {
126 // ignored
127 }
128 };
129 }
130
131 public static void PlayMusic(string name, float startPercentage) {
132 if (string.IsNullOrEmpty(name)) {
133 StopMusic();
134 }
135 else {
136 try {
137 StopMusic();
139 float volume = m_fadeSound != null ? 0f : Volume;
140 StreamingSource streamingSource = ContentManager.Get<StreamingSource>(name);
141 streamingSource = streamingSource.Duplicate();
142 streamingSource.Position = (long)(MathUtils.Saturate(startPercentage)
143 * (streamingSource.BytesCount / streamingSource.ChannelsCount / 2))
144 / 16
145 * 16;
147 streamingSource,
148 volume,
149 1f,
150 0f,
151 false,
152 true,
153 1f
154 );
155 m_sound.Play();
156 }
157 catch {
158 Log.Warning($"Error playing music \"{name}\".");
159 }
160 }
161 }
162
163 public static void StopMusic() {
164 if (m_sound != null) {
165 if (m_fadeSound != null) {
166 m_fadeSound.Dispose();
167 }
168 m_sound.Stop();
170 m_sound = null;
171 }
172 }
173 }
174}
static void Warning(object message)
定义 Log.cs:68
static int Min(int x1, int x2)
static float Saturate(float x)
StreamingSource Duplicate()
static float FrameDuration
定义 Time.cs:46
static double FrameStartTime
定义 Time.cs:42
static Action Closed
static object Get(Type type, string name)
static double m_fadeStartTime
static void ChangeMenuMusic()
static StreamingSound m_fadeSound
static StreamingSound m_sound
static void PlayMusic(string name, float startPercentage)
static ? float m_volume
static double m_nextSongTime
static void HookAction(string HookName, Func< ModLoader, bool > action)
执行Hook