当前位置: 首页>>代码示例>>Java>>正文


Java Role类代码示例

本文整理汇总了Java中net.sf.freecol.common.model.Role的典型用法代码示例。如果您正苦于以下问题:Java Role类的具体用法?Java Role怎么用?Java Role使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


Role类属于net.sf.freecol.common.model包,在下文中一共展示了Role类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getUnitImage

import net.sf.freecol.common.model.Role; //导入依赖的package包/类
public static BufferedImage getUnitImage(UnitType unitType, String roleId,
                                         boolean nativeEthnicity,
                                         Dimension size) {
    // units that can only be native don't need the .native key part
    if (unitType.hasAbility(Ability.BORN_IN_INDIAN_SETTLEMENT)) {
        nativeEthnicity = false;
    }

    // try to get an image matching the key
    String roleQual = (Role.isDefaultRoleId(roleId)) ? ""
        : "." + Role.getRoleSuffix(roleId);
    String key = "image.unit." + unitType.getId() + roleQual
        + ((nativeEthnicity) ? ".native" : "");
    if (!ResourceManager.hasImageResource(key) && nativeEthnicity) {
        key = "image.unit." + unitType.getId() + roleQual;
    }
    BufferedImage image = ResourceManager.getImage(key, size);
    return image;
}
 
开发者ID:FreeCol,项目名称:freecol,代码行数:20,代码来源:ImageLibrary.java

示例2: addOwnUnits

import net.sf.freecol.common.model.Role; //导入依赖的package包/类
/**
 * {@inheritDoc}
 */
@Override
protected void addOwnUnits() {
    final Specification spec = getSpecification();
    final Player player = getMyPlayer();

    reportPanel.add(Utility.localizedLabel(player.getForcesLabel()), NL_SPAN_SPLIT_2);
    reportPanel.add(new JSeparator(JSeparator.HORIZONTAL), "growx");

    // Report unit types that are inherently reportable, and units
    // with military roles.
    final List<Role> militaryRoles = spec.getMilitaryRolesList();
    for (UnitType ut : spec.getUnitTypeList()) {
        if (isReportable(ut)) {
            tryUnitRole(ut, Specification.DEFAULT_ROLE_ID);
        }
        for (Role r : militaryRoles) {
            tryUnitRole(ut, r.getId());
        }
    }
}
 
开发者ID:FreeCol,项目名称:freecol,代码行数:24,代码来源:ReportMilitaryPanel.java

示例3: equipForRole

import net.sf.freecol.common.model.Role; //导入依赖的package包/类
/**
 * Equips this AI unit for a particular role.
 *
 * The unit must be at a location where the required goods are available
 * (possibly requiring a purchase, which may fail due to lack of gold
 * or boycotts in effect).
 *
 * @param role The {@code Role} to equip for identifier.
 * @return True if the role change was successful.
 */
public boolean equipForRole(Role role) {
    final Player player = unit.getOwner();
    Location loc = Location.upLoc(unit.getLocation());
    if (!(loc instanceof UnitLocation)) return false;
    int count = role.getMaximumCount();
    if (count > 0) {
        for (; count > 0; count--) {
            List<AbstractGoods> req = unit.getGoodsDifference(role, count);
            try {
                int price = ((UnitLocation)loc).priceGoods(req);
                if (player.checkGold(price)) break;
            } catch (FreeColException fce) {
                continue;
            }
        }
        if (count <= 0) return false;
    }
    return AIMessage.askEquipForRole(this, role, count)
        && unit.getRole() == role && unit.getRoleCount() == count;
}
 
开发者ID:FreeCol,项目名称:freecol,代码行数:31,代码来源:AIUnit.java

示例4: trySwapExpert

import net.sf.freecol.common.model.Role; //导入依赖的package包/类
/**
 * Tries to swap an expert unit for another doing its job.
 *
 * @param expert The expert {@code Unit}.
 * @param others A list of other {@code Unit}s to test against.
 * @param colony The {@code Colony} the units are working in.
 * @return The unit that was replaced by the expert, or null if none.
 */
private Unit trySwapExpert(Unit expert, List<Unit> others, Colony colony) {
    final Role oldRole = expert.getRole();
    final int oldRoleCount = expert.getRoleCount();
    final GoodsType work = expert.getType().getExpertProduction();
    final GoodsType oldWork = expert.getWorkType();
    final Unit other = find(others, u ->
        (u.isPerson() && u.getWorkType() == work
            && u.getType().getExpertProduction() != work));
    if (other != null) {
        Location l1 = expert.getLocation();
        Location l2 = other.getLocation();
        other.setLocation(colony.getTile());
        expert.setLocation(l2);
        expert.changeWorkType(work);
        other.setLocation(l1);
        if (oldWork != null) other.changeWorkType(oldWork);
        Role tmpRole = other.getRole();
        int tmpRoleCount = other.getRoleCount();
        other.changeRole(oldRole, oldRoleCount);
        expert.changeRole(tmpRole, tmpRoleCount);
    }
    return other;
}
 
