package ga.deluded_fruitcakes_anonymous.velocityangles;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

/**
 * Created by Isaac on 8/3/2016.
 */
public class GameView extends View {
    float centerX, centerY;
    float borderHeight, MinHeight,MaxWidth, MinWidth;
    DrawableObject drawableObject;
    PhysicalObject physicalObject;
    Bitmap bitmapBase = BitmapFactory.decodeResource(getResources(), R.drawable.joystick_base);
    Bitmap bitmapTop = BitmapFactory.decodeResource(getResources(), R.drawable.joystick_top);
    public GameView(Context context, float sHeight, float sWidth) {
        super(context);
        borderHeight = sHeight * 0.1f;
        centerX = sWidth * 0.5f;
        centerY = sHeight * 0.5f;
        CreateJoystick();
    }
    public void CreateJoystick(){
        drawableObject = new DrawableObject(bitmapBase,centerX, centerY,200,200);
        physicalObject = new PhysicalObject(bitmapTop,centerX,centerY,100,100);
    }
    @Override
    public boolean onTouchEvent(MotionEvent e) {
        // MotionEvent reports input details from the touch screen
        // and other input controls. In this case, you are only
        // interested in events where the touch position changed.

        float x = e.getX();
        float y = e.getY();
        if (y >= borderHeight) {
            switch (e.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    Toast.makeText(getContext(), "Joystick moved to " + (x - centerX) + ", " + (y - centerY), Toast.LENGTH_SHORT).show();
                    physicalObject.xAcceleration = x-centerX;
                    physicalObject.yAcceleration = y-centerY;
                    if(physicalObject.yPos == y && physicalObject.xPos == x){
                        physicalObject.xAcceleration = 0;
                        physicalObject.yAcceleration = 0;
                        physicalObject.xVelocity = 0;
                        physicalObject.yVelocity = 0;
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    joystickReturn();
                    break;
            }
        }
        return true;
    }
    public void joystickReturn(){
        physicalObject.xPos = centerX;
        physicalObject.yPos = centerY;
        physicalObject.xAcceleration = 0;
        physicalObject.yAcceleration = 0;
        physicalObject.xVelocity = 0;
        physicalObject.yVelocity = 0;
    }
    public void onDraw(Canvas canvas){
        drawableObject.update(canvas);
        physicalObject.update(canvas);
        Paint paint = new Paint();
        paint.setTextSize(100);
        paint.setColor(Color.BLACK);
        canvas.drawText("hello",100,100,paint);
        try{
            Thread.sleep(100);
        }catch(InterruptedException e){
            
        }
    invalidate();}
}

@@ -1906,16 +1906,17 @@
      if(
+(
 physical
@@ -1931,16 +1931,21 @@
 Pos 
-=
+%3C
 = y
+ +5)
  && 
+(
 phys
@@ -1959,17 +1959,21 @@
 ect.
-x
+y
 Pos 
-== x
+%3E= y-5) 
 )%7B%0A 
@@ -1987,32 +1987,36 @@
                 
+if((
 physicalObject.x
@@ -2015,34 +2015,67 @@
 Object.x
-Acceleration = 0;%0A
+Pos %3C=x +5) && (physicalObject.xPos %3E= x-5) )%7B%0A    
         
@@ -2105,35 +2105,35 @@
 lObject.
-yAcceleration
+xVelocity
  = 0;%0A
+    
         
@@ -2155,33 +2155,33 @@
  physicalObject.
-x
+y
 Velocity = 0;%0A  
@@ -2194,32 +2194,36 @@
                 
+    
 physicalObject.y
@@ -2217,39 +2217,102 @@
 sicalObject.
-yVelocity
+xAcceleration = 0;%0A                        physicalObject.yAcceleration
  = 0;%0A
+%0A
             
@@ -2312,32 +2312,33 @@
                %7D
+%7D
 %0A               

Stop joystick from moving once reaching touch coordinates