UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

116 lines (91 loc) 2.35 kB
import { resolvePath } from "../../../core/json/resolvePath.js"; /** * * @deprecated use {@link Blackboard} class instead */ class PropertySet { /** * @param options * @constructor */ constructor(options) { this.data = {}; if (options !== null && options !== undefined) { this.fromJSON(options); } } /** * @template T * @param {string} path * @return {T} */ get(path) { return resolvePath(this.data, path); } /** * @template T * @param {string} path * @param {T} defaultValue * @return {T} */ getOrDefault(path, defaultValue) { let result; try { result = this.get(path); } catch (e) { result = defaultValue; this.set(path, defaultValue); } return result; } /** * * @param {String} path * @param {Number|Boolean|String} value */ set(path, value) { if (path.charAt(0) === '/') { //strip leading slash if it is present path = path.slice(1); } const parts = path.split("/"); const l = parts.length; if (parts.length === 0) { this.data = value; } else { let current = this.data; for (let i = 0; i < l - 1; i++) { const part = parts[i]; if (!current.hasOwnProperty(part)) { current[part] = {}; } current = current[part]; } const lastPropertyName = parts[l - 1]; current[lastPropertyName] = value; } } toJSON() { return JSON.parse(JSON.stringify(this.data)); } fromJSON(json) { this.data = json; } /** * * @param {BinaryBuffer} buffer */ toBinaryBuffer(buffer) { buffer.writeUTF8String(JSON.stringify(this.data)); } /** * * @param {BinaryBuffer} buffer */ fromBinaryBuffer(buffer) { const string = buffer.readUTF8String(); this.data = JSON.parse(string); } } PropertySet.typeName = "PropertySet"; export default PropertySet;