Browse Source

Refactor "Voice channels" into NiceGame as NiceCommentary

develop
Rob Colbert 2 years ago
parent
commit
40f9ea156a
  1. 6
      game/js/game-app.js
  2. 34
      game/js/lib/game-enemies.js
  3. 17
      game/js/lib/game-player.js
  4. 89
      game/js/lib/game-voice-channels.js
  5. 4
      yarn.lock

6
game/js/game-app.js

@ -9,6 +9,7 @@ import '../less/style.less';
import {
NiceColor,
NiceCommentary,
NiceEasing,
NiceGame,
NiceInputTools,
@ -23,7 +24,6 @@ import GameEggSimulator from './lib/game-egg-simulator.js';
import GameEnemies from './lib/game-enemies.js';
import GameEnemyFormation from './lib/game-enemy-formation.js';
import { GameVoiceChannels } from './lib/game-voice-channels.js';
export default class GameApp extends NiceGame {
@ -46,8 +46,6 @@ export default class GameApp extends NiceGame {
},
},
};
this.voiceChannels = new GameVoiceChannels(this);
}
async run ( ) {
@ -80,6 +78,8 @@ export default class GameApp extends NiceGame {
this.startMusicStream('/dist/assets/audio/music/blueberries');
this.commentary = new NiceCommentary(this.audio);
await this.loadGameAssets();
this.mode = 'menu';

34
game/js/lib/game-enemies.js

@ -33,26 +33,26 @@ export default class GameEnemies {
this.tweens = { };
this.voiceChannels = { };
this.voiceChannels.fuentes = this.game.voiceChannels.addChannel('nick');
this.voiceChannels.torba = this.game.voiceChannels.addChannel('torba');
this.voiceChannels.fuentes = this.game.commentary.createChannel('nick', '/dist/assets/audio/vox', 1);
this.voiceChannels.torba = this.game.commentary.createChannel('torba', '/dist/assets/audio/vox', 2);
}
async load ( ) {
const jobs = [ ];
jobs.push(this.voiceChannels.fuentes.loadQuip('fuentes-datingisgay'));
jobs.push(this.voiceChannels.fuentes.loadQuip('fuentes-gaysdategirls'));
jobs.push(this.voiceChannels.fuentes.loadQuip('fuentes-itsallgay'));
jobs.push(this.voiceChannels.fuentes.loadQuip('fuentes-needy'));
jobs.push(this.voiceChannels.fuentes.loadQuip('fuentes-sexisgay'));
jobs.push(this.voiceChannels.fuentes.loadQuip('fuentes-straightest'));
jobs.push(this.voiceChannels.fuentes.loadQuip('fuentes-suss'));
jobs.push(this.voiceChannels.fuentes.loadQuip('fuentes-timewithwoman'));
jobs.push(this.voiceChannels.fuentes.loadSound('fuentes-datingisgay'));
jobs.push(this.voiceChannels.fuentes.loadSound('fuentes-gaysdategirls'));
jobs.push(this.voiceChannels.fuentes.loadSound('fuentes-itsallgay'));
jobs.push(this.voiceChannels.fuentes.loadSound('fuentes-needy'));
jobs.push(this.voiceChannels.fuentes.loadSound('fuentes-sexisgay'));
jobs.push(this.voiceChannels.fuentes.loadSound('fuentes-straightest'));
jobs.push(this.voiceChannels.fuentes.loadSound('fuentes-suss'));
jobs.push(this.voiceChannels.fuentes.loadSound('fuentes-timewithwoman'));
jobs.push(this.voiceChannels.torba.loadQuip('torba-stirthepot'));
jobs.push(this.voiceChannels.torba.loadQuip('torba-controversy'));
jobs.push(this.voiceChannels.torba.loadQuip('torba-talkwithnick'));
jobs.push(this.voiceChannels.torba.loadQuip('torba-feminine'));
jobs.push(this.voiceChannels.torba.loadSound('torba-stirthepot'));
jobs.push(this.voiceChannels.torba.loadSound('torba-controversy'));
jobs.push(this.voiceChannels.torba.loadSound('torba-talkwithnick'));
jobs.push(this.voiceChannels.torba.loadSound('torba-feminine'));
return Promise.all(jobs);
}
@ -65,7 +65,7 @@ export default class GameEnemies {
}
this.game.audio.playSound(this.getRandomImpactSound());
if (Math.random() > QUIP_PROBABILITY) {
this.game.player.voiceChannel.playRandomQuip();
this.game.player.voiceChannel.playRandomSound();
}
return true;
});
@ -86,7 +86,7 @@ export default class GameEnemies {
this.spawnDescender(this.game.images['fuentes-fagface']);
// this.game.audio.playSound('enemy-spawn');
this.voiceChannels.fuentes.playRandomQuip();
this.voiceChannels.fuentes.playRandomSound();
this.game.flashBorder(new NiceColor(255, 255, 255, 1.0));
}
@ -106,7 +106,7 @@ export default class GameEnemies {
this.spawnDescender(this.game.images.torba);
if (Math.random() > QUIP_PROBABILITY) {
this.voiceChannels.torba.playRandomQuip();
this.voiceChannels.torba.playRandomSound();
} else {
this.game.audio.playSound('enemy-spawn');
}

17
game/js/lib/game-player.js

@ -26,8 +26,7 @@ export default class GamePlayer extends NiceSprite {
this.registration = new NiceVector2d(CELL_WIDTH / 2, CELL_HEIGHT);
this.margin = CELL_WIDTH / 2;
this.quips = [ ];
this.voiceChannel = this.game.voiceChannels.addChannel('player');
this.voiceChannel = this.game.commentary.createChannel('player', '/dist/assets/audio/vox', 0);
}
async load ( ) {
@ -35,13 +34,13 @@ export default class GamePlayer extends NiceSprite {
jobs.push(super.load('/dist/assets/img/baja-tex.png', CELL_WIDTH, CELL_HEIGHT));
jobs.push(this.voiceChannel.loadQuip('tex-biggerfish'));
jobs.push(this.voiceChannel.loadQuip('tex-candy'));
jobs.push(this.voiceChannel.loadQuip('tex-discontent'));
jobs.push(this.voiceChannel.loadQuip('tex-notreading'));
jobs.push(this.voiceChannel.loadQuip('tex-quotepost'));
jobs.push(this.voiceChannel.loadQuip('tex-shutuptwo'));
jobs.push(this.voiceChannel.loadQuip('tex-stayfresh'));
jobs.push(this.voiceChannel.loadSound('tex-biggerfish'));
jobs.push(this.voiceChannel.loadSound('tex-candy'));
jobs.push(this.voiceChannel.loadSound('tex-discontent'));
jobs.push(this.voiceChannel.loadSound('tex-notreading'));
jobs.push(this.voiceChannel.loadSound('tex-quotepost'));
jobs.push(this.voiceChannel.loadSound('tex-shutuptwo'));
jobs.push(this.voiceChannel.loadSound('tex-stayfresh'));
return Promise.all(jobs);
}

89
game/js/lib/game-voice-channels.js

@ -1,89 +0,0 @@
// lib/game-voice-channels.js
// Copyright (C) 2022 Rob Colbert @[email protected]
// License: Apache-2.0
'use strict';
import { NiceLog } from "dtp-nice-game";
export class VoiceChannel {
constructor (game, name) {
this.game = game;
this.name = name;
this.log = new NiceLog(`VoiceChannel:${name}`);
this.log.enable();
this.playing = false;
this.quips = [ ];
}
async loadQuip (soundId) {
if (this.quips.includes(soundId)) {
throw new Error(`Quip ${soundId} has already been loaded`);
}
this.quips.push(soundId);
return this.game.audio.loadSound(soundId, `/dist/assets/audio/vox/${soundId}.wav`);
}
playRandomQuip ( ) {
this.log.debug('playRandomQuip', 'request to play random quipt', { playing: this.playing });
if (this.playing) {
return;
}
const soundId = this.getRandomQuipSoundId();
this.log.debug('playRandomQuip', 'playing random voiceover sound', { soundId });
const { source } = this.game.audio.playSound(soundId);
source.addEventListener('ended', ( ) => {
this.log.debug('playRandomQuip', 'quip ending', { soundId });
this.playing = false;
});
this.playing = true;
}
getRandomQuipSoundId ( ) {
if (!Array.isArray(this.quips) || (this.quips.length === 0)) {
throw new Error(`No valid quips loaded on channel "${this.name}"`);
}
const quipIdx = Math.floor(Math.random() * (this.quips.length - 1));
const quipSoundId = this.quips[quipIdx];
this.log.debug('getRandomQuipSoundId', 'selected a random quip', { quipIdx, quipSoundId });
return quipSoundId;
}
}
/**
* GameVoiceChannels tracks audio playing in named channels and will prevent the
* game from playing a sound on a channel if a sound is currently playing on
* that channel.
*/
export class GameVoiceChannels {
constructor (game) {
this.game = game;
this.channels = { };
}
addChannel (channelName) {
const channel = new VoiceChannel(this.game, channelName);
this.channels[channelName] = channel;
return channel;
}
getChannel (channelName) {
const channel = this.channels[channelName];
if (!channel) {
throw new Error(`Invalid voice channel "${channelName}"`);
}
return channel;
}
playSound (channelName, soundId) {
const channel = this.getChannel(channelName);
channel.playSound(soundId);
}
}

4
yarn.lock

@ -833,8 +833,8 @@ dotenv@^16.0.0:
integrity sha512-qD9WU0MPM4SWLPJy/r2Be+2WgQj8plChsyrCNQzW/0WjvcJQiKQJ9mH3ZgB3fxbUUxgc/11ZJ0Fi5KiimWGz2Q==
"dtp-nice-game@https://git.digitaltelepresence.com/digital-telepresence/dtp-nice-game.git":
version "0.1.12"
resolved "https://git.digitaltelepresence.com/digital-telepresence/dtp-nice-game.git#829affa2dad2c8c78b7f7ec64e974fafb49bad1b"
version "0.1.14"
resolved "https://git.digitaltelepresence.com/digital-telepresence/dtp-nice-game.git#651049ff47414696381d8dddc85ba21bc954e632"
dependencies:
jshint "^2.13.4"

Loading…
Cancel
Save