feat: 开关音乐

This commit is contained in:
unanmed 2024-02-04 15:47:51 +08:00
parent c1378ea24b
commit f808a5e00c
4 changed files with 79 additions and 42 deletions

View File

@ -3363,11 +3363,11 @@ control.prototype.resumeBgm = function (resumeTime) {
////// 更改背景音乐的播放 ////// ////// 更改背景音乐的播放 //////
control.prototype.triggerBgm = function () { control.prototype.triggerBgm = function () {
if (main.mode !== 'play') return; if (main.mode !== 'play') return;
const bgm = Mota.require('var', 'bgm');
bgm.disable = !bgm.disable;
core.musicStatus.bgmStatus = !core.musicStatus.bgmStatus; if (!bgm.disable) this.resumeBgm();
if (core.musicStatus.bgmStatus) this.resumeBgm();
else this.pauseBgm(); else this.pauseBgm();
core.setLocalStorage('bgmStatus', core.musicStatus.bgmStatus);
}; };
// todo: deprecate playSound, stopSound, getPlayingSounds // todo: deprecate playSound, stopSound, getPlayingSounds
@ -3397,7 +3397,10 @@ control.prototype.getPlayingSounds = function (name) {
////// 检查bgm状态 ////// ////// 检查bgm状态 //////
control.prototype.checkBgm = function () { control.prototype.checkBgm = function () {
core.playBgm(core.musicStatus.playingBgm || main.startBgm); const bgm = Mota.require('var', 'bgm');
if (!bgm.playing) {
bgm.changeTo(bgm.now ?? main.startBgm);
}
}; };
///// 设置屏幕放缩 ////// ///// 设置屏幕放缩 //////

View File

@ -8,6 +8,7 @@ interface AnimatingBgm {
ani: Transition; ani: Transition;
timeout: number; timeout: number;
currentTime: number; currentTime: number;
endVolume: number;
} }
export class BgmController export class BgmController
@ -28,6 +29,11 @@ export class BgmController
/** 音量 */ /** 音量 */
volume: number = 1; volume: number = 1;
/** 是否关闭了bgm */
disable: boolean = false;
/** 是否正在播放bgm */
playing: boolean = false;
private transitionData: Map<BgmIds, AnimatingBgm> = new Map(); private transitionData: Map<BgmIds, AnimatingBgm> = new Map();
@ -61,6 +67,7 @@ export class BgmController
* @param when -1 * @param when -1
*/ */
changeTo(id: BgmIds, when: number = -1, noStack: boolean = false) { changeTo(id: BgmIds, when: number = -1, noStack: boolean = false) {
if (id === this.now) return;
let prevent = false; let prevent = false;
const preventDefault = () => { const preventDefault = () => {
prevent = true; prevent = true;
@ -71,8 +78,11 @@ export class BgmController
if (prevent) return; if (prevent) return;
this.playing = true;
if (!this.disable) {
this.setTransitionAnimate(id, 1); this.setTransitionAnimate(id, 1);
if (this.now) this.setTransitionAnimate(this.now, 0, when); if (this.now) this.setTransitionAnimate(this.now, 0, when);
}
if (!noStack) { if (!noStack) {
if (this.now) this.stack.push(this.now); if (this.now) this.stack.push(this.now);
@ -97,6 +107,8 @@ export class BgmController
if (prevent) return; if (prevent) return;
this.playing = false;
if (transition) this.setTransitionAnimate(this.now, 0); if (transition) this.setTransitionAnimate(this.now, 0);
else this.get(this.now).pause(); else this.get(this.now).pause();
} }
@ -113,20 +125,37 @@ export class BgmController
}; };
const ev = { preventDefault }; const ev = { preventDefault };
this.emit('pause', ev, this.now); this.emit('resume', ev, this.now);
if (prevent) return; if (prevent) return;
this.playing = true;
if (!this.disable) {
if (transition) this.setTransitionAnimate(this.now, 1); if (transition) this.setTransitionAnimate(this.now, 1);
else this.get(this.now).play(); else this.get(this.now).play();
} }
}
/** /**
* bgm * bgmchangeBgm事件preventDefault
* @param id bgm * @param id bgm
* @param when bgm的何时开始播放 * @param when bgm的何时开始播放
*/ */
play(id: BgmIds, when: number = 0, noStack: boolean = false) { play(id: BgmIds, when: number = 0, noStack: boolean = false) {
if (id === this.now) return;
let prevent = false;
const preventDefault = () => {
prevent = true;
};
const ev = { preventDefault };
this.emit('changeBgm', ev, id, this.now);
if (prevent) return;
this.playing = true;
const before = this.now ? null : this.get(this.now!); const before = this.now ? null : this.get(this.now!);
const to = this.get(id); const to = this.get(id);
if (before) { if (before) {
@ -135,12 +164,15 @@ export class BgmController
to.currentTime = when; to.currentTime = when;
to.volume = this.volume; to.volume = this.volume;
to.play(); to.play();
if (!this.disable) {
if (!noStack) { if (!noStack) {
if (this.now) this.stack.push(this.now); if (this.now) this.stack.push(this.now);
this.redoStack = []; this.redoStack = [];
} }
this.now = id; this.now = id;
} }
}
/** /**
* bgm * bgm
@ -201,7 +233,7 @@ export class BgmController
ani.value.volume = bgm.paused ? 0 : 1; ani.value.volume = bgm.paused ? 0 : 1;
const end = () => { const end = () => {
ani.ticker.destroy(); ani.ticker.destroy();
if (ani.value.volume === 0) { if (tran!.endVolume === 0) {
bgm.pause(); bgm.pause();
} else { } else {
bgm.volume = ani.value.volume * this.volume; bgm.volume = ani.value.volume * this.volume;
@ -212,7 +244,8 @@ export class BgmController
end, end,
ani: ani, ani: ani,
timeout: -1, timeout: -1,
currentTime: bgm.currentTime currentTime: bgm.currentTime,
endVolume: to
}; };
this.transitionData.set(id, tran); this.transitionData.set(id, tran);
ani.ticker.add(() => { ani.ticker.add(() => {
@ -227,6 +260,7 @@ export class BgmController
if (when !== -1) { if (when !== -1) {
bgm.currentTime = when; bgm.currentTime = when;
} }
tran.endVolume = to;
tran.ani tran.ani
.time(this.transitionTime) .time(this.transitionTime)

View File

@ -3,6 +3,7 @@ import { EmitableEvent, EventEmitter } from '../common/eventEmitter';
import { GameStorage } from './storage'; import { GameStorage } from './storage';
import { has, triggerFullscreen } from '@/plugin/utils'; import { has, triggerFullscreen } from '@/plugin/utils';
import { createSettingComponents } from './init/settings'; import { createSettingComponents } from './init/settings';
import { bgm } from '../audio/bgm';
export interface SettingComponentProps { export interface SettingComponentProps {
item: MotaSettingItem; item: MotaSettingItem;
@ -306,25 +307,7 @@ export const mainSetting = new MotaSetting();
// 添加不参与全局存储的设置 // 添加不参与全局存储的设置
MotaSetting.noStorage.push('action.autoSkill', 'screen.fullscreen'); MotaSetting.noStorage.push('action.autoSkill', 'screen.fullscreen');
export interface SettingStorage { const storage = new GameStorage(GameStorage.fromAuthor('AncTe', 'setting'));
showHalo: boolean;
frag: boolean;
itemDetail: boolean;
transition: boolean;
antiAlias: boolean;
fontSize: number;
smoothView: boolean;
criticalGem: boolean;
fixed: boolean;
betterLoad: boolean;
autoScale: boolean;
paraLight: boolean;
heroDetail: boolean;
}
const storage = new GameStorage<SettingStorage>(
GameStorage.fromAuthor('AncTe', 'setting')
);
export { storage as settingStorage }; export { storage as settingStorage };
@ -340,8 +323,8 @@ mainSetting.on('valueChange', (key, n, o) => {
handleScreenSetting(setting, n, o); handleScreenSetting(setting, n, o);
} else if (root === 'action') { } else if (root === 'action') {
handleActionSetting(setting, n, o); handleActionSetting(setting, n, o);
} else if (root === 'utils') { } else if (root === 'audio') {
handleUtilsSetting(setting, n, o); handleAudioSetting(setting, n, o);
} }
}); });
@ -385,11 +368,18 @@ function handleActionSetting<T extends number | boolean>(
} }
} }
function handleUtilsSetting<T extends number | boolean>( function handleAudioSetting<T extends number | boolean>(
key: string, key: string,
n: T, n: T,
o: T o: T
) {} ) {
if (key === 'bgmEnabled') {
bgm.disable = !n;
core.checkBgm();
} else if (key === 'bgmVolume') {
bgm.volume = (n as number) / 100;
}
}
// ----- 游戏的所有设置项 // ----- 游戏的所有设置项
// todo: 虚拟键盘缩放,小地图楼传缩放 // todo: 虚拟键盘缩放,小地图楼传缩放
@ -421,6 +411,13 @@ mainSetting
.register('toolbar', '自定义工具栏', false, COM.ToolbarEditor) .register('toolbar', '自定义工具栏', false, COM.ToolbarEditor)
.setDisplayFunc('toolbar', () => '') .setDisplayFunc('toolbar', () => '')
) )
.register(
'audio',
'音频设置',
new MotaSetting()
.register('bgmEnabled', '开启音乐', true, COM.Boolean)
.register('bgmVolume', '音乐音量', 80, COM.Number, [0, 100, 5])
)
.register( .register(
'utils', 'utils',
'系统设置', '系统设置',
@ -460,6 +457,8 @@ loading.once('coreInit', () => {
'screen.smoothView': !!storage.getValue('screen.smoothView', true), 'screen.smoothView': !!storage.getValue('screen.smoothView', true),
'screen.criticalGem': !!storage.getValue('screen.criticalGem', false), 'screen.criticalGem': !!storage.getValue('screen.criticalGem', false),
'action.fixed': !!storage.getValue('action.fixed', true), 'action.fixed': !!storage.getValue('action.fixed', true),
'audio.bgmEnabled': !!storage.getValue('sound.bgmEnabled', true),
'audio.bgmVolume': storage.getValue('sound.bgmVolume', 80),
'utils.betterLoad': !!storage.getValue('utils.betterLoad', true), 'utils.betterLoad': !!storage.getValue('utils.betterLoad', true),
'utils.autoScale': !!storage.getValue('utils.autoScale', true), 'utils.autoScale': !!storage.getValue('utils.autoScale', true),
'fx.paraLight': !!storage.getValue('fx.paraLight', true), 'fx.paraLight': !!storage.getValue('fx.paraLight', true),

View File

@ -66,13 +66,14 @@ import {
import { sleep } from 'mutate-animate'; import { sleep } from 'mutate-animate';
import { Matrix4 } from '../plugin/webgl/matrix'; import { Matrix4 } from '../plugin/webgl/matrix';
import { doByInterval, keycode } from '../plugin/utils'; import { doByInterval, keycode } from '../plugin/utils';
import { KeyCode } from '../plugin/keyCodes';
import { triggerFullscreen } from '../plugin/utils'; import { triggerFullscreen } from '../plugin/utils';
import { isMobile } from '../plugin/use'; import { isMobile } from '../plugin/use';
import { GameUi } from '@/core/main/custom/ui'; import { GameUi } from '@/core/main/custom/ui';
import { gameKey } from '@/core/main/init/hotkey'; import { gameKey } from '@/core/main/init/hotkey';
import { mainUi } from '@/core/main/init/ui'; import { mainUi } from '@/core/main/init/ui';
import { CustomToolbar } from '@/core/main/custom/toolbar'; import { CustomToolbar } from '@/core/main/custom/toolbar';
import { mainSetting } from '@/core/main/setting';
import { bgm as mainBgm } from '@/core/audio/bgm';
const props = defineProps<{ const props = defineProps<{
num: number; num: number;
@ -141,7 +142,6 @@ function setCursor(ele: HTMLSpanElement, i: number) {
} }
async function clickStartButton(id: string) { async function clickStartButton(id: string) {
core.checkBgm();
if (id === 'start-game') showHard(); if (id === 'start-game') showHard();
if (id === 'back') setButtonAnimate(); if (id === 'back') setButtonAnimate();
if (id === 'easy' || id === 'hard-hard') { if (id === 'easy' || id === 'hard-hard') {
@ -314,7 +314,8 @@ onMounted(async () => {
window.addEventListener('resize', resize); window.addEventListener('resize', resize);
resize(); resize();
soundChecked.value = core.musicStatus.bgmStatus; soundChecked.value = mainSetting.getValue('audio.bgmEnabled', true);
mainBgm.changeTo('title.mp3');
start.style.opacity = '1'; start.style.opacity = '1';
if (played) { if (played) {