Game state now contains a map of sounds to play next, for better perf

This commit is contained in:
Renan LE CARO 2025-03-17 19:47:16 +01:00
parent efa634cd8f
commit 581ee412d4
10 changed files with 1326 additions and 1153 deletions

View file

@ -23,7 +23,6 @@ There's also an easy mode for kids (slower ball).
# Next
- extract sound logic, only set the params as a gamestate object
- separate particles by type
- reuse coins and particles

33
dist/PWA/sw-b71.js vendored
View file

@ -1,2 +1,33 @@
function e(e,t,n,r,a,i,c){try{var o=e[i](c),u=o.value}catch(e){n(e);return}o.done?t(u):Promise.resolve(u).then(r,a)}function t(t){return function(){var n=this,r=arguments;return new Promise(function(a,i){var c=t.apply(n,r);function o(t){e(c,a,i,o,u,"next",t)}function u(t){e(c,a,i,o,u,"throw",t)}o(void 0)})}}function n(e,t){var n,r,a,i,c={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]};return i={next:o(0),throw:o(1),return:o(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function o(i){return function(o){return function(i){if(n)throw TypeError("Generator is already executing.");for(;c;)try{if(n=1,r&&(a=2&i[0]?r.return:i[0]?r.throw||((a=r.return)&&a.call(r),0):r.next)&&!(a=a.call(r,i[1])).done)return a;switch(r=0,a&&(i=[2&i[0],a.value]),i[0]){case 0:case 1:a=i;break;case 4:return c.label++,{value:i[1],done:!1};case 5:c.label++,r=i[1],i=[0];continue;case 7:i=c.ops.pop(),c.trys.pop();continue;default:if(!(a=(a=c.trys).length>0&&a[a.length-1])&&(6===i[0]||2===i[0])){c=0;continue}if(3===i[0]&&(!a||i[1]>a[0]&&i[1]<a[3])){c.label=i[1];break}if(6===i[0]&&c.label<a[1]){c.label=a[1],a=i;break}if(a&&c.label<a[2]){c.label=a[2],c.ops.push(i);break}a[2]&&c.ops.pop(),c.trys.pop();continue}i=t.call(e,c)}catch(e){i=[6,e],r=0}finally{n=a=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,o])}}}var r="breakout-71-".concat("29036807"),a=["/"];self.addEventListener("install",function(e){e.waitUntil(t(function(){return n(this,function(e){switch(e.label){case 0:return[4,caches.open(r)];case 1:return e.sent().addAll(a),[2]}})})())}),self.addEventListener("activate",function(e){e.waitUntil(t(function(){return n(this,function(e){switch(e.label){case 0:return[4,caches.keys()];case 1:return[4,Promise.all(e.sent().map(function(e){if(e!==r)return caches.delete(e)}))];case 2:return e.sent(),[4,clients.claim()];case 3:return e.sent(),[2]}})})())}),self.addEventListener("fetch",function(e){if("navigate"===e.request.mode&&e.request.url.endsWith("/index.html?isPWA=true")){e.respondWith(caches.match("/"));return}});
// The version of the cache.
const VERSION = "29036807";
// The name of the cache
const CACHE_NAME = `breakout-71-${VERSION}`;
// The static resources that the app needs to function.
const APP_STATIC_RESOURCES = [
"/"
];
// On install, cache the static resources
self.addEventListener("install", (event)=>{
event.waitUntil((async ()=>{
const cache = await caches.open(CACHE_NAME);
cache.addAll(APP_STATIC_RESOURCES);
})());
});
// delete old caches on activate
self.addEventListener("activate", (event)=>{
event.waitUntil((async ()=>{
const names = await caches.keys();
await Promise.all(names.map((name)=>{
if (name !== CACHE_NAME) return caches.delete(name);
}));
await clients.claim();
})());
});
self.addEventListener("fetch", (event)=>{
if (event.request.mode === "navigate" && event.request.url.endsWith("/index.html?isPWA=true")) {
event.respondWith(caches.match("/"));
return;
}
});
//# sourceMappingURL=sw-b71.js.map

File diff suppressed because one or more lines are too long

140
dist/index.html vendored
View file

@ -765,6 +765,7 @@ function tick() {
(0, _render.render)(gameState);
}
if (gameState.running) (0, _recording.recordOneFrame)(gameState);
if ((0, _options.isOptionOn)('sound')) (0, _sounds.playPendingSounds)(gameState);
requestAnimationFrame(tick);
}
window.addEventListener("visibilitychange", ()=>{
@ -1185,7 +1186,7 @@ window.stressTest = ()=>restart({
}
});
},{"./loadGameData":"l1B4x","./sounds":"dQKPV","./game_utils":"cEeac","./PWA/sw_loader":"2n0gK","./i18n/i18n":"eNPRm","./settings":"5blfu","./gameStateMutators":"9ZeQl","./render":"9AS2t","./recording":"godmD","./newGameState":"aQN6X","./asyncAlert":"rSqLY","./options":"d5NoS","@parcel/transformer-js/src/esmodule-helpers.js":"gkKU3","./getLevelBackground":"7OIPf"}],"l1B4x":[function(require,module,exports,__globalThis) {
},{"./loadGameData":"l1B4x","./sounds":"dQKPV","./game_utils":"cEeac","./PWA/sw_loader":"2n0gK","./i18n/i18n":"eNPRm","./settings":"5blfu","./gameStateMutators":"9ZeQl","./render":"9AS2t","./recording":"godmD","./newGameState":"aQN6X","./asyncAlert":"rSqLY","./options":"d5NoS","./getLevelBackground":"7OIPf","@parcel/transformer-js/src/esmodule-helpers.js":"gkKU3"}],"l1B4x":[function(require,module,exports,__globalThis) {
var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
parcelHelpers.defineInteropFlag(exports);
parcelHelpers.export(exports, "appVersion", ()=>appVersion);
@ -1756,49 +1757,62 @@ function levelIconHTML(bricks, levelSize, color) {
},{"@parcel/transformer-js/src/esmodule-helpers.js":"gkKU3"}],"dQKPV":[function(require,module,exports,__globalThis) {
var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
parcelHelpers.defineInteropFlag(exports);
parcelHelpers.export(exports, "playPendingSounds", ()=>playPendingSounds);
parcelHelpers.export(exports, "sounds", ()=>sounds);
parcelHelpers.export(exports, "getAudioContext", ()=>getAudioContext);
parcelHelpers.export(exports, "getAudioRecordingTrack", ()=>getAudioRecordingTrack);
var _game = require("./game");
var _options = require("./options");
let lastPlay = Date.now();
function playPendingSounds(gameState) {
if (lastPlay > Date.now() - 60) return;
lastPlay = Date.now();
for(let key in gameState.aboutToPlaySound){
const soundName = key;
const ex = gameState.aboutToPlaySound[soundName];
if (ex.vol) {
sounds[soundName](Math.min(2, ex.vol), pixelsToPan(gameState, ex.x), gameState.combo);
ex.vol = 0;
}
}
}
const sounds = {
wallBeep: (pan)=>{
wallBeep: (vol, pan, combo)=>{
if (!(0, _options.isOptionOn)("sound")) return;
createSingleBounceSound(800, pixelsToPan(pan));
createSingleBounceSound(800, pan, vol);
},
comboIncreaseMaybe: (combo, x, volume)=>{
comboIncreaseMaybe: (volume, pan, combo)=>{
if (!(0, _options.isOptionOn)("sound")) return;
let delta = 0;
if (!isNaN(lastComboPlayed)) {
if (lastComboPlayed < combo) delta = 1;
if (lastComboPlayed > combo) delta = -1;
}
playShepard(delta, pixelsToPan(x), volume);
playShepard(delta, pan, volume);
lastComboPlayed = combo;
},
comboDecrease () {
comboDecrease (volume, pan, combo) {
if (!(0, _options.isOptionOn)("sound")) return;
playShepard(-1, 0.5, 0.5);
playShepard(-1, pan, volume);
},
coinBounce: (pan, volume)=>{
coinBounce: (volume, pan, combo)=>{
if (!(0, _options.isOptionOn)("sound")) return;
createSingleBounceSound(1200, pixelsToPan(pan), volume, 0.1, "triangle");
createSingleBounceSound(1200, pan, volume, 0.1, "triangle");
},
explode: (pan)=>{
explode: (volume, pan, combo)=>{
if (!(0, _options.isOptionOn)("sound")) return;
createExplosionSound(pixelsToPan(pan));
createExplosionSound(pan);
},
lifeLost (pan) {
lifeLost (volume, pan, combo) {
if (!(0, _options.isOptionOn)("sound")) return;
createShatteredGlassSound(pixelsToPan(pan));
createShatteredGlassSound(pan);
},
coinCatch (pan) {
coinCatch (volume, pan, combo) {
if (!(0, _options.isOptionOn)("sound")) return;
createSingleBounceSound(900, pixelsToPan(pan), 0.8, 0.1, "triangle");
createSingleBounceSound(900, pan, volume, 0.1, "triangle");
},
colorChange (pan, volume) {
createSingleBounceSound(400, pixelsToPan(pan), volume, 0.5, "sine");
createSingleBounceSound(800, pixelsToPan(pan), volume * 0.5, 0.2, "square");
colorChange (volume, pan, combo) {
createSingleBounceSound(400, pan, volume, 0.5, "sine");
createSingleBounceSound(800, pan, volume * 0.5, 0.2, "square");
}
};
// How to play the code on the leftconst context = new window.AudioContext();
@ -1879,8 +1893,8 @@ function createExplosionSound(pan = 0.5) {
// Stop the noise source after the sound has played
noiseSource.stop(context.currentTime + 1);
}
function pixelsToPan(pan) {
return Math.max(0, Math.min(1, (pan - (0, _game.gameState).offsetXRoundedDown) / (0, _game.gameState).gameZoneWidthRoundedUp));
function pixelsToPan(gameState, pan) {
return Math.max(0, Math.min(1, (pan - gameState.offsetXRoundedDown) / gameState.gameZoneWidthRoundedUp));
}
let lastComboPlayed = NaN, shepard = 6;
function playShepard(delta, pan, volume) {
@ -1935,7 +1949,7 @@ function createOscillator(context, frequency, type) {
return oscillator;
}
},{"./game":"edeGs","./options":"d5NoS","@parcel/transformer-js/src/esmodule-helpers.js":"gkKU3"}],"d5NoS":[function(require,module,exports,__globalThis) {
},{"./options":"d5NoS","@parcel/transformer-js/src/esmodule-helpers.js":"gkKU3"}],"d5NoS":[function(require,module,exports,__globalThis) {
var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
parcelHelpers.defineInteropFlag(exports);
parcelHelpers.export(exports, "options", ()=>options);
@ -2001,6 +2015,7 @@ parcelHelpers.export(exports, "isTelekinesisActive", ()=>isTelekinesisActive);
parcelHelpers.export(exports, "findLast", ()=>findLast);
parcelHelpers.export(exports, "distance2", ()=>distance2);
parcelHelpers.export(exports, "distanceBetween", ()=>distanceBetween);
parcelHelpers.export(exports, "defaultSounds", ()=>defaultSounds);
var _loadGameData = require("./loadGameData");
function getMajorityValue(arr) {
const count = {};
@ -2058,6 +2073,44 @@ function distance2(a, b) {
function distanceBetween(a, b) {
return Math.sqrt(distance2(a, b));
}
function defaultSounds() {
return {
aboutToPlaySound: {
wallBeep: {
vol: 0,
x: 0
},
comboIncreaseMaybe: {
vol: 0,
x: 0
},
comboDecrease: {
vol: 0,
x: 0
},
coinBounce: {
vol: 0,
x: 0
},
explode: {
vol: 0,
x: 0
},
lifeLost: {
vol: 0,
x: 0
},
coinCatch: {
vol: 0,
x: 0
},
colorChange: {
vol: 0,
x: 0
}
}
};
}
},{"./loadGameData":"l1B4x","@parcel/transformer-js/src/esmodule-helpers.js":"gkKU3"}],"2n0gK":[function(require,module,exports,__globalThis) {
if ("serviceWorker" in navigator && window.location.search.includes("isPWA=true")) // @ts-ignore
@ -2115,6 +2168,7 @@ parcelHelpers.export(exports, "spawnExplosion", ()=>spawnExplosion);
parcelHelpers.export(exports, "explodeBrick", ()=>explodeBrick);
parcelHelpers.export(exports, "dontOfferTooSoon", ()=>dontOfferTooSoon);
parcelHelpers.export(exports, "pickRandomUpgrades", ()=>pickRandomUpgrades);
parcelHelpers.export(exports, "schedulGameSound", ()=>schedulGameSound);
parcelHelpers.export(exports, "addToScore", ()=>addToScore);
parcelHelpers.export(exports, "setLevel", ()=>setLevel);
parcelHelpers.export(exports, "rainbowColor", ()=>rainbowColor);
@ -2122,7 +2176,6 @@ parcelHelpers.export(exports, "repulse", ()=>repulse);
parcelHelpers.export(exports, "attract", ()=>attract);
parcelHelpers.export(exports, "gameStateTick", ()=>gameStateTick);
parcelHelpers.export(exports, "ballTick", ()=>ballTick);
var _sounds = require("./sounds");
var _gameUtils = require("./game_utils");
var _i18N = require("./i18n/i18n");
var _loadGameData = require("./loadGameData");
@ -2208,7 +2261,7 @@ function resetCombo(gameState, x, y) {
if (prev > gameState.combo && gameState.perks.soft_reset) gameState.combo += Math.floor((prev - gameState.combo) * (gameState.perks.soft_reset * 10) / 100);
const lost = Math.max(0, prev - gameState.combo);
if (lost) {
for(let i = 0; i < lost && i < 8; i++)setTimeout(()=>(0, _sounds.sounds).comboDecrease(), i * 100);
for(let i = 0; i < lost && i < 8; i++)setTimeout(()=>schedulGameSound(gameState, 'comboDecrease', x, 1), i * 100);
if (typeof x !== "undefined" && typeof y !== "undefined") gameState.flashes.push({
type: "text",
text: "-" + lost,
@ -2227,7 +2280,7 @@ function decreaseCombo(gameState, by, x, y) {
gameState.combo = Math.max(baseCombo(gameState), gameState.combo - by);
const lost = Math.max(0, prev - gameState.combo);
if (lost) {
(0, _sounds.sounds).comboDecrease();
schedulGameSound(gameState, 'comboDecrease', x, 1);
if (typeof x !== "undefined" && typeof y !== "undefined") gameState.flashes.push({
type: "text",
text: "-" + lost,
@ -2263,7 +2316,7 @@ function explodeBrick(gameState, index, ball, isExplosion) {
if (color === "black") {
delete gameState.bricks[index];
const x = (0, _gameUtils.brickCenterX)(gameState, index), y = (0, _gameUtils.brickCenterY)(gameState, index);
(0, _sounds.sounds).explode(ball.x);
schedulGameSound(gameState, 'explode', ball.x, 1);
const col = index % gameState.gridSize;
const row = Math.floor(index / gameState.gridSize);
const size = 1 + gameState.perks.bigger_explosions;
@ -2346,13 +2399,13 @@ function explodeBrick(gameState, index, ball, isExplosion) {
// color change
if ((gameState.perks.picky_eater || gameState.perks.pierce_color) && color !== gameState.ballsColor && color) {
if (gameState.perks.picky_eater) resetCombo(gameState, ball.x, ball.y);
(0, _sounds.sounds).colorChange(ball.x, 0.8);
schedulGameSound(gameState, 'colorChange', ball.x, 0.8);
gameState.lastExplosion = gameState.levelTime;
gameState.ballsColor = color;
if (!(0, _options.isOptionOn)("basic")) gameState.balls.forEach((ball)=>{
spawnExplosion(gameState, 7, ball.previousX, ball.previousY, color, 150, 15);
});
} else (0, _sounds.sounds).comboIncreaseMaybe(gameState.combo, ball.x, 1);
} else schedulGameSound(gameState, 'comboIncreaseMaybe', ball.x, 1);
}
gameState.flashes.push({
type: "ball",
@ -2390,6 +2443,14 @@ function pickRandomUpgrades(gameState, count) {
help: u.help(gameState.perks[u.id] + 1)
}));
}
function schedulGameSound(gameState, sound, x, vol) {
if (!vol) return;
x ??= gameState.offsetX + gameState.gameZoneWidth / 2;
const ex = gameState.aboutToPlaySound[sound];
if (ex.vol) console.log('Combined sounds for ' + sound);
ex.x = (x * vol + ex.x * ex.vol) / (vol + ex.vol);
ex.vol += vol;
}
function addToScore(gameState, coin) {
coin.destroyed = true;
gameState.score += coin.points;
@ -2413,7 +2474,7 @@ function addToScore(gameState, coin) {
});
if (Date.now() - gameState.lastPlayedCoinGrab > 16) {
gameState.lastPlayedCoinGrab = Date.now();
(0, _sounds.sounds).coinCatch(coin.x);
schedulGameSound(gameState, 'coinCatch', coin.x, 1);
}
gameState.runStatistics.score += coin.points;
}
@ -2553,7 +2614,6 @@ frames = 1) {
score: gameState.score
}));
} else if (gameState.running || gameState.levelTime) {
let playedCoinBounce = false;
const coinRadius = Math.round(gameState.coinSize / 2);
gameState.coins.forEach((coin)=>{
if (coin.destroyed) return;
@ -2585,17 +2645,14 @@ frames = 1) {
if (gameState.bricks[hitBrick] && coin.color !== gameState.bricks[hitBrick] && gameState.bricks[hitBrick] !== "black" && !coin.coloredABrick) {
gameState.bricks[hitBrick] = coin.color;
coin.coloredABrick = true;
(0, _sounds.sounds).colorChange(coin.x, 0.3);
schedulGameSound(gameState, 'colorChange', coin.x, 0.3);
}
}
if (typeof hitBrick !== "undefined" || hitBorder) {
coin.vx *= 0.8;
coin.vy *= 0.8;
coin.sa *= 0.9;
if (speed > 20 && !playedCoinBounce) {
playedCoinBounce = true;
(0, _sounds.sounds).coinBounce(coin.x, 0.2);
}
if (speed > 20) schedulGameSound(gameState, 'coinBounce', coin.x, 0.2);
if (Math.abs(coin.vy) < 3) coin.vy = 0;
}
});
@ -2739,7 +2796,7 @@ function ballTick(gameState, ball, delta) {
if (gameState.perks.left_is_lava && borderHitCode % 2 && ball.x < gameState.offsetX + gameState.gameZoneWidth / 2) resetCombo(gameState, ball.x, ball.y);
if (gameState.perks.right_is_lava && borderHitCode % 2 && ball.x > gameState.offsetX + gameState.gameZoneWidth / 2) resetCombo(gameState, ball.x, ball.y);
if (gameState.perks.top_is_lava && borderHitCode >= 2) resetCombo(gameState, ball.x, ball.y + gameState.ballSize);
(0, _sounds.sounds).wallBeep(ball.x);
schedulGameSound(gameState, 'wallBeep', ball.x, 1);
gameState.levelWallBounces++;
gameState.runStatistics.wall_bounces++;
}
@ -2752,11 +2809,11 @@ function ballTick(gameState, ball, delta) {
const angle = Math.atan2(-gameState.puckWidth / 2, (ball.x - gameState.puckPosition) * (gameState.perks.concave_puck ? -0.5 : 1));
ball.vx = speed * Math.cos(angle);
ball.vy = speed * Math.sin(angle);
(0, _sounds.sounds).wallBeep(ball.x);
schedulGameSound(gameState, 'wallBeep', ball.x, 1);
} else {
ball.vy *= -1;
gameState.perks.extra_life = Math.max(0, gameState.perks.extra_life - 1);
(0, _sounds.sounds).lifeLost(ball.x);
schedulGameSound(gameState, 'lifeLost', ball.x, 1);
if (!(0, _options.isOptionOn)("basic")) for(let i = 0; i < 10; i++)gameState.flashes.push({
type: "particle",
ethereal: false,
@ -2831,7 +2888,7 @@ function ballTick(gameState, ball, delta) {
}
}
if (sturdyBounce) {
(0, _sounds.sounds).wallBeep(x);
schedulGameSound(gameState, 'wallBeep', x, 1);
return;
}
if (typeof hitBrick !== "undefined") {
@ -2863,7 +2920,7 @@ function ballTick(gameState, ball, delta) {
}
}
},{"./sounds":"dQKPV","./game_utils":"cEeac","./i18n/i18n":"eNPRm","./loadGameData":"l1B4x","./settings":"5blfu","./render":"9AS2t","./gameOver":"caCAf","./game":"edeGs","./recording":"godmD","./options":"d5NoS","@parcel/transformer-js/src/esmodule-helpers.js":"gkKU3"}],"9AS2t":[function(require,module,exports,__globalThis) {
},{"./game_utils":"cEeac","./i18n/i18n":"eNPRm","./loadGameData":"l1B4x","./settings":"5blfu","./render":"9AS2t","./gameOver":"caCAf","./game":"edeGs","./recording":"godmD","./options":"d5NoS","@parcel/transformer-js/src/esmodule-helpers.js":"gkKU3"}],"9AS2t":[function(require,module,exports,__globalThis) {
var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
parcelHelpers.defineInteropFlag(exports);
parcelHelpers.export(exports, "gameCanvas", ()=>gameCanvas);
@ -3742,7 +3799,8 @@ function newGameState(params) {
levelTime: 0,
levelWallBounces: 0,
needsRender: true,
autoCleanUses: 0
autoCleanUses: 0,
...(0, _gameUtils.defaultSounds)()
};
(0, _gameStateMutators.resetBalls)(gameState);
if (!(0, _gameUtils.sumOfKeys)(gameState.perks)) {

View file

@ -8,7 +8,7 @@ import {
RunParams,
Upgrade,
} from "./types";
import { getAudioContext } from "./sounds";
import {getAudioContext, playPendingSounds} from "./sounds";
import {
currentLevelInfo,
getRowColIndex,
@ -429,7 +429,6 @@ export function bordersHitCheck(
return hhit + vhit * 2;
}
export function tick() {
const currentTick = performance.now();
const timeDeltaMs = currentTick - gameState.lastTick;
@ -458,6 +457,9 @@ export function tick() {
if (gameState.running) {
recordOneFrame(gameState);
}
if(isOptionOn('sound') ){
playPendingSounds(gameState)
}
requestAnimationFrame(tick);
}

View file

@ -1,5 +1,5 @@
import {Ball, BallLike, Coin, colorString, GameState, PerkId} from "./types";
import { sounds } from "./sounds";
import {
brickCenterX,
brickCenterY,
@ -166,7 +166,12 @@ export function resetCombo(
const lost = Math.max(0, prev - gameState.combo);
if (lost) {
for (let i = 0; i < lost && i < 8; i++) {
setTimeout(() => sounds.comboDecrease(), i * 100);
setTimeout(() => schedulGameSound(
gameState,
'comboDecrease', x, 1)
,
i * 100
);
}
if (typeof x !== "undefined" && typeof y !== "undefined") {
gameState.flashes.push({
@ -195,7 +200,9 @@ export function decreaseCombo(
const lost = Math.max(0, prev - gameState.combo);
if (lost) {
sounds.comboDecrease();
schedulGameSound(
gameState,
'comboDecrease', x, 1)
if (typeof x !== "undefined" && typeof y !== "undefined") {
gameState.flashes.push({
type: "text",
@ -254,8 +261,9 @@ export function explodeBrick(
delete gameState.bricks[index];
const x = brickCenterX(gameState, index),
y = brickCenterY(gameState, index);
sounds.explode(ball.x);
schedulGameSound(
gameState,
'explode', ball.x, 1)
const col = index % gameState.gridSize;
const row = Math.floor(index / gameState.gridSize);
@ -391,7 +399,9 @@ export function explodeBrick(
if (gameState.perks.picky_eater) {
resetCombo(gameState, ball.x, ball.y);
}
sounds.colorChange(ball.x, 0.8);
schedulGameSound(
gameState,
'colorChange', ball.x, 0.8)
gameState.lastExplosion = gameState.levelTime;
gameState.ballsColor = color;
if (!isOptionOn("basic")) {
@ -408,7 +418,9 @@ export function explodeBrick(
});
}
} else {
sounds.comboIncreaseMaybe(gameState.combo, ball.x, 1);
schedulGameSound(
gameState,
'comboIncreaseMaybe', ball.x, 1)
}
}
@ -473,6 +485,21 @@ export function pickRandomUpgrades(gameState: GameState, count: number) {
}));
}
export function schedulGameSound(gameState: GameState,
sound: keyof GameState["aboutToPlaySound"],
x: number|void,
vol: number) {
if (!vol) return
x??=gameState.offsetX+gameState.gameZoneWidth/2
const ex = gameState.aboutToPlaySound[sound] as { vol: number, x: number }
if(ex.vol){
console.log('Combined sounds for '+sound)
}
ex.x = (x * vol + ex.x * ex.vol) / (vol + ex.vol)
ex.vol += vol
}
export function addToScore(gameState: GameState, coin: Coin) {
coin.destroyed = true;
gameState.score += coin.points;
@ -500,7 +527,10 @@ export function addToScore(gameState: GameState, coin: Coin) {
if (Date.now() - gameState.lastPlayedCoinGrab > 16) {
gameState.lastPlayedCoinGrab = Date.now();
sounds.coinCatch(coin.x);
schedulGameSound(
gameState,
'coinCatch', coin.x, 1)
}
gameState.runStatistics.score += coin.points;
}
@ -702,7 +732,6 @@ export function gameStateTick(
);
}
} else if (gameState.running || gameState.levelTime) {
let playedCoinBounce = false;
const coinRadius = Math.round(gameState.coinSize / 2);
gameState.coins.forEach((coin) => {
@ -764,16 +793,20 @@ export function gameStateTick(
) {
gameState.bricks[hitBrick] = coin.color;
coin.coloredABrick = true;
sounds.colorChange(coin.x, 0.3);
schedulGameSound(
gameState,
'colorChange', coin.x, 0.3)
}
}
if (typeof hitBrick !== "undefined" || hitBorder) {
coin.vx *= 0.8;
coin.vy *= 0.8;
coin.sa *= 0.9;
if (speed > 20 && !playedCoinBounce) {
playedCoinBounce = true;
sounds.coinBounce(coin.x, 0.2);
if (speed > 20 ) {
schedulGameSound(
gameState,
'coinBounce', coin.x, 0.2)
}
if (Math.abs(coin.vy) < 3) {
@ -1024,7 +1057,10 @@ export function ballTick(gameState: GameState, ball: Ball, delta: number) {
if (gameState.perks.top_is_lava && borderHitCode >= 2) {
resetCombo(gameState, ball.x, ball.y + gameState.ballSize);
}
sounds.wallBeep(ball.x);
schedulGameSound(
gameState,
'wallBeep', ball.x, 1)
gameState.levelWallBounces++;
gameState.runStatistics.wall_bounces++;
}
@ -1051,11 +1087,16 @@ export function ballTick(gameState: GameState, ball: Ball, delta: number) {
);
ball.vx = speed * Math.cos(angle);
ball.vy = speed * Math.sin(angle);
sounds.wallBeep(ball.x);
schedulGameSound(
gameState,
'wallBeep', ball.x, 1)
} else {
ball.vy *= -1;
gameState.perks.extra_life = Math.max(0, gameState.perks.extra_life - 1);
sounds.lifeLost(ball.x);
schedulGameSound(
gameState,
'lifeLost', ball.x, 1)
if (!isOptionOn("basic")) {
for (let i = 0; i < 10; i++)
gameState.flashes.push({
@ -1164,7 +1205,9 @@ export function ballTick(gameState: GameState, ball: Ball, delta: number) {
}
if (sturdyBounce) {
sounds.wallBeep(x);
schedulGameSound(
gameState,
'wallBeep', x, 1)
return;
}
if (typeof hitBrick !== "undefined") {

View file

@ -99,3 +99,18 @@ export function distanceBetween(
) {
return Math.sqrt(distance2(a, b));
}
export function defaultSounds() {
return {
aboutToPlaySound: {
wallBeep: {vol: 0, x: 0},
comboIncreaseMaybe: {vol: 0, x: 0},
comboDecrease: {vol: 0, x: 0},
coinBounce: {vol: 0, x: 0},
explode: {vol: 0, x: 0},
lifeLost: {vol: 0, x: 0},
coinCatch: {vol: 0, x: 0},
colorChange: {vol: 0, x: 0},
}
}
}

View file

@ -1,11 +1,7 @@
import {GameState, RunParams} from "./types";
import {getTotalScore} from "./settings";
import {allLevels, upgrades} from "./loadGameData";
import {
getPossibleUpgrades,
makeEmptyPerksMap,
sumOfKeys,
} from "./game_utils";
import {defaultSounds, getPossibleUpgrades, makeEmptyPerksMap, sumOfKeys,} from "./game_utils";
import {dontOfferTooSoon, resetBalls} from "./gameStateMutators";
import {isOptionOn} from "./options";
@ -91,6 +87,7 @@ export function newGameState(params: RunParams): GameState {
levelWallBounces: 0,
needsRender: true,
autoCleanUses: 0,
...defaultSounds()
};
resetBalls(gameState);
@ -109,3 +106,4 @@ export function newGameState(params: RunParams): GameState {
}
return gameState;
}

View file

@ -1,48 +1,65 @@
import { gameState } from "./game";
import { isOptionOn } from "./options";
import {GameState} from "./types";
let lastPlay = Date.now()
export function playPendingSounds(gameState:GameState){
if(lastPlay>Date.now()-60){
return
}
lastPlay=Date.now()
for(let key in gameState.aboutToPlaySound){
const soundName = key as keyof GameState["aboutToPlaySound"]
const ex = gameState.aboutToPlaySound[soundName] as {vol:number, x:number}
if(ex.vol){
sounds[soundName](Math.min(2,ex.vol),pixelsToPan(gameState, ex.x), gameState.combo)
ex.vol=0
}
}
}
export const sounds = {
wallBeep: (pan: number) => {
wallBeep: (vol:number, pan: number, combo:number) => {
if (!isOptionOn("sound")) return;
createSingleBounceSound(800, pixelsToPan(pan));
createSingleBounceSound(800, pan, vol);
},
comboIncreaseMaybe: (combo: number, x: number, volume: number) => {
comboIncreaseMaybe: ( volume: number,pan: number,combo: number, ) => {
if (!isOptionOn("sound")) return;
let delta = 0;
if (!isNaN(lastComboPlayed)) {
if (lastComboPlayed < combo) delta = 1;
if (lastComboPlayed > combo) delta = -1;
}
playShepard(delta, pixelsToPan(x), volume);
playShepard(delta, pan, volume);
lastComboPlayed = combo;
},
comboDecrease() {
comboDecrease(volume: number,pan: number,combo: number) {
if (!isOptionOn("sound")) return;
playShepard(-1, 0.5, 0.5);
playShepard(-1, pan, volume);
},
coinBounce: (pan: number, volume: number) => {
coinBounce: (volume: number,pan: number,combo: number) => {
if (!isOptionOn("sound")) return;
createSingleBounceSound(1200, pixelsToPan(pan), volume, 0.1, "triangle");
createSingleBounceSound(1200, pan, volume, 0.1, "triangle");
},
explode: (pan: number) => {
explode: (volume: number,pan: number,combo: number) => {
if (!isOptionOn("sound")) return;
createExplosionSound(pixelsToPan(pan));
createExplosionSound(pan);
},
lifeLost(pan: number) {
lifeLost(volume: number,pan: number,combo: number) {
if (!isOptionOn("sound")) return;
createShatteredGlassSound(pixelsToPan(pan));
createShatteredGlassSound(pan);
},
coinCatch(pan: number) {
coinCatch(volume: number,pan: number,combo: number) {
if (!isOptionOn("sound")) return;
createSingleBounceSound(900, pixelsToPan(pan), 0.8, 0.1, "triangle");
createSingleBounceSound(900, (pan), volume, 0.1, "triangle");
},
colorChange(pan: number, volume: number) {
createSingleBounceSound(400, pixelsToPan(pan), volume, 0.5, "sine");
createSingleBounceSound(800, pixelsToPan(pan), volume * 0.5, 0.2, "square");
colorChange(volume: number,pan: number,combo: number) {
createSingleBounceSound(400, pan, volume, 0.5, "sine");
createSingleBounceSound(800, pan, volume * 0.5, 0.2, "square");
},
};
@ -158,7 +175,7 @@ function createExplosionSound(pan = 0.5) {
noiseSource.stop(context.currentTime + 1);
}
function pixelsToPan(pan: number) {
function pixelsToPan(gameState:GameState, pan: number) {
return Math.max(
0,
Math.min(

10
src/types.d.ts vendored
View file

@ -237,6 +237,16 @@ export type GameState = {
levelTime: number;
levelWallBounces: number;
autoCleanUses: number;
aboutToPlaySound:{
wallBeep:{vol:number, x:number},
comboIncreaseMaybe:{vol:number, x:number},
comboDecrease:{vol:number, x:number},
coinBounce:{vol:number, x:number},
explode:{vol:number, x:number},
lifeLost:{vol:number, x:number},
coinCatch:{vol:number, x:number},
colorChange:{vol:number, x:number},
}
};
export type RunParams = {