Пример #1
0
  /**
   * Requests the game to be started. This will only be successful if all players are ready to start
   * the game.
   */
  public void requestLaunch() {
    if (freeColClient.getGame().isAllPlayersReadyToLaunch()) {
      gui.showStatusPanel(Messages.message("status.startingGame"));
      freeColClient.askServer().requestLaunch();

    } else {
      gui.errorMessage("server.notAllReady");
    }
  }
Пример #2
0
  /** Starts the game. */
  public void startGame() {
    ResourceMapping gameMapping = new ResourceMapping();
    for (Player player : freeColClient.getGame().getPlayers()) {
      addPlayerResources(player.getNationID(), gameMapping);
    }
    // Unknown nation is not in getPlayers() list.
    addPlayerResources(Nation.UNKNOWN_NATION_ID, gameMapping);
    ResourceManager.addGameMapping(gameMapping);

    Player myPlayer = freeColClient.getMyPlayer();
    if (!freeColClient.isHeadless()) {
      gui.closeMainPanel();
      gui.closeMenus();
      gui.closeStatusPanel();
      gui.playSound(null); // Stop the long introduction sound
      gui.playSound("sound.intro." + myPlayer.getNationID());
    }
    freeColClient.askServer().registerMessageHandler(freeColClient.getInGameInputHandler());

    if (!freeColClient.isHeadless()) {
      freeColClient.setInGame(true);
      gui.setupInGameMenuBar();
    }

    InGameController igc = freeColClient.getInGameController();
    gui.setSelectedTile((Tile) myPlayer.getEntryLocation(), false);
    if (freeColClient.currentPlayerIsMyPlayer()) {
      igc.nextActiveUnit();
    }

    gui.setUpMouseListenersForCanvas();

    if (FreeColDebugger.isInDebugMode() && FreeColDebugger.getDebugRunTurns() > 0) {
      freeColClient.skipTurns(FreeColDebugger.getDebugRunTurns());
    } else if (freeColClient.getGame().getTurn().getNumber() == 1) {
      ModelMessage message =
          new ModelMessage(ModelMessage.MessageType.TUTORIAL, "tutorial.startGame", myPlayer);
      String direction = myPlayer.getNation().startsOnEastCoast() ? "west" : "east";
      message.add("%direction%", direction);
      myPlayer.addModelMessage(message);
      // force view of tutorial message
      igc.nextModelMessage();
    }
  }
