import * as RE from 'rogue-engine';
import { Vector2 } from 'three';
import RaycastReceiver from '../RaycastReceiver.re';

export default class ShipMoveReceiver extends RaycastReceiver {

  @RE.Prop("Object3D") ship

  awake() {

  }

  start() {
    // this.ship = RE.getComponentByName("Player", this.parent)
    // //let ship = RE.Runtime.scene.getObjectByName("Ship")
    // if(this.ship == null) {
    //   RE.Debug.logError("Did not find ship")
    //   return
    // }
    this.player = RE.getComponentByName("Player", this.ship)
    this.targetAngle = this.player.angle
  }

  update() {
    this.object3d.rotation.z = - this.player.angle + (Math.PI / 2)
  }

  onMouseOver(intersect) {

    if(RE.Input.mouse.getButtonPressed(2)) {
      return
    }

    this.targetAngle = this.angleTo(intersect.uv, new Vector2(0.5, 0.5))
    this.player.targetAngle = this.targetAngle

    
    if (RE.Input.touch.touches.length == 1) {
      this.player.thrust()
    }
  }

  onMouseOut(){
  }

  angleTo(source, destination) {
    return Math.atan2(source.y - destination.y, source.x - destination.x)
  }
}

RE.registerComponent(ShipMoveReceiver);
