@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
45 lines (35 loc) • 1.15 kB
JavaScript
import { Action } from "../../../src/core/process/undo/Action.js";
class ComponentAddAction extends Action {
constructor(entity, component) {
super();
this.entity = entity;
this.component = component;
/**
*
* @type {EntityComponentDataset}
*/
this.dataset = null;
}
async apply(editor) {
/**
* @type {EntityManager}
*/
const em = editor.engine.entityManager;
/**
*
* @type {EntityComponentDataset}
*/
const dataset = em.dataset;
this.dataset = dataset;
const clazz = this.component.constructor;
if (dataset.getComponent(this.entity, clazz)) {
throw new Error(`entity ${this.entity} already has a '${clazz.typeName}' component`);
}
dataset.addComponentToEntity(this.entity, this.component);
}
async revert(editor) {
const clazz = this.component.constructor;
this.dataset.removeComponentFromEntity(this.entity, clazz);
}
}
export default ComponentAddAction;