mirror of
https://github.com/tobspr/shapez.io.git
synced 2026-03-02 03:39:21 +00:00
Compress sounds into atlas
This commit is contained in:
2
src/js/.gitignore
vendored
2
src/js/.gitignore
vendored
@@ -1 +1 @@
|
||||
translations-built
|
||||
built-temp
|
||||
|
||||
@@ -1,25 +1,33 @@
|
||||
import { MusicInstanceInterface, SoundInstanceInterface, SoundInterface } from "../sound";
|
||||
import { MusicInstanceInterface, SoundInstanceInterface, SoundInterface, MUSIC, SOUNDS } from "../sound";
|
||||
import { cachebust } from "../../core/cachebust";
|
||||
import { createLogger } from "../../core/logging";
|
||||
import { globalConfig } from "../../core/config";
|
||||
|
||||
const { Howl, Howler } = require("howler");
|
||||
|
||||
const logger = createLogger("sound/browser");
|
||||
|
||||
class SoundInstance extends SoundInstanceInterface {
|
||||
constructor(key, url) {
|
||||
super(key, url);
|
||||
const sprites = require("../../built-temp/sfx.json");
|
||||
|
||||
class SoundSpritesContainer {
|
||||
constructor() {
|
||||
this.howl = null;
|
||||
|
||||
this.loadingPromise = null;
|
||||
}
|
||||
|
||||
load() {
|
||||
return Promise.race([
|
||||
if (this.loadingPromise) {
|
||||
return this.loadingPromise;
|
||||
}
|
||||
return (this.loadingPromise = Promise.race([
|
||||
new Promise((resolve, reject) => {
|
||||
setTimeout(reject, G_IS_DEV ? 5000 : 60000);
|
||||
}),
|
||||
new Promise(resolve => {
|
||||
this.howl = new Howl({
|
||||
src: cachebust("res/sounds/" + this.url),
|
||||
src: cachebust("res/sounds/sfx.mp3"),
|
||||
sprite: sprites.sprite,
|
||||
autoplay: false,
|
||||
loop: false,
|
||||
volume: 0,
|
||||
@@ -29,21 +37,21 @@ class SoundInstance extends SoundInstanceInterface {
|
||||
resolve();
|
||||
},
|
||||
onloaderror: (id, err) => {
|
||||
logger.warn("Sound", this.url, "failed to load:", id, err);
|
||||
logger.warn("SFX failed to load:", id, err);
|
||||
this.howl = null;
|
||||
resolve();
|
||||
},
|
||||
onplayerror: (id, err) => {
|
||||
logger.warn("Sound", this.url, "failed to play:", id, err);
|
||||
logger.warn("SFX failed to play:", id, err);
|
||||
},
|
||||
});
|
||||
}),
|
||||
]);
|
||||
]));
|
||||
}
|
||||
|
||||
play(volume) {
|
||||
play(volume, key) {
|
||||
if (this.howl) {
|
||||
const instance = this.howl.play();
|
||||
const instance = this.howl.play(key);
|
||||
this.howl.volume(volume, instance);
|
||||
}
|
||||
}
|
||||
@@ -56,6 +64,32 @@ class SoundInstance extends SoundInstanceInterface {
|
||||
}
|
||||
}
|
||||
|
||||
class WrappedSoundInstance extends SoundInstanceInterface {
|
||||
/**
|
||||
*
|
||||
* @param {SoundSpritesContainer} spriteContainer
|
||||
* @param {string} key
|
||||
*/
|
||||
constructor(spriteContainer, key) {
|
||||
super(key, "sfx.mp3");
|
||||
this.spriteContainer = spriteContainer;
|
||||
}
|
||||
|
||||
/** @returns {Promise<void>} */
|
||||
load() {
|
||||
return this.spriteContainer.load();
|
||||
}
|
||||
|
||||
play(volume) {
|
||||
logger.error("TDO: PLAY", this.key);
|
||||
this.spriteContainer.play(volume, this.key);
|
||||
}
|
||||
|
||||
deinitialize() {
|
||||
return this.spriteContainer.deinitialize();
|
||||
}
|
||||
}
|
||||
|
||||
class MusicInstance extends MusicInstanceInterface {
|
||||
constructor(key, url) {
|
||||
super(key, url);
|
||||
@@ -140,11 +174,32 @@ export class SoundImplBrowser extends SoundInterface {
|
||||
Howler.html5PoolSize = 20;
|
||||
Howler.pos(0, 0, 0);
|
||||
|
||||
super(app, SoundInstance, MusicInstance);
|
||||
super(app, WrappedSoundInstance, MusicInstance);
|
||||
}
|
||||
|
||||
initialize() {
|
||||
return super.initialize();
|
||||
this.sfxHandle = new SoundSpritesContainer();
|
||||
|
||||
// @ts-ignore
|
||||
const keys = Object.values(SOUNDS);
|
||||
keys.forEach(key => {
|
||||
this.sounds[key] = new WrappedSoundInstance(this.sfxHandle, key);
|
||||
});
|
||||
console.log(this.sounds);
|
||||
for (const musicKey in MUSIC) {
|
||||
const musicPath = MUSIC[musicKey];
|
||||
const music = new this.musicClass(musicKey, musicPath);
|
||||
this.music[musicPath] = music;
|
||||
}
|
||||
|
||||
this.musicMuted = this.app.settings.getAllSettings().musicMuted;
|
||||
this.soundsMuted = this.app.settings.getAllSettings().soundsMuted;
|
||||
|
||||
if (G_IS_DEV && globalConfig.debug.disableMusic) {
|
||||
this.musicMuted = true;
|
||||
}
|
||||
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
deinitialize() {
|
||||
|
||||
@@ -12,18 +12,18 @@ const logger = createLogger("sound");
|
||||
|
||||
export const SOUNDS = {
|
||||
// Menu and such
|
||||
uiClick: "ui/ui_click.mp3",
|
||||
uiError: "ui/ui_error.mp3",
|
||||
dialogError: "ui/dialog_error.mp3",
|
||||
dialogOk: "ui/dialog_ok.mp3",
|
||||
swishHide: "ui/ui_swish_hide.mp3",
|
||||
swishShow: "ui/ui_swish_show.mp3",
|
||||
badgeNotification: "ui/badge_notification.mp3",
|
||||
uiClick: "ui_click",
|
||||
uiError: "ui_error",
|
||||
dialogError: "dialog_error",
|
||||
dialogOk: "dialog_ok",
|
||||
swishHide: "ui_swish_hide",
|
||||
swishShow: "ui_swish_show",
|
||||
badgeNotification: "badge_notification",
|
||||
|
||||
levelComplete: "ui/level_complete.mp3",
|
||||
levelComplete: "level_complete",
|
||||
|
||||
placeBuilding: "game/place_building.mp3",
|
||||
placeBelt: "game/place_belt.mp3",
|
||||
placeBuilding: "place_building",
|
||||
placeBelt: "place_belt",
|
||||
};
|
||||
|
||||
export const MUSIC = {
|
||||
@@ -145,7 +145,9 @@ export class SoundInterface {
|
||||
}
|
||||
}
|
||||
|
||||
/** Deinits the sound */
|
||||
/** Deinits the sound
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
deinitialize() {
|
||||
const promises = [];
|
||||
for (const key in this.sounds) {
|
||||
@@ -154,7 +156,8 @@ export class SoundInterface {
|
||||
for (const key in this.music) {
|
||||
promises.push(this.music[key].deinitialize());
|
||||
}
|
||||
return Promise.all(promises);
|
||||
// @ts-ignore
|
||||
return Promise.all(...promises);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { globalConfig } from "./core/config";
|
||||
|
||||
const baseTranslations = require("./translations-built/base-en.json");
|
||||
const baseTranslations = require("./built-temp/base-en.json");
|
||||
|
||||
export const T = baseTranslations;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user