UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

77 lines (63 loc) 2.64 kB
import { Animation } from '../../../ecs/animation/Animation.js'; import { AnimationClip } from "../../../ecs/animation/AnimationClip.js"; import { System } from '../../../ecs/System.js'; import AnimationController from './AnimationController.js'; class AnimationControllerSystem extends System { constructor() { super(); this.dependencies = [AnimationController]; this.removers = []; } /** * * @param {AnimationController} component * @param entity */ link(component, entity) { const em = this.entityManager; const dataset = em.dataset; const callbacks = this.removers[entity] = []; function registerRule(r) { function startEventHandler() { const animation = dataset.getComponent(entity, Animation); //remove existing clips with that animation removeAnimation(animation); //create a new animation according to the rule const clip = new AnimationClip(); clip.fromJSON({ name: r.animation, timeScale: r.speed, repeatCount: r.loop ? Number.POSITIVE_INFINITY : 1, weight: r.weight }); animation.clips.add(clip); } function removeAnimation(animation) { animation.clips.removeIf(function (c) { return c.name.getValue() === r.animation; }); } function stopEventHandler() { const animation = dataset.getComponent(entity, Animation); removeAnimation(animation); } if (typeof r.stopEvent === "string") { dataset.addEntityEventListener(entity, r.stopEvent, stopEventHandler); callbacks.push({ name: r.stopEvent, handler: stopEventHandler }); } dataset.addEntityEventListener(entity, r.startEvent, startEventHandler); callbacks.push({ name: r.startEvent, handler: startEventHandler }); } component.rules.forEach(registerRule); } unlink(component, entity) { const callbacks = this.removers[entity]; delete this.removers[entity]; const em = this.entityManager; const dataset = em.dataset; callbacks.forEach(function (cb) { dataset.removeEntityEventListener(entity, cb.name, cb.handler); }); } } export default AnimationControllerSystem;