/**
 * 
 */
package net.phatcode.rel;

import java.awt.Graphics2D;
import java.awt.event.KeyEvent;

import net.phatcode.rel.utils.ImageAtlas;
import net.phatcode.rel.utils.Keyboard;
import net.phatcode.rel.utils.Sonics;


/**
 * @author Richard Eric M. Lope
 *
 */
public class Fighter extends Entity
{

	private Shot shot = new Shot();
	private int lives = 3;
	private int score = 0;
	
	public Fighter()
	{
		speed = 2;
		numFrames = 3;
		x = Constants.SCREEN_WIDTH / 2;
		y = Constants.SCREEN_HEIGHT - 80;
	}
	
	public void reset()
	{
		frame = 0;
		baseFrame = 9;
		dx = 0;
		dy = 0;
		x = Constants.SCREEN_WIDTH / 2;
		y = Constants.SCREEN_HEIGHT - 80;
		shot.kill();
	}
	
	public void update( boolean controlMe )
	{
		
		if( shot.isActive() )
		{
			shot.update();
		}
		
		dx = 0;
		dy = 0;
		
		if( controlMe )
		{
			if( Keyboard.getInstance().isKeyPressed(KeyEvent.VK_RIGHT))
			{
				dx = speed;
			}
			
			if( Keyboard.getInstance().isKeyPressed(KeyEvent.VK_LEFT))
			{
				dx = -speed;
			}
			
			if( Keyboard.getInstance().isKeyPressed(KeyEvent.VK_SPACE))
			{
			
				if( !shot.isActive() )
				{
					shot.setX(x);
					shot.setY(y);
					shot.setActive(true);
					Sonics.getInstance().playEffect("audio/shot.wav");
				}
				
			}
		}
		
		x += dx;
		y += dy;
		
		if( x < (width/2) )
		{
			x = (width/2);
		}
		
		if( x > (Constants.SCREEN_WIDTH-(width/2)) )
		{
			x = Constants.SCREEN_WIDTH-(width/2);
		}
		
		if( (++ticks & 7) == 0)
		{
			frame = (++frame) % 3; 
		}
		
	}
	
	public void render( Screen screen, Graphics2D g,
						ImageAtlas imageAtlas, ImageAtlas shotAtlas )
	{
		
		shot.render( screen, g, shotAtlas );
		
		int screenX = (int)(x - width/2.0f);
		int screenY = (int)(y - height/2.0f);
		
		g.drawImage( imageAtlas.getSprite( baseFrame + frame ), 
					 screenX,
					 screenY,
			 	     screen );
	}

	public Shot getShot()
	{
		return shot;
	}

	public int getLives()
	{
		return lives;
	}

	public void setLives(int lives)
	{
		this.lives = lives;
	}

	public void incLives()
	{
		this.lives++;
	}

	public void decLives()
	{
		this.lives--;
	}

	
	public int getScore()
	{
		return score;
	}

	public void setScore(int score)
	{
		this.score = score;
	}

	public void addToScore(int value)
	{
		this.score += value;
		if( this.score > 9999999 ) this.score =  9999999;
	}
				
}
