Beispiel #1
0
 private int[] getDiceX() {
   int w = getWidth();
   int diceWidth = red_dice.getWidth();
   int ax = w / 2 - MoveDialog.distanceFromCenter - diceWidth / 2;
   int dx = w / 2 + MoveDialog.distanceFromCenter - diceWidth / 2;
   return new int[] {ax, dx};
 }
Beispiel #2
0
  public void drawDice(boolean isAttacker, int result, int dx, int dy, Graphics2D g) {
    g.translate(dx, dy);

    if (isAttacker) {
      g.drawSprite(red_dice, DICE_NORMAL, 0, 0);
    } else {
      g.drawSprite(blue_dice, DICE_NORMAL, 0, 0);
    }

    int w = red_dice.getWidth();

    int size = (int) (w / (29D / 3D) + 0.5);
    int close = (int) (w / (29D / 7D) + 0.5);
    int middle = (w - size) / 2;
    int far = w - close - size;

    g.setColor(0xC8FFFFFF);

    if (result == 0) {
      g.fillOval(middle, middle, size, size);
    } else if (result == 1) {
      g.fillOval(close, close, size, size);
      g.fillOval(far, far, size, size);
    } else if (result == 2) {
      g.fillOval(close, close, size, size);
      g.fillOval(middle, middle, size, size);
      g.fillOval(far, far, size, size);
    } else if (result == 3) {
      g.fillOval(close, close, size, size);
      g.fillOval(far, close, size, size);
      g.fillOval(far, far, size, size);
      g.fillOval(close, far, size, size);
    } else if (result == 4) {
      g.fillOval(close, close, size, size);
      g.fillOval(far, close, size, size);
      g.fillOval(far, far, size, size);
      g.fillOval(close, far, size, size);
      g.fillOval(middle, middle, size, size);
    } else if (result == 5) {
      g.fillOval(close, close, size, size);
      g.fillOval(far, close, size, size);
      g.fillOval(far, far, size, size);
      g.fillOval(close, far, size, size);
      g.fillOval(middle, close, size, size);
      g.fillOval(middle, far, size, size);
    }

    g.translate(-dx, -dy);
  }
  /**
   * Set up all of the data.
   *
   * <p>This is called from a separate init method in order to limit the amount of resource loading
   * that is done by the thread that called the startApp method.
   */
  public void init() throws Exception {
    // TODO: [Exercise 3][step 1] - initialization code. Please, don't forget to fix the missing
    // imports and fields
    mazeDesign = new MazeGameDesign(); // [Exercise3]
    jamesSprite = mazeDesign.getJamesS(); // [Exercise3]
    dukeSprite = mazeDesign.getDukeS(); // [Exercise3]
    dukeSprite.defineReferencePixel(dukeSprite.getWidth() / 2, 0); // [Exercise3]
    dukeSpriteAnimator = new SpriteAnimationTask(dukeSprite, false); // [Exercise3]
    myWalls = mazeDesign.getMaze1(); // [Exercise3]
    mazeDesign.updateLayerManagerForLevel1(this); // [Exercise3]

    timer = new Timer(); // [Exercise3]
    timer.scheduleAtFixedRate(dukeSpriteAnimator, 0, mazeDesign.dukeSseq001Delay); // [Exercise3]
    // this sets the view screen so that the player is
    // in the center.
    myViewWindowX = dukeSprite.getX() - ((DISP_HEIGHT - SQUARE_WIDTH) / 2);
    myViewWindowY = dukeSprite.getY() - ((DISP_HEIGHT - SQUARE_WIDTH) / 2);

    System.gc();
  }
Beispiel #4
0
  public int insideButton(int x, int y) {

    // <copy-pasta>
    int imageAreaHeight = getImageAreaHeight();
    int heightOfComponents =
        ((MoveDialog.DialogLayout) getContentPane().getLayout())
            .getHeightOfComponents(getContentPane());
    // this is the MIDDLE of the images area
    int xOffset = getContentPane().getWidth() / 2;
    int yOffset =
        (getContentPane().getHeight() - heightOfComponents) / 2
            + imageAreaHeight / 4
            + getContentPane().getY();

    int y1 = yOffset + imageAreaHeight / 4; // top of dice
    int y2 = y1 + red_dice.getHeight() + XULLoader.adjustSizeToDensity(2);
    int y3 = y2 + red_dice.getHeight() + XULLoader.adjustSizeToDensity(2);
    // </copy-pasta>

    int[] diceXs = getDiceX();
    int ax = diceXs[0], dx = diceXs[1];

    int W = red_dice.getWidth();
    int H = red_dice.getHeight();

    if (x >= ax && x < (ax + W) && y >= y1 && y < (y1 + H)) {
      return 1;
    } else if (x >= ax && x < (ax + W) && y >= y2 && y < (y2 + H)) {
      return 2;
    } else if (x >= ax && x < (ax + W) && y >= y3 && y < (y3 + H)) {
      return 3;
    } else if (x >= dx && x < (dx + W) && y >= y1 && y < (y1 + H)) {
      return 4;
    } else if (x >= dx && x < (dx + W) && y >= y2 && y < (y2 + H)) {
      return 5;
    } else if (x >= dx && x < (dx + W) && y >= y3 && y < (y3 + H)) {
      return 6;
    }
    return 0;
  }
