Allow snap to grid (closes #14)

This commit is contained in:
Lonami Exo 2017-02-21 21:08:08 +01:00
parent f1b05d4c79
commit 05d1e99117
9 changed files with 156 additions and 38 deletions

View file

@ -50,6 +50,8 @@ public class Klooni extends Game {
skin.add("share_texture", new Texture(Gdx.files.internal("ui/share.png")));
skin.add("sound_on_texture", new Texture(Gdx.files.internal("ui/sound_on.png")));
skin.add("sound_off_texture", new Texture(Gdx.files.internal("ui/sound_off.png")));
skin.add("snap_on_texture", new Texture(Gdx.files.internal("ui/snap_on.png")));
skin.add("snap_off_texture", new Texture(Gdx.files.internal("ui/snap_off.png")));
skin.add("issues_texture", new Texture(Gdx.files.internal("ui/issues.png")));
skin.add("credits_texture", new Texture(Gdx.files.internal("ui/credits.png")));
skin.add("web_texture", new Texture(Gdx.files.internal("ui/web.png")));
@ -121,6 +123,14 @@ public class Klooni extends Game {
prefs.putBoolean("muteSound", soundsEnabled()).flush();
}
public static boolean shouldSnapToGrid() {
return prefs.getBoolean("snapToGrid", false);
}
public static void toggleSnapToGrid() {
prefs.putBoolean("snapToGrid", !shouldSnapToGrid()).flush();
}
public static boolean isThemeBought(Theme theme) {
if (theme.getPrice() == 0)
return true;

View file

@ -121,6 +121,21 @@ public class Board implements BinSerializable {
return putPiece(piece, x, y);
}
Vector2 snapToGrid(final Piece piece, final Vector2 position) {
// Snaps the given position (e.g. mouse) to the grid,
// assuming piece wants to be put at the specified position.
// If the piece was not on the grid, the original position is returned
//
// Logic to determine the x and y is a copy-paste from putScreenPiece
final Vector2 local = position.cpy().sub(pos);
int x = MathUtils.round(local.x / piece.cellSize);
int y = MathUtils.round(local.y / piece.cellSize);
if (canPutPiece(piece, x, y))
return new Vector2(pos.x + x * piece.cellSize, pos.y + y * piece.cellSize);
else
return position;
}
// This will clear both complete rows and columns, all at once.
// The reason why we can't check first rows and then columns
// (or vice versa) is because the following case (* filled, _ empty):

View file

@ -209,7 +209,8 @@ public class PieceHolder implements BinSerializable {
}
// Updates the state of the piece holder (and the held piece)
public void update() {
// TODO Passing the board seems expensive Should it rather be an attribute?
public void update(Board board) {
Piece piece;
if (heldPiece > -1) {
piece = pieces[heldPiece];
@ -227,6 +228,8 @@ public class PieceHolder implements BinSerializable {
// avoiding to cover it with the finger (issue on Android devices)
mouse.sub(piece.getRectangle().width * 0.5f, -pickedCellSize);
}
if (Klooni.shouldSnapToGrid())
mouse.set(board.snapToGrid(piece, mouse));
piece.pos.lerp(mouse, DRAG_SPEED);
piece.cellSize = Interpolation.linear.apply(piece.cellSize, pickedCellSize, DRAG_SPEED);

View file

@ -80,6 +80,20 @@ class CustomizeScreen implements Screen {
});
optionsGroup.addActor(soundButton);
// Snap to grid on/off
final SoftButton snapButton = new SoftButton(
2, Klooni.shouldSnapToGrid() ? "snap_on_texture" : "snap_off_texture");
snapButton.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
Klooni.toggleSnapToGrid();
snapButton.image = CustomizeScreen.this.game.skin.getDrawable(
Klooni.shouldSnapToGrid() ? "snap_on_texture" : "snap_off_texture");
}
});
optionsGroup.addActor(snapButton);
// Issues
final SoftButton issuesButton = new SoftButton(3, "issues_texture");
issuesButton.addListener(new ChangeListener() {

View file

@ -166,7 +166,7 @@ class GameScreen implements Screen, InputProcessor, BinSerializable {
scorer.draw(batch);
board.draw(batch);
holder.update();
holder.update(board);
holder.draw(batch);
bonusParticleHandler.run(batch);