package entities;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Animation.PlayMode;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Array;
import com.ld52.agent.Agent;

import Global.Global;
import levelGen.LevelTmx;
import u.Uinput;

public class Player extends Sprite {
	public enum State {
		STANDLEFT, STANDRIGHT, STANDUP, STANDDOWN, WALKLEFT, WALKRIGHT, WALKUP, WALKDOWN, DUCKLEFT, DUCKRIGHT, DUCKUP, DUCKDOWN, JUMPLEFT, JUMPRIGHT, JUMPUP,
		JUMPDOWN, SHOOTSTANDLEFT, SHOOTSTANDRIGHT, SHOOTSTANDUP, SHOOTSTANDDOWN, SHOOTWALKLEFT, SHOOTWALKRIGHT, SHOOTWALKUP, SHOOTWALKDOWN, SHOOTDUCKLEFT,
		SHOOTDUCKRIGHT, SHOOTDUCKUP, SHOOTDUCKDOWN, SHOOTJUMPLEFT, SHOOTJUMPRIGHT, SHOOTJUMPUP, SHOOTJUMPDOWN, DYING
	};

	public State currentState;
	public State previousState;
	private Animation<TextureRegion> walkLeft;
	private Animation<TextureRegion> walkRight;
	private Animation<TextureRegion> walkUp;
	private Animation<TextureRegion> walkDown;
	private Animation<TextureRegion> shootWalkLeft;
	private Animation<TextureRegion> shootWalkRight;
	private Animation<TextureRegion> shootWalkUp;
	private Animation<TextureRegion> shootWalkDown;
	private Animation<TextureRegion> dying;
	// private TextureAtlas atlas;
	private Texture t1;
	private TextureRegion standLeft;
	private TextureRegion standRight;
	private TextureRegion standUp;
	private TextureRegion standDown;
	private TextureRegion duckLeft;
	private TextureRegion duckRight;
	private TextureRegion duckUp;
	private TextureRegion duckDown;
	private TextureRegion jumpLeft;
	private TextureRegion jumpRight;
	private TextureRegion jumpUp;
	private TextureRegion jumpDown;
	private TextureRegion shootStandLeft;
	private TextureRegion shootStandRight;
	private TextureRegion shootStandUp;
	private TextureRegion shootStandDown;
	private TextureRegion shootDuckLeft;
	private TextureRegion shootDuckRight;
	private TextureRegion shootDuckUp;
	private TextureRegion shootDuckDown;
	private TextureRegion shootJumpLeft;
	private TextureRegion shootJumpRight;
	private TextureRegion shootJumpUp;
	private TextureRegion shootJumpDown;
	private float _stateTimer;
	public Color color;
	public Float speed = 300f;// was 400
	public Rectangle rect;
	public ShapeRenderer sr;
	private boolean isAnimationOver;
	public boolean dead;
	public boolean rmove;
	public boolean lmove;
	public boolean umove;
	public boolean dmove;
	public boolean isDying;
	public static boolean r;
	public static boolean l;
	public static boolean u;
	public static boolean d;
	private int _sw;// sprite width
	private int _sh;// sprite height
	// private Level _level;
	protected int MoveBufferX = 0, MoveBufferY = 0;
	private int fallUpTimer = 10;
	// sfx
	private Sound death;
	private Sound hurt;
	private Sound hurt2;
	private Sound hurt3;
	private Sound hurt4;
	private Sound hurt5;
	private Sound hurt6;
	private Sound shoot;
	private Sound sfx_jump;
	private Sound sfx_reload;
	// private Sound chargeShot;
	// private Sound shoot3;
	private Sound click;
	private Sound hardClick;
	private Sound click2;
	private Sound click3;
	private Sound click4;
	private Sound click5;
	private Sound click6;
	/// <summary> //i have no idea if this translates. This is from another collider program because i can't invent this stuff a bizzilions times
	/// Determines how many units represent 1 pixel. Default is 100. Example: A speed of 100 would
	/// move the object 1 pixel per update.
	/// </summary>
	public int SpeedScale = 100;// this... um. did nothing hehehehahahhaha
	private float normSpeed = speed;
	private float diagSpeed = speed - speed / 4;
	private float sprintSpeed = speed * 2;
	private float jumpMax = 10;
	private float jumpingTimer = jumpMax;
	public boolean isJumping = false;
	public static boolean firesGun;
	public static boolean outOfAmmo = true;
	public boolean hasGun;
	public boolean isReloading;
	public boolean isDucked;
	private boolean isJumped;
	private Float duckSpeed = speed / 2;
	public int ammoSize = 8;
	public int ammo = ammoSize;
	public int ammoPower = 1;
	public float swirlingObjects;

