UNPKG

playcanvas

Version:

Open-source WebGL/WebGPU 3D engine for the web

55 lines (54 loc) 2.01 kB
import { EventHandler } from "../../core/event-handler.js"; import { AppBase } from "../app-base.js"; import { ScriptAttributes } from "./script-attributes.js"; import { ScriptType } from "./script-type.js"; import { ScriptTypes } from "./script-types.js"; import { reservedScriptNames } from "./constants.js"; import { Script, getScriptName } from "./script.js"; function getReservedScriptNames() { return reservedScriptNames; } function createScript(name, app) { if (reservedScriptNames.has(name)) { throw new Error(`Script name '${name}' is reserved, please rename the script`); } const scriptType = function(args) { EventHandler.prototype.initEventHandler.call(this); ScriptType.prototype.initScriptType.call(this, args); }; scriptType.prototype = Object.create(ScriptType.prototype); scriptType.prototype.constructor = scriptType; scriptType.extend = ScriptType.extend; scriptType.attributes = new ScriptAttributes(scriptType); registerScript(scriptType, name, app); return scriptType; } const reservedAttributes = {}; ScriptAttributes.reservedNames.forEach((value, value2, set) => { reservedAttributes[value] = 1; }); createScript.reservedAttributes = reservedAttributes; function registerScript(script, name, app) { if (typeof script !== "function") { throw new Error(`script class: '${script}' must be a constructor function (i.e. class).`); } if (!(script.prototype instanceof Script)) { throw new Error(`script class: '${ScriptType.__getScriptName(script)}' does not extend pc.Script.`); } name = name || Object.prototype.hasOwnProperty.call(script, "__name") && script.__name || getScriptName(script); if (!name) { return; } if (reservedScriptNames.has(name)) { throw new Error(`script name: '${name}' is reserved, please change script name`); } script.__name = name; const registry = app ? app.scripts : AppBase.getApplication().scripts; registry.add(script); ScriptTypes.push(script); } export { createScript, getReservedScriptNames, registerScript };