Create an EffectCard for the shop to show them

This commit is contained in:
Lonami Exo 2017-07-19 13:32:33 +02:00
parent 5a9a5c5a3b
commit 779f37045b
4 changed files with 181 additions and 2 deletions

View file

@ -34,6 +34,8 @@ public class Effect {
private final int effectId; private final int effectId;
private final Sound effectSound; private final Sound effectSound;
public final String name;
public final int price;
//endregion //endregion
@ -41,8 +43,10 @@ public class Effect {
// This method will load the sound "sound/effect_{effectName}.mp3" // This method will load the sound "sound/effect_{effectName}.mp3"
public Effect(final String effectName) { public Effect(final String effectName) {
effectId = effectNameToInt(effectName); name = effectName;
effectSound = Gdx.audio.newSound(Gdx.files.internal("sound/effect_" + effectName + ".mp3")); 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 //endregion
@ -53,6 +57,11 @@ public class Effect {
effectSound.play(MathUtils.random(0.7f, 1f), MathUtils.random(0.8f, 1.2f), 0); 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 //endregion
//region Static methods //region Static methods

View file

@ -182,6 +182,43 @@ public class Klooni extends Game {
theme.update(newTheme.getName()); 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 // Money related
public static void addMoneyFromScore(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,7 @@ import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap; import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.NinePatch; 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.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Array;
@ -198,6 +199,13 @@ public class Theme {
return colorIndex < 0 ? emptyCell : cells[colorIndex]; 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() { public void glClearBackground() {
Gdx.gl.glClearColor(background.r, background.g, background.b, background.a); Gdx.gl.glClearColor(background.r, background.g, background.b, background.a);
} }

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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
}