Ejemplo n.º 1
0
  @Override
  public void mouseDragged(MouseEvent e) {

    if (_mousedown != null && _up == null) {
      _display_offset[0] += e.getX() - _mousedown[0];
      _display_offset[1] += e.getY() - _mousedown[1];
      _mousedown = new int[] {e.getX(), e.getY()};
    }
    if (_up != null) {
      if (_up.getType() == BoardObject.type.ROAD) {
        if (((Road) _up).oneDown == false) {
          _up.setX(e.getX());
          _up.setY(e.getY());
        } else {
          ((Road) _up).setX2(e.getX());
          ((Road) _up).setY2(e.getY());
        }
      } else {
        _up.setX(e.getX() - _up.getW() / 2);
        _up.setY(e.getY() - _up.getH() / 2);
      }
    }

    repaint();
  }
Ejemplo n.º 2
0
  /** A frissítő függvény, amely meghívja az összes autóra a frissítést, így léptetve a játékot. */
  public void Update() {
    int i = 0;

    // Lámpák frissítése
    for (i = 0; i < lamps.size(); i++) {
      ((Lamp) lamps.get(i)).Update();
    }

    for (i = 0; i < cars.size(); i++) {
      if (!(((Car) cars.get(i)).Update())) {
        removeActualCar(i);
        --i; // Ez azért kell, mert csökken az utána jövők indexe.
      }
    }

    // Pickupok frissítése
    for (i = 0; i < pickups.size(); i++) {
      if (!(((Pickup) pickups.get(i)).Update())) {
        Road road;
        if ((road = pickups.get(i).getRoadUnderMe()) != null) road.setPickup(null);
        pickups.remove(i);
        --i; // Ez azért kell, mert csökken az utána jövők indexe.
        // Autóval valami?
      }
    }

    // Civilek beengedése 10% hogy belép 1 ha van hely //
    populateRoad(roadStart);
    pickUpGen();
  }
Ejemplo n.º 3
0
 /** (Re)Calculates the width of this junction */
 public void calculateWidth() {
   Road road;
   width = 4;
   for (int i = 0; i < 4; i++) {
     road = allRoads[i];
     if (road != null && road.getWidth() > width) width = road.getWidth();
   }
 }
Ejemplo n.º 4
0
  @Override
  public void mouseReleased(MouseEvent e) {

    if (_up != null) {
      int[] pos = setUpNearest(e);

      if (_up.getType() == BoardObject.type.SETTLEMENT)
        gameLogic.writeBuildSettlement(pos[0], pos[1]);
      else if (_up.getType() == BoardObject.type.CITY) gameLogic.writeBuildCity(pos[0], pos[1]);
      else if ((_up.getType() == BoardObject.type.ROAD) && (((Road) _up).oneDown == true)) {

        gameLogic.writeBuildRoad(((Road) _up).mycoord[0], ((Road) _up).mycoord[1], pos[0], pos[1]);
      }

      if (_up.getType() == BoardObject.type.ROAD) {
        if (((Road) _up).oneDown == false) {

          _up.setX(
              hexleft
                  + ((pos[0] - (pos[0] % 2)) / 2 * intervalSide[0]
                      + (pos[0] - (pos[0] % 2)) / 2 * intervalSide[1]
                      + (pos[0] % 2) * intervalSide[0])
                  + _display_offset[0]);
          _up.setY(hextop + pos[1] * intervalUp + _display_offset[1]);

          ((Road) _up).oneDown = true;
          ((Road) _up).mycoord = pos;

          _mousedown = null;
          e.consume();
        } else {
          ((Road) _up)
              .setX2(
                  hexleft
                      + ((pos[0] - (pos[0] % 2)) / 2 * intervalSide[0]
                          + (pos[0] - (pos[0] % 2)) / 2 * intervalSide[1]
                          + (pos[0] % 2) * intervalSide[0]));
          ((Road) _up).setY2(hextop + pos[1] * intervalUp);
          ((Road) _up).oneDown = true;
          _up = null;
          _mousedown = null;
        }
      } else {
        _up.setX(
            hexleft
                + ((pos[0] - (pos[0] % 2)) / 2 * intervalSide[0]
                    + (pos[0] - (pos[0] % 2)) / 2 * intervalSide[1]
                    + (pos[0] % 2) * intervalSide[0])
                - _up.getW() / 2);
        _up.setY(hextop + pos[1] * intervalUp - _up.getH() / 2);
        _up = null;
        _mousedown = null;
      }
    }
    repaint();
  }
