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

View file

@ -1,9 +1,5 @@
package io.github.lonamiwebs.klooni.game; 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.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
@ -18,10 +14,7 @@ public class Scorer extends BaseScorer implements BinSerializable {
//region Members //region Members
private int currentScore, highScore; private int highScore;
// To interpolate between shown score -> real score
private float shownScore;
//endregion //endregion
@ -30,8 +23,6 @@ public class Scorer extends BaseScorer implements BinSerializable {
// The board size is required when calculating the score // The board size is required when calculating the score
public Scorer(final Klooni game, GameLayout layout) { public Scorer(final Klooni game, GameLayout layout) {
super(game, layout, Klooni.getMaxScore()); super(game, layout, Klooni.getMaxScore());
currentScore = 0;
highScore = Klooni.getMaxScore(); highScore = Klooni.getMaxScore();
} }
@ -39,23 +30,6 @@ public class Scorer extends BaseScorer implements BinSerializable {
//region Public methods //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() { public void saveScore() {
if (isNewRecord()) { if (isNewRecord()) {
Klooni.setMaxScore(currentScore); Klooni.setMaxScore(currentScore);
@ -72,15 +46,6 @@ public class Scorer extends BaseScorer implements BinSerializable {
return false; 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 //endregion
//region Serialization //region Serialization

View file

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