Example #1
0
  private boolean placeUnit(Map map, Building building) {
    int minX = building.getX() - 1;
    int maxX = building.getX() + building.getType().getWidth();
    int minY = building.getY() - 1;
    int maxY = building.getY() + building.getType().getHeight();

    Tile freeTile = map.getNearestFreeTileForUnitPlacement(minX, maxX, minY, maxY);
    if (freeTile == null) {
      return false;
    }
    Unit unit = new Unit();
    unit.setOwnerId(building.getOwnerId());

    unit.setX(freeTile.getX());
    unit.setY(freeTile.getY());
    unit.setUnitType(unitType);
    ViewDirection unitDirection =
        ViewDirection.getDirection(unit.getPoint(), map.getClosestPoint(building, unit));
    unit.setViewDirection(ViewDirection.getDirectionByAngle(unitDirection.getAngle() + 180));

    if (unit.getUnitType() == UnitType.HARVESTER) {
      unit.setGoal(new Harvest());
    } else {
      unit.addDefaultGoal();
    }
    map.addUnit(unit);

    return true;
  }
Example #2
0
  @Override
  public void process(Building building, Map map, long tickCount) {
    blocked = false;
    Player player = map.getPlayerById(building.getOwnerId());

    if (building.getTicksAccumulated() >= unitType.getTicksToBuild()) {
      if (placeUnit(map, building)) {
        building.setTicksAccumulated(0);
        building.removeGoal(this);
      } else {
        blocked = true;
      }
    } else {
      if (player.getElectricity() < 0) {
        // throttle 3/4 of ticks, if not enough electricity
        if (tickCount % 4 != 3) {
          return;
        }
      }

      if (player.getMoney() >= unitType.getCostPerTick()) {
        player.setMoney(player.getMoney() - unitType.getCostPerTick());
        building.setTicksAccumulated(building.getTicksAccumulated() + 1);
        if (building.getTicksAccumulated() >= unitType.getTicksToBuild()) {
          if (placeUnit(map, building)) {
            building.setTicksAccumulated(0);
            building.removeGoal(this);
          }
        }
      }
    }
  }