public void update() { welcomeLabel.setText("Welcome! You have " + credits + " credits."); if (game != null) { GameSquare gs = game.getGameSquareAt(leftClickRow, leftClickCol); if (game.getWinner() != -1) { gamePanel.add(returnToMenuButton); if (hasWon) gameInfo.setText("You won!"); else gameInfo.setText("You lost."); } else if (gs.hasOccupant()) { Occupant o = gs.getOccupant(); if (o instanceof Unit) { Unit u = (Unit) o; gameInfo.setText(u.getInfo()); } else gameInfo.setText(o.toString()); itemListModel.removeAllElements(); if (o instanceof Unit) { List<Item> list = ((Unit) o).getItemList(); for (Item i : list) { itemListModel.addElement(i); } } } else { gameInfo.setText("Empty"); } boardPanel.repaint(); } }
private synchronized void sendNewUnits() { System.out.println("User unit list model: " + userUnitListModel.getSize()); for (int i = 0; i < userUnitListModel.getSize(); i++) { Unit u = userUnitListModel.get(i); System.out.println(u.toString()); sendCommand(new NewUnit(u.getName(), u.getUnitClass())); System.out.println(i); } }
@Override public void mousePressed(MouseEvent arg0) { if (arg0.getButton() == MouseEvent.BUTTON1) { leftClickRow = arg0.getPoint().y / 32; leftClickCol = arg0.getPoint().x / 32; selected = true; } else if (arg0.getButton() == MouseEvent.BUTTON3) { rightClickRow = arg0.getPoint().y / 32; rightClickCol = arg0.getPoint().x / 32; if (selected) { GameSquare gs = game.getGameSquareAt(leftClickRow, leftClickCol); if (!gs.hasOccupant() || !(gs.getOccupant() instanceof Unit)) return; Unit performer = (Unit) gs.getOccupant(); actionMenu.removeAll(); if (performer instanceof ExplosivesUnit) { if (performer.canAttack(rightClickRow, rightClickCol)) actionMenu.add(placeMineItem); } else { if (performer.canAttack(rightClickRow, rightClickCol)) actionMenu.add(attackItem); } if (performer.canMoveTo(rightClickRow, rightClickCol)) actionMenu.add(moveItem); if (performer.canUseAbility(rightClickRow, rightClickCol)) actionMenu.add(specialItem); if (performer.canUseItem(rightClickRow, rightClickCol)) { List<Item> items = performer.getItemList(); int size = items.size(); for (Item item : performer.getItemList()) if (item instanceof MineItem) size = size - 1; useItemMenu.removeAll(); for (int i = 0; i < size; i++) { JMenuItem item = new JMenuItem(itemListModel.get(i).getName()); useItemMenu.add(item); final int j = i; item.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { UseItemCommand c = new UseItemCommand( new int[] {leftClickRow, leftClickCol}, new int[] {rightClickRow, rightClickCol}, j); sendCommand(c); itemListModel.removeElementAt(j); selected = false; actionMenu.setVisible(false); update(); } }); } actionMenu.add(useItemMenu); } if (performer.getItemList().size() > 0 && performer.canGiveItem( rightClickRow, rightClickCol, performer.getItemList().get(0))) { giveItemMenu.removeAll(); for (int i = 0; i < itemListModel.getSize(); i++) { JMenuItem item = new JMenuItem(itemListModel.get(i).getName()); giveItemMenu.add(item); final int j = i; item.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { GiveItemCommand c = new GiveItemCommand( new int[] {leftClickRow, leftClickCol}, new int[] {rightClickRow, rightClickCol}, itemListModel.get(j)); sendCommand(c); itemListModel.removeElementAt(j); selected = false; actionMenu.setVisible(false); update(); } }); } actionMenu.add(giveItemMenu); } actionMenu.add(cancelItem); actionMenu.show(arg0.getComponent(), rightClickCol * 32 + 16, rightClickRow * 32 + 16); } } update(); }
@Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; GameSquare[][] map = game.getBoard(); int size = 32; for (int r = 0; r < map.length; r++) { for (int c = 0; c < map[0].length; c++) { int upper = r * size; int left = c * size; Rectangle2D square = new Rectangle2D.Double(left, upper, size, size); // g2.draw(square); g2.setColor(map[r][c].getOccupant() instanceof WallObstacle ? Color.black : Color.gray); if (leftClickRow == r && leftClickCol == c && selected) g2.setColor(Color.yellow); g2.fill(square); GameSquare srcSquare = game.getGameSquareAt(leftClickRow, leftClickCol); if (selected && srcSquare.getOccupant() instanceof Unit) { Unit u = (Unit) srcSquare.getOccupant(); if (u.canUseAbility(r, c)) { g2.setColor(Color.blue); square = new Rectangle2D.Double(left + 6, upper + 6, size - 13, size - 13); g2.draw(square); g2.fill(square); } if (u.canAttack(r, c)) { g2.setColor(Color.red); square = new Rectangle2D.Double(left + 6, upper + 6, size - 13, size - 13); g2.draw(square); g2.fill(square); } if (u.getItemList().size() > 0 && u.canGiveItem(r, c, u.getItem(0))) { g2.setColor(Color.green); square = new Rectangle2D.Double(left + 8, upper + 8, size - 17, size - 17); g2.draw(square); g2.fill(square); } if (u.canMoveTo(r, c)) { g2.setColor(Color.white); square = new Rectangle2D.Double(left + 10, upper + 10, size - 21, size - 21); g2.draw(square); g2.fill(square); } } if (map[r][c].getOccupant() instanceof MineObstacle) { MineObstacle mo = (MineObstacle) map[r][c].getOccupant(); if (mo.isVisible(units)) { System.out.println("its visible!"); g2.setColor(Color.magenta); Rectangle2D mine = new Rectangle2D.Double(left + 6, upper + 6, size - 13, size - 13); ; g2.fill(mine); } } if (map[r][c].getOccupant() instanceof BombObstacle) { g2.setColor(Color.lightGray); g2.fill(new Rectangle2D.Double(left + 6, upper + 6, size - 13, size - 13)); } } } for (Unit u : game.getBlueUnitList()) { if (u.isDead()) continue; int uRow = u.getLocation().getRow(); int uCol = u.getLocation().getCol(); int upper = uRow * size; int left = uCol * size; int imagey = 0; if (u instanceof SoldierUnit) imagey = size * 0; if (u instanceof EngineerUnit) imagey = size * 1; if (u instanceof DemolitionUnit) imagey = size * 2; if (u instanceof RocketUnit) imagey = size * 3; if (u instanceof MeleeUnit) imagey = size * 4; if (u instanceof ExplosivesUnit) imagey = size * 0; g2.drawImage( sprites, left, upper, left + size, upper + size, 0, imagey, size, imagey + size, null); } for (Unit u : game.getRedUnitList()) { if (u.isDead()) continue; int uRow = u.getLocation().getRow(); int uCol = u.getLocation().getCol(); int upper = uRow * size; int left = uCol * size; int imagey = 0; if (u instanceof SoldierUnit) imagey = size * 0; if (u instanceof EngineerUnit) imagey = size * 1; if (u instanceof DemolitionUnit) imagey = size * 2; if (u instanceof RocketUnit) imagey = size * 3; if (u instanceof MeleeUnit) imagey = size * 4; if (u instanceof ExplosivesUnit) imagey = size * 0; g2.drawImage( sprites, left, upper, left + size, upper + size, size, imagey, size + size, imagey + size, null); } if (isAnimating && animType == AnimationType.MOVEMENT) { int r = animationCommand.getSource()[0]; int c = animationCommand.getSource()[1]; int upper = r * size; int left = c * size; Rectangle2D square = new Rectangle2D.Double(left, upper, size, size); g2.setColor(Color.gray); g2.fill(square); Unit u = (Unit) game.getGameSquareAt(r, c).getOccupant(); int imagey = 0; int imagex = game.getRedUnitList().contains(u) ? size : 0; if (u instanceof SoldierUnit) imagey = size * 0; if (u instanceof EngineerUnit) imagey = size * 1; if (u instanceof DemolitionUnit) imagey = size * 2; if (u instanceof RocketUnit) imagey = size * 3; if (u instanceof MeleeUnit) imagey = size * 4; g2.drawImage( sprites, moverX, moverY, moverX + size, moverY + size, imagex, imagey, imagex + size, imagey + size, null); } else if (isAnimating && animType == AnimationType.ATTACK) { int r = animationCommand.getSource()[0]; int c = animationCommand.getSource()[1]; Unit u = (Unit) game.getGameSquareAt(r, c).getOccupant(); Ellipse2D circle = new Ellipse2D.Double(moverX + 12, moverY + 12, 8, 8); g2.setColor(Color.orange); if (u instanceof SoldierUnit) { circle = new Ellipse2D.Double(moverX + 12, moverY + 12, 8, 8); g2.setColor(Color.green); } if (u instanceof EngineerUnit) { circle = new Ellipse2D.Double(moverX + 12, moverY + 12, 8, 8); g2.setColor(Color.green); } if (u instanceof DemolitionUnit) { circle = new Ellipse2D.Double(moverX + 10, moverY + 10, 12, 12); g2.setColor(Color.pink); } if (u instanceof RocketUnit) { circle = new Ellipse2D.Double(moverX + 8, moverY + 8, 16, 16); g2.setColor(Color.orange); } if (u instanceof MeleeUnit) { circle = new Ellipse2D.Double(moverX + 8, moverY + 8, 16, 16); g2.setColor(Color.orange); } g2.fill(circle); } if (game.isWon()) { g2.setFont(new Font("Courier", Font.BOLD, 24)); if (hasWon) { g2.setColor(Color.white); g2.drawString("You are victorious!", 60, 192); } else { g2.setColor(Color.red); g2.drawString("You have been defeated!", 30, 192); } } }