UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

169 lines (144 loc) 4 kB
import List from '../../../core/collection/list/List.js'; import { BinaryClassSerializationAdapter } from "../../ecs/storage/binary/BinaryClassSerializationAdapter.js"; class Rule { constructor() { /** * * @type {Array<string>} */ this.tracks = []; /** * * @type {String|null} */ this.startEvent = null; /** * * @type {String|null} */ this.stopEvent = null; /** * * @type {boolean} */ this.loop = false; /** * * @type {number} */ this.volume = 1; /** * * @type {String|null} */ this.channel = null; } toJSON() { return { tracks: this.tracks, startEvent: this.startEvent, stopEvent: this.stopEvent, loop: this.loop, volume: this.volume, channel: this.channel }; } fromJSON(json) { this.url = json.url; this.startEvent = json.startEvent; this.stopEvent = json.stopEvent; this.loop = json.loop; if (typeof json.volume === 'number') { this.volume = json.volume; } else { this.volume = 1; } if (json.tracks !== undefined) { this.tracks = json.tracks; } else { this.tracks = []; } //legacy "url" attribute if (typeof json.url === "string") { console.warn("deprecated 'url' attribute, use 'tracks' instead"); this.tracks.push(json.url); } this.channel = json.channel; } /** * * @param {BinaryBuffer} buffer */ toBinaryBuffer(buffer) { buffer.writeUTF8String(this.startEvent); buffer.writeUTF8String(this.stopEvent); const numTracks = this.tracks.length; buffer.writeUint8(numTracks); for (let i = 0; i < numTracks; i++) { const track = this.tracks[i]; buffer.writeUTF8String(track); } buffer.writeUint8(this.loop ? 1 : 0); buffer.writeFloat64(this.volume); buffer.writeUTF8String(this.channel); } /** * * @param {BinaryBuffer} buffer */ fromBinaryBuffer(buffer) { this.startEvent = buffer.readUTF8String(); this.stopEvent = buffer.readUTF8String(); const numTracks = buffer.readUint8(); this.tracks = []; for (let i = 0; i < numTracks; i++) { const track = buffer.readUTF8String(); this.tracks[i] = track; } this.loop = buffer.readUint8() !== 0; this.volume = buffer.readFloat64(); this.channel = buffer.readUTF8String(); } } class SoundController { /** * * @constructor * @property {List.<Rule>} rule */ constructor(options) { this.rules = new List(); if (options !== undefined) { this.fromJSON(options); } } fromJSON(json) { this.rules.fromJSON(json, Rule); } toJSON() { return this.rules.toJSON(); } } SoundController.Rule = Rule; SoundController.typeName = "SoundController"; export default SoundController; export class SoundControllerSerializationAdapter extends BinaryClassSerializationAdapter { klass = SoundController; version = 0; /** * * @param {BinaryBuffer} buffer * @param {SoundController} value */ serialize(buffer, value) { value.rules.toBinaryBuffer(buffer); } /** * * @param {BinaryBuffer} buffer * @param {SoundController} value */ deserialize(buffer, value) { value.rules.fromBinaryBuffer(buffer, Rule); } }