package player;

import engine.Game;
import engine.Handler;
import hud.RightClickMenu;
import interfaces.Directions;
import utils.Utils;
import world.Tile;

/*  ------------- Nihilne ------------
    * Created by Mitch Weaver - 2016 *
      ~~~~ All Rights Reserved ~~~~ */


final class PlayerPosition implements Directions {
	
	private static final float SNOW_SPEED_REDUCTION = 0.75f;
	private static float tempX, tempY;
	
	public final static void update() {
		if (!Player.collided) {
			if (Player.velX != 0 && Player.velY != 0){ Player.velX /= Utils.sqrtOf2; Player.velY /= Utils.sqrtOf2; }
			
			if(!Player.keepWalkingX && !Player.keepWalkingY && !RightClickMenu.OVERRIDE_F && !RightClickMenu.OVERRIDE_SPACE){ /* ie if not doing mouse-movement */
				
				if (Player.velX == 0 && Player.velY < 0)	  Player.facing = NORTH;
				else if (Player.velX == 0 && Player.velY > 0) Player.facing = SOUTH;
				else if (Player.velX > 0 && Player.velY == 0) Player.facing = EAST;
				else if (Player.velX < 0 && Player.velY == 0) Player.facing = WEST;
				else if (Player.velX > 0 && Player.velY > 0)  Player.facing = SOUTHEAST;
				else if (Player.velX < 0 && Player.velY < 0)  Player.facing = NORTHWEST;
				else if (Player.velX > 0 && Player.velY < 0)  Player.facing = NORTHEAST;
				else if (Player.velX < 0 && Player.velY > 0)  Player.facing = SOUTHWEST;
			

			tempX = (float) Utils.rotateX(Player.velX, Player.velY, (Game.rotation - 1) * -Utils._45);
			tempY = (float) Utils.rotateY(Player.velX, Player.velY, (Game.rotation - 1) * -Utils._45);
			
			if(!Game.noClip)
		outerLoop1:
			for(Tile i : Handler.currentTileSection(Player.center.x, Player.center.y)) {
				switch(i.biome){
					case Tile.OCEAN: { if(i.getBounds().contains(Player.center.x + tempX, Player.center.y + tempY)){ Player.collided = true; break outerLoop1; }}
					case Tile.LAKE:  { if(i.getBounds().contains(Player.center.x + tempX, Player.center.y + tempY)){ Player.collided = true; break outerLoop1; }}
					case Tile.RIVER: { if(i.getBounds().contains(Player.center.x + tempX, Player.center.y + tempY)){ Player.collided = true; break outerLoop1; }}
					case Tile.SNOW_TUNDA: { if(i.getBounds().contains(Player.center.x, Player.center.y)){tempX *= SNOW_SPEED_REDUCTION; tempY *= SNOW_SPEED_REDUCTION; break outerLoop1; }}
				}
			}
			
			if(!Player.collided) { Player.velX = tempX; Player.velY = tempY; }
		} else {
			
			if(!Game.noClip)
		outerLoop2:
			for(Tile i : Handler.currentTileSection(Player.center.x, Player.center.y)) {
				switch(i.biome){
					case Tile.OCEAN: { if(i.getBounds().contains(Player.center.x + Player.velX, Player.center.y + Player.velY)){ Player.collided = true; break outerLoop2; }}
					case Tile.LAKE: { if(i.getBounds().contains(Player.center.x + Player.velX, Player.center.y + Player.velY)) { Player.collided = true; break outerLoop2; }}
					case Tile.RIVER: { if(i.getBounds().contains(Player.center.x + Player.velX, Player.center.y + Player.velY)){ Player.collided = true; break outerLoop2; }}
					case Tile.SNOW_TUNDA: { if(i.getBounds().contains(Player.center.x, Player.center.y)){Player.velX *= SNOW_SPEED_REDUCTION; Player.velY *= SNOW_SPEED_REDUCTION;  break outerLoop2; }}
				}
			}
		}
			
			if(!Player.collided) { Player.pos.x += Player.velX; Player.pos.y += Player.velY; }
		}

		Player.center.x = Player.pos.x + Player.width * 0.5f;
		Player.center.y = Player.pos.y + Player.height * 0.5f;
		
		Player.bounds.x = (int) Player.pos.x;
		Player.bounds.y = (int) Player.pos.y;
	}
}
