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; public static boolean onDesktop;
private final static float SCORE_TO_MONEY = 1f / 100f;
//endregion //endregion
//region Creation //region Creation
@ -112,6 +114,7 @@ public class Klooni extends Game {
private static Preferences prefs; private static Preferences prefs;
// Score related
public static int getMaxScore() { public static int getMaxScore() {
return prefs.getInteger("maxScore", 0); return prefs.getInteger("maxScore", 0);
} }
@ -128,6 +131,7 @@ public class Klooni extends Game {
prefs.putInteger("maxTimeScore", maxTimeScore).flush(); prefs.putInteger("maxTimeScore", maxTimeScore).flush();
} }
// Settings related
public static boolean soundsEnabled() { public static boolean soundsEnabled() {
return !prefs.getBoolean("muteSound", false); return !prefs.getBoolean("muteSound", false);
} }
@ -148,6 +152,7 @@ public class Klooni extends Game {
return result; return result;
} }
// Themes related
public static boolean isThemeBought(Theme theme) { public static boolean isThemeBought(Theme theme) {
if (theme.getPrice() == 0) if (theme.getPrice() == 0)
return true; return true;
@ -165,5 +170,22 @@ public class Klooni extends Game {
theme.update(newTheme.getName()); 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 //endregion
} }

View file

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

View file

@ -47,6 +47,12 @@ class GameScreen implements Screen, InputProcessor, BinSerializable {
private boolean gameOverDone; 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 //endregion
//region Static members //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 // Save the state, the user might leave the game in any of the following 2 methods
private void showPauseMenu() { 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); pauseMenu.show(false);
save(); save();
} }
@ -293,6 +306,10 @@ class GameScreen implements Screen, InputProcessor, BinSerializable {
if (handle.exists()) { if (handle.exists()) {
try { try {
BinSerializer.deserialize(this, handle.read()); 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 // After it's been loaded, delete the save file
deleteSave(); deleteSave();
return true; return true;