
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;





public abstract class FlyingUnit extends Unit implements RunnableUnit {

	Unit target;		// 攻撃目標。
	int presentSpeed;	// 現在速度
	int movementPower;


	public FlyingUnit(int x, int y, Board board, Direction direction, int nationality, Unit target) {
		this.x = x;
		this.y = y;
		this.board = board;

		this.nationality = nationality;
		this.direction = direction;

		this.target = target;

		init();
	}

	public void init() {
		presentSpeed = 7;
		movementPower = 0;
	}

	public void run() {

		// 目標物に到着したのなら削除。到着していないのならば目標物めがけて移動。
		if ( checkArrival() ) {
			remove();
		}
		else {
			move();
		}

	}

	public void move() {

		// ターゲットの位置を元に方向を修正。
		direction.calculateDirectionStoD(x, y,  target.getX(), target.getY());

		// ユニットの移動力を速度に応じて増やす。
		movementPower += presentSpeed;

		// もし移動力が十分にたまっているのなら、いまいるスクウェアから抜け出す。
		if (10 < movementPower ) {

			// 位置を変更。
			x += direction.getNextX();
			y += direction.getNextY();

			// 現在速度を修正。
			///

			// 移動力を0に。
			movementPower = 0;

		}

	}


	public void show(Graphics g, int x, int y, Component view) {
		uge.show(g, x,y, this, view);
	}

	/** ターゲットの座標まで到着したか否か。 */
	public boolean checkArrival() {
		if ( x == target.getX() && y == target.getY() ) 
			return true;
		else
			return false;
	}

	public void setTarget(Unit target) { this.target = target; }
	public Unit getTarget() { return target; }


	/** ボードから自分自身を削除する。 */
	public void remove() {

		// ボードのRunnableUnitListから自分自身を削除する。
		board.getFlyingUnitList().remove( this );
	}
}
