ublic class Unit {
private final GamePanel game;
private int row;
private int col;
private UnitType type;
protected int hitpoints;
protected int strength;
public Unit(GamePanel game, int row, int col,UnitType type) {
this.game = game;
this.type=type;
this.row = row;
this.col = col;
}
public void placeUnit(int row, int col) {
if (game.occupied(row, col)) return;
game.removeUnit(this.row, this.col);
game.placeUnit( row, col,type);
this.row = row;
this.col = col;
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
public boolean isStationary() {
return false;
}
public boolean isLivingPlant() {
return false;
}
public boolean isLivingBug() {
return false;
}
public boolean isLivingEnemy() {
return false;
}
public void act(Set<Unit> units) {
}
protected void moveRandomly() {
placeUnit(randomWalk(row), randomWalk(col));
}
protected void moveTowards(Unit other) {
int nextrow = row;
int nextcol = col;
if (row < other.row) nextrow++;
if (row > other.row) nextrow--;
if (col < other.col) nextcol++;
if (col > other.col) nextcol--;
placeUnit(nextrow, nextcol);
}
private int randomWalk(int r) {
if (Math.random() < 0.25) return r - 1;
if (Math.random() > 0.75) return r + 1;
return r;
}
protected void attack(Unit other) {
System.out
.println(this.type + " hits " + other.type + " Remaining Hitpoints: " + other.hitpoints);
other.hit(strength);
}
private void hit(int strength) {
hitpoints -= strength;
}
protected boolean isNeighbor(Unit other, int dist) {
return Math.abs(row - other.row) <= dist &&
Math.abs(col - other.col) <= dist;
}
protected int distance(Unit me, Unit a, Unit b) {
var d1 = distance(me, a);
var d2 = distance(me, b);
return Double.compare(d1, d2);
}
private double distance(Unit me, Unit b) {
return Math.sqrt(square(me.row - b.row) + square(me.col - b.col));
}
private double square(int v) {
return v * v;
}
public boolean isDead() {
return hitpoints <= 0;
}
public boolean isAlive() {
return hitpoints > 0;
}
}