import { System, Entity } from "ecsy"
import { PathWalker } from "../Components/pathWalker"
import { addOrSetComponent } from "../util";
import { Position } from "../Components/position";

// MovableSystem
export class PathWalkerSystem extends System {
  priority = 50
  // This method will get called on every frame by default
  execute(delta : number) {
    // Iterate through all the entities on the query
    this.queries.walkers.results.forEach((entity: Entity) => {
      let walker = entity.getMutableComponent(PathWalker)
      walker.progress += walker.speed * delta
      let position = walker.path.calculateAbsolutePoint(walker.progress)
      addOrSetComponent(entity, Position, <Position>{value: position})

      let pathLength = walker.path.calculateLength()
      if(walker.progress > pathLength)
        entity.removeComponent(PathWalker)
    });
  }

  static queries = {
    walkers: {
      components: [ PathWalker ]
    }
  }
  queries: any
}