/** 兵士クラスや、馬防柵クラスや、馬クラスや、山クラス、川クラス
などの盤上(boardクラス)におかれるすべてのオブジェクトの親クラス */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;




public abstract class Unit {

	String name;	//オブジェクトの名前。
	int x;	//そのユニットがBoard上のどの位置に設置されるか。
	int y;
	Board board; //そのユニットが配置される盤。
	static Component viewer;	//描画用のパネル。

	static UnitGraphicsEngine uge;


	int weight;	//そのユニットの重量。
	int hp;		//ヒットポイント。ゼロになると、そのユニットは消滅する。
	int maxHP = 100;	//そのクラスの最大ヒットポイント。

	int nationality;	// 敵・味方を識別する。味方なら同じ数字。中立あるいは無所属ユニットの値は0。
	Direction direction;	//そのユニットが、いまどの方向を向いているか。


	public int getX() { return x; }
	public void setX( int x ) { this.x = x; }
	public int getY() { return y; }
	public void setY( int y ) { this.y = y; }
	public void setHp( int hp ) { this.hp = hp; }
	public int getHp() { return hp; }
	public int getMaxHp() { return maxHP; }
	public int getNationality() { return nationality; }
	public Direction getDirection() { return direction; }

	public void setDirection(int direction) {
		if (this.direction == null) {
			this.direction = new Direction(direction);
		}
		else {
			this.direction.setDirection(direction);
		}
	}

	public void setDirection(Direction direction) {
		if (this.direction == null) {
			this.direction = new Direction(direction);
		}
		else {
			this.direction.setDirection(direction);
		}
	}


	public void show(Graphics g, int x, int y, Component view) {
		uge.show(g, x,y, this, view);
	}

	public void setBoard(Board board) {
		this.board = board;
	}
	public static void setViewer(Component viewer) {
		Unit.viewer = viewer;
	}
	public static void setUGE(UnitGraphicsEngine uge) {
		Unit.uge = uge;
	}

	/** 
	 * 生きているか(HPがゼロより大きく、
	 * なおかつ盤上の正しい位置に存在するか)をチェックする。
	*/
	public boolean checkExistence() {
		if (hp > 0) {
			if (this == board.getUnit(x,y))
				return true;
			else
				return false;
		}
		else {
			return false;
		}
	}

	/** ボードから自分自身を削除する。 */
	public void remove() {
		if (this == board.getUnit(x,y))
			board.removeUnit(x,y);
	}

	/** 引数として与えられたユニットが敵か味方かを判別する */
	boolean checkEnemy(Unit unIdentification) {
		if (unIdentification != null) {
			if ( getNationality() != unIdentification.getNationality() )
				return true;
			else
				return false;
		}
		else
			return false;
	}
}