Skip to content
This repository has been archived by the owner on Nov 29, 2017. It is now read-only.

Commit

Permalink
Playing around with natural stone placement
Browse files Browse the repository at this point in the history
  • Loading branch information
vapour101 committed Aug 31, 2017
1 parent a07e248 commit f36123c
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/main/java/ui/controller/BoardController.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import logic.LocalGameHandler;
import ui.drawer.BoardDrawer;
import ui.drawer.BoardScoreDrawer;
import ui.drawer.WabiSabiBoardDrawer;
import util.*;

import java.net.URL;
Expand Down Expand Up @@ -138,7 +139,7 @@ private void constructCanvas() {

pane.getChildren().add(boardCanvas);

boardDrawer = new BoardDrawer(boardCanvas);
boardDrawer = new WabiSabiBoardDrawer(boardCanvas);
}

private void drawBoard() {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/ui/drawer/BoardDrawer.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ CoordProjector getProjector() {
return new CoordProjector(getBoardLength(canvas), getTopLeftCorner(canvas));
}

Canvas getCanvas()
{
return canvas;
}

void drawStonesToCanvas(Collection<Coords> stones, double radius, StoneColour colour) {
for (Coords stone : stones) {
drawStoneToCanvas(getProjector().fromBoardCoords(stone), radius, colour);
Expand Down
97 changes: 97 additions & 0 deletions src/main/java/ui/drawer/WabiSabiBoardDrawer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright (c) 2017
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package ui.drawer;

import javafx.scene.canvas.Canvas;
import util.CoordProjector;
import util.Coords;
import util.DrawCoords;
import util.StoneColour;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import static util.DimensionHelper.getBoardLength;
import static util.DimensionHelper.getTopLeftCorner;

public class WabiSabiBoardDrawer extends BoardDrawer {

Map<Coords, DrawCoords> coordsMap;

public WabiSabiBoardDrawer(Canvas canvas) {
super(canvas);
coordsMap = new HashMap<>();
}

@Override
void drawStonesToCanvas(Collection<Coords> stones, double radius, StoneColour colour) {
for (Coords stone : stones) {
drawStoneToCanvas(getWSProjector().fromBoardCoords(stone), radius, colour);
}
}

private WabiSabiProjector getWSProjector() {
return new WabiSabiProjector(getBoardLength(getCanvas()), getTopLeftCorner(getCanvas()), coordsMap);
}

private class WabiSabiProjector extends CoordProjector {

Map<Coords, DrawCoords> coordsMap;
Random generator;

WabiSabiProjector(double boardLength, DrawCoords topLeft, Map<Coords, DrawCoords> map) {
super(boardLength, topLeft);
generator = new Random();
coordsMap = map;
}

private double getRandomOffset() {
double result = generator.nextGaussian();
result /= 6;

if ( result > 0.3 )
result = 0.3;
else if ( result < -0.3 )
result = -0.3;

return result;
}

@Override
public DrawCoords fromBoardCoords(Coords boardCoords) {

if ( coordsMap.containsKey(boardCoords) ) {
DrawCoords regularCoords = super.fromBoardCoords(boardCoords);
DrawCoords modifier = coordsMap.get(boardCoords);

double x = regularCoords.getX() + modifier.getX() * getSpacing();
double y = regularCoords.getY() + modifier.getY() * getSpacing();

return new DrawCoords(x, y);
}

DrawCoords wabiSabiOffset = new DrawCoords(getRandomOffset(), getRandomOffset());

coordsMap.put(boardCoords, wabiSabiOffset);

return fromBoardCoords(boardCoords);
}
}
}
7 changes: 6 additions & 1 deletion src/main/java/util/CoordProjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,16 @@ public DrawCoords fromBoardCoords(Coords boardCoords) {
return result;
}

protected double getSpacing()
{
return boardLength / BOARD_SIZE;
}

public Coords nearestCoords(DrawCoords point) {
point = snapToBounds(point);
point.removeOffset(topLeft);

double spacing = boardLength / BOARD_SIZE;
double spacing = getSpacing();

int boardX = (int) Math.round(point.getX() / spacing + 0.5);
int boardY = (int) Math.round(point.getY() / spacing + 0.5);
Expand Down

0 comments on commit f36123c

Please sign in to comment.