Пример #1
0
  public void simulateBall() {
    ballPreviousX = ballX;
    ballPreviousY = ballY;
    leftEdgeActive = rightEdgeActive = topEdgeActive = bottomEdgeActive = true;

    if (ballY >= MaxY) {
      ballStarted = false;
    }

    if (!ballStarted) {
      ballX = padx - ballDiameter / 2;
      ballY = padTop - ballDiameter;
    } else {
      ballX = ballX + (int) (Math.cos((angle * pi) / 180.0) * unit);
      ballY = ballY - (int) (Math.sin((angle * pi) / 180.0) * unit); // Y increases downward
      if (Math.sin((angle * pi) / 180.0) * unit >= 0) {
        top = true;
      } else top = false;

      if (Math.cos((angle * pi) / 180.0) * unit >= 0) {
        right = true;
      } else right = false;

      if (right) leftEdgeActive = false;
      else rightEdgeActive = false;

      if (top) bottomEdgeActive = false;
      else topEdgeActive = false;

      Pair temp;

      if (leftEdgeActive) {
        int xBlockLeft = ((ballX - blockStartX) / blockLength) * blockLength;
        int yBlockLeft = ((ballY - blockStartY + ballDiameter / 2) / blockHeight) * blockHeight;

        // Left Boundary Edge
        temp = new Pair(xBlockLeft, yBlockLeft);
        if ((blockMap.containsKey(temp.toString())
                && (ballX >= blockStartX)
                && (ballY >= blockStartY))
            || ballX <= 0) {
          if (immutableBlocks.containsKey(temp.toString()) == false) {

            /*				if(fireBlocks.containsKey(temp.toString())){
            				fireEnabled = true;
            			}
            */
            angle = 180.0 - angle;
            blockMap.remove(temp.toString());
            repaintBlocks = true;
            count = numRepaints;
            repaint();
            return;
          } else {
            // angle = 180.0 - angle;
            blockMap.put(temp.toString(), Color.RED);
            repaintBlocks = true;
            count = numRepaints;
            repaint();
            return;
          }
        }
      }

      if (rightEdgeActive) {
        int xBlockRight = ((ballX - blockStartX + ballDiameter) / blockLength) * blockLength;
        int yBlockRight = ((ballY - blockStartY + ballDiameter / 2) / blockHeight) * blockHeight;
        // Right Boundary Edge
        temp = new Pair(xBlockRight, yBlockRight);
        if ((blockMap.containsKey(temp.toString())
                && (ballX - blockStartX + ballDiameter > 0)
                && (ballY - blockStartY + ballDiameter / 2) >= 0)
            || (ballX + ballDiameter) >= MaxX) {
          // System.out.println("Right edge");
          if (immutableBlocks.containsKey(temp.toString()) == false) {
            /*		if(fireBlocks.containsKey(temp.toString())){
            	fireEnabled = true;
            }*/
            angle = 180.0 - angle;
            blockMap.remove(temp.toString());
            repaintBlocks = true;
            count = numRepaints;
            repaint();
            return;
          } else {
            // angle = 180.0 - angle;
            blockMap.put(temp.toString(), Color.RED);
            repaintBlocks = true;
            count = numRepaints;
            repaint();
            return;
          }
        }
      }
      if (topEdgeActive) {
        int xBlockTop = ((ballX - blockStartX + ballDiameter / 2) / blockLength) * blockLength;
        int yBlockTop = ((ballY - blockStartY) / blockHeight) * blockHeight;
        // top Boundary Edge
        temp = new Pair(xBlockTop, yBlockTop);

        if ((blockMap.containsKey(temp.toString())
                && (ballX >= blockStartX)
                && (ballY >= blockStartY))
            || (ballY - ballDiameter) <= 0) {
          if (immutableBlocks.containsKey(temp.toString()) == false) {
            /*if(fireBlocks.containsKey(temp.toString())){
            		fireEnabled = true;
            }*/

            angle = 360.0 - angle;
            blockMap.remove(temp.toString());
            repaintBlocks = true;
            count = numRepaints;
            repaint();
            // System.out.println("temp " + temp.toString());

            return;
          } else {
            // angle = 360.0 - angle;
            blockMap.put(temp.toString(), Color.RED);
            repaintBlocks = true;
            count = numRepaints;
            repaint();
            return;
          }
        }
      }
      if (bottomEdgeActive) {
        int xBlockBottom = ((ballX - blockStartX + ballDiameter / 2) / blockLength) * blockLength;
        int yBlockBottom = ((ballY - blockStartY + ballDiameter) / blockHeight) * blockHeight;
        // top Boundary Edge
        temp = new Pair(xBlockBottom, yBlockBottom);
        if ((blockMap.containsKey(temp.toString())
                && (ballX - blockStartX + ballDiameter / 2 >= 0)
                && (ballY - blockStartY + ballDiameter >= 0))
            || (((((ballY + ballDiameter) >= padTop && (ballY + ballDiameter) <= padBottom))
                    || (ballY >= padTop && ballY <= padBottom))
                && (ballX) >= (padx - padLength / 2)
                && ballX <= (padx + padLength / 2))) {

          if (immutableBlocks.containsKey(temp.toString()) == false) {
            /*if(fireBlocks.containsKey(temp.toString())){
            	fireEnabled = true;
            }*/
            angle = 360.0 - angle;
            blockMap.remove(temp.toString());
            repaintBlocks = true;
            count = numRepaints;
            repaint();
            return;
          } else {
            // angle = 360.0 - angle;
            blockMap.put(temp.toString(), Color.RED);
            repaintBlocks = true;
            count = numRepaints;
            repaint();
            return;
          }
        }
      }
    }
  }