package ei.game.scene.units;

import ei.engine.math.Vector2f;
import ei.engine.math.Vector2i;
import ei.engine.scene.Sprite;
import ei.engine.sound.Sound;
import ei.game.network.entities.NetworkUnit;
import ei.game.player.Player;
import ei.game.scene.SelectBox;
import ei.game.scene.weapons.CannonBall;
import ei.game.scene.weapons.Explotion;
import ei.game.scene.weapons.Weapon;
import ei.game.scene.weapons.WeaponHandler;

/**
 * 
 * @author Jesper Lundin
 *
 */
public class Tank extends NetworkUnit{
	private SelectBox selectionBox;
	private Sprite sprite;
	
	private Sound gunSound;
	private Sound[] moveSound;
	private Sound attackSound;
	private Sound selectSound;
	
	public Tank(Player p) {
		super(100, p);
	}
	
	public Tank(int x, int y, Player p){
		super(100, new Vector2i(x,y), p);
	}
	
	public void init(){
		setBuildTime(300);
		setPrice(400);
	}	
	
	public void initGraphics(){
		sprite = new Sprite("Tank", "data/units/tank/tank0000.png");
		sprite.setSize(new Vector2f(40,40));
		getNode().add(sprite);
		
		gunSound = new Sound("gunSound", "data/sounds/tankweapon.wav");
		moveSound = new Sound[2];
		moveSound[0] = new Sound("moveSound", "data/sounds/TANKmove1.wav");
		moveSound[1] = new Sound("moveSound", "data/sounds/TANKmove2.wav");
		selectSound = new Sound("selectSound", "data/sounds/TANKselect.wav");
		attackSound = new Sound("attackSound", "data/sounds/TANKattack.wav");
		
		gunSound.setLocation(getNode().getLocation());
		moveSound[0].setLocation(getNode().getLocation());
		moveSound[1].setLocation(getNode().getLocation());
		selectSound.setLocation(getNode().getLocation());
		attackSound.setLocation(getNode().getLocation());
		
		selectionBox = new SelectBox(0,40,40,getMaxLife());
	}

	protected SelectBox getSelection() {	
		return selectionBox;
	}
	/**
	 * Returns the weapon connected to this type
	 * of unit.
	 */
	public Weapon getWeapon(Vector2f startPos) {
		return new CannonBall(startPos);
	}
	public Sound getGunSound() {
		return gunSound;
	}
	public Sound getMoveSound() {
		return moveSound[(int)(Math.random()*2)];
	}
	public Sound getSelectSound() {
		return selectSound;
	}
	public Sound getAttackSound() {
		return attackSound;
	}
	/**
	 * This unit type is now destroyed.
	 */
	public void destroyed(){
		WeaponHandler.getInstance().addWeapon(new Explotion(new Vector2f(getNode().getLocation().getX(), getNode().getLocation().getY())));
	}
	/**
	 * returns the velocity of the unit type.
	 * @return
	 */
	public float getVelocity() {
		return 2;
	}
	/**
	 * Manages the sprite connected to this unit.
	 * @param s
	 */
	public void setSprite(Sprite s) {
		this.sprite = s;
	}
	/**
	 * returns the sprite connected to this type
	 * of unit.
	 * @return
	 */
	public Sprite getSprite() {
		return this.sprite;
	}

	@Override
	public int getMaintenanceCost() {
		return 50;
	}
}
