const EventEmitter = require('eventemitter3');
const Utils = require('../common/Utils');

/**
 * This class handles touch device controls
 */
class MobileControls{

    constructor(renderer){
        Object.assign(this, EventEmitter.prototype);
        this.renderer = renderer;

        this.touchContainer = document.querySelector('#renderCanvas');
        this.setupListeners();

        this.activeInput = {
            up: false,
            left: false,
            right: false
        };

        let onRequestAnimationFrame = () => {
            this.handleMovementInput();
            window.requestAnimationFrame(onRequestAnimationFrame);
        };

        onRequestAnimationFrame();

    }

    setupListeners(){
        let touchHandler = (e) => {
            // If there's exactly one finger inside this element
            let touch = e.targetTouches[0];
            this.currentTouch = {
                x: touch.pageX,
                y: touch.pageY
            };

            if (e.type === 'touchstart' && e.targetTouches[1]){
                this.emit('fire');
            }
        };

        this.touchContainer.addEventListener('touchstart', touchHandler, false);
        this.touchContainer.addEventListener('touchmove', (e) =>{
            touchHandler(e);
            // if ingame prevent scrolling
            if (this.renderer.playerShip) {
                e.preventDefault();
            }
        }, false);

        this.touchContainer.addEventListener('touchend', (e) => {
            this.currentTouch = false;
            this.activeInput.up = false;
            this.activeInput.left = false;
            this.activeInput.right = false;
            this.renderer.onKeyChange({ keyName: 'up', isDown: false });
        }, false);

        document.querySelector('.fireButton').addEventListener('click', () => {
            this.emit('fire');
        });
    }

    handleMovementInput(){
        // no touch, no movement
        if (!this.currentTouch) return;

        // by default no touch
        this.activeInput.right = false;
        this.activeInput.left = false;
        this.activeInput.up = false;

        let playerShip = this.renderer.playerShip;
        // no player ship, no movement
        if (!playerShip) return;

        let playerShipScreenCoords = this.renderer.gameCoordsToScreen(playerShip);

        let dx = this.currentTouch.x - playerShipScreenCoords.x;
        let dy = this.currentTouch.y - playerShipScreenCoords.y;
        let shortestArc = Utils.shortestArc(Math.atan2(dx, -dy),
            Math.atan2(Math.sin(playerShip.actor.shipContainerSprite.rotation + Math.PI / 2), Math.cos(playerShip.actor.shipContainerSprite.rotation + Math.PI / 2)));

        let rotateThreshold = 0.3;
        let distanceThreshold = 120;

        // turn left or right
        if (shortestArc > rotateThreshold){
            this.activeInput.left = true;
            this.activeInput.right = false;
        } else if (shortestArc < -rotateThreshold) {
            this.activeInput.right = true;
            this.activeInput.left = false;
        }

        // don't turn if too close
        if (Math.sqrt(dx * dx + dy * dy) > distanceThreshold) {
            this.activeInput.up = true;
            this.renderer.onKeyChange({ keyName: 'up', isDown: true });
        } else {
            this.renderer.onKeyChange({ keyName: 'up', isDown: false });
        }

    }

}

module.exports = MobileControls;

@@ -371,25 +371,24 @@
 isteners();%0A
-%0A
         this
@@ -392,106 +392,25 @@
 his.
-activeInput = %7B%0A            up: false,%0A            left: false,%0A            right: false%0A        %7D
+destinations = %5B%5D
 ;%0A%0A 
@@ -1021,32 +1021,34 @@
     %7D;%0A%0A        
+//
 this.touchContai
@@ -1104,32 +1104,34 @@
 false);%0A        
+//
 this.touchContai
@@ -1184,16 +1184,18 @@
         
+//
 touchHan
@@ -1217,16 +1217,18 @@
       //
+//
  if inga
@@ -1256,24 +1256,26 @@
             
+//
 if (this.ren
@@ -1286,20 +1286,25 @@
 r.player
-Ship
+Character
 ) %7B%0A    
@@ -1315,16 +1315,18 @@
         
+//
 e.preven
@@ -1341,32 +1341,34 @@
 ();%0A            
+//
 %7D%0A        %7D, fal
@@ -1353,32 +1353,34 @@
     //%7D%0A        
+//
 %7D, false);%0A%0A    
@@ -1375,32 +1375,34 @@
 alse);%0A%0A        
+//
 this.touchContai
@@ -1432,24 +1432,28 @@
 ouchend', (e
+, pr
 ) =%3E %7B%0A     
@@ -1463,117 +1463,83 @@
     
-this.currentTouch = false;%0A            this.activeInput.up = false;%0A            this.activeInput.left = false
+//let destination = pr.pickedPoint.clone();%0A            //destination.y = 0
 ;%0A  
@@ -1552,38 +1552,45 @@
     
+//
 this.
-activeInput.right = false
+destinations.push(destination)
 ;%0A  
@@ -1591,32 +1591,34 @@
 n);%0A            
+//
 this.renderer.on
@@ -1621,50 +1621,30 @@
 r.on
-KeyChange(%7B keyName: 'up', isDown: false %7D
+MouseClick(destination
 );%0A 
@@ -1650,16 +1650,18 @@
         
+//
 %7D, false
@@ -1672,16 +1672,18 @@
         
+//
 document
@@ -1750,32 +1750,34 @@
 %3E %7B%0A            
+//
 this.emit('fire'
@@ -1787,18 +1787,311 @@
         
+//%7D);%0A        this.renderer.scene.onPointerDown = (evt, pr) =%3E %7B%0A            evt.preventDefault();%0A            let destination = pr.pickedPoint.clone();%0A            destination.y = 0;%0A            this.destinations.push(destination);%0A            this.renderer.onMouseClick(destination);%0A        
 %7D
-)
 ;%0A    %7D%0A

Fix mobile