playcanvas
Version:
Open-source WebGL/WebGPU 3D engine for the web
119 lines (118 loc) • 3.26 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
import { EventHandler } from "../../core/event-handler.js";
const _Component = class _Component extends EventHandler {
/**
* Base constructor for a Component.
*
* @param {ComponentSystem} system - The ComponentSystem used to create this component.
* @param {Entity} entity - The Entity that this Component is attached to.
*/
constructor(system, entity) {
super();
/**
* The ComponentSystem used to create this Component.
*
* @type {ComponentSystem}
*/
__publicField(this, "system");
/**
* The Entity that this Component is attached to.
*
* @type {Entity}
*/
__publicField(this, "entity");
this.system = system;
this.entity = entity;
if (this.system.schema && !this._accessorsBuilt) {
this.buildAccessors(this.system.schema);
}
this.on("set", function(name, oldValue, newValue) {
this.fire(`set_${name}`, name, oldValue, newValue);
});
this.on("set_enabled", this.onSetEnabled, this);
}
/** @ignore */
static _buildAccessors(obj, schema) {
schema.forEach((descriptor) => {
const name = typeof descriptor === "object" ? descriptor.name : descriptor;
Object.defineProperty(obj, name, {
get: function() {
return this.data[name];
},
set: function(value) {
const data = this.data;
const oldValue = data[name];
data[name] = value;
this.fire("set", name, oldValue, value);
},
configurable: true
});
});
obj._accessorsBuilt = true;
}
/** @ignore */
buildAccessors(schema) {
_Component._buildAccessors(this, schema);
}
/** @ignore */
onSetEnabled(name, oldValue, newValue) {
if (oldValue !== newValue) {
if (this.entity.enabled) {
if (newValue) {
this.onEnable();
} else {
this.onDisable();
}
}
}
}
/** @ignore */
onEnable() {
}
/** @ignore */
onDisable() {
}
/** @ignore */
onPostStateChange() {
}
/**
* Access the component data directly. Usually you should access the data properties via the
* individual properties as modifying this data directly will not fire 'set' events.
*
* @type {*}
* @ignore
*/
get data() {
const record = this.system.store[this.entity.guid];
return record ? record.data : null;
}
/**
* Sets the enabled state of the component.
*
* @type {boolean}
*/
set enabled(arg) {
}
/**
* Gets the enabled state of the component.
*
* @type {boolean}
*/
get enabled() {
return true;
}
};
/**
* Component order. When an entity with multiple components gets enabled, this order specifies
* in which order the components get enabled. The lowest number gets enabled first.
*
* @type {number} - Component order number.
* @private
*/
__publicField(_Component, "order", 0);
let Component = _Component;
export {
Component
};