开发者ID:FreeCol,项目名称:freecol,代码行数:32,代码来源:ColonyPlan.java

示例5: changeRole

import net.sf.freecol.common.model.Role; //导入依赖的package包/类
/**
 * Debug action to change the roles of a unit.
 *
 * Called from tile popup menu.
 *
 * @param freeColClient The {@code FreeColClient} for the game.
 * @param unit The {@code Unit} to change the role of.
 */
public static void changeRole(final FreeColClient freeColClient,
                              final Unit unit) {
    final FreeColServer server = freeColClient.getFreeColServer();
    final Game sGame = server.getGame();
    final Unit sUnit = sGame.getFreeColGameObject(unit.getId(), Unit.class);
    final GUI gui = freeColClient.getGUI();
    final Function<Role, ChoiceItem<Role>> roleMapper = r ->
        new ChoiceItem<Role>(r.getId(), r);

    Role roleChoice = gui.getChoice(null,
        StringTemplate.template("prompt.selectRole"), "cancel",
        transform(sGame.getSpecification().getRoles(), alwaysTrue(),
                  roleMapper, Comparator.naturalOrder()));
    if (roleChoice == null) return;

    sUnit.changeRole(roleChoice, roleChoice.getMaximumCount());
    reconnect(freeColClient);
}
 
开发者ID:FreeCol,项目名称:freecol,代码行数:27,代码来源:DebugUtils.java

示例6: getUnitImage

import net.sf.freecol.common.model.Role; //导入依赖的package包/类
/**
 * Gets the image that will represent a given unit.
 *
 * @param unitType The type of unit to be represented.
 * @param roleId The id of the unit role.
 * @param nativeEthnicity If true the unit is a former native.
 * @param grayscale If true draw in inactive/disabled-looking state.
 * @param scale How much the image is scaled.
 * @return A suitable {@code BufferedImage}.
 */
public static BufferedImage getUnitImage(UnitType unitType, String roleId,
                                         boolean nativeEthnicity,
                                         boolean grayscale, float scale) {
    // units that can only be native don't need the .native key part
    if (unitType.hasAbility(Ability.BORN_IN_INDIAN_SETTLEMENT)) {
        nativeEthnicity = false;
    }

    // try to get an image matching the key
    String roleQual = (Role.isDefaultRoleId(roleId)) ? ""
        : "." + Role.getRoleSuffix(roleId);
    String key = "image.unit." + unitType.getId() + roleQual
        + ((nativeEthnicity) ? ".native" : "");
    if (!ResourceManager.hasImageResource(key) && nativeEthnicity) {
        key = "image.unit." + unitType.getId() + roleQual;
    }
    BufferedImage image = (grayscale)
        ? ResourceManager.getGrayscaleImage(key, scale)
        : ResourceManager.getImage(key, scale);
    return image;
}
 
开发者ID:wintertime,项目名称:FreeCol,代码行数:32,代码来源:ImageLibrary.java

示例7: equipUnitIfPossible

import net.sf.freecol.common.model.Role; //导入依赖的package包/类
private boolean equipUnitIfPossible(UnitLabel unitLabel,
                                    AbstractGoods goods) {
    final Unit unit = unitLabel.getUnit();
    if (!unit.hasAbility(Ability.CAN_BE_EQUIPPED)
        || unit.getRole().hasAbility(Ability.ESTABLISH_MISSION)) {
        // Do not equip missionaries.  The test below will succeed
        // when dragging incompatible goods (anything:-) because
        // there is no actual missionary equipment.
        return false;
    }

    for (Role role : transform(unit.getAvailableRoles(null),
                               r -> !r.isDefaultRole())) {
        List<AbstractGoods> required = unit.getGoodsDifference(role, 1);
        int count;
        if (required.size() == 1
            && required.get(0).getType() == goods.getType()
            && (count = Math.min(role.getMaximumCount(),
                    goods.getAmount() / required.get(0).getAmount())) > 0
            && (role != unit.getRole() || count != unit.getRoleCount())) {
            freeColClient.getInGameController()
                .equipUnitForRole(unit, role, count);
            unitLabel.updateIcon();
            return true;
        }
    }
    return false;
}
 
