Improve piece holder drop piece

This commit is contained in:
Lonami Exo 2017-02-10 18:39:02 +01:00
parent d5f778240d
commit eb5591bcef
2 changed files with 41 additions and 13 deletions

View file

@ -178,7 +178,9 @@ public class PieceHolder implements BinSerializable {
// Tries to drop the piece on the given board. As a result, it
// returns one of the following: NO_DROP, NORMAL_DROP, ON_BOARD_DROP
public int dropPiece(Board board) {
public DropResult dropPiece(Board board) {
DropResult result;
if (heldPiece > -1) {
boolean put;
put = enabled && board.putScreenPiece(pieces[heldPiece]);
@ -189,19 +191,23 @@ public class PieceHolder implements BinSerializable {
float pitch = 1.104f - pieces[heldPiece].calculateArea() * 0.04f;
pieceDropSound.play(1, pitch, 0);
}
result = new DropResult(calculateHeldPieceArea(), calculateHeldPieceCenter());
pieces[heldPiece] = null;
} else {
if (Klooni.soundsEnabled())
invalidPieceDropSound.play();
result = new DropResult(true);
}
heldPiece = -1;
if (handFinished())
takeMore();
return put ? ON_BOARD_DROP : NORMAL_DROP;
} else
return NO_DROP;
result = new DropResult(false);
return result;
}
// Updates the state of the piece holder (and the held piece)
@ -285,4 +291,30 @@ public class PieceHolder implements BinSerializable {
}
//endregion
//region Sub-classes
public class DropResult {
public final boolean dropped;
public final boolean onBoard;
public final int area;
public final Vector2 pieceCenter;
DropResult(final boolean dropped) {
this.dropped = dropped;
onBoard = false;
area = 0;
pieceCenter = null;
}
DropResult(final int area, final Vector2 pieceCenter) {
dropped = onBoard = true;
this.area = area;
this.pieceCenter = pieceCenter;
}
}
//endregion
}

View file

@ -8,7 +8,6 @@ import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import java.io.DataInputStream;
@ -207,18 +206,15 @@ class GameScreen implements Screen, InputProcessor, BinSerializable {
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
int area = holder.calculateHeldPieceArea();
Vector2 pos = holder.calculateHeldPieceCenter();
int action = holder.dropPiece(board);
if (action == PieceHolder.NO_DROP)
PieceHolder.DropResult result = holder.dropPiece(board);
if (!result.dropped)
return false;
if (action == PieceHolder.ON_BOARD_DROP) {
scorer.addPieceScore(area);
if (result.onBoard) {
scorer.addPieceScore(result.area);
int bonus = scorer.addBoardScore(board.clearComplete(), board.cellCount);
if (bonus > 0)
bonusParticleHandler.addBonus(pos, bonus);
bonusParticleHandler.addBonus(result.pieceCenter, bonus);
// After the piece was put, check if it's game over
if (isGameOver()) {