Beispiel #5
0
 public int getSizeX() {
   return sprite.getWidth();
 }
Beispiel #6
0
  @Override
  public void paintComponent(Graphics2D g) {

    int csrc = myrisk.hasArmiesInt(c1num);
    int cdes = myrisk.hasArmiesInt(c2num);
    int color1 = myrisk.getColorOfOwner(c1num);
    int color2 = myrisk.getColorOfOwner(c2num);

    int imageAreaHeight = getImageAreaHeight();
    int heightOfComponents =
        ((MoveDialog.DialogLayout) getContentPane().getLayout())
            .getHeightOfComponents(getContentPane());
    // this is the MIDDLE of the images area
    int xOffset = getContentPane().getWidth() / 2;
    int yOffset =
        (getContentPane().getHeight() - heightOfComponents) / 2
            + imageAreaHeight / 4
            + getContentPane().getY();

    // g.setColor(0xFFFF0000);
    // g.drawRect( (getContentPane().getWidth()-imageAreaHeight)/2 ,
    // (getContentPane().getHeight()-heightOfComponents)/2 + getContentPane().getY(),
    // imageAreaHeight, imageAreaHeight);

    MoveDialog.paintMove(
        g,
        xOffset,
        yOffset,
        c1img,
        c2img,
        color1,
        color2,
        myrisk.getCountryCapital(c1num),
        myrisk.getCountryCapital(c2num),
        myrisk.getCountryName(c1num),
        myrisk.getCountryName(c2num),
        csrc,
        cdes,
        0);

    // #####################################################
    // ################## drawing DICE!!!!! ################

    int[] diceXs = getDiceX();
    int ax = diceXs[0], dx = diceXs[1];

    int y1 = yOffset + imageAreaHeight / 4; // top of dice
    int y2 = y1 + red_dice.getHeight() + XULLoader.adjustSizeToDensity(2);
    int y3 = y2 + red_dice.getHeight() + XULLoader.adjustSizeToDensity(2);

    // just in case in the middle of the draw the att and def get set to null
    int[] atti = att;
    int[] defi = def;

    // this is the max defend dice allowed for this battle
    int deadDice = myrisk.hasArmiesInt(c2num);
    if (deadDice > myrisk.getGame().getMaxDefendDice()) {
      deadDice = myrisk.getGame().getMaxDefendDice();
    }

    // if we need input
    if (max != 0) {
      // selecting the number of attacking dice
      if (canRetreat) {
        g.drawSprite(red_dice, DICE_NORMAL, ax, y1);

        if (noda > 1) {
          g.drawSprite(red_dice, DICE_NORMAL, ax, y2);
        } else if (max > 1) {
          g.drawSprite(red_dice, DICE_DARK, ax, y2);
        }

        if (noda > 2) {
          g.drawSprite(red_dice, DICE_NORMAL, ax, y3);
        } else if (max > 2) {
          g.drawSprite(red_dice, DICE_DARK, ax, y3);
        }

        // draw the dead dice
        g.drawSprite(blue_dice, DICE_DARK, dx, y1);
        if (deadDice > 1) {
          g.drawSprite(blue_dice, DICE_DARK, dx, y2);
        }
        if (deadDice > 2) {
          g.drawSprite(blue_dice, DICE_DARK, dx, y3);
        }
      }
      // selecting the number of dice to defend
      else {
        g.drawSprite(blue_dice, DICE_NORMAL, dx, y1);

        if (nodd > 1) {
          g.drawSprite(blue_dice, DICE_NORMAL, dx, y2);
        } else if (max > 1) {
          g.drawSprite(blue_dice, DICE_DARK, dx, y2);
        }

        if (nodd > 2) {
          g.drawSprite(blue_dice, DICE_NORMAL, dx, y3);
        } else if (max > 2) {
          g.drawSprite(blue_dice, DICE_DARK, dx, y3);
        }
      }
    }
    // battle open and waiting for the attacker to select there number of dice
    else if (atti == null && defi == null && !spinD) {

      // draw the dead dice
      g.drawSprite(blue_dice, DICE_DARK, dx, y1);
      if (deadDice > 1) {
        g.drawSprite(blue_dice, DICE_DARK, dx, y2);
      }
      if (deadDice > 2) {
        g.drawSprite(blue_dice, DICE_DARK, dx, y3);
      }

      if (!spinA) {
        // draw dead dice for attacker
        int AdeadDice = myrisk.hasArmiesInt(c1num) - 1;
        // we assume that the attacker can attack with max of 3 dice

        g.drawSprite(red_dice, DICE_DARK, ax, y1);
        if (AdeadDice > 1) {
          g.drawSprite(red_dice, DICE_DARK, ax, y2);
        }
        if (AdeadDice > 2) {
          g.drawSprite(red_dice, DICE_DARK, ax, y3);
        }
      }
    }

    // #####################################################
    // ##################### END DICE ######################

    final int SPINS_OFFSET = 3;

    if (spinA) {
      g.drawSprite(red_dice, SPINS_OFFSET + r.nextInt(6), ax, y1);
      if (noda > 1) {
        g.drawSprite(red_dice, SPINS_OFFSET + r.nextInt(6), ax, y2);
      }
      if (noda > 2) {
        g.drawSprite(red_dice, SPINS_OFFSET + r.nextInt(6), ax, y3);
      }
      // g.drawString("ROLLING ATTACKER " + noda +"    " + Math.random() , 50, 100);

      if (spinD) {
        g.drawSprite(blue_dice, SPINS_OFFSET + r.nextInt(6), dx, y1);
        if (nodd > 1) {
          g.drawSprite(blue_dice, SPINS_OFFSET + r.nextInt(6), dx, y2);
        }
        if (nodd > 2) {
          g.drawSprite(blue_dice, SPINS_OFFSET + r.nextInt(6), dx, y3);
        }
        // g.drawString("ROLLING DEFENDER " + nodd +"    " + Math.random(), 300, 100);
      }
    }

    DirectGraphics g2 = DirectUtils.getDirectGraphics(g.getGraphics());

    int offset = (int) (red_dice.getWidth() / (29D / 4D) + 0.5);
    int bottom = red_dice.getHeight() - offset - 1; // TODO not sure why -1??
    int halfDice = red_dice.getHeight() / 2;

    if (atti != null && defi != null) {
      {
        int yCoords[] = {y1 + offset, y1 + bottom, y1 + halfDice};
        if (defi[0] >= atti[0]) {
          int xCoords[] = {dx + offset, dx + offset, ax + bottom};
          g2.fillPolygon(xCoords, 0, yCoords, 0, xCoords.length, COLOR_BLUE);
        } else {
          int xCoords[] = {ax + bottom, ax + bottom, dx + offset};
          g2.fillPolygon(xCoords, 0, yCoords, 0, xCoords.length, COLOR_RED);
        }
      }
      if (atti.length > 1 && defi.length > 1) {
        int yCoords[] = {y2 + offset, y2 + bottom, y2 + halfDice};
        if (defi[1] >= atti[1]) {
          int xCoords[] = {dx + offset, dx + offset, ax + bottom};
          g2.fillPolygon(xCoords, 0, yCoords, 0, xCoords.length, COLOR_BLUE);
        } else {
          int xCoords[] = {ax + bottom, ax + bottom, dx + offset};
          g2.fillPolygon(xCoords, 0, yCoords, 0, xCoords.length, COLOR_RED);
        }
      }
      if (atti.length > 2 && defi.length > 2) {
        int yCoords[] = {y3 + offset, y3 + bottom, y3 + halfDice};
        if (defi[2] >= atti[2]) {
          int xCoords[] = {dx + offset, dx + offset, ax + bottom};
          g2.fillPolygon(xCoords, 0, yCoords, 0, xCoords.length, COLOR_BLUE);
        } else {
          int xCoords[] = {ax + bottom, ax + bottom, dx + offset};
          g2.fillPolygon(xCoords, 0, yCoords, 0, xCoords.length, COLOR_RED);
        }
      }

      // draw attacker dice
      drawDice(true, atti[0], ax, y1, g);
      if (atti.length > 1) {
        drawDice(true, atti[1], ax, y2, g);
      }
      if (atti.length > 2) {
        drawDice(true, atti[2], ax, y3, g);
      }

      // draw defender dice
      drawDice(false, defi[0], dx, y1, g);
      if (defi.length > 1) {
        drawDice(false, defi[1], dx, y2, g);
      }
      if (defi.length > 2) {
        drawDice(false, defi[2], dx, y3, g);
      }
    }
  }
Beispiel #7
0
 public boolean collidesWith(Sprite s, boolean pixelLevel) {
   java.awt.Rectangle r1 =
       new java.awt.Rectangle(this.getX(), this.getY(), getWidth(), getHeight());
   java.awt.Rectangle r2 = new java.awt.Rectangle(s.getX(), s.getY(), s.getWidth(), s.getHeight());
   return r1.intersects(r2);
 }