Select Git revision
Tower.java 3.59 KiB
package world;
import interfaces.IBuyableItem;
import interfaces.IEnemyObserver;
import interfaces.IMagicrock;
import interfaces.ITickable;
import interfaces.IUpgradeable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import javax.print.attribute.standard.Fidelity;
import world.magicrocks.DecSpeedRock;
import world.magicrocks.IncRangeRock;
import engine.Engine;
import world.magicrocks.IncDMGRock;
public class Tower extends Placeable implements ITickable, IEnemyObserver,
IUpgradeable, IBuyableItem {
private HashMap<EnemyType, Integer> damage;
private int range;
private int recoilTimeLeft;
private Enemy target;
private ArrayList<Enemy> enemiesInRange;
private static ArrayList<IMagicrock> availableMagicRocks;
private static int recoilTime;
private boolean fogDescends = false;
private int fogTimeLeft = 0;
static {
availableMagicRocks = new ArrayList<IMagicrock>();
availableMagicRocks.add(new IncDMGRock());
availableMagicRocks.add(new IncRangeRock());
}
public Tower() {
damage = new HashMap<EnemyType, Integer>();
damage.put(EnemyType.HUMAN, 30);
damage.put(EnemyType.DWARF, 30);
damage.put(EnemyType.ELF, 30);
damage.put(EnemyType.HOBBIT, 30);
enemiesInRange = new ArrayList<Enemy>();
recoilTime = 4;
range = 1;
}
private Enemy searchTarget() {
if (!enemiesInRange.isEmpty())
return enemiesInRange.get(0);
else
return null;
}
public int getDamage(EnemyType type) {
return damage.get(type);
}
public void setDamage(EnemyType type, int d) {
damage.put(type, d);
}
public int getRecoilTime() {
return recoilTime;
}
public void setRecoilTime(int r) {
recoilTime = r;
}
private void getVisibleFields(ArrayList<Field> visibleFields, int iterations) {
if (iterations == 0)
return;
ArrayList<Field> newVisible = new ArrayList<Field>();
for (Field field : visibleFields) {
if (field == null)
continue;
for (Field neigh : field.getNeighbours()) {
if(!newVisible.contains(neigh))
newVisible.add(neigh);
}
}
for (Field neigh : newVisible) {
if(!visibleFields.contains(neigh))
visibleFields.add(neigh);
}
getVisibleFields(visibleFields, iterations - 1);
}
public void enemyMoved(Enemy enemy) {
enemiesInRange.clear();
ArrayList<Field> visibleFields = new ArrayList<Field>();
visibleFields.add(field);
if (fogDescends)
getVisibleFields(visibleFields, range - 1);
else
getVisibleFields(visibleFields, range);
for (Field field : visibleFields) {
if (field == null)
continue;
for (Enemy e : field.getEnemies()) {
if(!enemiesInRange.contains(e))
enemiesInRange.add(e);
}
}
target = null;
}
public void enemyDied(Enemy enemy) {
enemiesInRange.remove(enemy);
if (enemy == target)
target = null;
}
public ArrayList<IMagicrock> getAvailableMagicRocks() {
return availableMagicRocks;
}
public void tick() {
if (!fogDescends) {
if ((new Random()).nextInt(50) == 0) {
fogDescends = true;
fogTimeLeft = 3 + (new Random()).nextInt(10);
}
}
if (recoilTimeLeft <= 0) {
if (target == null)
target = searchTarget();
if (target != null)
target.hurt(damage.get(target.getType()));
recoilTimeLeft = recoilTime;
} else
recoilTimeLeft--;
if (fogTimeLeft == 0)
fogDescends = false;
if (fogTimeLeft != 0)
fogTimeLeft--;
}
public boolean getFogDescends()
{
return fogDescends;
}
public void setRange(int range)
{
if (range > 4)
return;
this.range = range;
}
public int getRange()
{
if (fogDescends) {
return range-1;
}
return range;
}
@Override
public int getCost() {
return 10;
}
}