Add money system

This commit is contained in:
Lonami Exo 2017-03-02 21:08:35 +01:00
parent 32a8e70129
commit 3ef87fecd3
3 changed files with 41 additions and 3 deletions

View file

@ -23,6 +23,8 @@ public class Klooni extends Game {
public static boolean onDesktop;
private final static float SCORE_TO_MONEY = 1f / 100f;
//endregion
//region Creation
@ -112,6 +114,7 @@ public class Klooni extends Game {
private static Preferences prefs;
// Score related
public static int getMaxScore() {
return prefs.getInteger("maxScore", 0);
}
@ -128,6 +131,7 @@ public class Klooni extends Game {
prefs.putInteger("maxTimeScore", maxTimeScore).flush();
}
// Settings related
public static boolean soundsEnabled() {
return !prefs.getBoolean("muteSound", false);
}
@ -148,6 +152,7 @@ public class Klooni extends Game {
return result;
}
// Themes related
public static boolean isThemeBought(Theme theme) {
if (theme.getPrice() == 0)
return true;
@ -165,5 +170,22 @@ public class Klooni extends Game {
theme.update(newTheme.getName());
}
// Money related
public static void addMoney(int score) {
setMoney(getRealMoney() + score * SCORE_TO_MONEY);
}
private static void setMoney(float money) {
prefs.putFloat("money", money).flush();
}
public static int getMoney() {
return (int)getRealMoney();
}
private static float getRealMoney() {
return prefs.getFloat("money");
}
//endregion
}

View file

@ -76,7 +76,7 @@ public class MoneyBuyBand extends Table {
//region Private methods
private void showCurrentMoney() {
setText("money: 0");
setText("money: " + Klooni.getMoney());
}
// Set the text to which the shown text will interpolate.
@ -138,8 +138,7 @@ public class MoneyBuyBand extends Table {
// Asks the user to buy the given theme, or shows
// that they don't have enough money to buy it
public void askBuy(final Theme toBuy) {
int moneyIHaz = 10; // TODO use a real value
if (toBuy.getPrice() > moneyIHaz) {
if (toBuy.getPrice() > Klooni.getMoney()) {
setTempText("cannot buy!");
confirmButton.setVisible(false);
cancelButton.setVisible(false);

View file

@ -47,6 +47,12 @@ class GameScreen implements Screen, InputProcessor, BinSerializable {
private boolean gameOverDone;
// The last score that was saved when adding the money.
// We use this so we don't add the same old score to the money twice,
// but rather subtract it from the current score and then update it
// with the current score to get the "increase" of money score.
private int savedMoneyScore;
//endregion
//region Static members
@ -144,6 +150,13 @@ class GameScreen implements Screen, InputProcessor, BinSerializable {
// Save the state, the user might leave the game in any of the following 2 methods
private void showPauseMenu() {
// Calculate new money since the previous saving
int nowScore = scorer.getCurrentScore();
int newMoney = nowScore - savedMoneyScore;
savedMoneyScore = nowScore;
Klooni.addMoney(newMoney);
// Show the pause menu
pauseMenu.show(false);
save();
}
@ -293,6 +306,10 @@ class GameScreen implements Screen, InputProcessor, BinSerializable {
if (handle.exists()) {
try {
BinSerializer.deserialize(this, handle.read());
// No cheating! We need to load the previous money
// or it would seem like we earned it on this game
savedMoneyScore = scorer.getCurrentScore();
// After it's been loaded, delete the save file
deleteSave();
return true;