package;

import flixel.addons.display.shapes.FlxShapeCircle;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxObject;
import flixel.math.FlxPoint;
import flixel.system.FlxAssets.FlxGraphicAsset;
import flixel.util.FlxSpriteUtil;
import flixel.util.FlxColor;


/**
 * ...
 * @author Laboon
 */
class Player extends FlxSprite
{
	var _up:Bool = false;
	var _down:Bool = false;
	var _left:Bool = false;
	var _right:Bool = false;
	var _shift:Bool = false;
	var _space:Bool = false;
	public var speed:Float = 75;
	public var isHidden:Bool = false;
	public var clipsCount:Int; //количество прищепок для "выключения" врагов
	public var isSeenByEnemy:Bool; // видит ли его сейчас враг
	
	public function new(X:Float=0, Y:Float=0, ?SimpleGraphic:FlxGraphicAsset)
	{
		super(X, Y, SimpleGraphic);
		this.drag.x = this.drag.y = 1800;
		this.health = 9;//Здоровье
		loadGraphic(AssetPaths.playerCat__png, true, 29, 30);
		setFacingFlip(FlxObject.LEFT, false, false);
		setFacingFlip(FlxObject.RIGHT, true, false);
		animation.add("down", [0, 1, 2, 1], 7, false);
		animation.add("leftRight", [3, 4, 5, 4], 7, false);
		animation.add("up", [6, 7, 8, 7], 7, false);
		animation.add("box", [9], 0, false);
		
		setSize(16, 20);
		offset.set(5, 5);
	}
	
	override public function update(elapsed:Float):Void
	{
		movement();
		if (clipsCount < 0) clipsCount = 0;
		if (health <= 0) health = 0;
		super.update(elapsed);
	}
	
	private function movement():Void
	{
		_up = FlxG.keys.anyPressed([UP, W]);
		_down = FlxG.keys.anyPressed([DOWN, S]);
		_left = FlxG.keys.anyPressed([LEFT, A]);
		_right = FlxG.keys.anyPressed([RIGHT, D]);
		_shift = FlxG.keys.anyPressed([SHIFT]);
		_space = FlxG.keys.anyPressed([SPACE]);
		
		/*mouseX = FlxG.mouse.x - this.x - this.width/2;
		mouseY = FlxG.mouse.y - this.y - this.height/2;
		var aRadians:Float = Math.atan2(mouseY,mouseX);
		var aDegrees:Float = 360*(aRadians/(2*Math.PI));*/
		if (_shift) 
		{
			speed = 150;
		}
		else speed = 100;
		if (_up && _down)
			_up = _down = false;
		if (_left && _right)
			_left = _right = false;
		if (_up || _down || _left || _right && !isHidden)
		{
			var mA:Float = 0;
			if (_up)
			{
				mA = -90;
				if (_left)
					mA -= 45;
				else if (_right)
					mA += 45;
				facing = FlxObject.UP;
			}
			else if (_down)
			{
				mA = 90;
				if (_left)
					mA += 45;
				else if (_right)
					mA -= 45;
				facing = FlxObject.DOWN;
			}
			else if (_left)
			{
				mA = 180;
				facing = FlxObject.LEFT; 
			}
			else if (_right)
			{
				mA = 0;
				facing = FlxObject.RIGHT; 
			}
			if (!isHidden)
			{
				velocity.set(speed, 0);
				velocity.rotate(FlxPoint.weak(0, 0), mA);
			}
		}
		if ((velocity.x != 0 || velocity.y != 0) && touching == FlxObject.NONE && !isHidden)
		{
			switch (facing)
			{
				case FlxObject.LEFT, FlxObject.RIGHT:
					animation.play("leftRight");
				case FlxObject.UP:
					animation.play("up");
				case FlxObject.DOWN:
					animation.play("down");
			}
		}
		
	}
	
}