Пример #1
0
 private void callObjectAtLocation(int x, int y) {
   for (Hex o : _hexes) {
     if (Math.sqrt((o.getX() - x) * (o.getX() - x) + (o.getY() - y) * (o.getY() - y))
         < o.getRadius()) {
       o.clicked(x, y);
       return;
     }
   }
 }
Пример #2
0
 /**
  * Place rebels, fill hexes starting from closest available hex. Return true if all rebels placed,
  * false if we run out of planet hexes before all rebels are placed.
  *
  * @param rebels list of units to place
  * @return true iff all rebels placed
  */
 private boolean placeRebels(LinkedList<Unit> rebels, Util.FindHexesAround.Hextype type) {
   Hex center = null;
   Hex target = null;
   Hex prev_target = null;
   Util.FindHexesAround hex_finder = null;
   Point p = new Point(C.NEUTRAL, C.NEUTRAL);
   int p_idx = -1;
   int x = -1;
   int y = -1;
   while (!rebels.isEmpty()) {
     Unit unit = rebels.pop();
     if (unit.y != y || unit.x != x || unit.p_idx != p_idx) {
       y = unit.y;
       x = unit.x;
       p_idx = unit.p_idx;
       addMessage(
           new Message(
               "Rebellion on " + game.getPlanet(p_idx).name + " " + x + "," + y + "!",
               C.Msg.REBELLION,
               game.getYear(),
               game.getPlanet(p_idx)));
     }
     Hex hex_tmp = game.getHexFromPXY(unit.p_idx, unit.x, unit.y);
     if (!hex_tmp.equals(center)) {
       center = hex_tmp;
       hex_finder =
           new Util.FindHexesAround(center, C.NEUTRAL, type, game.getPlanet(p_idx).tile_set_type);
       prev_target = target;
       target = hex_finder.next();
     }
     while (target != null && Util.stackSize(target.getStack()) >= C.STACK_SIZE) {
       target = hex_finder.next();
     }
     prev_target = spot(prev_target, target);
     if (target == null) {
       return false;
     }
     if (unit.carrier == null) {
       if (unit.type_data.cargo > 0 && unit.cargo_list.size() > 0) {
         List<Unit> tmp = new LinkedList<>();
         tmp.addAll(unit.cargo_list);
         for (Unit u : tmp) {
           unit.disembark(u);
           center.addUnit(u);
         }
       }
     } else {
       Unit u = unit.carrier;
       u.disembark(unit);
     }
     center.getStack().remove(unit);
     game.changeOwnerOfUnit(p, unit);
     game.relocateUnit(false, unit.p_idx, target.getX(), target.getY(), unit);
     target.addUnit(unit);
     game.getUnits().add(unit);
     // Util.dP(" hex " + target.getX() + "," + target.getY());
   }
   spot(target, prev_target);
   return true;
 }
Пример #3
0
  public MapPanel(ClientGameBoard gl) {
    super();

    _dismiss = false;
    gameLogic = gl;
    gameLogic._mapPanel = this;
    _hexes = new ArrayList<Hex>();
    vertexContents = new Hashtable<CoordPair, Pair>();
    roadContents = new Hashtable<Pair, Integer>();
    portContents = new Hashtable<Pair, BoardObject.type>();

    diceImage = new BufferedImage(582, 98, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = diceImage.createGraphics();
    g.drawImage(BoardObject.images.get(BoardObject.type.DICE), null, null);
    g.dispose();

    rings = gameLogic.getNumRings();

    hexleft =
        100
            - (int)
                (radius
                    + (Math.floor(rings / 2) * radius + Math.floor((rings - 1) / 2) * radius * 2));
    if (rings % 2 == 0) {
      hexleft -= radius / 2;
    }

    hextop = 300 - (int) (radius * 0.866 + (rings - 1) * 2 * (radius * 0.866));

    double border = 0.4;

    HashMap<Pair, Pair> hexData = gameLogic.getHexInfo(); // call the gamelogic

    Pair currCoord = gameLogic.getStartPoint();

    Pair topCoord = currCoord;

    int ring = 0;

    int currentDir = 5;
    int current = 0;
    int[][] directions = {{1, 1}, {0, 2}, {-1, 1}, {-1, -1}, {0, -2}, {1, -1}};

    int[][] HexCoordDirections = {{2, 1}, {0, 2}, {-2, 1}, {-2, -1}, {0, -2}, {2, -1}};

    Hex top =
        new Hex(
            100,
            300,
            radius,
            (BoardObject.type) (hexData.get(currCoord).getA()),
            (Integer) (hexData.get(currCoord).getB()));
    Hex curr = top;

    _hexes.add(top);
    while (true) {
      if (current == ring) {
        currentDir++;
        current = 0;
      }
      if (currentDir > 5) {
        currentDir = 0;
        current = 0;
        ring++;
        if (ring < rings) {
          topCoord = new Pair(currCoord.getA(), (Double) (currCoord.getB()) - 2);
          currCoord = topCoord;

          top =
              new Hex(
                  curr.getX(),
                  (curr.getY() - 2 * (Math.cos(Math.PI / 6) * (curr.getRadius() + border))),
                  curr.getRadius(),
                  (BoardObject.type) (hexData.get(currCoord).getA()),
                  (Integer) (hexData.get(currCoord).getB()));
          curr = top;

        } else {
          break;
        }
      }
      currCoord.setA((Object) ((Double) (currCoord.getA()) + HexCoordDirections[currentDir][0]));
      currCoord.setB((Object) ((Double) (currCoord.getB()) + HexCoordDirections[currentDir][1]));

      curr =
          new Hex(
              (curr.getX() + directions[currentDir][0] * (curr.getRadius() + border) * 3 / 2),
              (curr.getY()
                  + directions[currentDir][1]
                      * (Math.cos(Math.PI / 6) * (curr.getRadius() + border))),
              curr.getRadius(),
              (BoardObject.type) (hexData.get(currCoord).getA()),
              (Integer) (hexData.get(currCoord).getB()));
      _hexes.add(curr);

      current++;
    }

    addMouseListener(this);
    addMouseMotionListener(this);
  }