Пример #3
0
  /**
   * The constructor that will add the items to this panel.
   *
   * @param freeColClient The <code>FreeColClient</code> for the game.
   * @param id The object identifier of the item to select.
   */
  public ColopediaPanel(FreeColClient freeColClient, String id) {
    super(
        freeColClient,
        new MigLayout("fill", "[200:]unrelated[550:, grow, fill]", "[][grow, fill][]"));

    header = GUI.getDefaultHeader(Messages.message("menuBar.colopedia"));
    add(header, "span, align center");

    listPanel = new MigPanel("ColopediaPanelUI");
    listPanel.setOpaque(true);
    JScrollPane sl =
        new JScrollPane(
            listPanel,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    sl.getVerticalScrollBar().setUnitIncrement(16);
    sl.getViewport().setOpaque(false);
    add(sl);

    detailPanel = new MigPanel("ColopediaPanelUI");
    detailPanel.setOpaque(true);
    JScrollPane detail =
        new JScrollPane(
            detailPanel,
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    detail.getVerticalScrollBar().setUnitIncrement(16);
    detail.getViewport().setOpaque(false);
    add(detail, "grow");

    add(okButton, "newline 20, span, tag ok");

    getGUI().restoreSavedSize(this, 850, 600);
    tree = buildTree();

    select(id);
  }
Пример #4
0
  private void update() {

    List<GoodsType> selectedTypes = new ArrayList<GoodsType>();

    reportPanel.removeAll();

    reportPanel.add(selectLabel, "span, split " + (NUMBER_OF_GOODS + 2));

    for (int index = 0; index < NUMBER_OF_GOODS; index++) {
      reportPanel.add(boxes[index]);
      int selectedIndex = boxes[index].getSelectedIndex();
      if (selectedIndex > 0) {
        selectedTypes.add(goodsTypes.get(selectedIndex - 1));
      }
    }

    reportPanel.add(selectButton, "wrap 20");

    if (!selectedTypes.isEmpty()) {

      TypeCountMap<BuildingType> buildingCount = new TypeCountMap<BuildingType>();
      List<List<BuildingType>> basicBuildingTypes = new ArrayList<List<BuildingType>>();
      for (GoodsType goodsType : selectedTypes) {
        List<BuildingType> buildingTypes = new ArrayList<BuildingType>();
        for (BuildingType buildingType : getSpecification().getBuildingTypeList()) {
          if (goodsType.equals(buildingType.getProducedGoodsType())
              || !buildingType.getModifierSet(goodsType.getId()).isEmpty()) {
            BuildingType firstLevel = buildingType.getFirstLevel();
            if (!buildingTypes.contains(firstLevel)) {
              buildingTypes.add(firstLevel);
            }
          }
        }
        basicBuildingTypes.add(buildingTypes);
      }

      JLabel newLabel;

      // labels
      newLabel = new JLabel(Messages.message("Colony"));
      newLabel.setBorder(FreeColPanel.TOPLEFTCELLBORDER);
      reportPanel.add(newLabel, "newline 20");

      for (int index = 0; index < selectedTypes.size(); index++) {
        newLabel = localizedLabel(selectedTypes.get(index).getNameKey());
        newLabel.setBorder(FreeColPanel.TOPCELLBORDER);
        reportPanel.add(newLabel);

        for (BuildingType buildingType : basicBuildingTypes.get(index)) {
          newLabel = localizedLabel(buildingType.getNameKey());
          newLabel.setBorder(FreeColPanel.TOPCELLBORDER);
          reportPanel.add(newLabel);
        }
      }

      int[] totalProduction = new int[selectedTypes.size()];
      for (Colony colony : getFreeColClient().getMySortedColonies()) {
        // colonyButton
        JButton colonyButton = GUI.getLinkButton(colony.getName(), null, colony.getId());
        colonyButton.setBorder(FreeColPanel.LEFTCELLBORDER);
        colonyButton.addActionListener(this);
        reportPanel.add(colonyButton, "newline");

        // production
        for (int index = 0; index < selectedTypes.size(); index++) {
          GoodsType goodsType = selectedTypes.get(index);
          int newValue = colony.getNetProductionOf(goodsType);
          totalProduction[index] += newValue;
          Goods goods = new Goods(colony.getGame(), colony, goodsType, newValue);
          GoodsLabel goodsLabel = new GoodsLabel(goods, getGUI());
          goodsLabel.setHorizontalAlignment(JLabel.LEADING);
          goodsLabel.setBorder(FreeColPanel.CELLBORDER);
          reportPanel.add(goodsLabel);

          for (BuildingType buildingType : basicBuildingTypes.get(index)) {
            Building building = colony.getBuilding(buildingType);
            if (building == null) {
              newLabel = new JLabel();
              newLabel.setBorder(FreeColPanel.CELLBORDER);
              reportPanel.add(newLabel);
            } else {
              buildingCount.incrementCount(building.getType(), 1);
              BuildingPanel buildingPanel = new BuildingPanel(getFreeColClient(), building);
              buildingPanel.setBorder(FreeColPanel.CELLBORDER);
              buildingPanel.initialize();
              reportPanel.add(buildingPanel);
            }
          }
        }
      }
    }
    revalidate();
    repaint();
  }
Пример #5
0
 /** Displays an input dialog box where the user should specify a goods transfer amount. */
 private int getAmount(GoodsType goodsType, int available, int defaultAmount, boolean needToPay) {
   return gui.showSelectAmountDialog(goodsType, available, defaultAmount, needToPay);
 }
Пример #6
0
  /**
   * Imports the data represented by the given Transferable into the given component. Returns 'true'
   * on success, 'false' otherwise.
   *
   * @param comp The component to import the data to.
   * @param t The Transferable that holds the data.
   * @return 'true' on success, 'false' otherwise.
   */
  public boolean importData(JComponent comp, Transferable t) {
    try {
      JLabel data;

      /*
        This variable is used to temporarily keep the old selected unit,
        while moving cargo from one carrier to another:
      */
      UnitLabel oldSelectedUnit = null;

      // Check flavor.
      if (t.isDataFlavorSupported(DefaultTransferHandler.flavor)) {
        data = (JLabel) t.getTransferData(DefaultTransferHandler.flavor);
      } else {
        logger.warning("Data flavor is not supported!");
        return false;
      }

      // Do not allow a transferable to be dropped upon itself:
      if (comp == data) {
        return false;
      }

      // Make sure we don't drop onto other Labels.
      if (comp instanceof UnitLabel) {
        UnitLabel unitLabel = (UnitLabel) comp;
        /**
         * If the unit/cargo is dropped on a carrier in port (EuropePanel.InPortPanel), then the
         * ship is selected and the unit is added to its cargo. If the unit is not a carrier, but
         * can be equipped, and the goods can be converted to equipment, equip the unit.
         *
         * <p>If not, assume that the user wished to drop the unit/cargo on the panel below.
         */
        if (unitLabel.getUnit().isCarrier()
            && unitLabel.getParent() instanceof InPortPanel
            && parentPanel instanceof PortPanel) {
          PortPanel portPanel = (PortPanel) parentPanel;
          if (data instanceof Draggable && ((Draggable) data).isOnCarrier()) {
            oldSelectedUnit = portPanel.getSelectedUnitLabel();
          }
          portPanel.setSelectedUnitLabel(unitLabel);
          comp = portPanel.getCargoPanel();
        } else if (unitLabel.canUnitBeEquipedWith(data)) {
          // don't do anything before partial amount has been checked
        } else {
          comp = getDropTarget(comp);
        }
      } else if (comp instanceof AbstractGoodsLabel) {
        comp = getDropTarget(comp);
      }

      // t is already in comp:
      if (data.getParent() == comp) {
        return false;
      }

      if (data instanceof UnitLabel) {

        // Check if the unit can be dragged to comp.

        Unit unit = ((UnitLabel) data).getUnit();
        if (comp instanceof DropTarget) {
          DropTarget target = (DropTarget) comp;
          if (!target.accepts(unit)) {
            return false;
          }

          if ((comp instanceof ColonyPanel.OutsideColonyPanel
                  || comp instanceof ColonyPanel.ColonyCargoPanel)
              && !gui.tryLeaveColony(unit)) {
            return false;
          }
          target.add(data, true);

          // Update unit selection

          // new unit selection has already been taken care
          // of if this unit was moved to ToAmericaPanel
          restoreSelection(oldSelectedUnit);
          comp.revalidate();

          return true;
        } else {
          return false;
        }
      } else if (data instanceof GoodsLabel) {
        // Check if the goods can be dragged to comp.
        GoodsLabel label = (GoodsLabel) data;
        Goods goods = label.getGoods();

        // Import the data.
        if (label.isPartialChosen()) {
          int defaultAmount = -1;
          if (goods.getLocation() instanceof GoodsLocation) {
            GoodsLocation loc = (GoodsLocation) goods.getLocation();
            if (goods.getAmount() > loc.getGoodsCapacity()) {
              // If over capacity, favour the amount that would
              // correct the problem.
              defaultAmount =
                  Math.min(goods.getAmount() - loc.getGoodsCapacity(), GoodsContainer.CARGO_SIZE);
            }
          }
          int amount = getAmount(goods.getType(), goods.getAmount(), defaultAmount, false);
          if (amount <= 0) return false;
          goods.setAmount(amount);
        } else if (goods.getAmount() > GoodsContainer.CARGO_SIZE) {
          goods.setAmount(GoodsContainer.CARGO_SIZE);
        }

        if (comp instanceof UnitLabel) {
          return equipUnitIfPossible((UnitLabel) comp, goods);
        } else if (comp instanceof JLabel) {
          logger.warning("Oops, I thought we didn't have to write this part.");
          return true;
        } else if (comp instanceof DropTarget) {
          DropTarget target = (DropTarget) comp;
          if (target.accepts(goods)) {
            target.add(data, true);
          } else {
            return false;
          }

          restoreSelection(oldSelectedUnit);

          comp.revalidate();

          return true;
        }
      } else if (data instanceof MarketLabel) {

        // Check if the unit can be dragged to comp.

        MarketLabel label = ((MarketLabel) data);

        // Import the data.

        if (label.isPartialChosen()) {
          int amount = getAmount(label.getType(), label.getAmount(), -1, true);
          if (amount <= 0) {
            return false;
          }
          label.setAmount(amount);
        }

        if (comp instanceof UnitLabel) {
          return equipUnitIfPossible((UnitLabel) comp, label.getGoods());
        } else if (comp instanceof JLabel) {
          logger.warning("Oops, I thought we didn't have to write this part.");
          return true;
        } else if (comp instanceof JPanel) {
          if (comp instanceof CargoPanel) {
            ((CargoPanel) comp).add(data, true);
          } else {
            logger.warning("The receiving component is of an invalid type.");
            return false;
          }

          comp.revalidate();
          return true;
        }
      }

      logger.warning("The dragged component is of an invalid type.");

    } catch (Exception e) { // TODO: Suggest a reconnect.
      logger.log(Level.WARNING, "Import data fail", e);
    }
    return false;
  }