import { System, Entity, Not } from "ecsy"
import { PathWalker } from "../Components/pathWalker"
import { Position } from "../Components/position"
import { WalkRandomly } from "../Components/walkRandomly"
import { roomBounds } from "../constants"
import { Path } from "../Datatypes/MathTypes/path"

export class RandomWalkSystem extends System {
  priority = 0
  // 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) => {
      var pos = entity.getComponent(Position).value
      var target = roomBounds.randomPoint()
      var path = new Path(pos, target)
      entity.addComponent(PathWalker, <PathWalker>{ path: path, speed: 10 })
    })
  }

  static queries = {
    walkers: {
      components: [WalkRandomly, Position, Not(PathWalker)],
    },
  }
  queries: any
}
