package net.phatcode.rel;

import net.phatcode.rel.math.Utils;


public class Animator
{

	public static enum Mode
	{
		NORMAL, ORIENTED
	}

	protected int counter = 0;
	protected int delay = 4;

	public int frame = 0;
	public int baseFrame = 0;
	public int numFrames = 1;
	public int angle = 0;
	
	public Mode mode = Mode.NORMAL;
	public int angledFrame = 0;
	public int angledImages =  1;

	
	public Animator()
	{
	}

	public Animator( Animator animator )
	{

		counter = 0;
		delay = 4;

		this.frame = 0;
		this.baseFrame = animator.baseFrame;
		this.numFrames = animator.numFrames;
		this.angle = animator.angle;

	}

	public Animator( int baseFrame, int numFrames )
	{

		this.baseFrame = baseFrame;
		this.numFrames = numFrames;
		this.angle = 0;
		
	}

	public Animator( int baseFrame, int numFrames, int angledImages, Mode mode )
	{

		counter = 0;
		delay = 4;

		this.frame = 0;
		this.angle = 0;
		this.mode = mode;
		this.baseFrame = baseFrame;
		this.numFrames = numFrames;
		this.angledImages = angledImages;

	}

	public void set( int baseFrame, int numFrames )
	{
		
		this.baseFrame = baseFrame;
		this.numFrames = numFrames;
		
		counter = 0;
		delay = 4;

		frame = 0;
		angle = 0;
		
		mode = Mode.NORMAL;
		angledFrame = 0;
		angledImages =  1;

	}
	
	public void set( int baseFrame, int numFrames, int angledImages, Mode mode )
	{
		
		this.baseFrame = baseFrame;
		this.numFrames = numFrames;
		this.angledImages = angledImages;
		this.mode = mode;
		
		counter = 0;
		delay = 4;

		frame = 0;
		angle = 0;
		
		angledFrame = 0;
		
	}

	public void set( Animator animator )
	{
		
		this.counter = animator.counter;
		this.delay = animator.delay;

		this.frame = animator.frame;
		this.baseFrame = animator.baseFrame;
		this.numFrames = animator.numFrames;
		this.angle = animator.angle;
		
		this.mode = animator.mode;
		this.angledFrame = animator.angledFrame;
		this.angledImages = animator.angledImages;

	}
	
	public void reset( Animator animator )
	{
		
		this.counter = 0;
		this.delay = 4;

		this.frame = animator.frame;
		this.baseFrame = animator.baseFrame;
		this.numFrames = animator.numFrames;
		this.angle = animator.angle;
		
		this.mode = animator.mode;
		this.angledFrame = animator.angledFrame;
		this.angledImages = animator.angledImages;

	}

	
	public int getFrame()
	{
		return baseFrame + angledFrame + frame;
	}

	public void update()
	{

		counter++;
		if ((counter % delay) == 0)
		{
			frame = (frame + 1) % numFrames;
		}

		if( mode == Mode.ORIENTED )
		{
			angle = Utils.wrap( angle, 0, 360 - angledImages );
			angledFrame =((int)( (angle * angledImages / 360.0f) + 0.5f)); 
		}


	}

	public int getAngle()
	{
		return angle;
	}

	public void setAngle(int angle)
	{
		this.angle = angle;
	}

	public Mode getMode()
	{
		return mode;
	}

	public void setMode(Mode mode)
	{
		this.mode = mode;
	}

	
	
}
