import * as RE from 'rogue-engine'
import * as THREE from 'three'

export default class FloorCheckComponent extends RE.Component {
  raycaster: THREE.Raycaster
  down = new THREE.Vector3(0, -1, 0)
  @RE.props.num() near: number = 0
  @RE.props.num() far: number = Infinity
  isOnFloor: boolean = false
  @RE.props.num() touchingFloorDistance: number = 1
  awake() {
    this.raycaster = new THREE.Raycaster(this.object3d.position, this.down, this.near, this.far)
  }

  start() {

  }

  update() {
    const wasOnFloor = this.isOnFloor
    this.raycaster.set(this.object3d.position, this.down)

    let intersects = this.raycaster.intersectObjects(RE.App.currentScene.children)
    intersects.forEach(intersect => {
      this.isOnFloor = intersect.distance <= this.touchingFloorDistance
    })
  

    // if(this.isOnFloor && !wasOnFloor) {
    //   RE.Debug.log("On floor")
    // }
  }
}

RE.registerComponent(FloorCheckComponent);
