Search in sources :

Example 46 with TripleAUnit

use of games.strategy.triplea.TripleAUnit in project triplea by triplea-game.

the class UnitsDrawer method draw.

@Override
public void draw(final Rectangle bounds, final GameData data, final Graphics2D graphics, final MapData mapData, final AffineTransform unscaled, final AffineTransform scaled) {
    // If there are too many Units at one point a black line is drawn to make clear which units belong to where
    if (overflow) {
        graphics.setColor(Color.BLACK);
        graphics.fillRect(placementPoint.x - bounds.x - 2, placementPoint.y - bounds.y + uiContext.getUnitImageFactory().getUnitImageHeight(), uiContext.getUnitImageFactory().getUnitImageWidth() + 2, 3);
    }
    final UnitType type = data.getUnitTypeList().getUnitType(unitType);
    if (type == null) {
        throw new IllegalStateException("Type not found:" + unitType);
    }
    final PlayerID owner = data.getPlayerList().getPlayerId(playerName);
    final Optional<Image> img = uiContext.getUnitImageFactory().getImage(type, owner, damaged > 0 || bombingUnitDamage > 0, disabled);
    if (!img.isPresent()) {
        ClientLogger.logError("MISSING IMAGE (this unit or image will be invisible): " + type);
    }
    if (img.isPresent() && enabledFlags) {
        final int maxRange = new TripleAUnit(type, owner, data).getMaxMovementAllowed();
        switch(drawUnitNationMode) {
            case BELOW:
                // If unit is not in the "excluded list" it will get drawn
                if (maxRange != 0) {
                    final Image flag = uiContext.getFlagImageFactory().getFlag(owner);
                    // centered flag in the middle
                    final int xoffset = img.get().getWidth(null) / 2 - flag.getWidth(null) / 2;
                    final int yoffset = img.get().getHeight(null) / 2 - flag.getHeight(null) / 4 - // centered flag in the middle moved it 1/2 - 5 down
                    5;
                    graphics.drawImage(flag, (placementPoint.x - bounds.x) + xoffset, (placementPoint.y - bounds.y) + yoffset, null);
                }
                drawUnit(graphics, img.get(), bounds);
                break;
            case NEXT_TO:
                drawUnit(graphics, img.get(), bounds);
                // If unit is not in the "excluded list" it will get drawn
                if (maxRange != 0) {
                    final Image flag = uiContext.getFlagImageFactory().getSmallFlag(owner);
                    final int xoffset = img.get().getWidth(null) - flag.getWidth(// If someone wants to put more effort in this, he could add an algorithm to calculate the real
                    null);
                    final int yoffset = // lower right corner - transparency/alpha channel etc.
                    img.get().getHeight(null) - flag.getHeight(null);
                    // currently the flag is drawn in the lower right corner of the image's bounds -> offsets on some unit
                    // images
                    // This Method draws the Flag in the lower right corner of the unit image. Since the position is the upper
                    // left corner we have to move the picture up by the height and left by the width.
                    graphics.drawImage(flag, (placementPoint.x - bounds.x) + xoffset, (placementPoint.y - bounds.y) + yoffset, null);
                }
                break;
            default:
                throw new AssertionError("unknown unit flag draw mode: " + drawUnitNationMode);
        }
    } else {
        img.ifPresent(image -> drawUnit(graphics, image, bounds));
    }
    // more then 1 unit of this category
    if (count != 1) {
        final int stackSize = mapData.getDefaultUnitsStackSize();
        if (stackSize > 0) {
            // Display more units as a stack
            for (int i = 1; i < count && i < stackSize; i++) {
                if (img.isPresent()) {
                    graphics.drawImage(img.get(), placementPoint.x + 2 * i - bounds.x, placementPoint.y - 2 * i - bounds.y, null);
                }
            }
            if (count > stackSize) {
                final Font font = MapImage.getPropertyMapFont();
                if (font.getSize() > 0) {
                    graphics.setColor(MapImage.getPropertyUnitCountColor());
                    graphics.setFont(font);
                    // draws how many units there are
                    graphics.drawString(// draws how many units there are
                    String.valueOf(count), placementPoint.x - bounds.x + 2 * stackSize + (uiContext.getUnitImageFactory().getUnitImageWidth() * 6 / 10), placementPoint.y - 2 * stackSize - bounds.y + uiContext.getUnitImageFactory().getUnitImageHeight() / 3);
                }
            }
        } else {
            // Display a white number at the bottom of the unit
            final Font font = MapImage.getPropertyMapFont();
            if (font.getSize() > 0) {
                graphics.setColor(MapImage.getPropertyUnitCountColor());
                graphics.setFont(font);
                // draws how many units there are
                graphics.drawString(// draws how many units there are
                String.valueOf(count), placementPoint.x - bounds.x + (uiContext.getUnitImageFactory().getUnitCounterOffsetWidth()), placementPoint.y - bounds.y + uiContext.getUnitImageFactory().getUnitCounterOffsetHeight());
            }
        }
    }
    displayHitDamage(bounds, graphics);
    // Display Factory Damage
    if (isDamageFromBombingDoneToUnitsInsteadOfTerritories(data) && Matches.unitTypeCanBeDamaged().test(type)) {
        displayFactoryDamage(bounds, graphics);
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) UnitType(games.strategy.engine.data.UnitType) Image(java.awt.Image) MapImage(games.strategy.triplea.image.MapImage) TripleAUnit(games.strategy.triplea.TripleAUnit) Point(java.awt.Point) Font(java.awt.Font)

Example 47 with TripleAUnit

use of games.strategy.triplea.TripleAUnit in project triplea by triplea-game.

the class ProductionRepairPanel method initRules.

private void initRules(final PlayerID player, final Collection<PlayerID> allowedPlayersToRepair, final GameData data, final HashMap<Unit, IntegerMap<RepairRule>> initialPurchase) {
    if (!Properties.getDamageFromBombingDoneToUnitsInsteadOfTerritories(data)) {
        return;
    }
    this.data.acquireReadLock();
    try {
        this.id = player;
        this.allowedPlayersToRepair = allowedPlayersToRepair;
        final Predicate<Unit> myDamagedUnits = Matches.unitIsOwnedByOfAnyOfThesePlayers(this.allowedPlayersToRepair).and(Matches.unitHasTakenSomeBombingUnitDamage());
        final Collection<Territory> terrsWithPotentiallyDamagedUnits = CollectionUtils.getMatches(data.getMap().getTerritories(), Matches.territoryHasUnitsThatMatch(myDamagedUnits));
        for (final RepairRule repairRule : player.getRepairFrontier()) {
            for (final Territory terr : terrsWithPotentiallyDamagedUnits) {
                for (final Unit unit : CollectionUtils.getMatches(terr.getUnits().getUnits(), myDamagedUnits)) {
                    if (!repairRule.getResults().keySet().iterator().next().equals(unit.getType())) {
                        continue;
                    }
                    final TripleAUnit taUnit = (TripleAUnit) unit;
                    final Rule rule = new Rule(repairRule, player, unit, terr);
                    int initialQuantity = 0;
                    if (initialPurchase.get(unit) != null) {
                        initialQuantity = initialPurchase.get(unit).getInt(repairRule);
                    }
                    rule.setQuantity(initialQuantity);
                    rule.setMax(taUnit.getHowMuchCanThisUnitBeRepaired(unit, terr));
                    rule.setName(unit.toString());
                    rules.add(rule);
                }
            }
        }
    } finally {
        this.data.releaseReadLock();
    }
}
Also used : Territory(games.strategy.engine.data.Territory) RepairRule(games.strategy.engine.data.RepairRule) RepairRule(games.strategy.engine.data.RepairRule) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) TripleAUnit(games.strategy.triplea.TripleAUnit)

Aggregations

TripleAUnit (games.strategy.triplea.TripleAUnit)47 Unit (games.strategy.engine.data.Unit)42 Territory (games.strategy.engine.data.Territory)25 PlayerID (games.strategy.engine.data.PlayerID)19 UnitType (games.strategy.engine.data.UnitType)16 CompositeChange (games.strategy.engine.data.CompositeChange)15 Test (org.junit.jupiter.api.Test)14 Route (games.strategy.engine.data.Route)13 ITestDelegateBridge (games.strategy.engine.data.ITestDelegateBridge)12 ArrayList (java.util.ArrayList)11 GameData (games.strategy.engine.data.GameData)10 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)8 IntegerMap (games.strategy.util.IntegerMap)8 Change (games.strategy.engine.data.Change)7 RepairRule (games.strategy.engine.data.RepairRule)5 HashSet (java.util.HashSet)4 Resource (games.strategy.engine.data.Resource)3 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 ResourceCollection (games.strategy.engine.data.ResourceCollection)2