开发者ID:FreeCol,项目名称:freecol,代码行数:29,代码来源:DefaultTransferHandler.java

示例8: itemStateChanged

import net.sf.freecol.common.model.Role; //导入依赖的package包/类
/**
 * {@inheritDoc}
 */
@Override
public void itemStateChanged(ItemEvent e) {
    // When the unit type changes, we have to reset the role choices
    JComboBox<String> box = this.roleUI.getComponent();
    DefaultComboBoxModel<String> model;
    boolean enable = false;
    UnitType type = (UnitType)this.typeUI.getComponent().getSelectedItem();
    if (type != null && type.hasAbility(Ability.CAN_BE_EQUIPPED)) {
        final Specification spec = type.getSpecification();
        final NationType nt = getOption().getNationType();
        int n = 0;
        model = new DefaultComboBoxModel<>();
        for (String ri : getOption().getRole().getChoices()) {
            Role role = spec.getRole(ri);
            if (role.isAvailableTo(type, nt)) {
                model.addElement(ri);
                n++;
            }
        }
        enable = isEditable() && n > 1;
    } else {
        model = new DefaultComboBoxModel<>(new String[] {
                Specification.DEFAULT_ROLE_ID });
    }
    box.setModel(model);
    box.setEnabled(enable);
}
 
开发者ID:FreeCol,项目名称:freecol,代码行数:31,代码来源:AbstractUnitOptionUI.java

示例9: equipForRole

import net.sf.freecol.common.model.Role; //导入依赖的package包/类
/**
 * Equip a unit for a specific role.
 * Currently the unit is either in Europe or in a settlement.
 * Might one day allow the unit to be on a tile co-located with
 * an equipment-bearing wagon.
 *
 * @param serverPlayer The {@code ServerPlayer} that owns the unit.
 * @param unit The {@code Unit} to equip.
 * @param role The {@code Role} to equip for.
 * @param roleCount The role count.
 * @return A {@code ChangeSet} encapsulating this action.
 */
public ChangeSet equipForRole(ServerPlayer serverPlayer, Unit unit,
                              Role role, int roleCount) {
    ChangeSet cs = new ChangeSet();
    boolean ret = false;
    if (unit.isInEurope()) {
        ServerEurope serverEurope = (ServerEurope)serverPlayer.getEurope();
        ret = serverEurope.csEquipForRole(unit, role, roleCount,
                                          random, cs);
    } else if (unit.getColony() != null) {
        ServerColony serverColony = (ServerColony)unit.getColony();
        ret = serverColony.csEquipForRole(unit, role, roleCount,
                                          random, cs);
    } else if (unit.getIndianSettlement() != null) {
        ServerIndianSettlement sis
            = (ServerIndianSettlement)unit.getIndianSettlement();
        ret = sis.csEquipForRole(unit, role, roleCount, random, cs);
    } else {
        return serverPlayer.clientError("Unsuitable equip location for: "
            + unit.getId());
    }
    if (!ret) return null;

    if (unit.getInitialMovesLeft() != unit.getMovesLeft()) {
        unit.setMovesLeft(0);
    }
    Unit carrier = unit.getCarrier();
    if (carrier != null
        && carrier.getInitialMovesLeft() != carrier.getMovesLeft()
        && carrier.getMovesLeft() != 0) {
        carrier.setMovesLeft(0);
    }
    return cs;
}
 
开发者ID:FreeCol,项目名称:freecol,代码行数:46,代码来源:InGameController.java

示例10: trainUnitInEurope

import net.sf.freecol.common.model.Role; //导入依赖的package包/类
/**
 * Train a unit in Europe.
 *
 * @param serverPlayer The {@code ServerPlayer} that is demanding.
 * @param type The {@code UnitType} to train.
 * @return A {@code ChangeSet} encapsulating this action.
 */