Ejemplo n.º 5
0
  public void Draw() {
    int i;

    for (i = 0; i < roads.size(); i++) {
      Road r = roads.get(i);
      r.Draw();

      if (r.hasBuilding() != null) r.hasBuilding().Draw();
      if (r.hasPickup() != null) r.hasPickup().Draw();
      if (r.hasCar() != null) r.hasCar().Draw();
      if (r.hasTrafficController() != null) r.hasTrafficController().Draw();
    }
  }
Ejemplo n.º 6
0
 public void reset() {
   Road road = edgenode.getRoad();
   if (road != null) {
     roadLink.setText(road.getName());
     roadLink.setEnabled(true);
     nodeLink.setText(road.getOtherNode(edgenode).getName());
     nodeLink.setEnabled(true);
   } else {
     roadLink.setText("null");
     roadLink.setEnabled(false);
     nodeLink.setText("null");
     nodeLink.setEnabled(false);
   }
 }
Ejemplo n.º 7
0
  /**
   * Az úthoz kapcsolódó elemek (autó, épület) elhelyezése rá - inicalizáláskor.
   *
   * @param road Az út amelyet módosítunk.
   */
  public void populateRoad(Road road) {
    try {
      if (road.hasCar() != null) return;
      if (cars.size() < maxCarsOnMap) {
        if (Math.random() < 0.1) {
          Civil c =
              new Civil((int) (Math.random() % (MinCivilSpeed - MaxCivilSpeed)) + MaxCivilSpeed);
          road.setCar(c);
        }
      }
    } catch (Exception ex) {

    }
  }
Ejemplo n.º 8
0
 /* clockwise order guaranteed */
 public Drivelane[] getAllLanes() throws InfraException {
   int pointer = 0;
   Drivelane[] lanes = new Drivelane[getNumAllLanes()];
   Drivelane[] temp;
   Road road;
   for (int i = 0; i < allRoads.length; i++) {
     road = allRoads[i];
     if (road != null) {
       temp = road.getInboundLanes(this);
       System.arraycopy(temp, 0, lanes, pointer, temp.length);
       pointer += temp.length;
       temp = road.getOutboundLanes(this);
       System.arraycopy(temp, 0, lanes, pointer, temp.length);
       pointer += temp.length;
     }
   }
   return lanes;
 }
Ejemplo n.º 9
0
 public void addRoad(Road r, int pos) throws InfraException {
   if (r == null) throw new InfraException("Parameter r is null");
   if (pos > 3 || pos < 0) throw new InfraException("Position out of range");
   if (allRoads[pos] != null)
     throw new InfraException("Road already connected to position " + pos);
   allRoads[pos] = r;
   Node other = r.getOtherNode(this);
   if (other == null || !other.isAlphaRoad(r))
     alphaRoads = (Road[]) Arrayutils.addElement(alphaRoads, r);
   updateLanes();
   calculateWidth();
 }
Ejemplo n.º 10
0
 public void loadSecondStage(Dictionary dictionaries)
     throws XMLInvalidInputException, XMLTreeException {
   super.loadSecondStage(dictionaries);
   // Load roads
   Dictionary roadDictionary = (Dictionary) (dictionaries.get("road"));
   allRoads = new Road[loadData.roads.length];
   for (int t = 0; t < loadData.roads.length; t++) {
     allRoads[t] = (Road) (roadDictionary.get(new Integer(loadData.roads[t])));
     if (allRoads[t] == null && loadData.roads[t] != -1)
       System.out.println("Warning : " + getName() + " could not find road " + loadData.roads[t]);
   }
   // Load normal signs
   Dictionary laneDictionary = (Dictionary) (dictionaries.get("lane"));
   signs = new Sign[loadData.signs.length];
   for (int t = 0; t < loadData.signs.length; t++)
     signs[t] = getSign(laneDictionary, loadData.signs[t]);
   // Load Signconfigurations
   signconfigs = new Sign[loadData.signconfigs.length][2];
   for (int t = 0; t < signconfigs.length; t++) {
     signconfigs[t] = new Sign[loadData.signconfigs[t].length];
     for (int u = 0; u < signconfigs[t].length; u++) {
       signconfigs[t][u] = getSign(laneDictionary, loadData.signconfigs[t][u]);
     }
   }
   // Tell *all* roads to load themselves
   // It's possible that this Node has a BetaLane that has not been SecondStageLoaded
   // And so we cant do an UpdateLanes() as that one needs secondStageData to proceed.
   // Hence, we need to 2ndStage all Roads.
   Enumeration e = new ArrayEnumeration(allRoads);
   Road tmpRoad;
   while (e.hasMoreElements()) {
     tmpRoad = (Road) e.nextElement();
     if (tmpRoad != null) tmpRoad.loadSecondStage(dictionaries);
   }
   try { // System.out.println("Trying to updateLanes()");
     updateLanes();
   } catch (InfraException x) {
     throw new XMLInvalidInputException("Cannot initialize lanes of node " + nodeId);
   }
 }
