sparkletown/sparkle

View on GitHub
src/components/templates/AnimateMap/game/map/systems/MotionTeleportSystem.ts

Summary

Maintainability
A
3 hrs
Test Coverage
import { Engine, NodeList } from "@ash.ts/ash";

import { Easing } from "../../utils/Easing";
import EntityFactory from "../entities/EntityFactory";
import { MotionTeleportNode } from "../nodes/MotionTeleportNode";

import { MotionBaseSystem } from "./MotionBaseSystem";

export class MotionTeleportSystem extends MotionBaseSystem {
  private nodes?: NodeList<MotionTeleportNode>;

  constructor(public creator: EntityFactory) {
    super();
  }

  addToEngine(engine: Engine) {
    this.nodes = engine.getNodeList(MotionTeleportNode);
    this.nodes.nodeAdded.add(this.nodeAdded);
  }

  removeFromEngine(engine: Engine) {
    if (this.nodes) {
      this.nodes.nodeAdded.remove(this.nodeAdded);
      this.nodes = undefined;
    }
  }

  update(time: number) {
    for (
      let node: MotionTeleportNode | null | undefined = this.nodes?.head;
      node;
      node = node.next
    ) {
      if (node.tween.toX) {
        node.tween.toX.update(time);
        node.position.x = node.tween.toX.currentValue;
      }
      if (node.tween.toY) {
        node.tween.toY.update(time);
        node.position.y = node.tween.toY.currentValue;
      }
    }
  }

  private nodeAdded = (node: MotionTeleportNode) => {
    const duration = 500;
    const toX: Easing = new Easing(node.position.x, node.tween.x, duration);
    const toY: Easing = new Easing(node.position.y, node.tween.y, duration);

    node.tween.toX = toX;
    node.tween.toY = toY;
  };
}