From 2e864b00172810a1da35fdbfa2e728d12e7fb5ed Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Fri, 3 Mar 2017 13:43:51 +0100 Subject: [PATCH] Allow buying "non-free" themes --- .../io/github/lonamiwebs/klooni/Klooni.java | 20 ++++++++++++- .../klooni/actors/MoneyBuyBand.java | 28 ++++++++++++++++--- .../lonamiwebs/klooni/actors/ThemeCard.java | 14 ++++++++++ .../klooni/screens/CustomizeScreen.java | 4 +-- .../lonamiwebs/klooni/screens/GameScreen.java | 4 +-- 5 files changed, 61 insertions(+), 9 deletions(-) diff --git a/core/src/io/github/lonamiwebs/klooni/Klooni.java b/core/src/io/github/lonamiwebs/klooni/Klooni.java index 03c3799..33e1f3d 100644 --- a/core/src/io/github/lonamiwebs/klooni/Klooni.java +++ b/core/src/io/github/lonamiwebs/klooni/Klooni.java @@ -165,13 +165,31 @@ public class Klooni extends Game { return false; } + public static boolean buyTheme(Theme theme) { + final float money = getRealMoney(); + if (theme.getPrice() > money) + return false; + + setMoney(money - theme.getPrice()); + + String bought = prefs.getString("boughtThemes", ""); + if (bought.equals("")) + bought = theme.getName(); + else + bought += ":" + theme.getName(); + + prefs.putString("boughtThemes", bought); + + return true; + } + public static void updateTheme(Theme newTheme) { prefs.putString("themeName", newTheme.getName()).flush(); theme.update(newTheme.getName()); } // Money related - public static void addMoney(int score) { + public static void addMoneyFromScore(int score) { setMoney(getRealMoney() + score * SCORE_TO_MONEY); } diff --git a/core/src/io/github/lonamiwebs/klooni/actors/MoneyBuyBand.java b/core/src/io/github/lonamiwebs/klooni/actors/MoneyBuyBand.java index 293a319..d4f5088 100644 --- a/core/src/io/github/lonamiwebs/klooni/actors/MoneyBuyBand.java +++ b/core/src/io/github/lonamiwebs/klooni/actors/MoneyBuyBand.java @@ -23,6 +23,11 @@ public class MoneyBuyBand extends Table { private String infoText; private boolean showingTemp; + // The theme card that is going to be bought next. We can't + // only save the Theme because we need to tell the ThemeCard + // that it was bought so it can reflect the new theme status. + private ThemeCard toBuy; + // Used to interpolate between strings private StringBuilder shownText; @@ -52,6 +57,15 @@ public class MoneyBuyBand extends Table { add(infoLabel).expandX().left().padLeft(20); confirmButton = new SoftButton(0, "ok_texture"); + confirmButton.addListener(new ChangeListener() { + @Override + public void changed(ChangeEvent event, Actor actor) { + if (toBuy != null) + toBuy.performBuy(); + showCurrentMoney(); + hideBuyButtons(); + } + }); add(confirmButton).pad(8, 0, 8, 4); confirmButton.setVisible(false); @@ -60,8 +74,7 @@ public class MoneyBuyBand extends Table { @Override public void changed(ChangeEvent event, Actor actor) { showCurrentMoney(); - confirmButton.setVisible(false); - cancelButton.setVisible(false); + hideBuyButtons(); } }); add(cancelButton).pad(8, 4, 8, 8); @@ -79,6 +92,12 @@ public class MoneyBuyBand extends Table { setText("money: " + Klooni.getMoney()); } + private void hideBuyButtons() { + confirmButton.setVisible(false); + cancelButton.setVisible(false); + toBuy = null; + } + // Set the text to which the shown text will interpolate. // This will remove any temporary shown text or otherwise // it would mess up this new text. @@ -137,13 +156,14 @@ 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) { - if (toBuy.getPrice() > Klooni.getMoney()) { + public void askBuy(final ThemeCard toBuy) { + if (toBuy.theme.getPrice() > Klooni.getMoney()) { setTempText("cannot buy!"); confirmButton.setVisible(false); cancelButton.setVisible(false); } else { + this.toBuy = toBuy; setText("confirm?"); confirmButton.setVisible(true); cancelButton.setVisible(true); diff --git a/core/src/io/github/lonamiwebs/klooni/actors/ThemeCard.java b/core/src/io/github/lonamiwebs/klooni/actors/ThemeCard.java index 75b5f95..425202c 100644 --- a/core/src/io/github/lonamiwebs/klooni/actors/ThemeCard.java +++ b/core/src/io/github/lonamiwebs/klooni/actors/ThemeCard.java @@ -101,6 +101,20 @@ public class ThemeCard extends Actor { priceLabel.setText("buy for "+theme.getPrice()); } + public void use() { + Klooni.updateTheme(theme); + usedThemeUpdated(); + } + + void performBuy() { + Klooni.buyTheme(theme); + use(); + } + + //endregion + + //region Private methods + // Used to determine the best foreground color (black or white) given a background color // Formula took from http://alienryderflex.com/hsp.html private static boolean shouldUseWhite(Color color) { diff --git a/core/src/io/github/lonamiwebs/klooni/screens/CustomizeScreen.java b/core/src/io/github/lonamiwebs/klooni/screens/CustomizeScreen.java index 42e933e..c7e49ae 100644 --- a/core/src/io/github/lonamiwebs/klooni/screens/CustomizeScreen.java +++ b/core/src/io/github/lonamiwebs/klooni/screens/CustomizeScreen.java @@ -128,9 +128,9 @@ class CustomizeScreen implements Screen { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (Klooni.isThemeBought(card.theme)) - Klooni.updateTheme(card.theme); + card.use(); else - buyBand.askBuy(card.theme); + buyBand.askBuy(card); for (Actor a : themesGroup.getChildren()) { ThemeCard c = (ThemeCard)a; diff --git a/core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java b/core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java index ce92186..46295f7 100644 --- a/core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java +++ b/core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java @@ -152,9 +152,9 @@ class GameScreen implements Screen, InputProcessor, BinSerializable { private void showPauseMenu() { // Calculate new money since the previous saving int nowScore = scorer.getCurrentScore(); - int newMoney = nowScore - savedMoneyScore; + int newMoneyScore = nowScore - savedMoneyScore; savedMoneyScore = nowScore; - Klooni.addMoney(newMoney); + Klooni.addMoneyFromScore(newMoneyScore); // Show the pause menu pauseMenu.show(false);