Use normal scoring system for time mode

This commit is contained in:
Lonami Exo 2017-02-18 20:00:15 +01:00
parent f997a099ef
commit 26eb958116
3 changed files with 36 additions and 57 deletions

View file

@ -4,6 +4,8 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.utils.Align;
@ -15,6 +17,8 @@ public abstract class BaseScorer implements BinSerializable {
//region Members
protected int currentScore;
final Label currentScoreLabel;
final Label highScoreLabel;
@ -23,6 +27,9 @@ public abstract class BaseScorer implements BinSerializable {
private final Color cupColor;
// To interpolate between shown score -> real score
private float shownScore;
//endregion
//region Constructor
@ -52,7 +59,7 @@ public abstract class BaseScorer implements BinSerializable {
// If < 1 were cleared, score = 0
// If = 1 was cleared, score = cells cleared
// If > 1 were cleared, score = cells cleared + score(cleared - 1)
protected final int calculateClearScore(int stripsCleared, int boardSize) {
final int calculateClearScore(int stripsCleared, int boardSize) {
if (stripsCleared < 1) return 0;
if (stripsCleared == 1) return boardSize;
else return boardSize * stripsCleared + calculateClearScore(stripsCleared - 1, boardSize);
@ -63,10 +70,21 @@ public abstract class BaseScorer implements BinSerializable {
//region Public methods
// Adds the score a given piece would give
public abstract int addPieceScore(int areaPut);
public int addPieceScore(int areaPut) {
currentScore += areaPut;
return areaPut;
}
// Adds the score given by the board, this is, the count of cleared strips
public abstract int addBoardScore(int stripsCleared, int boardSize);
public int addBoardScore(int stripsCleared, int boardSize) {
int score = calculateClearScore(stripsCleared, boardSize);
currentScore += score;
return score;
}
public int getCurrentScore() {
return currentScore;
}
public void pause() { }
public void resume() { }
@ -74,7 +92,6 @@ public abstract class BaseScorer implements BinSerializable {
abstract public boolean isGameOver();
abstract protected boolean isNewRecord();
abstract public int getCurrentScore();
abstract public void saveScore();
public void draw(SpriteBatch batch) {
@ -83,6 +100,12 @@ public abstract class BaseScorer implements BinSerializable {
batch.setColor(cupColor);
batch.draw(cupTexture, cupArea.x, cupArea.y, cupArea.width, cupArea.height);
int roundShown = MathUtils.round(shownScore);
if (roundShown != currentScore) {
shownScore = Interpolation.linear.apply(shownScore, currentScore, 0.1f);
currentScoreLabel.setText(Integer.toString(MathUtils.round(shownScore)));
}
currentScoreLabel.setColor(Klooni.theme.currentScore);
currentScoreLabel.draw(batch, 1f);

View file

@ -1,9 +1,5 @@
package io.github.lonamiwebs.klooni.game;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.math.MathUtils;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
@ -18,10 +14,7 @@ public class Scorer extends BaseScorer implements BinSerializable {
//region Members
private int currentScore, highScore;
// To interpolate between shown score -> real score
private float shownScore;
private int highScore;
//endregion
@ -30,8 +23,6 @@ public class Scorer extends BaseScorer implements BinSerializable {
// The board size is required when calculating the score
public Scorer(final Klooni game, GameLayout layout) {
super(game, layout, Klooni.getMaxScore());
currentScore = 0;
highScore = Klooni.getMaxScore();
}
@ -39,23 +30,6 @@ public class Scorer extends BaseScorer implements BinSerializable {
//region Public methods
@Override
public int addPieceScore(int areaPut) {
currentScore += areaPut;
return areaPut;
}
@Override
public int addBoardScore(int stripsCleared, int boardSize) {
int score = calculateClearScore(stripsCleared, boardSize);
currentScore += score;
return score;
}
public int getCurrentScore() {
return currentScore;
}
public void saveScore() {
if (isNewRecord()) {
Klooni.setMaxScore(currentScore);
@ -72,15 +46,6 @@ public class Scorer extends BaseScorer implements BinSerializable {
return false;
}
public void draw(SpriteBatch batch) {
int roundShown = MathUtils.round(shownScore);
if (roundShown != currentScore) {
shownScore = Interpolation.linear.apply(shownScore, currentScore, 0.1f);
currentScoreLabel.setText(Integer.toString(MathUtils.round(shownScore)));
}
super.draw(batch);
}
//endregion
//region Serialization

View file

@ -20,7 +20,7 @@ public class TimeScorer extends BaseScorer implements BinSerializable {
private final Label timeLeftLabel;
private long startTime;
private int highScoreTime;
private int highScore;
// Indicates where we would die in time. Score adds to this, so we take
// longer to die. To get the "score" we simply calculate `deadTime - startTime`
@ -47,7 +47,7 @@ public class TimeScorer extends BaseScorer implements BinSerializable {
// The board size is required when calculating the score
public TimeScorer(final Klooni game, GameLayout layout) {
super(game, layout, Klooni.getMaxTimeScore());
highScoreTime = Klooni.getMaxTimeScore();
highScore = Klooni.getMaxTimeScore();
Label.LabelStyle labelStyle = new Label.LabelStyle();
labelStyle.font = game.skin.getFont("font");
@ -81,19 +81,11 @@ public class TimeScorer extends BaseScorer implements BinSerializable {
//region Public methods
@Override
public int addPieceScore(int areaPut) { return 0; /* Nope, no score for single pieces */ }
@Override
public int addBoardScore(int stripsCleared, int boardSize) {
int scoreBefore = getCurrentScore();
// Only clearing strips adds extra time
deadTime += scoreToNanos(calculateClearScore(stripsCleared, boardSize));
return getCurrentScore() - scoreBefore;
}
@Override
public int getCurrentScore() {
return nanosToSeconds(deadTime - startTime);
return super.addBoardScore(stripsCleared, boardSize);
}
@Override
@ -110,7 +102,7 @@ public class TimeScorer extends BaseScorer implements BinSerializable {
@Override
protected boolean isNewRecord() {
return getCurrentScore() > highScoreTime;
return getCurrentScore() > highScore;
}
@Override
@ -133,7 +125,6 @@ public class TimeScorer extends BaseScorer implements BinSerializable {
@Override
public void draw(SpriteBatch batch) {
currentScoreLabel.setText(Integer.toString(getCurrentScore()));
super.draw(batch);
int timeLeft = pausedTimeLeft < 0 ? getTimeLeft() : pausedTimeLeft;
@ -148,9 +139,9 @@ public class TimeScorer extends BaseScorer implements BinSerializable {
@Override
public void write(DataOutputStream out) throws IOException {
// current/dead offset ("how long until we die"), highScoreTime
// current/dead offset ("how long until we die"), highScore
out.writeLong(TimeUtils.nanoTime() - startTime);
out.writeInt(highScoreTime);
out.writeInt(highScore);
}
@Override
@ -159,7 +150,7 @@ public class TimeScorer extends BaseScorer implements BinSerializable {
// is different and we couldn't save absolute values
long deadOffset = in.readLong();
deadTime = startTime + deadOffset;
highScoreTime = in.readInt();
highScore = in.readInt();
}
//endregion