Allow buying "non-free" themes

This commit is contained in:
Lonami Exo 2017-03-03 13:43:51 +01:00
parent 3ef87fecd3
commit 2e864b0017
5 changed files with 61 additions and 9 deletions

View file

@ -165,13 +165,31 @@ public class Klooni extends Game {
return false; 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) { public static void updateTheme(Theme newTheme) {
prefs.putString("themeName", newTheme.getName()).flush(); prefs.putString("themeName", newTheme.getName()).flush();
theme.update(newTheme.getName()); theme.update(newTheme.getName());
} }
// Money related // Money related
public static void addMoney(int score) { public static void addMoneyFromScore(int score) {
setMoney(getRealMoney() + score * SCORE_TO_MONEY); setMoney(getRealMoney() + score * SCORE_TO_MONEY);
} }

View file

@ -23,6 +23,11 @@ public class MoneyBuyBand extends Table {
private String infoText; private String infoText;
private boolean showingTemp; 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 // Used to interpolate between strings
private StringBuilder shownText; private StringBuilder shownText;
@ -52,6 +57,15 @@ public class MoneyBuyBand extends Table {
add(infoLabel).expandX().left().padLeft(20); add(infoLabel).expandX().left().padLeft(20);
confirmButton = new SoftButton(0, "ok_texture"); 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); add(confirmButton).pad(8, 0, 8, 4);
confirmButton.setVisible(false); confirmButton.setVisible(false);
@ -60,8 +74,7 @@ public class MoneyBuyBand extends Table {
@Override @Override
public void changed(ChangeEvent event, Actor actor) { public void changed(ChangeEvent event, Actor actor) {
showCurrentMoney(); showCurrentMoney();
confirmButton.setVisible(false); hideBuyButtons();
cancelButton.setVisible(false);
} }
}); });
add(cancelButton).pad(8, 4, 8, 8); add(cancelButton).pad(8, 4, 8, 8);
@ -79,6 +92,12 @@ public class MoneyBuyBand extends Table {
setText("money: " + Klooni.getMoney()); 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. // Set the text to which the shown text will interpolate.
// This will remove any temporary shown text or otherwise // This will remove any temporary shown text or otherwise
// it would mess up this new text. // 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 // 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 ThemeCard toBuy) {
if (toBuy.getPrice() > Klooni.getMoney()) { if (toBuy.theme.getPrice() > Klooni.getMoney()) {
setTempText("cannot buy!"); setTempText("cannot buy!");
confirmButton.setVisible(false); confirmButton.setVisible(false);
cancelButton.setVisible(false); cancelButton.setVisible(false);
} }
else { else {
this.toBuy = toBuy;
setText("confirm?"); setText("confirm?");
confirmButton.setVisible(true); confirmButton.setVisible(true);
cancelButton.setVisible(true); cancelButton.setVisible(true);

View file

@ -101,6 +101,20 @@ public class ThemeCard extends Actor {
priceLabel.setText("buy for "+theme.getPrice()); 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 // Used to determine the best foreground color (black or white) given a background color
// Formula took from http://alienryderflex.com/hsp.html // Formula took from http://alienryderflex.com/hsp.html
private static boolean shouldUseWhite(Color color) { private static boolean shouldUseWhite(Color color) {

View file

@ -128,9 +128,9 @@ class CustomizeScreen implements Screen {
@Override @Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
if (Klooni.isThemeBought(card.theme)) if (Klooni.isThemeBought(card.theme))
Klooni.updateTheme(card.theme); card.use();
else else
buyBand.askBuy(card.theme); buyBand.askBuy(card);
for (Actor a : themesGroup.getChildren()) { for (Actor a : themesGroup.getChildren()) {
ThemeCard c = (ThemeCard)a; ThemeCard c = (ThemeCard)a;

View file

@ -152,9 +152,9 @@ class GameScreen implements Screen, InputProcessor, BinSerializable {
private void showPauseMenu() { private void showPauseMenu() {
// Calculate new money since the previous saving // Calculate new money since the previous saving
int nowScore = scorer.getCurrentScore(); int nowScore = scorer.getCurrentScore();
int newMoney = nowScore - savedMoneyScore; int newMoneyScore = nowScore - savedMoneyScore;
savedMoneyScore = nowScore; savedMoneyScore = nowScore;
Klooni.addMoney(newMoney); Klooni.addMoneyFromScore(newMoneyScore);
// Show the pause menu // Show the pause menu
pauseMenu.show(false); pauseMenu.show(false);