Skip to content

Commit

Permalink
Refactor effects to use the Factory pattern (#43)
Browse files Browse the repository at this point in the history
This shows several advantages:
- No need for several switchs with hardcoded names.
- Easier and better control per theme (name, price…).
- Easier to keep track of available effects and their order.
  • Loading branch information
andidevi authored and Lonami committed Feb 5, 2018
1 parent 3429eda commit d7db9b8
Show file tree
Hide file tree
Showing 19 changed files with 694 additions and 516 deletions.
142 changes: 0 additions & 142 deletions core/src/io/github/lonamiwebs/klooni/Effect.java

This file was deleted.

86 changes: 70 additions & 16 deletions core/src/io/github/lonamiwebs/klooni/Klooni.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,59 @@
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;

import java.util.HashMap;
import java.util.Map;

import io.github.lonamiwebs.klooni.effects.EvaporateEffectFactory;
import io.github.lonamiwebs.klooni.effects.ExplodeEffectFactory;
import io.github.lonamiwebs.klooni.effects.SpinEffectFactory;
import io.github.lonamiwebs.klooni.effects.VanishEffectFatory;
import io.github.lonamiwebs.klooni.effects.WaterdropEffectFactory;
import io.github.lonamiwebs.klooni.interfaces.IEffectFactory;
import io.github.lonamiwebs.klooni.screens.MainMenuScreen;
import io.github.lonamiwebs.klooni.screens.TransitionScreen;

public class Klooni extends Game {
// region Effects

// ordered list of effects. index 0 will get default if VanishEffectFactory is removed from list
public final static IEffectFactory[] EFFECTS = {
new VanishEffectFatory(),
new WaterdropEffectFactory(),
new EvaporateEffectFactory(),
new SpinEffectFactory(),
new ExplodeEffectFactory(),
};


private Map<String, Sound> effectSounds;

private void loadEffectSound(final String effectName) {
FileHandle soundFile = Gdx.files.internal("sound/effect_" + effectName + ".mp3");
if (!soundFile.exists())
soundFile = Gdx.files.internal("sound/effect_vanish.mp3");

effectSounds.put(effectName, Gdx.audio.newSound(soundFile));
}

public void playEffectSound() {
effectSounds.get(effect.getName())
.play(MathUtils.random(0.7f, 1f), MathUtils.random(0.8f, 1.2f), 0);
}

// endregion

//region Members

// TODO Not sure whether the theme should be static or not since it might load textures
// FIXME theme should NOT be static as it might load textures which will expose it to the race condition iff GDX got initialized before or not
public static Theme theme;
public Effect effect;
public IEffectFactory effect;


public Skin skin;

Expand Down Expand Up @@ -74,13 +115,21 @@ public void create() {

Gdx.input.setCatchBackKey(true); // To show the pause menu
setScreen(new MainMenuScreen(this));
effect = new Effect(prefs.getString("effectName", "vanish"));
String effectName = prefs.getString("effectName", "vanish");
effectSounds = new HashMap<String, Sound>(EFFECTS.length);
effect = EFFECTS[0];
for (IEffectFactory e : EFFECTS) {
loadEffectSound(e.getName());
if (e.getName().equals(effectName)) {
effect = e;
}
}
}

//endregion

//region Screen

// TransitionScreen will also dispose by default the previous screen
public void transitionTo(Screen screen) {
transitionTo(screen, true);
Expand All @@ -99,7 +148,12 @@ public void dispose() {
super.dispose();
skin.dispose();
theme.dispose();
effect.dispose();
if (effectSounds != null) {
for (Sound s : effectSounds.values()) {
s.dispose();
}
effectSounds = null;
}
}

//endregion
Expand Down Expand Up @@ -183,40 +237,40 @@ public static void updateTheme(Theme newTheme) {
}

// Effects related
public static boolean isEffectBought(Effect effect) {
if (effect.price == 0)
public static boolean isEffectBought(IEffectFactory effect) {
if (effect.getPrice() == 0)
return true;

String[] effects = prefs.getString("boughtEffects", "").split(":");
for (String e : effects)
if (e.equals(effect.name))
if (e.equals(effect.getName()))
return true;

return false;
}

public static boolean buyEffect(Effect effect) {
public static boolean buyEffect(IEffectFactory effect) {
final float money = getRealMoney();
if (effect.price > money)
if (effect.getPrice() > money)
return false;

setMoney(money - effect.price);
setMoney(money - effect.getPrice());

String bought = prefs.getString("boughtEffects", "");
if (bought.equals(""))
bought = effect.name;
bought = effect.getName();
else
bought += ":" + effect.name;
bought += ":" + effect.getName();

prefs.putString("boughtEffects", bought);

return true;
}

public void updateEffect(Effect newEffect) {
prefs.putString("effectName", newEffect.name).flush();
public void updateEffect(IEffectFactory newEffect) {
prefs.putString("effectName", newEffect.getName()).flush();
// Create a new effect, since the one passed through the parameter may dispose later
effect = new Effect(newEffect.name);
effect = newEffect;
}

// Money related
Expand Down
1 change: 1 addition & 0 deletions core/src/io/github/lonamiwebs/klooni/SkinLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class SkinLoader {

private final static float bestMultiplier;

// FIXME this static code is exposed to a race condition and will fail if called class gets loaded before execution of Klooni.create
static {
// Use the height to determine the best match
// We cannot use a size which is over the device height,
Expand Down
14 changes: 7 additions & 7 deletions core/src/io/github/lonamiwebs/klooni/actors/EffectCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Rectangle;

import io.github.lonamiwebs.klooni.Effect;
import io.github.lonamiwebs.klooni.Klooni;
import io.github.lonamiwebs.klooni.Theme;
import io.github.lonamiwebs.klooni.game.Board;
import io.github.lonamiwebs.klooni.game.GameLayout;
import io.github.lonamiwebs.klooni.game.Piece;
import io.github.lonamiwebs.klooni.interfaces.IEffectFactory;

// Card-like actor used to display information about a given theme
public class EffectCard extends ShopCard {

//region Members

private final Effect effect;
private final IEffectFactory effect;
private final Board board;

// We want to create an effect from the beginning
Expand All @@ -45,7 +45,7 @@ public class EffectCard extends ShopCard {

//region Constructor

public EffectCard(final Klooni game, final GameLayout layout, final Effect effect) {
public EffectCard(final Klooni game, final GameLayout layout, final IEffectFactory effect) {
super(game, layout, effect.getDisplay(), Klooni.theme.background);
background = Theme.getBlankTexture();
this.effect = effect;
Expand Down Expand Up @@ -118,12 +118,12 @@ public boolean showcase(Batch batch, float yDisplacement) {

@Override
public void usedItemUpdated() {
if (game.effect.name.equals(effect.name))
if (game.effect.getName().equals(effect.getName()))
priceLabel.setText("currently used");
else if (Klooni.isEffectBought(effect))
priceLabel.setText("bought");
else
priceLabel.setText("buy for "+effect.price);
priceLabel.setText("buy for "+effect.getPrice());
}

@Override
Expand All @@ -139,12 +139,12 @@ public boolean isBought() {

@Override
public boolean isUsed() {
return game.effect.name.equals(effect.name);
return game.effect.getName().equals(effect.getName());
}

@Override
public float getPrice() {
return effect.price;
return effect.getPrice();
}

@Override
Expand Down
Loading

0 comments on commit d7db9b8

Please sign in to comment.