package spaceinvaders.objects;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import java.util.TimerTask;

import spaceinvaders.images.Images;

public class Player extends Ship implements KeyListener{
	private Timer movetimer;
	private int movedir, lives, score;
	
	public Player(int x, int y) {
		super(Images.player,x,y);
		lives = 3;
		movetimer = new Timer();
		movetimer.schedule(new TimerTask() {
			@Override
			public void run() {
				border.setLocation((int)border.getX()+movedir*7, (int)border.getY());
			}
		}, 0, 40);
	}
	
	@Override
	public void keyTyped(KeyEvent e) {
	}
	@Override
	public void keyReleased(KeyEvent e) {
		if(((e.getKeyChar() == 'd' ||  e.getKeyCode() == KeyEvent.VK_RIGHT) && movedir == 1) || ((e.getKeyChar() == 'a' || e.getKeyCode() == KeyEvent.VK_LEFT) && movedir == -1)){
			movedir = 0;
		}
	}
	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getKeyChar() == 'd' || e.getKeyCode() == KeyEvent.VK_RIGHT){
			movedir = 1;
		}else if(e.getKeyChar() == 'a' || e.getKeyCode() == KeyEvent.VK_LEFT){
			movedir = -1;
		}
	}
	public void setLives(int lives){
		this.lives = lives;
	}

	public int getScore() {
		return score;
	}

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

	public int getLives() {
		return lives;
	}
	public void explode(){
		lives--;
	}
	public void reset(){
		lives = 3;
		score = 0;
	}
}