	public Player(int _x, int _y) {
		rect = new Rectangle();
		rect.x = _x;
		rect.y = _y;
		rect.width = 15;
		rect.height = 25;

		_sw = 32;
		_sh = 48;
		// _level = level;
		System.out.println("x: " + _x + "y: " + _y);

		// sounds
		death = Gdx.audio.newSound(Gdx.files.internal("sfx/death.ogg"));
		hurt = Gdx.audio.newSound(Gdx.files.internal("sfx/hurt.ogg"));
		hurt2 = Gdx.audio.newSound(Gdx.files.internal("sfx/hurt2.ogg"));
		hurt3 = Gdx.audio.newSound(Gdx.files.internal("sfx/hurt3.ogg"));
		hurt4 = Gdx.audio.newSound(Gdx.files.internal("sfx/hurt4.ogg"));
		hurt5 = Gdx.audio.newSound(Gdx.files.internal("sfx/hurt5.ogg"));
		hurt6 = Gdx.audio.newSound(Gdx.files.internal("sfx/hurt6.ogg"));
		shoot = Gdx.audio.newSound(Gdx.files.internal("sfx/shoot.ogg"));
		sfx_jump = Gdx.audio.newSound(Gdx.files.internal("sfx/hurt2.ogg"));
		sfx_reload = Gdx.audio.newSound(Gdx.files.internal("sfx/reload.ogg"));
		// chargeShot = Gdx.audio.newSound(Gdx.files.internal("sfx/chargeShot.ogg"));
		hardClick = Gdx.audio.newSound(Gdx.files.internal("sfx/hardClick.ogg"));
		click = Gdx.audio.newSound(Gdx.files.internal("sfx/click.ogg"));
		click2 = Gdx.audio.newSound(Gdx.files.internal("sfx/click2.ogg"));
		click3 = Gdx.audio.newSound(Gdx.files.internal("sfx/click3.ogg"));
		click4 = Gdx.audio.newSound(Gdx.files.internal("sfx/click4.ogg"));
		click5 = Gdx.audio.newSound(Gdx.files.internal("sfx/click5.ogg"));
		click6 = Gdx.audio.newSound(Gdx.files.internal("sfx/click6.ogg"));
		// this is for the animations
		currentState = State.STANDRIGHT;
		previousState = State.STANDRIGHT;
		_stateTimer = 0f;
		// get Player atlas
		// atlas = new TextureAtlas(Gdx.files.internal("playersheettest.png"));
		t1 = new Texture(Gdx.files.internal("player2.png"));
		// declare all the static frames (static states (no animation))
		standLeft = new TextureRegion(t1, (0 * _sw), (0 * _sh), _sw, _sh);
		standRight = new TextureRegion(t1, (1 * _sw), (0 * _sh), _sw, _sh);
		standUp = new TextureRegion(t1, (2 * _sw), (0 * _sh), _sw, _sh);
		standDown = new TextureRegion(t1, (3 * _sw), (0 * _sh), _sw, _sh);
		duckLeft = new TextureRegion(t1, (3 * _sw), (1 * _sh), _sw, _sh);
		duckRight = new TextureRegion(t1, (4 * _sw), (1 * _sh), _sw, _sh);
		duckUp = new TextureRegion(t1, (0 * _sw), (2 * _sh), _sw, _sh);
		duckDown = new TextureRegion(t1, (1 * _sw), (2 * _sh), _sw, _sh);
		jumpLeft = new TextureRegion(t1, (2 * _sw), (2 * _sh), _sw, _sh);
		jumpRight = new TextureRegion(t1, (3 * _sw), (2 * _sh), _sw, _sh);
		jumpUp = new TextureRegion(t1, (4 * _sw), (2 * _sh), _sw, _sh);
		jumpDown = new TextureRegion(t1, (0 * _sw), (3 * _sh), _sw, _sh);
		shootStandLeft = new TextureRegion(t1, (1 * _sw), (3 * _sh), _sw, _sh);
		shootStandRight = new TextureRegion(t1, (2 * _sw), (3 * _sh), _sw, _sh);
		shootStandUp = new TextureRegion(t1, (3 * _sw), (3 * _sh), _sw, _sh);
		shootStandDown = new TextureRegion(t1, (4 * _sw), (3 * _sh), _sw, _sh);
		shootDuckLeft = new TextureRegion(t1, (4 * _sw), (4 * _sh), _sw, _sh);
		shootDuckRight = new TextureRegion(t1, (0 * _sw), (5 * _sh), _sw, _sh);
		shootDuckUp = new TextureRegion(t1, (1 * _sw), (5 * _sh), _sw, _sh);
		shootDuckDown = new TextureRegion(t1, (2 * _sw), (5 * _sh), _sw, _sh);
		shootJumpLeft = new TextureRegion(t1, (3 * _sw), (5 * _sh), _sw, _sh);
		shootJumpRight = new TextureRegion(t1, (4 * _sw), (5 * _sh), _sw, _sh);
		shootJumpUp = new TextureRegion(t1, (0 * _sw), (6 * _sh), _sw, _sh);
		shootJumpDown = new TextureRegion(t1, (1 * _sw), (6 * _sh), _sw, _sh);
		// animated frames
		Array<TextureRegion> frames = new Array<TextureRegion>();
		// TextureRegion l = new TextureRegion(t1, (0 * _sw), (0 * _sh), _sw, _sh);
		// frames.add(l);//i think it's written this way so i could flip them. I don't need to since i already drew it that way...
		// TextureRegion l2 = new TextureRegion(t1, (1 * _sw), (1 * _sh), _sw, _sh);
		// frames.add(l2);
		frames.add(new TextureRegion(t1, (0 * _sw), (0 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (4 * _sw), (0 * _sh), _sw, _sh));
		walkLeft = new Animation<TextureRegion>(.1f, frames, PlayMode.LOOP);
		frames.clear();
		frames.add(new TextureRegion(t1, (1 * _sw), (0 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (0 * _sw), (1 * _sh), _sw, _sh));
		walkRight = new Animation<TextureRegion>(.1f, frames, PlayMode.LOOP);
		frames.clear();
		frames.add(new TextureRegion(t1, (2 * _sw), (0 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (1 * _sw), (1 * _sh), _sw, _sh));
		walkUp = new Animation<TextureRegion>(.1f, frames, PlayMode.LOOP);
		frames.clear();
		frames.add(new TextureRegion(t1, (3 * _sw), (0 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (2 * _sw), (1 * _sh), _sw, _sh));
		walkDown = new Animation<TextureRegion>(.1f, frames, PlayMode.LOOP);
		frames.clear();
		frames.add(new TextureRegion(t1, (1 * _sw), (3 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (0 * _sw), (4 * _sh), _sw, _sh));
		shootWalkLeft = new Animation<TextureRegion>(.1f, frames, PlayMode.LOOP);
		frames.clear();
		frames.add(new TextureRegion(t1, (2 * _sw), (3 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (1 * _sw), (4 * _sh), _sw, _sh));
		shootWalkRight = new Animation<TextureRegion>(.1f, frames, PlayMode.LOOP);
		frames.clear();
		frames.add(new TextureRegion(t1, (3 * _sw), (3 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (2 * _sw), (4 * _sh), _sw, _sh));
		shootWalkUp = new Animation<TextureRegion>(.1f, frames, PlayMode.LOOP);
		frames.clear();
		frames.add(new TextureRegion(t1, (4 * _sw), (3 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (3 * _sw), (4 * _sh), _sw, _sh));
		shootWalkDown = new Animation<TextureRegion>(.1f, frames, PlayMode.LOOP);
		frames.clear();
		frames.add(new TextureRegion(t1, (3 * _sw), (1 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (0 * _sw), (2 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (4 * _sw), (1 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (1 * _sw), (2 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (2 * _sw), (2 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (4 * _sw), (2 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (3 * _sw), (2 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (0 * _sw), (3 * _sh), _sw, _sh));
		dying = new Animation<TextureRegion>(.2f, frames, PlayMode.LOOP);
		frames.clear();// definitely starts right. then we say, when the line is one length + one _sw worth which would be what in actual numbers?
		// these two are specifically sprite settings to set the sprite's initial bounds and region
		// assign random color
		color = new Color(Color.WHITE);// new Color(MathUtils.random(), MathUtils.random(), MathUtils.random(), 1);
		setBounds(-10, -40, _sw, _sh);
		setRegion(standRight);
		r = true;
	}

	// for all the animations: //this isn't even updated unless i call it from the other screen oh bother. *facepalm*
	public void update(float dt, OrthographicCamera camera) {
		// setRegion(getFrame(dt));
		handleInput(dt, camera);
		checkIfDead();
		Global.P = rect;
		hasGun = true;
		if (Gdx.input.isButtonJustPressed(Uinput.Fire()) && hasGun) {
			if (ammo > 0) {
				if (!Global.SetVolumeToZero)
					shoot.play();
			} else if (ammo <= 0) {
				if (!Global.SetVolumeToZero)
					hardClick.play(.2f);
			}
		}
		if (Gdx.input.isKeyJustPressed(Uinput.Inventory())) {// going to use Inventory to 'reload'. i think!
//			normSpeed = speed;
//			diagSpeed = speed - speed / 4;
//			sprintSpeed = speed * 2;
			System.out.println("reload: ");
			isReloading = true;
			sfx_reload.play();
		}
		if (Gdx.input.isKeyPressed(Uinput.Jump()) && !isJumping) {// going to use Inventory to 'reload'. i think!
			isJumped = true;// for jump animation
			isJumping = true;// for actual jump
			System.out.println("jump! " + isJumped);
			if (!Global.SetVolumeToZero)
				sfx_jump.play();
		} else if (!isJumping)
			isJumped = false;
		if (Gdx.input.isKeyPressed(Uinput.Crouch()) && !isJumping) {// going to use Inventory to 'reload'. i think!
			isDucked = true;
			System.out.println("duck! " + isDucked);
		} else
			isDucked = false;
	}

	public void checkIfDead() {
		if (dead) {
			rmove = false;
			lmove = false;
			umove = false;
			dmove = false;
		}

	}

	public TextureRegion getFrame(float dt, OrthographicCamera camera) {
		// System.out.println("getting frame: " + getState());
		// System.out.println("_stateTimer: " + _stateTimer);
		// System.out.println("animation Index: " + walkLeft.getKeyFrame(_stateTimer));
		update(dt, camera);
		currentState = getState();
		// System.out.println("state: " + currentState);
		TextureRegion region;
		switch (currentState) {
		case WALKRIGHT:
			region = walkRight.getKeyFrame(_stateTimer, true);
			break;
		case WALKLEFT:
			region = walkLeft.getKeyFrame(_stateTimer, true);
			break;
		case WALKUP:
			region = walkUp.getKeyFrame(_stateTimer, true);
			break;
		case WALKDOWN:
			region = walkDown.getKeyFrame(_stateTimer, true);
			break;
		case DUCKLEFT:
			region = duckLeft;
			break;
		case DUCKRIGHT:
			region = duckRight;
			break;
		case DUCKUP:
			region = duckUp;
			break;
		case DUCKDOWN:
			region = duckDown;
			break;
		case JUMPLEFT:
			region = jumpLeft;
			break;
		case JUMPRIGHT:
			region = jumpRight;
			break;
		case JUMPUP:
			region = jumpUp;
			break;
		case JUMPDOWN:
			region = jumpDown;
			break;
		case STANDLEFT:
			region = standLeft;
			break;
		case STANDUP:
			region = standUp;
			break;
		case STANDDOWN:
			region = standDown;
			break;
		case SHOOTWALKRIGHT:
			region = shootWalkRight.getKeyFrame(_stateTimer, true);
			break;
		case SHOOTWALKLEFT:
			region = shootWalkLeft.getKeyFrame(_stateTimer, true);
			break;
		case SHOOTWALKUP:
			region = shootWalkUp.getKeyFrame(_stateTimer, true);
			break;
		case SHOOTWALKDOWN:
			region = shootWalkDown.getKeyFrame(_stateTimer, true);
			break;
		case SHOOTDUCKLEFT:
			region = shootDuckLeft;
			break;
		case SHOOTDUCKRIGHT:
			region = shootDuckRight;
			break;
		case SHOOTDUCKUP:
			region = shootDuckUp;
			break;
		case SHOOTDUCKDOWN:
			region = shootDuckDown;
			break;
		case SHOOTJUMPLEFT:
			region = shootJumpLeft;
			break;
		case SHOOTJUMPRIGHT:
			region = shootJumpRight;
			break;
		case SHOOTJUMPUP:
			region = shootJumpUp;
			break;
		case SHOOTJUMPDOWN:
			region = shootJumpDown;
			break;
		case SHOOTSTANDLEFT:
			region = shootStandLeft;
			break;
		case SHOOTSTANDRIGHT:
			region = shootStandRight;
			break;
		case SHOOTSTANDUP:
			region = shootStandUp;
			break;
		case SHOOTSTANDDOWN:
			region = shootStandDown;
			break;
		case DYING:
			region = dying.getKeyFrame(_stateTimer, true);
			break;
		case STANDRIGHT:
		default:
			region = standRight;
			break;
		}
		if (currentState != previousState)
			isAnimationOver = false;

		if (!isAnimationOver && currentState == previousState) {
			_stateTimer += dt;
		} else
			_stateTimer = 0;
		// System.out.println("statetimer: " + _stateTimer);
		previousState = currentState;

		return region;
	}

	public State getState() {
		// System.out.println("l: " + l + " r: " + r + " u: " + u + " d: " + d);
		// System.out.println("lmove: " + lmove + " rmove: " + rmove + " umove: " + umove + " dmove: " + dmove);
		if (isDying)
			return State.DYING;
		else if (hasGun && isDucked && lmove)
			return State.SHOOTDUCKLEFT;
		else if (hasGun && isDucked && rmove)
			return State.SHOOTDUCKRIGHT;
		else if (hasGun && isDucked && umove)
			return State.SHOOTDUCKUP;
		else if (hasGun && isDucked && dmove)
			return State.SHOOTDUCKDOWN;
		else if (hasGun && isDucked && l)
			return State.SHOOTDUCKLEFT;
		else if (hasGun && isDucked && r)
			return State.SHOOTDUCKRIGHT;
		else if (hasGun && isDucked && u)
			return State.SHOOTDUCKUP;
		else if (hasGun && isDucked && d)
			return State.SHOOTDUCKDOWN;
		else if (hasGun && isJumped && lmove)
			return State.SHOOTJUMPLEFT;
		else if (hasGun && isJumped && rmove)
			return State.SHOOTJUMPRIGHT;
		else if (hasGun && isJumped && umove)
			return State.SHOOTJUMPUP;
		else if (hasGun && isJumped && dmove)
			return State.SHOOTJUMPDOWN;
		else if (hasGun && isJumped && l)
			return State.SHOOTJUMPLEFT;
		else if (hasGun && isJumped && r)
			return State.SHOOTJUMPRIGHT;
		else if (hasGun && isJumped && u)
			return State.SHOOTJUMPUP;
		else if (hasGun && isJumped && d)
			return State.SHOOTJUMPDOWN;
		else if (hasGun && l && lmove && umove || hasGun && l && lmove && dmove)
			return State.SHOOTWALKLEFT;
		else if (hasGun && r && rmove && dmove || hasGun && r && rmove && umove)
			return State.SHOOTWALKRIGHT;
		else if (hasGun && u && umove && lmove || hasGun && u && umove && rmove)
			return State.SHOOTWALKUP;
		else if (hasGun && d && dmove && lmove || hasGun && d && dmove && rmove)
			return State.SHOOTWALKDOWN;
		else if (hasGun && lmove)
			return State.SHOOTWALKLEFT;
		else if (hasGun && rmove)
			return State.SHOOTWALKRIGHT;
		else if (hasGun && umove)
			return State.SHOOTWALKUP;
		else if (hasGun && dmove)
			return State.SHOOTWALKDOWN;
		else if (hasGun && l)
			return State.SHOOTSTANDLEFT;
		else if (hasGun && r)
			return State.SHOOTSTANDRIGHT;
		else if (hasGun && u)
			return State.SHOOTSTANDUP;
		else if (hasGun && d)
			return State.SHOOTSTANDDOWN;
		else if (isDucked && lmove)
			return State.DUCKLEFT;
		else if (isDucked && rmove)
			return State.DUCKRIGHT;
		else if (isDucked && umove)
			return State.DUCKUP;
		else if (isDucked && dmove)
			return State.DUCKDOWN;
		else if (isDucked && l)
			return State.DUCKLEFT;
		else if (isDucked && r)
			return State.DUCKRIGHT;
		else if (isDucked && u)
			return State.DUCKUP;
		else if (isDucked && d)
			return State.DUCKDOWN;
		else if (isJumped && lmove)
			return State.JUMPLEFT;
		else if (isJumped && rmove)
			return State.JUMPRIGHT;
		else if (isJumped && umove)
			return State.JUMPUP;
		else if (isJumped && dmove)
			return State.JUMPDOWN;
		else if (isJumped && l)
			return State.JUMPLEFT;
		else if (isJumped && r)
			return State.JUMPRIGHT;
		else if (isJumped && u)
			return State.JUMPUP;
		else if (isJumped && d)
			return State.JUMPDOWN;
		else if (l && lmove && umove || l && lmove && dmove)
			return State.WALKLEFT;
		else if (r && rmove && dmove || r && rmove && umove)
			return State.WALKRIGHT;
		else if (u && umove && lmove || u && umove && rmove)
			return State.WALKUP;
		else if (d && dmove && lmove || d && dmove && rmove)
			return State.WALKDOWN;
		else if (lmove)
			return State.WALKLEFT;
		else if (rmove)
			return State.WALKRIGHT;
		else if (umove)
			return State.WALKUP;
		else if (dmove)
			return State.WALKDOWN;
		else if (l)
			return State.STANDLEFT;
		else if (r)
			return State.STANDRIGHT;
		else if (u)
			return State.STANDUP;
		else if (d)
			return State.STANDDOWN;
		else
			return State.STANDRIGHT;
	}

	private void handleInput(float dt, OrthographicCamera camera) {
		boolean isCT = false;// collision tile

		if (Gdx.input.isKeyPressed(Uinput.Right())) {
			isCT = false;
			// isCT = (GetTileYouWillBeOn("right", dt)) ? true : false;// right
			MoveRight(speed, isCT, dt);
			l = false;
			r = true;
			u = false;
			d = false;
			rmove = true;
		} else
			rmove = false;
		if (Gdx.input.isKeyPressed(Uinput.Left())) {
			isCT = false;
			// isCT = (GetTileYouWillBeOn("left", dt)) ? true : false;// left
			MoveLeft(speed, isCT, dt);
			l = true;
			r = false;
			u = false;
			d = false;
			lmove = true;
		} else
			lmove = false;
		if (Gdx.input.isKeyPressed(Uinput.Up())) {
			isCT = false;
			// isCT = (GetTileYouWillBeOn("up", dt)) ? true : false;
			MoveUp(speed, isCT, dt);
			u = true;
			d = false;
			l = false;
			r = false;
			umove = true;
		} else
			umove = false;
		if (Gdx.input.isKeyPressed(Uinput.Down())) {
			isCT = false;
			// isCT = (GetTileYouWillBeOn("down", dt)) ? true : false;// down
			MoveDown(speed, isCT, dt);
			u = false;
			d = true;
			l = false;
			r = false;
			dmove = true;
		} else
			dmove = false;
		if (isDying)
			MoveFalling(speed, dt);
		// diag catches.// priority is top to bottom
		if (Gdx.input.isKeyPressed(Uinput.Sprint()))
			speed = sprintSpeed;
		else
			speed = normSpeed;
		if (isDucked)
			speed = duckSpeed;
		if (umove && lmove || umove && rmove || dmove && lmove || dmove && rmove) {
			if (speed == sprintSpeed)
				speed = sprintSpeed - sprintSpeed / 4;
			else if (speed == duckSpeed)
				speed = duckSpeed - duckSpeed / 4;
			else
				speed = diagSpeed;
		} else if (sprintSpeed != speed && duckSpeed != speed)
			speed = normSpeed;
		if (isJumping)
			Jumping(speed, dt);

		// System.out.println("speeed: " + speed);
	}

	public void MoveFalling(float speed, float dt) {
		if (fallUpTimer > 0) {
			rect.y += speed * 2 * dt;
			fallUpTimer--;
			if (!Global.SetVolumeToZero)
				death.play();
		} else
			rect.y -= 2 * speed * dt;
	}

	public void Jumping(float speed, float dt) {
		if (jumpingTimer > 0) {
			rect.y += speed * 2 * dt;
		} else
			rect.y -= 2 * speed * dt;
		if (jumpingTimer <= -jumpMax) {
			jumpingTimer = jumpMax;
			isJumping = false;
		}
		jumpingTimer--;
	}

	public void MoveRight(float speed, boolean isColl, float dt) {
		if (isColl)
			rect.x -= 0;
		else
			rect.x += speed * dt;
		// rect.add(velocity);
		// rect.x.add(velocity);
		// rect.setPosition(position)
		// rect.getPosition(position)
	}

	public void MoveLeft(float speed, boolean isColl, float dt) {
		// test this:
		if (isColl)
			rect.x += 0;
		else
			rect.x -= speed * dt;
	}

	public void MoveUp(float speed, boolean isColl, float dt) {
		// test this:
		if (isColl)
			rect.y -= 0;
		else
			rect.y += speed * dt;
	}

	public void MoveDown(float speed, boolean isColl, float dt) {
		// test this:
		if (isColl)
			rect.y += 0;
		else
			rect.y -= speed * dt;
	}

	public boolean GetTileYouWillBeOn(String dr, float dt) {
		// test to see what tile we will be standing on.
		if (dr == "right") {
			String _k = ("" + Math.abs(((int) rect.x + (int) (speed * dt)) / Agent.TW) + "," + Math.abs((int) rect.y / Agent.TH));
			String c = getMapId(_k);
			if (c == "null")
				return true;// collision tile
			else
				return false;
		}
		if (dr == "left") {
			String _k = ("" + Math.abs(((int) rect.x - (int) (speed * dt)) / Agent.TW) + "," + Math.abs((int) rect.y / Agent.TH));
			String c = getMapId(_k);
			if (c == "null")
				return true;// collision tile
			else
				return false;
		}
		if (dr == "up") {
			String _k = ("" + Math.abs((int) rect.x / Agent.TW) + "," + Math.abs((int) (rect.y + (int) (speed * dt)) / Agent.TH));
			String c = getMapId(_k);
			if (c == "null")
				return true;// collision tile
			else
				return false;
		}
		if (dr == "down") {
			String _k = ("" + Math.abs((int) rect.x / Agent.TW) + "," + Math.abs((int) (rect.y - (int) (speed * dt)) / Agent.TH));
			String c = getMapId(_k);
			if (c == "null")
				return true;// collision tile
			else
				return false;
		} else
			return false;
	}

	public String getMapId(String _k) {
		Integer _o = LevelTmx.Map.get(_k);
		String s;
		if (_o == null)// translate the integers into actual, um, tile types Not sure I need this. We'll see.
			s = "null";
		else if (_o == 0)
			s = "water";
		else if (_o == 1)
			s = "rock";
		else if (_o == 2)
			s = "grass";
		else if (_o == 3)
			s = "deep water";
		else if (_o == 4)
			s = "deepest water";
		else if (_o == 5)
			s = "dark rock";
		else if (_o == 6)
			s = "snow";
		else if (_o == 7)
			s = "sand";
		else if (_o == 8)
			s = "lava";
		else
			s = "something else";
		// System.out.println("tileType: " + s);
		return s;
	}

	public void playRandomHurtSound(int numberOfSounds) {
		int randomSound = MathUtils.random(1, numberOfSounds);
		if (!Global.SetVolumeToZero) {
			if (randomSound == 1)
				hurt.play();
			else if (randomSound == 2)
				hurt2.play();
			else if (randomSound == 3)
				hurt3.play();
			else if (randomSound == 4)
				hurt4.play();
			else if (randomSound == 5)
				hurt5.play();
			else if (randomSound == 6)
				hurt6.play();
		}
	}

	public void playRandomClickSound(int numberOfSounds) {
		int randomSound = MathUtils.random(1, numberOfSounds);
		if (!Global.SetVolumeToZero) {
			if (randomSound == 1)
				click.play(.2f);
			else if (randomSound == 2)
				click2.play(.2f);
			else if (randomSound == 3)
				click3.play(.2f);
			else if (randomSound == 4)
				click4.play(.2f);
			else if (randomSound == 5)
				click5.play(.2f);
			else if (randomSound == 6)
				click6.play(.2f);
		}
	}

	public void dispose() {
		// atlas.dispose();
		death.dispose();
		hurt.dispose();
		hurt2.dispose();
		hurt3.dispose();
		hurt4.dispose();
		hurt5.dispose();
		hurt6.dispose();
		// chargeShot.dispose();
		// chargeGun.dispose();
		// shoot3.dispose();
		sfx_jump.dispose();
		sfx_reload.dispose();
		click.dispose();
		hardClick.dispose();
		click2.dispose();
		click3.dispose();
		click4.dispose();
		click5.dispose();
		click6.dispose();
	}
}
