Add some sound functionality

This commit is contained in:
2021-11-05 14:28:01 +01:00
parent dd066f0aa3
commit c5a4167846
32 changed files with 187 additions and 30 deletions

View File

@@ -40,6 +40,11 @@ export default class ItemBuilder {
return this;
}
withDropCallback(callback) {
this.item.addDropCallback(callback);
return this;
}
withTickCallback(callback) {
this.item.addTickCallback(callback);
return this;

View File

@@ -3,6 +3,6 @@ export default function EchoCommand(args, context) {
context.print(`Usage: echo <on/off>`);
} else {
context.setInputEcho(args[1] == "on" ? true : false);
context.print(`Command echo is now ${args[1]}`);
context.print(`Command echo is now ${args[1]}.`);
}
}

View File

@@ -12,7 +12,7 @@ export default function LookCommand(args, context) {
}
}
if (!item) {
context.output.say(`I could not find a ${args[1]}`);
context.output.say(`I could not find a ${args[1]}.`);
} else {
context.output.say(item.name);
context.output.say(item.description);

View File

@@ -9,14 +9,14 @@ export default function TakeCommand(args, context) {
}
}
if (!item) {
context.print(`You can't find any ${args[1]}`);
context.print(`You can't find any ${args[1]}.`);
} else {
if (!item.takeable) {
context.print(`You can't take ${item.name}`);
context.print(`You can't take ${item.name}.`);
} else {
room.removeItem(item.id);
context.player.addItem(item.id);
context.print(`You take ${item.name}`);
context.print(`You take ${item.name}.`);
item.onTake();
}
}

View File

@@ -9,7 +9,16 @@ export default async function UseCommand(args, context) {
}
}
if (!item) {
context.output.say(`I could not find a ${args[1]}`);
const items = context.player.getInventory();
for (let i of items) {
if (i.name.includes(args[1])) {
item = i;
break;
}
}
}
if (!item) {
context.output.say(`I could not find a ${args[1]}.`);
} else {
await item.onUse();
}

View File

@@ -24,7 +24,6 @@ export default class Game {
}
init(data) {
console.log(data);
this.rooms = data.rooms.map((room) => {
room.context = this;
return room;
@@ -33,7 +32,7 @@ export default class Game {
item.context = this;
return item;
});
this.state = data.state;
this.state = data.state || State;
this.commandHandler.addCommands(data.commands);
this.player = new Player();
this.player.context = this;
@@ -49,7 +48,7 @@ export default class Game {
start() {
this.interval = setInterval(() => this.advanceTick(), 1000);
}
stop() {
clearInterval(this.interval);
this.interval = null;
@@ -70,16 +69,38 @@ export default class Game {
examineItems() {
const room = this.getRoom(this.player.currentRoom);
const items = room.getItems();
items.forEach((item) => this.output.say(item.name));
if (items.length < 1) return;
let itemDescription = `You see `;
items.forEach((item, index) => {
if (index < items.length - 2) {
itemDescription += `${item.name}, `;
} else if (index < items.length - 1) {
itemDescription += `${item.name} and `;
} else {
itemDescription += item.name
}
});
this.output.say(itemDescription + ".");
}
examineExits() {
const room = this.getRoom(this.player.currentRoom);
let exits = [];
let exitDescription = "You can go ";
for (let exit of room.exits.keys()) {
exitDescription += " " + exit;
const exitKeys = room.exits.keys();
for (let exit of exitKeys) {
exits.push(exit);
}
this.output.say(exitDescription);
exits.forEach((item, index) => {
if (index < exits.length - 2) {
exitDescription += `${item}, `;
} else if (index < exits.length - 1) {
exitDescription += `${item} and `;
} else {
exitDescription += item
}
});
this.output.say(exitDescription + ".");
}
getRoom(id) {

View File

@@ -17,9 +17,12 @@ export default class Item {
}
async onTake() {
if (this.takeCallback) return this.takeCallback();
if (this.takeCallback) return this.takeCallback(this.context);
}
async onDrop() {
if (this.dropCallback) return this.dropCallback(this.context);
}
async onTick() {
if (this.tickCallback) return this.tickCallback(this.context);
}

View File

@@ -1,13 +1,16 @@
import { TTS } from '../framework/tts';
import { AriaOutput } from '../framework/tts/outputs/aria';
import Sound from './sound';
export default class Output {
constructor() {
this.tts = new TTS(new AriaOutput());
this.history = document.getElementById("output-area");
this.sound = new Sound();
}
say(string) {
this.sound.play(`assets/scroll.wav`);
const node = document.createElement("p");
string.split("\n").forEach((line) => {
node.appendChild(document.createTextNode(line));
@@ -16,4 +19,8 @@ export default class Output {
this.history.appendChild(node);
// this.tts.speak(string);
}
play(file) {
this.sound.play(file);
}
}

18
src/engine/sound.js Normal file
View File

@@ -0,0 +1,18 @@
import Resonator from '../framework/resonator';
export default class Sound {
constructor() {
this.res = new Resonator();
this.res.setEnvironmentImpulse(`assets/Greek7EchoHall.wav`);
this.ambience = null;
this.music = null;
this.previousAmbience = null;
this.previousMusic = null;
}
play(file) {
const sound = this.res.loadImmediate(file);
sound.play();
}
}

View File

@@ -4,6 +4,9 @@ class State {
}
get(key) {
if (!this.states.has(key)) {
return null;
}
return this.states.get(key);
}