public ChangeSet trainUnitInEurope(ServerPlayer serverPlayer,
                                   UnitType type) {

    Europe europe = serverPlayer.getEurope();
    if (europe == null) {
        return serverPlayer.clientError("No Europe to train in.");
    }
    int price = europe.getUnitPrice(type);
    if (price <= 0) {
        return serverPlayer.clientError("Bogus price: " + price);
    } else if (!serverPlayer.checkGold(price)) {
        return serverPlayer.clientError("Not enough gold ("
            + serverPlayer.getGold() + " < " + price
            + ") to train " + type);
    }

    final Game game = getGame();
    final Specification spec = game.getSpecification();
    Role role = (spec.getBoolean(GameOptions.EQUIP_EUROPEAN_RECRUITS))
        ? type.getDefaultRole()
        : spec.getDefaultRole();
    Unit unit = new ServerUnit(game, europe, serverPlayer, type,
                               role);//-vis: safe, Europe
    unit.setName(serverPlayer.getNameForUnit(type, random));
    serverPlayer.modifyGold(-price);
    ((ServerEurope)europe).increasePrice(type, price);

    // Only visible in Europe
    ChangeSet cs = new ChangeSet();
    cs.addPartial(See.only(serverPlayer), serverPlayer,
        "gold", String.valueOf(serverPlayer.getGold()));
    cs.add(See.only(serverPlayer), europe);
    return cs;
}
 
开发者ID:FreeCol,项目名称:freecol,代码行数:42,代码来源:InGameController.java

示例11: invalidColonyReason

import net.sf.freecol.common.model.Role; //导入依赖的package包/类
/**
 * Why would a PioneeringMission be invalid with the given unit and colony.
 *
 * @param aiUnit The {@code AIUnit} to check.
 * @param colony The {@code Colony} to check.
 * @return A reason why the mission would be invalid, or null if
 *     none found.
 */
private static String invalidColonyReason(AIUnit aiUnit, Colony colony) {
    String reason = invalidTargetReason(colony, aiUnit.getUnit().getOwner());
    Role role = aiUnit.getUnit().getSpecification().getRole("model.role.pioneer");
    return (reason != null)
        ? reason
        : (!hasTools(aiUnit)
           && !colony.canProvideGoods(role.getRequiredGoodsList()))
        ? "colony-can-not-provide-equipment"
        : null;
}
 
开发者ID:FreeCol,项目名称:freecol,代码行数:19,代码来源:PioneeringMission.java

示例12: equipBraves

import net.sf.freecol.common.model.Role; //导入依赖的package包/类
/**
 * Greedily equips braves with horses and muskets.
 * Public for the test suite.
 *
 * @param is The {@code IndianSettlement} where the equipping occurs.
 * @param lb A {@code LogBuilder} to log to.
 */
public void equipBraves(IndianSettlement is, LogBuilder lb) {
    // Prioritize promoting partially equipped units to full dragoon
    final Comparator<Unit> comp = getGame().getCombatModel()
        .getMilitaryStrengthComparator();
    for (Unit u : sort(is.getAllUnitsList(), comp)) {
        Role r = is.canImproveUnitMilitaryRole(u);
        if (r != null) {
            Role old = u.getRole();
            if (getAIUnit(u).equipForRole(r) && u.getRole() != old) {
                lb.add(u, " upgraded from ", old.getSuffix(), ", ");
            }
        }
    }
}
 
开发者ID:FreeCol,项目名称:freecol,代码行数:22,代码来源:NativeAIPlayer.java

示例13: createUnits

import net.sf.freecol.common.model.Role; //导入依赖的package包/类
/**
 * Create units from a list of abstract units.  Only used by
 * Europeans at present.
 *
 * -vis: Visibility issues depending on location.
 * -til: Tile appearance issue if created in a colony (not ATM)
 *
 * @param abstractUnits The list of {@code AbstractUnit}s to
 *     create.
 * @param location The {@code Location} where the units will
 *     be created.
 * @return A list of units created.
 */
public List<Unit> createUnits(List<AbstractUnit> abstractUnits,
                              Location location) {
    final Game game = getGame();
    final Specification spec = game.getSpecification();
    List<Unit> units = new ArrayList<>();
    LogBuilder lb = new LogBuilder(32);
    lb.add("createUnits for ", this, " at ", location);
    for (AbstractUnit au : abstractUnits) {
        UnitType type = au.getType(spec);
        Role role = au.getRole(spec);
        if (!type.isAvailableTo(this)) {
            lb.add(" ignoring type-unavailable ", au);
            continue;
        }
        if (!role.isAvailableTo(this, type)) {
            lb.add(" ignoring role-unavailable ", au);
            continue;
        }
        for (int i = 0; i < au.getNumber(); i++) {
            ServerUnit su = new ServerUnit(game, location, this, type,
                                           role);//-vis(this)
            units.add(su);
            lb.add(' ', su);
        }
    }
    lb.log(logger, Level.FINEST);
    return units;
}
 
开发者ID:FreeCol,项目名称:freecol,代码行数:42,代码来源:ServerPlayer.java

示例14: csCaptureEquipment

