package dev.game.spacechaos.game.entities;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import dev.game.spacechaos.engine.camera.CameraWrapper;
import dev.game.spacechaos.engine.game.BaseGame;
import dev.game.spacechaos.engine.time.GameTime;
import dev.game.spacechaos.engine.utils.MouseUtils;
import dev.game.spacechaos.engine.utils.SpriteBatcherUtils;
import dev.game.spacechaos.game.camera.SmoothCamera;

/**
 * Created by Jo on 29.03.2017.
 */
public class PlayerSpaceShuttle extends SpaceShuttle {

    private float MAX_SPEED = 2f;
    protected Vector2 tmpVector = new Vector2();

    //smooth camera value
    protected float lerp = 0.1f;

    public PlayerSpaceShuttle(Texture shuttleTexture, float xPos, float yPos) {
        super(shuttleTexture, xPos, yPos);
    }

    @Override
    public void move(float x, float y) {
        super.move(x, y);
    }

    @Override
    public void update(BaseGame game, CameraWrapper camera, GameTime time) {
        //get mouse position relative to shuttle
        Vector2 vector = MouseUtils.getRelativePositionToEntity(camera, getX(), getY());
        float mouseX = vector.x;
        float mouseY = vector.y;

        //avoid jerky
        if (Math.abs(mouseX) < 1) {
            mouseX = 0;
        }

        if (Math.abs(mouseY) < 1) {
            mouseY = 0;
        }

        //set values to vector
        tmpVector.set(mouseX, mouseY);

        //normalize and scale vector
        tmpVector.nor();
        tmpVector.scl(MAX_SPEED);

        if (Math.abs(mouseX) > 1 || Math.abs(mouseY) > 1) {
            //move entity
            move(tmpVector.x, tmpVector.y);
        }

        //move and centralize camera
        //camera.setPosition(getMiddleX() - game.getViewportWidth() / 2, getMiddleY() - game.getViewportHeight() / 2);

        //update smooth camera
        SmoothCamera.update(game, camera, getMiddleX(), getMiddleY(), this.lerp);

        //update super class (SpaceShuttle)
        super.update(game, camera, time);
    }

    @Override
    public void draw(GameTime time, CameraWrapper camera, SpriteBatch batch) {
        //get mouse angle relative to shuttle
        float angle = MouseUtils.getRelativeMouseAngle(camera, getMiddleX(), getMiddleY()) - 90;

        batch.draw(shuttleTextureRegion, this.getX(), this.getY(), getWidth() / 2, getHeight() / 2, getWidth(), getHeight(), 1f, 1f, angle); //center shuttle at given coordinates

        //draw center, only for debugging purposes
        SpriteBatcherUtils.fillRectangle(batch, getMiddleX() - 10, getMiddleY() - 10, 20, 20, Color.YELLOW);

        //draw mouse position
        Vector3 mousePos = camera.getMousePosition();
        SpriteBatcherUtils.fillRectangle(batch, mousePos.x - 5, mousePos.y - 5, 10, 10, Color.RED);

        //draw position
        SpriteBatcherUtils.fillRectangle(batch, getX() - 5, getY() - 5, 10, 10, Color.BLUE);
    }

}

@@ -741,24 +741,186 @@
 Vector2();%0A%0A
+    //used to store front coordinates%0A    private float frontX, frontY;%0A    //front vector: origin shuttle center.%0A    private Vector2 frontVec = new Vector2();%0A%0A
     //smooth
@@ -1090,16 +1090,97 @@
  yPos);%0A
+        frontVec.set(shuttleTexture.getWidth()/2, shuttleTexture.getHeight()/2);%0A
     %7D%0A%0A 
@@ -1482,22 +1482,22 @@
 ra, 
-getX(), getY()
+frontX, frontY
 );%0A 
@@ -1611,28 +1611,50 @@
 s(mouseX) %3C 
-1
+5
 ) %7B
+ //changed from 1 to 5
 %0A           
@@ -1708,20 +1708,42 @@
 useY) %3C 
-1
+5
 ) %7B
+ //changed from 1 to 5
 %0A       
@@ -1969,17 +1969,17 @@
 useX) %3E 
-1
+5
  %7C%7C Math
@@ -1997,12 +1997,58 @@
 ) %3E 
-1
+5
 ) %7B
+  //from 1 to 5 so the shuttle is able to stop
 %0A   
@@ -2117,32 +2117,142 @@
 .y);%0A        %7D%0A%0A
+        //update front%0A        frontX = getMiddleX()+ frontVec.x;%0A        frontY = getMiddleY()+ frontVec.y;%0A%0A
         //move a
@@ -2272,24 +2272,26 @@
  camera%0A
+//
         
 //camera
@@ -2282,18 +2282,16 @@
         
-//
 camera.s
@@ -2391,16 +2391,18 @@
 ) / 2);%0A
+//
 %0A       
@@ -2838,16 +2838,83 @@
  - 90;%0A%0A
+        //adjust front angle%0A        frontVec.setAngle(angle+90);%0A%0A
         
@@ -3547,16 +3547,141 @@
 r.BLUE);
+%0A%0A        //draw front position%0A        SpriteBatcherUtils.fillRectangle(batch, frontX - 5, frontY - 5, 10, 10, Color.GREEN);
 %0A    %7D%0A%0A

Move fix. Shuttle is now able to stop. Shuttle is able to fly backwards.