From 3ef87fecd32285c515cf02544ae640ae688d3119 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Thu, 2 Mar 2017 21:08:35 +0100 Subject: [PATCH] Add money system --- .../io/github/lonamiwebs/klooni/Klooni.java | 22 +++++++++++++++++++ .../klooni/actors/MoneyBuyBand.java | 5 ++--- .../lonamiwebs/klooni/screens/GameScreen.java | 17 ++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/core/src/io/github/lonamiwebs/klooni/Klooni.java b/core/src/io/github/lonamiwebs/klooni/Klooni.java index a842159..03c3799 100644 --- a/core/src/io/github/lonamiwebs/klooni/Klooni.java +++ b/core/src/io/github/lonamiwebs/klooni/Klooni.java @@ -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 } diff --git a/core/src/io/github/lonamiwebs/klooni/actors/MoneyBuyBand.java b/core/src/io/github/lonamiwebs/klooni/actors/MoneyBuyBand.java index e2c099e..293a319 100644 --- a/core/src/io/github/lonamiwebs/klooni/actors/MoneyBuyBand.java +++ b/core/src/io/github/lonamiwebs/klooni/actors/MoneyBuyBand.java @@ -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); diff --git a/core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java b/core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java index 5e85598..ce92186 100644 --- a/core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java +++ b/core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java @@ -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;