/** Groundクラスの上に配置される動きまわるユニット。軍隊・市民・輸送車。歩兵・騎兵・弓兵など */

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

public abstract class Creature extends Unit implements RunnableUnit {

	boolean auto;	// trueなら自己判断して行動する。
	Unit target;	// 攻撃目標。(もしくは移動目標)

	boolean walking;	//前に進む(true)？、それとも、止まる(false)？

	int maxSpeed;		// 最高速度
	int movementPower;	// 移動力。(各ユニットは1ターンごとに、現在速度に応じた移動力がたまっていく。現在そのユニットがいる地形におうじた移動力がたまれば、つぎのマスにすすむことができる。)

	public Creature(int x, int y, Board board, Direction direction, int nationality) {
		this.x = x;
		this.y = y;
		this.board = board;

		this.direction = direction;
		this.nationality = nationality;

		walking = true;

		this.target = null;
		this.auto = false;

		init();
	}
	public Creature(int x, int y, Board board, Direction direction, int nationality, Unit target, boolean auto) {
		this.x = x;
		this.y = y;
		this.board = board;

		this.direction = direction;
		this.nationality = nationality;

		walking = true;

		this.target = target;
		this.auto = auto;

		init();
	}

	public void init() {
		hp = 100;

		maxSpeed = 2;
		movementPower = 0;
	}

	public void run() { };

	public void show(Graphics g, int x, int y, Component view) {
		uge.show(g, x,y, this, view);
	}

	protected void move() { };

	/** Creatureを自己判断するようにしたり、させなかったりするメソッド。 */
	public void setAuto(boolean auto) { this.auto = auto; }
	public boolean getAuto() { return auto; }

	/** 兵士が現在の状況を自分で判断して行動するためのメソッド。 */
	void selfJudge() {
		if ( x == target.getX() && y ==target.getY() ) {
			walking = false;
		}
		else {
			walking = true;
			direction.calculateDirectionStoD(x, y,  target.getX(), target.getY());
		}
	}

	public void setTarget(Unit target) { this.target = target; }
	public Unit getTarget() { return target; }

	public boolean getWalking() {
		return walking;
	}
	public void setWalking(boolean walking) {
		this.walking = walking;
	}


	/** 
	 * 生きているか(HPがゼロより大きく、
	 * なおかつ盤上の正しい位置に存在するか)をチェックする。
	*/
	public boolean checkExistence() {
		if (hp > 0) {
			if (this == board.getCreature(x,y))
				return true;
			else
				return false;
		}
		else {
			return false;
		}
	}

	/** ボードから自分自身を削除する。 */
	public void remove() {

		// ボードの二次元配列から自分自身を削除する。
		if (this == board.getCreature(x,y))
			board.removeCreature(x,y);

		// ボードのRunnableUnitListから自分自身を削除する。
		board.getRunnableUnitList().remove( this );
	}

}

