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
tobspr 931c8a5821
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 16:32:05 +02:00

287 lines
6.8 KiB
JavaScript

/* 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";
const logger = createLogger("sound");
export const SOUNDS = {
// Menu and such
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",
destroyBuilding: "destroy_building",
placeBuilding: "place_building",
placeBelt: "place_belt",
copy: "copy",
};
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",
menu: "menu",
};
if (G_IS_STANDALONE || G_IS_DEV) {
MUSIC.puzzle = "puzzle-full";
}
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) {
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;
}
/**
* 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;
if (G_IS_DEV && globalConfig.debug.disableMusic) {
this.musicVolume = 0.0;
}
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();
}
}
/** Deinits the sound
* @returns {Promise<void>}
*/
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());
}
// @ts-ignore
return Promise.all(...promises);
}
/**
* 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);
}
/**
* 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);
}
} 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);
}
/**
*
* @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) {
return;
}
// hack, but works
if (root.time.getIsPaused()) {
return;
}
let volume = this.soundVolume;
if (!root.camera.isWorldPointOnScreen(worldPosition)) {
volume = this.soundVolume / 5; // In the old implementation this value was fixed to 0.2 => 20% of 1.0
}
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) {
logger.log("Starting", this.currentMusic.key);
music.play(this.musicVolume);
}
}
}
}