/** It enables only the pits from the player whose turn it is */ void enableActiveSide() { boolean enableNorth; boolean enableSouth; if (usersSide == null) { // sideTheUserIs is not yet initialized enableNorth = false; enableSouth = false; } else if (state.getWhoseTurn().isNorth() && usersSide.isNorth()) { enableNorth = true; enableSouth = false; } else if (state.getWhoseTurn().isNorth() && usersSide.isSouth()) { enableNorth = false; enableSouth = false; } else if (state.getWhoseTurn().isSouth() && usersSide.isSouth()) { enableNorth = false; enableSouth = true; } else { enableNorth = false; enableSouth = false; } for (int i = 0; i < state.getNorthPits().length - 1; i++) { graphics.setPitEnabled(PlayerColor.N, i, enableNorth); graphics.setPitEnabled(PlayerColor.S, i, enableSouth); } }
/** * A player can only select certain pits for their move. That's why some have to be enabled and * some have to be disabled before a player's turn. */ @Override public void setPitEnabled(PlayerColor side, int col, boolean enabled) { AbsolutePanel pnl; if (col < 6) { if (side.isNorth()) { pnl = (AbsolutePanel) gameGrid.getWidget(0, State.getMirrorIndex(col, 5)); } else if (side.isSouth()) { pnl = (AbsolutePanel) gameGrid.getWidget(1, col); } else throw new IllegalMoveException(); final int colB = col; int row = side.isNorth() ? 0 : 1; if (enabled) { // the handerRegs keeps track of all click handlers of the pits // by removing it from this array the handler is removed from // the pit (absolutePanel) as well handlerRegs[row][col].removeHandler(); handlerRegs[row][col] = pnl.addDomHandler( new ClickHandler() { @Override public void onClick(ClickEvent event) { presenter.makeMove(colB); } }, ClickEvent.getType()); } else { handlerRegs[row][col].removeHandler(); handlerRegs[row][col] = pnl.addDomHandler( new ClickHandler() { @Override public void onClick(ClickEvent event) { setWarnLabelText(messages.warnMessage()); } }, ClickEvent.getType()); } } }
/** The seedAmount will be placed on the right side at the correct index */ @Override public void setSeeds(PlayerColor side, int col, int seedAmount) { if (col < 6) { AbsolutePanel pnl; if (side.isNorth()) { pnl = (AbsolutePanel) gameGrid.getWidget(0, State.getMirrorIndex(col, 5)); } else if (side.isSouth()) { pnl = (AbsolutePanel) gameGrid.getWidget(1, col); } else throw new IllegalMoveException(); addSeeds(pnl, seedAmount); } else if (col == 6) { AbsolutePanel pnl; if (side.isNorth()) { pnl = (AbsolutePanel) treasureGridN.getWidget(0, 0); } else if (side.isSouth()) { pnl = (AbsolutePanel) treasureGridS.getWidget(0, 0); } else throw new IllegalMoveException(); addSeedsToTreasureChest(pnl, seedAmount); } else throw new IllegalMoveException(); }
/** To animate a seed from one pit to another */ @Override public void animateFromPitToPit( PlayerColor startSide, int startCol, PlayerColor endSide, int endCol, double delay, boolean finalAnimation) { int startRow = startSide.isNorth() ? 0 : 1; int actualStartCol = (startRow == 0) ? 5 - startCol : startCol; int endRow = endSide.isNorth() ? 0 : 1; int actualEndCol = (endRow == 0) ? 5 - endCol : endCol; AbsolutePanel startPanel = (AbsolutePanel) gameGrid.getWidget(startRow, actualStartCol); final Image seed; seed = (Image) startPanel.getWidget(startPanel.getWidgetCount() - 2); int startXStartPanel = startPanel.getWidgetLeft(seed); int startYStartPanel = startPanel.getWidgetTop(seed); int startX = 2 + actualStartCol * 2 + TREASURE_CHEST_WIDTH + PIT_WIDTH * actualStartCol + PADDING * (actualStartCol * 2 + 3) + startXStartPanel; int startY = 2 + PADDING + startRow * (PIT_HEIGHT + 2 * PADDING + 2) + startYStartPanel; AbsolutePanel endPanel; int[] endPointEndPanel; int endXEndPanel; int endYEndPanel; int endX; int endY; if (endCol < 6) { endPanel = (AbsolutePanel) gameGrid.getWidget(endRow, actualEndCol); endPointEndPanel = getTargetPoint(endPanel.getWidgetCount() - 1); endXEndPanel = endPointEndPanel[0]; endYEndPanel = endPointEndPanel[1]; endX = 2 + actualEndCol * 2 + TREASURE_CHEST_WIDTH + PIT_WIDTH * actualEndCol + PADDING * (actualEndCol * 2 + 3) + endXEndPanel; endY = 2 + PADDING + endRow * (PIT_HEIGHT + 2 * PADDING + 2) + endYEndPanel; } else { Grid hGrid = endRow == 0 ? treasureGridN : treasureGridS; endPanel = (AbsolutePanel) hGrid.getWidget(0, 0); endPointEndPanel = getTargetPointTreasureChest(endPanel.getWidgetCount() - 1); endXEndPanel = endPointEndPanel[0]; endYEndPanel = endPointEndPanel[1]; endX = 2 + PADDING + (TREASURE_CHEST_WIDTH + PIT_WIDTH * 6 + PADDING * 14 + 6 * 2) * endRow + endXEndPanel; endY = 2 + PADDING + endYEndPanel; } animation = new SeedMovingAnimation( seed, gameImages.redSeed(), startPanel, endPanel, startXStartPanel, startYStartPanel, endXEndPanel, endYEndPanel, startX, startY, endX, endY, finalAnimation, this, dotSound); animation.run(1000, Duration.currentTimeMillis() + delay); }