Don't hardcode the used effect, add a new sound for Evaporate one

This commit is contained in:
Lonami Exo 2017-07-09 14:12:59 +02:00
parent 79fac8c328
commit 5cadc74e3e
5 changed files with 81 additions and 23 deletions

Binary file not shown.

View file

@ -22,11 +22,15 @@ import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences; import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.Screen; import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.graphics.g2d.NinePatch; import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import io.github.lonamiwebs.klooni.effects.EvaporateEffect;
import io.github.lonamiwebs.klooni.effects.IEffect;
import io.github.lonamiwebs.klooni.effects.VanishEffect;
import io.github.lonamiwebs.klooni.game.Cell;
import io.github.lonamiwebs.klooni.screens.MainMenuScreen; import io.github.lonamiwebs.klooni.screens.MainMenuScreen;
import io.github.lonamiwebs.klooni.screens.TransitionScreen; import io.github.lonamiwebs.klooni.screens.TransitionScreen;
@ -47,6 +51,9 @@ public class Klooni extends Game {
public static final int GAME_HEIGHT = 680; public static final int GAME_HEIGHT = 680;
public static final int GAME_WIDTH = 408; public static final int GAME_WIDTH = 408;
private static int usedEffect;
private Sound effectSound;
//endregion //endregion
//region Creation //region Creation
@ -75,6 +82,9 @@ public class Klooni extends Game {
Gdx.input.setCatchBackKey(true); // To show the pause menu Gdx.input.setCatchBackKey(true); // To show the pause menu
setScreen(new MainMenuScreen(this)); setScreen(new MainMenuScreen(this));
usedEffect = effectNameToInt(getUsedEffect());
setUsedEffect(null); // Update the effect sound
} }
//endregion //endregion
@ -99,6 +109,32 @@ public class Klooni extends Game {
super.dispose(); super.dispose();
skin.dispose(); skin.dispose();
theme.dispose(); theme.dispose();
if (effectSound != null)
effectSound.dispose();
}
//endregion
//region Effects
// Effects used when clearing a row
public static IEffect createEffect(final Cell deadCell, final Vector2 culprit) {
final IEffect effect;
switch (usedEffect) {
default:
case 0:
effect = new VanishEffect();
break;
case 1:
effect = new EvaporateEffect();
break;
}
effect.setInfo(deadCell, culprit);
return effect;
}
public void playEffectSound() {
effectSound.play(MathUtils.random(0.7f, 1f), MathUtils.random(0.8f, 1.2f), 0);
} }
//endregion //endregion
@ -181,6 +217,38 @@ public class Klooni extends Game {
theme.update(newTheme.getName()); theme.update(newTheme.getName());
} }
// Effects related
public static String getUsedEffect() {
return prefs.getString("effectName");
}
public void setUsedEffect(final String name) {
if (name != null)
prefs.putString("effectName", name).flush();
if (effectSound != null)
effectSound.dispose();
switch (effectNameToInt(getUsedEffect())) {
default:
case 0:
effectSound = Gdx.audio.newSound(Gdx.files.internal("sound/effect_vanish.mp3"));
break;
case 1:
effectSound = Gdx.audio.newSound(Gdx.files.internal("sound/effect_evaporate.mp3"));
break;
}
}
private static int effectNameToInt(final String name) {
// String comparision is more expensive compared to a single integer one,
// and when creating instances of a lot of effects it's better if we can
// save some processor cycles.
if (name.equals("vanish")) return 0;
if (name.equals("evaporate")) return 1;
return -1;
}
// 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