Ejemplo n.º 11
0
  /**
   * ShowMap indítja el ezt a rekurzív fv-t ami bejárja az utakat és kitölti az X,Y koordinátájuk
   *
   * @param current Az aktuális, amelynek beállítjuk pozícióját
   * @param prev Az út, ami mellé pozícionálunk
   * @param direction Az irány, amiben csatlakozunk az előző úthoz
   */
  public void ShowMapSetRoad(Road current, Road prev, int direction) {
    if (current.Iterated) return; // Már bejártuk ezt az utat

    AvailableRoads ar = current.getNextRoads();
    // Pozícionáld ezt az új utat (prev)//
    switch (direction) {
      case 0: // A prev út tőlünk jobbra van//
        current.X = prev.X - 1;
        current.Y = prev.Y;
        break;
      case 1: // A prev út alattunk van //
        current.X = prev.X;
        current.Y = prev.Y - 1;
        break;
      case 2: // A prev út tőlünk balra van //
        current.X = prev.X + 1;
        current.Y = prev.Y;
        break;
      case 3: // A prev út felettünk van //
        current.X = prev.X;
        current.Y = prev.Y + 1;
        break;
    }

    current.Iterated = true;

    if (ar.roads[0] != null) {
      ShowMapSetRoad(ar.roads[0], current, 0);
    }
    if (ar.roads[1] != null) {
      ShowMapSetRoad(ar.roads[1], current, 1);
    }
    if (ar.roads[2] != null) {
      ShowMapSetRoad(ar.roads[2], current, 2);
    }
    if (ar.roads[3] != null) {
      ShowMapSetRoad(ar.roads[3], current, 3);
    }
  }
Ejemplo n.º 12
0
  public void CreateMap() {
    if (roadStart.Iterated == false) {
      roadStart.X = roadStart.Y = 0;
      roadStart.Iterated = true;
    }

    AvailableRoads ar = roadStart.getNextRoads();
    if (ar.roads[0] != null) {
      ShowMapSetRoad(ar.roads[0], roadStart, 0);
    }
    if (ar.roads[1] != null) {
      ShowMapSetRoad(ar.roads[1], roadStart, 1);
    }
    if (ar.roads[2] != null) {
      ShowMapSetRoad(ar.roads[2], roadStart, 2);
    }
    if (ar.roads[3] != null) {
      ShowMapSetRoad(ar.roads[3], roadStart, 3);
    }

    // Ezen a ponton minden útnak már be van állítva az X és Y koordinátája //

    int MinX = 0, MinY = 0;
    MaxX = 0;
    MaxY = 0;

    ListIterator<Road> i = roads.listIterator();

    Road current;
    while (i.hasNext()) {
      current = i.next();
      if (current.X < MinX) MinX = current.X;
      if (current.Y < MinY) MinY = current.Y;

      if (current.X > MaxX) MaxX = current.X;
      if (current.Y > MaxY) MaxY = current.Y;
    }

    while (i.hasPrevious()) {
      current = i.previous();
      current.X -= MinX;
      current.Y -= MinY;
    }

    MaxX = (MaxX - MinX) + 1;
    MaxY = (MaxY - MinY) + 1;
  }
