Ejemplo n.º 1
0
 public XYZCanvas() {
   super();
   addMouseListener(this);
   addMouseMotionListener(this);
   addMouseWheelListener(this);
 }
Ejemplo n.º 2
0
  public WorldPanel(LD33Client client, int worldWidth, int worldHeight) {
    this.client = client;
    world = new World(worldWidth, worldHeight);
    cameraX = 0;
    cameraY = 0;
    addMouseListener(
        new MouseAdapter() {
          @Override
          public void mouseClicked(MouseEvent event) {
            if (isRightMouseButton(event)) {
              client.sendPacket(
                  new UnitMoveServerBoundPacket(
                      getSelectedUnit(),
                      (cameraX + event.getX()) / 64,
                      (cameraY + event.getY()) / 64));
              Tile tile =
                  getWorld()
                      .getTileAt((cameraX + event.getX()) / 64, (cameraY + event.getY()) / 64);
              if (tile != null) {
                if (tile.getUnit() != null) {
                  if (!tile.getUnit()
                      .getPlayerUUID()
                      .toString()
                      .equals(client.getPlayer().getUUID().toString())) {
                    new Thread(
                            () ->
                                client
                                    .getSoundPlayer()
                                    .play(getClass().getResourceAsStream("/burn.ogg")))
                        .start();
                  }
                }
              }
            } else if (isLeftMouseButton(event)) {
              ShopItem selectedShopItem = client.getShopPanel().getSelectedItem();
              if (selectedShopItem != null) {
                selectedShopItem.buy(
                    getWorld()
                        .getTileAt((cameraX + event.getX()) / 64, (cameraY + event.getY()) / 64));
                client.getShopPanel().setSelectedItem(null);
                client.getShopPanel().repaint();
              }
            }
          }

          @Override
          public void mousePressed(MouseEvent event) {
            mousePoint = event.getPoint();
          }
        });
    addMouseMotionListener(
        new MouseMotionAdapter() {
          @Override
          public void mouseDragged(MouseEvent event) {
            if (isMiddleMouseButton(event)) {
              int dx = event.getX() - (int) mousePoint.getX();
              int dy = event.getY() - (int) mousePoint.getY();
              cameraX -= dx;
              cameraY -= dy;
              mousePoint = event.getPoint();
            }
          }

          @Override
          public void mouseMoved(MouseEvent event) {
            Tile tile =
                getWorld().getTileAt((cameraX + event.getX()) / 64, (cameraY + event.getY()) / 64);
            if (tile != null) {
              Unit unit = tile.getUnit();
              if (unit != null) {
                try {
                  Player player = unit.getPlayer();
                  if (player != null) {
                    setToolTipText(
                        "Owner: "
                            + player.getName()
                            + " "
                            + "Health: "
                            + unit.getHealth()
                            + "/"
                            + unit.getMaxHealth()
                            + " "
                            + "Tile: "
                            + tile.getX()
                            + ", "
                            + tile.getY()
                            + (unit.getTimeToComplete() > 0
                                ? " Seconds till completion: " + (unit.getTimeToComplete() / 1000)
                                : ""));
                  } else {
                    setToolTipText("Could not retrieve player information for this unit.");
                  }
                } catch (SQLException exception) {
                  client.getLogger().log(SEVERE, "Failed to get player for unit", exception);
                }
              } else {
                setToolTipText(null);
              }
            } else {
              setToolTipText(null);
            }
          }
        });
    setDoubleBuffered(true);
    setMinimumSize(new Dimension(640, 480));
    setPreferredSize(new Dimension(640, 480));
  }