@ -46,7 +46,6 @@ public class Board implements BinSerializable {
private final Array<IEffect> effects; // Particle effects once they vanish private final Array<IEffect> effects; // Particle effects once they vanish
final Vector2 pos; final Vector2 pos;
private final Sound stripClearSound;
// Used to animate cleared cells vanishing // Used to animate cleared cells vanishing
private final Vector2 lastPutPiecePos; private final Vector2 lastPutPiecePos;
@ -58,8 +57,6 @@ public class Board implements BinSerializable {
public Board(final GameLayout layout, int cellCount) { public Board(final GameLayout layout, int cellCount) {
this.cellCount = cellCount; this.cellCount = cellCount;
stripClearSound = Gdx.audio.newSound(Gdx.files.internal("sound/strip_clear.mp3"));
lastPutPiecePos = new Vector2(); lastPutPiecePos = new Vector2();
pos = new Vector2(); pos = new Vector2();
effects = new Array<IEffect>(32); // 32 capacity for 3 rows (most common) effects = new Array<IEffect>(32); // 32 capacity for 3 rows (most common)
@ -206,16 +203,11 @@ public class Board implements BinSerializable {
clearCount++; clearCount++;
} }
if (clearCount > 0) { if (clearCount > 0) {
float pan = 0;
// Do clear those rows and columns // Do clear those rows and columns
// TODO Don't always use "vanish effect"
for (int i = 0; i < cellCount; ++i) { for (int i = 0; i < cellCount; ++i) {
if (clearedRows[i]) { if (clearedRows[i]) {
for (int j = 0; j < cellCount; ++j) { for (int j = 0; j < cellCount; ++j) {
IEffect effect = new EvaporateEffect(); effects.add(Klooni.createEffect(cells[i][j], lastPutPiecePos));
effect.setInfo(cells[i][j], lastPutPiecePos);
effects.add(effect);
cells[i][j].set(-1); cells[i][j].set(-1);
} }
} }
@ -223,21 +215,12 @@ public class Board implements BinSerializable {
for (int j = 0; j < cellCount; ++j) { for (int j = 0; j < cellCount; ++j) {
if (clearedCols[j]) { if (clearedCols[j]) {
pan += 2f * (j - cellCount / 2) / (float)cellCount;
for (int i = 0; i < cellCount; ++i) { for (int i = 0; i < cellCount; ++i) {
IEffect effect = new EvaporateEffect(); effects.add(Klooni.createEffect(cells[i][j], lastPutPiecePos));
effect.setInfo(cells[i][j], lastPutPiecePos);
effects.add(effect);
cells[i][j].set(-1); cells[i][j].set(-1);
} }
} }
} }
if (Klooni.soundsEnabled()) {
pan = MathUtils.clamp(pan, -1, 1);
stripClearSound.play(
MathUtils.random(0.7f, 1f), MathUtils.random(0.8f, 1.2f), pan);
}
} }
return clearCount; return clearCount;

View file

@ -25,6 +25,7 @@ import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
@ -47,6 +48,7 @@ class GameScreen implements Screen, InputProcessor, BinSerializable {
//region Members //region Members
private final Klooni game;
private final BaseScorer scorer; private final BaseScorer scorer;
private final BonusParticleHandler bonusParticleHandler; private final BonusParticleHandler bonusParticleHandler;
@ -93,6 +95,7 @@ class GameScreen implements Screen, InputProcessor, BinSerializable {
GameScreen(final Klooni game, final int gameMode, final boolean loadSave) { GameScreen(final Klooni game, final int gameMode, final boolean loadSave) {
batch = new SpriteBatch(); batch = new SpriteBatch();
this.game = game;
this.gameMode = gameMode; this.gameMode = gameMode;
final GameLayout layout = new GameLayout(); final GameLayout layout = new GameLayout();
@ -236,8 +239,12 @@ class GameScreen implements Screen, InputProcessor, BinSerializable {
if (result.onBoard) { if (result.onBoard) {
scorer.addPieceScore(result.area); scorer.addPieceScore(result.area);
int bonus = scorer.addBoardScore(board.clearComplete(), board.cellCount); int bonus = scorer.addBoardScore(board.clearComplete(), board.cellCount);
if (bonus > 0) if (bonus > 0) {
bonusParticleHandler.addBonus(result.pieceCenter, bonus); bonusParticleHandler.addBonus(result.pieceCenter, bonus);
if (Klooni.soundsEnabled()) {
game.playEffectSound();
}
}
// After the piece was put, check if it's game over // After the piece was put, check if it's game over
if (isGameOver()) { if (isGameOver()) {