1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-06-13 13:04:03 +00:00
tobspr_shapez.io/src/js/platform/sound.js

287 lines
6.8 KiB
JavaScript
Raw Normal View History

2020-05-09 14:45:23 +00:00
/* typehints:start */
import { Application } from "../application";
import { Vector } from "../core/vector";
import { GameRoot } from "../game/root";
/* typehints:end */
import { newEmptyMap, clamp } from "../core/utils";
import { createLogger } from "../core/logging";
import { globalConfig } from "../core/config";
2020-05-09 14:45:23 +00:00
const logger = createLogger("sound");
export const SOUNDS = {
// Menu and such
2020-05-19 09:08:28 +00:00
uiClick: "ui_click",
uiError: "ui_error",
dialogError: "dialog_error",
dialogOk: "dialog_ok",
swishHide: "ui_swish_hide",
swishShow: "ui_swish_show",
badgeNotification: "badge_notification",
levelComplete: "level_complete",
2020-06-22 12:32:24 +00:00
destroyBuilding: "destroy_building",
2020-05-19 09:08:28 +00:00
placeBuilding: "place_building",
placeBelt: "place_belt",
2020-09-28 12:45:53 +00:00
copy: "copy",
2020-05-09 14:45:23 +00:00
};
export const MUSIC = {
// The theme always depends on the standalone only, even if running the full
// version in the browser
theme: G_IS_STANDALONE ? "theme-full" : "theme-short",
2020-05-23 10:26:04 +00:00
menu: "menu",
2020-05-09 14:45:23 +00:00
};
Puzzle DLC (#1172) * Puzzle mode (#1135) * Add mode button to main menu * [WIP] Add mode menu. Add factory-based gameMode creation * Add savefile migration, serialize, deserialize * Add hidden HUD elements, zone, and zoom, boundary constraints * Clean up lint issues * Add building, HUD exclusion, building exclusion, and refactor - [WIP] Add ConstantProducer building that combines ConstantSignal and ItemProducer functionality. Currently using temp assets. - Add pre-placement check to the zone - Use Rectangles for zone and boundary - Simplify zone drawing - Account for exclusion in savegame data - [WIP] Add puzzle play and edit buttons in puzzle mode menu * [WIP] Add building, component, and systems for producing and accepting user-specified items and checking goal criteria * Add ingame puzzle mode UI elements - Add minimal menus in puzzle mode for back, next navigation - Add lower menu for changing zone dimenensions Co-authored-by: Greg Considine <gconsidine@users.noreply.github.com> * Performance optimizations (#1154) * 1.3.1 preparations * Minor fixes, update translations * Fix achievements not working * Lots of belt optimizations, ~15% performance boost * Puzzle mode, part 1 * Puzzle mode, part 2 * Fix missing import * Puzzle mode, part 3 * Fix typo * Puzzle mode, part 4 * Puzzle Mode fixes: Correct zone restrictions and more (#1155) * Hide Puzzle Editor Controls in regular game mode, fix typo * Disallow shrinking zone if there are buildings * Fix multi-tile buildings for shrinking * Puzzle mode, Refactor hud * Puzzle mode * Fixed typo in latest puzzle commit (#1156) * Allow completing puzzles * Puzzle mode, almost done * Bump version to 1.4.0 * Fixes * [puzzle] Prevent pipette cheats (miners, emitters) (#1158) * Puzzle mode, almost done * Allow clearing belts with 'B' * Multiple users for the puzzle dlc * Bump api key * Minor adjustments * Update * Minor fixes * Fix throughput * Fix belts * Minor puzzle adjustments * New difficulty * Minor puzzle improvements * Fix belt path * Update translations * Added a button to return to the menu after a puzzle is completed (#1170) * added another button to return to the menu * improved menu return * fixed continue button to not go back to menu * [Puzzle] Added ability to lock buildings in the puzzle editor! (#1164) * initial test * tried to get it to work * added icon * added test exclusion * reverted css * completed flow for building locking * added lock option * finalized look and changed locked building to same sprite * removed unused art * added clearing every goal acceptor on lock to prevent creating impossible puzzles * heavily improved validation and prevented autocompletion * validation only checks every 100 ticks to improve performance * validation only checks every 100 ticks to improve performance * removed clearing goal acceptors as it isn't needed because of validation * Add soundtrack, puzzle dlc fixes Co-authored-by: Greg Considine <gconsidine@users.noreply.github.com> Co-authored-by: dengr1065 <dengr1065@gmail.com> Co-authored-by: Sense101 <67970865+Sense101@users.noreply.github.com>
2021-05-23 14:32:05 +00:00
if (G_IS_STANDALONE || G_IS_DEV) {
MUSIC.puzzle = "puzzle-full";
}
2020-05-09 14:45:23 +00:00
export class SoundInstanceInterface {
constructor(key, url) {
this.key = key;
this.url = url;
}
/** @returns {Promise<void>} */
load() {
abstract;
return Promise.resolve();
}
play(volume) {
abstract;
}
deinitialize() {}
}
export class MusicInstanceInterface {
constructor(key, url) {
this.key = key;
this.url = url;
}
stop() {
abstract;
}
play(volume) {
abstract;
}
setVolume(volume) {
2020-05-09 14:45:23 +00:00
abstract;
}
/** @returns {Promise<void>} */
load() {
abstract;
return Promise.resolve();
}
/** @returns {boolean} */
isPlaying() {
abstract;
return false;
}
deinitialize() {}
}
export class SoundInterface {
constructor(app, soundClass, musicClass) {
/** @type {Application} */
this.app = app;
this.soundClass = soundClass;
this.musicClass = musicClass;
/** @type {Object<string, SoundInstanceInterface>} */
this.sounds = newEmptyMap();
/** @type {Object<string, MusicInstanceInterface>} */
this.music = newEmptyMap();
/** @type {MusicInstanceInterface} */
this.currentMusic = null;
this.pageIsVisible = true;
this.musicVolume = 1.0;
this.soundVolume = 1.0;
2020-05-09 14:45:23 +00:00
}
/**
* Initializes the sound
* @returns {Promise<any>}
*/
initialize() {
for (const soundKey in SOUNDS) {
const soundPath = SOUNDS[soundKey];
const sound = new this.soundClass(soundKey, soundPath);
this.sounds[soundPath] = sound;
}
for (const musicKey in MUSIC) {
const musicPath = MUSIC[musicKey];
const music = new this.musicClass(musicKey, musicPath);
this.music[musicPath] = music;
}
this.musicVolume = this.app.settings.getAllSettings().musicVolume;
this.soundVolume = this.app.settings.getAllSettings().soundVolume;
2020-05-09 14:45:23 +00:00
if (G_IS_DEV && globalConfig.debug.disableMusic) {
this.musicVolume = 0.0;
2020-05-09 14:45:23 +00:00
}
return Promise.resolve();
}
/**
* Pre-Loads the given sounds
* @param {string} key
* @returns {Promise<void>}
*/
loadSound(key) {
if (this.sounds[key]) {
return this.sounds[key].load();
} else if (this.music[key]) {
return this.music[key].load();
} else {
logger.error("Sound/Music by key not found:", key);
return Promise.resolve();
}
}
2020-05-19 09:08:28 +00:00
/** Deinits the sound
* @returns {Promise<void>}
*/
2020-05-09 14:45:23 +00:00
deinitialize() {
const promises = [];
for (const key in this.sounds) {
promises.push(this.sounds[key].deinitialize());
}
for (const key in this.music) {
promises.push(this.music[key].deinitialize());
}
2020-05-19 09:08:28 +00:00
// @ts-ignore
return Promise.all(...promises);
2020-05-09 14:45:23 +00:00
}
/**
* Returns the music volume
* @returns {number}
*/
getMusicVolume() {
return this.musicVolume;
}
/**
* Returns the sound volume
* @returns {number}
*/
getSoundVolume() {
return this.soundVolume;
}
/**
* Sets the music volume
* @param {number} volume
*/
setMusicVolume(volume) {
this.musicVolume = clamp(volume, 0, 1);
if (this.currentMusic) {
this.currentMusic.setVolume(this.musicVolume);
}
}
/**
* Sets the sound volume
* @param {number} volume
*/
setSoundVolume(volume) {
this.soundVolume = clamp(volume, 0, 1);
}
2020-05-09 14:45:23 +00:00
/**
* Focus change handler, called by the pap
* @param {boolean} pageIsVisible
*/
onPageRenderableStateChanged(pageIsVisible) {
this.pageIsVisible = pageIsVisible;
if (this.currentMusic) {
if (pageIsVisible) {
if (!this.currentMusic.isPlaying()) {
this.currentMusic.play(this.musicVolume);
2020-05-09 14:45:23 +00:00
}
} else {
this.currentMusic.stop();
}
}
}
/**
* @param {string} key
*/
playUiSound(key) {
if (!this.sounds[key]) {
logger.warn("Sound", key, "not found, probably not loaded yet");
return;
}
this.sounds[key].play(this.soundVolume);
2020-05-09 14:45:23 +00:00
}
/**
*
* @param {string} key
* @param {Vector} worldPosition
* @param {GameRoot} root
*/
play3DSound(key, worldPosition, root) {
if (!this.sounds[key]) {
logger.warn("Music", key, "not found, probably not loaded yet");
return;
}
if (!this.pageIsVisible) {
2020-05-09 14:45:23 +00:00
return;
}
// hack, but works
if (root.time.getIsPaused()) {
return;
}
let volume = this.soundVolume;
2020-05-09 14:45:23 +00:00
if (!root.camera.isWorldPointOnScreen(worldPosition)) {
volume = this.soundVolume / 5; // In the old implementation this value was fixed to 0.2 => 20% of 1.0
2020-05-09 14:45:23 +00:00
}
volume *= clamp(root.camera.zoomLevel / 3);
this.sounds[key].play(clamp(volume));
}
/**
* @param {string} key
*/
playThemeMusic(key) {
const music = this.music[key];
if (key !== null && !music) {
logger.warn("Music", key, "not found");
}
if (this.currentMusic !== music) {
if (this.currentMusic) {
logger.log("Stopping", this.currentMusic.key);
this.currentMusic.stop();
}
this.currentMusic = music;
if (music && this.pageIsVisible) {
2020-05-09 14:45:23 +00:00
logger.log("Starting", this.currentMusic.key);
music.play(this.musicVolume);
2020-05-09 14:45:23 +00:00
}
}
}
}