package entities;

import com.haxepunk.Entity;
import com.haxepunk.graphics.Image;
import com.haxepunk.graphics.Spritemap;
import com.haxepunk.masks.Circle;

class Keeper extends Entity 
{
	private var sprite: Spritemap;
	private var lastx: Float;
	private var lasty: Float;
	private var curMove: Int;
	
    public function new(x : Float, y : Float)
    {
//        super(x, y, Image.createCircle(15,0x47FE5F));
        super(x, y);

        sprite = new Spritemap("gfx/Keeper.png", 30, 30);
        sprite.add("stay", [0,0,0,0,0,0,0,2,0,0,0,0,0,1,1,1,0,0,0,0,0,2,0,0,0,0,0,0,3,3,0,0,0,0,1,1,1,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,3,3,0,0,0,0,0,0,1,1,1], 3);
        sprite.add("moveR", [16,17,18,19], 14);
        sprite.add("moveL", [20,21,22,23], 14);
        sprite.add("moveB", [13,14,15,14,13,12], 25);
        sprite.add("moveT", [9,10,11,10,9,8], 25);
        sprite.add("hiting", [4,5,6,7,6,5,4], 20);
        graphic = sprite;
        sprite.play("stay");

        type = "keeper";
		
		lastx = x; lasty = y;
		curMove = 0;
    }
	
	public function Hit()
	{
		sprite.play("hiting");
	}

	public function Stay()
	{
		sprite.play("stay");
	}
	
    public override function update() : Void
    {
		super.update();
		
		var difx: Float = x - lastx;
		var dify: Float = y - lasty;
		lastx = x; lasty = y;
		var difMov: Int = 0;
		if (Math.abs(difx) > Math.abs(dify)) {
			if (difx > 0) { difMov = 1; } else if (difx < 0) { difMov = 2; }
		} else {
			if (dify > 0) { difMov = 3; } else if (dify < 0) { difMov = 4; }
		}
		if (difMov != curMove) {
			curMove = difMov;
			switch (curMove)
			{
				case 0: sprite.play("stay");
				case 1: sprite.play("moveR");
				case 2: sprite.play("moveL");
				case 3: sprite.play("moveB");
				case 4: sprite.play("moveT");
			}
		}		
	}
}