package com.veil.ai;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.controllers.Controllers;
import com.veil.game.GameConstant;

public class Controller {

	public static Controller instance = new Controller();
	
	public boolean up,down,left,right,jump,shoot,pause;
	private com.badlogic.gdx.controllers.Controller extController = null;
	
	public void setup(){
		extController = null;
		if(Controllers.getControllers().size > 0){
			extController = Controllers.getControllers().get(0);
		}
	}
	
	public void preUpdate(){
		up = false;
		down = false;
		left = false;
		right = false;
		jump = false;
		shoot = false;
		pause = false;
		
		if(!GameConstant.useAI){
			if(GameConstant.profilingMode && !BattleProfile.instance.isStart())
				return;
			if(extController == null){
				controlWithKeyboard();
			}else{
				controlWithExtController();
			}
		}
	}
	
	private void controlWithKeyboard(){
		if(Gdx.input.isKeyPressed(Input.Keys.UP)){
			up = true;
		}
		if(Gdx.input.isKeyPressed(Input.Keys.DOWN)){
			down = true;
		}
		if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
			left = true;
		}
		if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
			right = true;
		}
		if(Gdx.input.isKeyPressed(Input.Keys.Z)){
			jump = true;
		}
		if(Gdx.input.isKeyPressed(Input.Keys.X)){
			shoot = true;
		}
		if(Gdx.input.isKeyJustPressed(Input.Keys.ESCAPE)){
			pause = true;
		}
	}
	
	private void controlWithExtController(){
		/*
		Logitech
		Left analog: axis2 (-1,1 = left,right)  axis3 (-1,1 = up,down)
		Right analog: axis0 (-1,1 = left,right)  axis1 (-1,1 = up,down)
		0: btnLeft
		1: btnDown
		2: btnRight
		3: btnUp
		4: L1
		5: R1
		6: L2
		7: R2
		8: Select
		9: Start
		
		PovDirection controllerPOV = controller.getPov(0);
		if(PovDirection == PovDirection.north)
		*/
		float x = extController.getAxis(3);
		float y = extController.getAxis(2);
		if(x < -0.7f){
			left = true;
		}else if(x > 0.7f){
			right = true;
		}
		if(y < -0.7f){
			up = true;
		}else if(y > 0.7f){
			down = true;
		}
		if(extController.getButton(1)){
			jump = true;
		}
		if(extController.getButton(0)){
			shoot = true;
		}
		if(extController.getButton(9)){
			pause = true;
		}
	}
	
	public boolean isUsingController(){
		return extController != null;
	}
}

@@ -124,16 +124,66 @@
 ollers;%0A
+import com.badlogic.gdx.controllers.PovDirection;%0A
 import c
@@ -418,16 +418,70 @@
 = null;%0A
+%09private boolean extControllerPauseLastFrame = false;%0A
 %09%0A%09publi
@@ -2120,32 +2120,473 @@
 own = true;%0A%09%09%7D%0A
+%09%09/*%0A%09%09PovDirection controllerPOV = extController.getPov(0);%0A%09%09switch(controllerPOV)%7B%0A%09%09case north: up = true; break;%0A%09%09case east: right = true; break;%0A%09%09case south: down = true; break;%0A%09%09case west: left = true; break;%0A%09%09case northEast: right = true; up = true; break;%0A%09%09case northWest: left = true; up = true; break;%0A%09%09case southEast: right = true; down = true; break;%0A%09%09case southWest: left = true; down = true; break;%0A%09%09default:%0A%09%09%7D%0A%09%09*/%0A
 %09%09if(extControll
@@ -2711,35 +2711,167 @@
 ton(9))%7B%0A%09%09%09
-pause = tru
+if(!extControllerPauseLastFrame)%7B%0A%09%09%09%09extControllerPauseLastFrame = true;%0A%09%09%09%09pause = true;%0A%09%09%09%7D%0A%09%09%7Delse%7B%0A%09%09%09extControllerPauseLastFrame = fals
 e;%0A%09%09%7D%0A%09%7D%0A%09%0A

Fix pause button on controller