Improve piece holder drop piece
This commit is contained in:
parent
d5f778240d
commit
eb5591bcef
2 changed files with 41 additions and 13 deletions
|
@ -178,7 +178,9 @@ public class PieceHolder implements BinSerializable {
|
||||||
|
|
||||||
// Tries to drop the piece on the given board. As a result, it
|
// 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
|
// 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) {
|
if (heldPiece > -1) {
|
||||||
boolean put;
|
boolean put;
|
||||||
put = enabled && board.putScreenPiece(pieces[heldPiece]);
|
put = enabled && board.putScreenPiece(pieces[heldPiece]);
|
||||||
|
@ -189,19 +191,23 @@ public class PieceHolder implements BinSerializable {
|
||||||
float pitch = 1.104f - pieces[heldPiece].calculateArea() * 0.04f;
|
float pitch = 1.104f - pieces[heldPiece].calculateArea() * 0.04f;
|
||||||
pieceDropSound.play(1, pitch, 0);
|
pieceDropSound.play(1, pitch, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result = new DropResult(calculateHeldPieceArea(), calculateHeldPieceCenter());
|
||||||
pieces[heldPiece] = null;
|
pieces[heldPiece] = null;
|
||||||
} else {
|
} else {
|
||||||
if (Klooni.soundsEnabled())
|
if (Klooni.soundsEnabled())
|
||||||
invalidPieceDropSound.play();
|
invalidPieceDropSound.play();
|
||||||
|
|
||||||
|
result = new DropResult(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
heldPiece = -1;
|
heldPiece = -1;
|
||||||
if (handFinished())
|
if (handFinished())
|
||||||
takeMore();
|
takeMore();
|
||||||
|
|
||||||
return put ? ON_BOARD_DROP : NORMAL_DROP;
|
|
||||||
} else
|
} else
|
||||||
return NO_DROP;
|
result = new DropResult(false);
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Updates the state of the piece holder (and the held piece)
|
// Updates the state of the piece holder (and the held piece)
|
||||||
|
@ -285,4 +291,30 @@ public class PieceHolder implements BinSerializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
//endregion
|
//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
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,6 @@ import com.badlogic.gdx.audio.Sound;
|
||||||
import com.badlogic.gdx.files.FileHandle;
|
import com.badlogic.gdx.files.FileHandle;
|
||||||
import com.badlogic.gdx.graphics.GL20;
|
import com.badlogic.gdx.graphics.GL20;
|
||||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||||
import com.badlogic.gdx.math.Vector2;
|
|
||||||
import com.badlogic.gdx.scenes.scene2d.ui.Label;
|
import com.badlogic.gdx.scenes.scene2d.ui.Label;
|
||||||
|
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
|
@ -207,18 +206,15 @@ class GameScreen implements Screen, InputProcessor, BinSerializable {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
|
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
|
||||||
int area = holder.calculateHeldPieceArea();
|
PieceHolder.DropResult result = holder.dropPiece(board);
|
||||||
Vector2 pos = holder.calculateHeldPieceCenter();
|
if (!result.dropped)
|
||||||
|
|
||||||
int action = holder.dropPiece(board);
|
|
||||||
if (action == PieceHolder.NO_DROP)
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (action == PieceHolder.ON_BOARD_DROP) {
|
if (result.onBoard) {
|
||||||
scorer.addPieceScore(area);
|
scorer.addPieceScore(result.area);
|
||||||
int bonus = scorer.addBoardScore(board.clearComplete(), board.cellCount);
|
int bonus = scorer.addBoardScore(board.clearComplete(), board.cellCount);
|
||||||
if (bonus > 0)
|
if (bonus > 0)
|
||||||
bonusParticleHandler.addBonus(pos, bonus);
|
bonusParticleHandler.addBonus(result.pieceCenter, bonus);
|
||||||
|
|
||||||
// After the piece was put, check if it's game over
|
// After the piece was put, check if it's game over
|
||||||
if (isGameOver()) {
|
if (isGameOver()) {
|
||||||
|
|
Loading…
Reference in a new issue