Ejemplo n.º 13
0
  public synchronized void paint(Graphics graphics) {

    Graphics2D g = (Graphics2D) graphics;

    Image water = Toolkit.getDefaultToolkit().getImage("catanui/water.jpg");
    g.drawImage(water, 0, 0, this);

    for (Hex o : _hexes) {
      o.paint(g, _display_offset[0], _display_offset[1]);
    }

    g.translate(_display_offset[0] + 2, _display_offset[1] - 1);
    synchronized (portContents) {
      for (Pair c : portContents.keySet()) {

        int lowx =
            hexleft
                + (((CoordPair) c.getA()).getX() - (((CoordPair) c.getA()).getX() % 2))
                    / 2
                    * intervalSide[0]
                + (((CoordPair) c.getA()).getX() - (((CoordPair) c.getA()).getX() % 2))
                    / 2
                    * intervalSide[1]
                + (((CoordPair) c.getA()).getX() % 2) * intervalSide[0];
        int lowy = hextop + ((CoordPair) c.getA()).getY() * intervalUp;
        int highx =
            hexleft
                + (((CoordPair) c.getB()).getX() - (((CoordPair) c.getB()).getX() % 2))
                    / 2
                    * intervalSide[0]
                + (((CoordPair) c.getB()).getX() - (((CoordPair) c.getB()).getX() % 2))
                    / 2
                    * intervalSide[1]
                + (((CoordPair) c.getB()).getX() % 2) * intervalSide[0];
        int highy = hextop + ((CoordPair) c.getB()).getY() * intervalUp;

        int dx = highx - lowx;
        int dy = highy - lowy;
        double rad = Math.atan((1.0) * dy / dx);

        if (dx < 0) rad += Math.PI;

        g.translate(lowx, lowy);
        g.rotate(rad);
        g.drawImage(
            BoardObject.images.get(BoardObject.type2port.get(portContents.get(c))), 0, -75, null);
        g.rotate(-rad);
        g.translate((-1) * lowx, (-1) * lowy);
      }
    }
    g.translate((-1) * _display_offset[0], (-1) * _display_offset[1]);

    synchronized (roadContents) {
      for (Pair c : roadContents.keySet()) {

        Road r =
            new Road(
                hexleft
                    + (((CoordPair) c.getA()).getX() - (((CoordPair) c.getA()).getX() % 2))
                        / 2
                        * intervalSide[0]
                    + (((CoordPair) c.getA()).getX() - (((CoordPair) c.getA()).getX() % 2))
                        / 2
                        * intervalSide[1]
                    + (((CoordPair) c.getA()).getX() % 2) * intervalSide[0],
                hextop + ((CoordPair) c.getA()).getY() * intervalUp);

        r.setX2(
            hexleft
                + (((CoordPair) c.getB()).getX() - (((CoordPair) c.getB()).getX() % 2))
                    / 2
                    * intervalSide[0]
                + (((CoordPair) c.getB()).getX() - (((CoordPair) c.getB()).getX() % 2))
                    / 2
                    * intervalSide[1]
                + (((CoordPair) c.getB()).getX() % 2) * intervalSide[0]);
        r.setY2(hextop + ((CoordPair) c.getB()).getY() * intervalUp);

        r.setColor(roadContents.get(c));
        r.paint(g, _display_offset[0], _display_offset[1]);
      }
    }

    synchronized (vertexContents) {
      for (CoordPair c : vertexContents.keySet()) {
        int newx =
            hexleft
                + ((c._x - (c._x % 2)) / 2 * intervalSide[0]
                    + (c._x - (c._x % 2)) / 2 * intervalSide[1]
                    + (c._x % 2) * intervalSide[0])
                - 20;
        int newy = hextop + c._y * intervalUp - 20;

        if ((BoardObject.type) (vertexContents.get(c).getA()) == BoardObject.type.SETTLEMENT) {
          Settlement s = new Settlement(newx, newy, (Integer) (vertexContents.get(c).getB()));
          s.paint(g, _display_offset[0], _display_offset[1]);
        } else if ((BoardObject.type) (vertexContents.get(c).getA()) == BoardObject.type.CITY) {
          City s = new City(newx, newy, (Integer) (vertexContents.get(c).getB()));
          s.paint(g, _display_offset[0], _display_offset[1]);
        } else System.out.println("neither -_-");
      }
    }

    g.setColor(Color.GRAY);
    g.fill(new Rectangle(0, 0, 110, 60));
    g.setColor(Color.LIGHT_GRAY);
    g.fill(new Rectangle(3, 3, 104, 56));
    if (_dieRoll > 0) {
      BufferedImage r1img =
          diceImage.getSubimage((int) (Math.floor((twoDice[0] - 1) * 94.7)), 0, 94, 93);
      g.drawImage(r1img, 5, 7, 48, 47, null);
      BufferedImage r2img =
          diceImage.getSubimage((int) (Math.floor((twoDice[1] - 1) * 94.7)), 0, 94, 93);
      g.drawImage(r2img, 55, 7, 48, 47, null);
    }

    if (_up != null) _up.paint(g);

    if (!_gameOver.equals("") && !_dismiss) {
      _currAlpha += 0.007;
    }
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, _currAlpha));
    g.setColor(Color.GRAY);
    g.fill(new Rectangle(-20, 0, 1020, 650));
    g.setColor(Color.BLACK);
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) 1.0));
    if (!_gameOver.equals("")) {

      if (_currAlpha >= 0.8) {
        if (_gameOver.equals(gameLogic._name)) {
          g.drawString("Congratulations, you won!", 350, 200);
        } else {
          g.drawString(_gameOver + " has won!", 350, 200);
        }
        _dismiss = true;
      } else repaint();
    }
  }