import net.sf.freecol.common.model.Role; //导入依赖的package包/类
/**
 * Capture equipment.
 *
 * @param winner The {@code Unit} that is capturing equipment.
 * @param loser The {@code Unit} that is losing equipment.
 * @param role The {@code Role} wrest from the loser.
 * @param cs A {@code ChangeSet} to update.
 */
private void csCaptureEquipment(Unit winner, Unit loser,
                                Role role, ChangeSet cs) {
    ServerPlayer winnerPlayer = (ServerPlayer) winner.getOwner();
    ServerPlayer loserPlayer = (ServerPlayer) loser.getOwner();
    Role newRole = winner.canCaptureEquipment(role);
    if (newRole != null) {
        List<AbstractGoods> newGoods
            = winner.getGoodsDifference(newRole, 1);
        GoodsType goodsType = newGoods.get(0).getType(); // FIXME: generalize
        winner.changeRole(newRole, 1);

        // Currently can not capture equipment back so this only
        // makes sense for native players, and the message is
        // native specific.
        if (winnerPlayer.isIndian()) {
            StringTemplate winnerNation = winner.getApparentOwnerName();
            cs.addMessage(loserPlayer,
                          new ModelMessage(ModelMessage.MessageType.COMBAT_RESULT,
                                           "combat.equipmentCaptured",
                                           winnerPlayer)
                              .addStringTemplate("%nation%", winnerNation)
                              .addNamed("%equipment%", goodsType));

            // CHEAT: Immediately transferring the captured goods
            // back to a potentially remote settlement is pretty
            // dubious.  Apparently Col1 did it.  Better would be
            // to give the capturing unit a go-home-with-plunder mission.
            IndianSettlement is = winner.getHomeIndianSettlement();
            if (is != null) {
                for (AbstractGoods ag : newGoods) {
                    is.addGoods(ag);
                    winnerPlayer.logCheat("teleported " + ag
                        + " back to " + is.getName());
                }
                cs.add(See.only(winnerPlayer), is);
            }
        }
    }
}
 
开发者ID:FreeCol,项目名称:freecol,代码行数:48,代码来源:ServerPlayer.java

示例15: csLoseAutoEquip

import net.sf.freecol.common.model.Role; //导入依赖的package包/类
/**
 * Unit auto equips but loses equipment.
 *
 * @param attacker The {@code Unit} that attacked.
 * @param defender The {@code Unit} that defended and loses equipment.
 * @param cs A {@code ChangeSet} to update.
 */
private void csLoseAutoEquip(Unit attacker, Unit defender, ChangeSet cs) {
    ServerPlayer defenderPlayer = (ServerPlayer) defender.getOwner();
    StringTemplate defenderNation = defenderPlayer.getNationLabel();
    Settlement settlement = defender.getSettlement();
    Role role = defender.getAutomaticRole();
    StringTemplate defenderLabel = Messages.getUnitLabel(null,
        defender.getType().getId(), 1, defenderPlayer.getNation().getId(),
        role.getId(), null);
    ServerPlayer attackerPlayer = (ServerPlayer) attacker.getOwner();
    StringTemplate attackerNation = attacker.getApparentOwnerName();

    // Autoequipment is not actually with the unit, it is stored
    // in the settlement of the unit.  Remove it from there.
    for (AbstractGoods ag : role.getRequiredGoodsList()) {
        settlement.removeGoods(ag);
    }

    // No special message, attacker can not distinguish auto-armed
    // from actual-armed.
    cs.addMessage(attackerPlayer,
        new ModelMessage(ModelMessage.MessageType.COMBAT_RESULT,
                         "combat.unitDemotedToUnarmed.enemy", attacker)
            .addStringTemplate("%location%",
                 settlement.getLocationLabelFor(attackerPlayer))
            .addStringTemplate("%unit%", attacker.getLabel())
            .addStringTemplate("%oldName%", defenderLabel)
            .addStringTemplate("%enemyNation%", defenderNation)
            .addStringTemplate("%enemyUnit%", defender.getLabel()));
    cs.addMessage(defenderPlayer,
        new ModelMessage(ModelMessage.MessageType.COMBAT_RESULT,
                         "combat.unitLoseAutoEquip", defender)
            .addStringTemplate("%location%",
                settlement.getLocationLabelFor(defenderPlayer))
            .addStringTemplate("%unit%", defender.getLabel())
            .addStringTemplate("%enemyNation%", attackerNation)
            .addStringTemplate("%enemyUnit%", attacker.getLabel()));
}
 
开发者ID:FreeCol,项目名称:freecol,代码行数:45,代码来源:ServerPlayer.java


注:本文中的net.sf.freecol.common.model.Role类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。