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;
}
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);
}

View file

@ -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);

View file

@ -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) {

View file

@ -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;

View file

@ -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);