From 779f37045b9e56fac7e74a50af483606f6199b3f Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Wed, 19 Jul 2017 13:32:33 +0200 Subject: [PATCH] Create an EffectCard for the shop to show them --- .../io/github/lonamiwebs/klooni/Effect.java | 13 +- .../io/github/lonamiwebs/klooni/Klooni.java | 37 ++++++ .../io/github/lonamiwebs/klooni/Theme.java | 8 ++ .../lonamiwebs/klooni/actors/EffectCard.java | 125 ++++++++++++++++++ 4 files changed, 181 insertions(+), 2 deletions(-) create mode 100644 core/src/io/github/lonamiwebs/klooni/actors/EffectCard.java diff --git a/core/src/io/github/lonamiwebs/klooni/Effect.java b/core/src/io/github/lonamiwebs/klooni/Effect.java index 9ca9796..978b24b 100644 --- a/core/src/io/github/lonamiwebs/klooni/Effect.java +++ b/core/src/io/github/lonamiwebs/klooni/Effect.java @@ -34,6 +34,8 @@ public class Effect { private final int effectId; private final Sound effectSound; + public final String name; + public final int price; //endregion @@ -41,8 +43,10 @@ public class Effect { // This method will load the sound "sound/effect_{effectName}.mp3" public Effect(final String effectName) { - effectId = effectNameToInt(effectName); - effectSound = Gdx.audio.newSound(Gdx.files.internal("sound/effect_" + effectName + ".mp3")); + name = effectName; + effectId = effectNameToInt(name); + effectSound = Gdx.audio.newSound(Gdx.files.internal("sound/effect_" + name + ".mp3")); + price = effectId == 0 ? 0 : 200; // TODO For now they're all 200 coins except the first } //endregion @@ -53,6 +57,11 @@ public class Effect { effectSound.play(MathUtils.random(0.7f, 1f), MathUtils.random(0.8f, 1.2f), 0); } + public String getDisplay() { + // TODO For now, uppercase the first letter + return name.substring(0, 1).toUpperCase() + name.substring(1); + } + //endregion //region Static methods diff --git a/core/src/io/github/lonamiwebs/klooni/Klooni.java b/core/src/io/github/lonamiwebs/klooni/Klooni.java index 172fe26..c68979e 100644 --- a/core/src/io/github/lonamiwebs/klooni/Klooni.java +++ b/core/src/io/github/lonamiwebs/klooni/Klooni.java @@ -182,6 +182,43 @@ public class Klooni extends Game { theme.update(newTheme.getName()); } + // Effects related + public static boolean isEffectBought(Effect effect) { + if (effect.price == 0) + return true; + + String[] effects = prefs.getString("boughtEffects", "").split(":"); + for (String e : effects) + if (e.equals(effect.name)) + return true; + + return false; + } + + public static boolean buyEffect(Effect effect) { + final float money = getRealMoney(); + if (effect.price > money) + return false; + + setMoney(money - effect.price); + + String bought = prefs.getString("boughtEffects", ""); + if (bought.equals("")) + bought = effect.name; + else + bought += ":" + effect.name; + + prefs.putString("boughtEffects", bought); + + return true; + } + + public void updateEffect(Effect newEffect) { + prefs.putString("effectName", newEffect.name).flush(); + // Create a new effect, since the one passed through the parameter may dispose later + effect = new Effect(newEffect.name); + } + // Money related public static void addMoneyFromScore(int score) { setMoney(getRealMoney() + score * SCORE_TO_MONEY); diff --git a/core/src/io/github/lonamiwebs/klooni/Theme.java b/core/src/io/github/lonamiwebs/klooni/Theme.java index e5d4abd..3ae8663 100644 --- a/core/src/io/github/lonamiwebs/klooni/Theme.java +++ b/core/src/io/github/lonamiwebs/klooni/Theme.java @@ -23,6 +23,7 @@ import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Pixmap; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.NinePatch; +import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.scenes.scene2d.ui.ImageButton; import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.utils.Array; @@ -198,6 +199,13 @@ public class Theme { return colorIndex < 0 ? emptyCell : cells[colorIndex]; } + public Color getRandomCellColor() { + if (cells.length == 0) + return emptyCell; + else + return cells[MathUtils.random(cells.length - 1)]; + } + public void glClearBackground() { Gdx.gl.glClearColor(background.r, background.g, background.b, background.a); } diff --git a/core/src/io/github/lonamiwebs/klooni/actors/EffectCard.java b/core/src/io/github/lonamiwebs/klooni/actors/EffectCard.java new file mode 100644 index 0000000..2ceb5a3 --- /dev/null +++ b/core/src/io/github/lonamiwebs/klooni/actors/EffectCard.java @@ -0,0 +1,125 @@ +/* + 1010! Klooni, a free customizable puzzle game for Android and Desktop + Copyright (C) 2017 Lonami Exo | LonamiWebs + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +package io.github.lonamiwebs.klooni.actors; + +import com.badlogic.gdx.graphics.Color; +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.math.Rectangle; +import com.badlogic.gdx.scenes.scene2d.Actor; +import com.badlogic.gdx.scenes.scene2d.ui.Label; + +import io.github.lonamiwebs.klooni.Effect; +import io.github.lonamiwebs.klooni.Klooni; +import io.github.lonamiwebs.klooni.Theme; +import io.github.lonamiwebs.klooni.game.Cell; +import io.github.lonamiwebs.klooni.game.GameLayout; + +// Card-like actor used to display information about a given theme +public class EffectCard extends Actor { + + //region Members + + private final Klooni game; + public final Effect effect; + private final Texture background; + private Color color; + + private final Label nameLabel; + private final Label priceLabel; + + public final Rectangle nameBounds; + public final Rectangle priceBounds; + + public float cellSize; + + //endregion + + //region Constructor + + public EffectCard(final Klooni game, final GameLayout layout, final Effect effect) { + this.game = game; + this.effect = effect; + background = Theme.getBlankTexture(); + + Label.LabelStyle labelStyle = new Label.LabelStyle(); + labelStyle.font = game.skin.getFont("font_small"); + + priceLabel = new Label("", labelStyle); + nameLabel = new Label(effect.getDisplay(), labelStyle); + + Color labelColor = Theme.shouldUseWhite(Klooni.theme.background) ? Color.WHITE : Color.BLACK; + priceLabel.setColor(labelColor); + nameLabel.setColor(labelColor); + + priceBounds = new Rectangle(); + nameBounds = new Rectangle(); + + layout.update(this); + usedEffectUpdated(); + + color = Klooni.theme.getRandomCellColor(); + } + + //endregion + + //region Public methods + + @Override + public void draw(Batch batch, float parentAlpha) { + final float x = getX(), y = getY(); + + batch.setColor(Klooni.theme.background); + batch.draw(background, x, y, getWidth(), getHeight()); + + // Avoid drawing on the borders by adding +1 cell padding +1 to center it + // so it's becomes cellSize * 2 + Cell.draw(color, batch, x + cellSize * 2, y + cellSize * 2, cellSize); + + nameLabel.setBounds(x + nameBounds.x, y + nameBounds.y, nameBounds.width, nameBounds.height); + nameLabel.draw(batch, parentAlpha); + + priceLabel.setBounds(x + priceBounds.x, y + priceBounds.y, priceBounds.width, priceBounds.height); + priceLabel.draw(batch, parentAlpha); + } + + public void usedEffectUpdated() { + if (game.effect.name.equals(effect.name)) + priceLabel.setText("currently used"); + else if (Klooni.isEffectBought(effect)) + priceLabel.setText("bought"); + else + priceLabel.setText("buy for "+effect.price); + } + + public void use() { + game.updateEffect(effect); + usedEffectUpdated(); + } + + public boolean isUsed() { + return game.effect.equals(effect.name); + } + + void performBuy() { + Klooni.buyEffect(effect); + use(); + } + + //endregion +}