Create an Effect class similar to Theme and make use of it
This commit is contained in:
parent
2c2b512742
commit
5a9a5c5a3b
4 changed files with 121 additions and 90 deletions
111
core/src/io/github/lonamiwebs/klooni/Effect.java
Normal file
111
core/src/io/github/lonamiwebs/klooni/Effect.java
Normal file
|
@ -0,0 +1,111 @@
|
|||
/*
|
||||
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;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.audio.Sound;
|
||||
import com.badlogic.gdx.math.MathUtils;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
|
||||
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.effects.WaterdropEffect;
|
||||
import io.github.lonamiwebs.klooni.game.Cell;
|
||||
|
||||
public class Effect {
|
||||
|
||||
//region Members
|
||||
|
||||
private final int effectId;
|
||||
private final Sound effectSound;
|
||||
|
||||
//endregion
|
||||
|
||||
//region Constructor
|
||||
|
||||
// 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"));
|
||||
}
|
||||
|
||||
//endregion
|
||||
|
||||
//region Public methods
|
||||
|
||||
public void playSound() {
|
||||
effectSound.play(MathUtils.random(0.7f, 1f), MathUtils.random(0.8f, 1.2f), 0);
|
||||
}
|
||||
|
||||
//endregion
|
||||
|
||||
//region Static methods
|
||||
|
||||
public static Effect[] getEffects() {
|
||||
// TODO Load effects
|
||||
return new Effect[] {
|
||||
new Effect("vanish"),
|
||||
new Effect("waterdrop"),
|
||||
new Effect("evaporate")
|
||||
};
|
||||
}
|
||||
|
||||
//endregion
|
||||
|
||||
//region Name <-> ID <-> IEffect
|
||||
|
||||
// Effects used when clearing a row
|
||||
public IEffect create(final Cell deadCell, final Vector2 culprit) {
|
||||
final IEffect effect;
|
||||
switch (effectId) {
|
||||
default:
|
||||
case 0:
|
||||
effect = new VanishEffect();
|
||||
break;
|
||||
case 1:
|
||||
effect = new WaterdropEffect();
|
||||
break;
|
||||
case 2:
|
||||
effect = new EvaporateEffect();
|
||||
break;
|
||||
}
|
||||
effect.setInfo(deadCell, culprit);
|
||||
return effect;
|
||||
}
|
||||
|
||||
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("waterdrop")) return 1;
|
||||
if (name.equals("evaporate")) return 2;
|
||||
return -1;
|
||||
}
|
||||
|
||||
//endregion
|
||||
|
||||
//region Disposal
|
||||
|
||||
void dispose() {
|
||||
effectSound.dispose();
|
||||
}
|
||||
|
||||
//endregion
|
||||
}
|
|
@ -22,16 +22,8 @@ import com.badlogic.gdx.Game;
|
|||
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.math.MathUtils;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
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.effects.WaterdropEffect;
|
||||
import io.github.lonamiwebs.klooni.game.Cell;
|
||||
import io.github.lonamiwebs.klooni.screens.MainMenuScreen;
|
||||
import io.github.lonamiwebs.klooni.screens.TransitionScreen;
|
||||
|
||||
|
@ -41,6 +33,8 @@ public class Klooni extends Game {
|
|||
|
||||
// TODO Not sure whether the theme should be static or not since it might load textures
|
||||
public static Theme theme;
|
||||
public Effect effect;
|
||||
|
||||
public Skin skin;
|
||||
|
||||
public ShareChallenge shareChallenge;
|
||||
|
@ -52,9 +46,6 @@ public class Klooni extends Game {
|
|||
public static final int GAME_HEIGHT = 680;
|
||||
public static final int GAME_WIDTH = 408;
|
||||
|
||||
private static int usedEffect;
|
||||
private Sound effectSound;
|
||||
|
||||
//endregion
|
||||
|
||||
//region Creation
|
||||
|
@ -83,9 +74,7 @@ public class Klooni extends Game {
|
|||
|
||||
Gdx.input.setCatchBackKey(true); // To show the pause menu
|
||||
setScreen(new MainMenuScreen(this));
|
||||
|
||||
usedEffect = effectNameToInt(getUsedEffect());
|
||||
setUsedEffect(null); // Update the effect sound
|
||||
effect = new Effect(prefs.getString("effectName"));
|
||||
}
|
||||
|
||||
//endregion
|
||||
|
@ -110,37 +99,7 @@ public class Klooni extends Game {
|
|||
super.dispose();
|
||||
skin.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) {
|
||||
// TODO Every effect should probably return a string corresponding to their sound
|
||||
// so there's only one switch around
|
||||
final IEffect effect;
|
||||
switch (usedEffect) {
|
||||
default:
|
||||
case 0:
|
||||
effect = new VanishEffect();
|
||||
break;
|
||||
case 1:
|
||||
effect = new WaterdropEffect();
|
||||
break;
|
||||
case 2:
|
||||
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);
|
||||
effect.dispose();
|
||||
}
|
||||
|
||||
//endregion
|
||||
|
@ -223,42 +182,6 @@ public class Klooni extends Game {
|
|||
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_waterdrop.mp3"));
|
||||
break;
|
||||
case 2:
|
||||
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("waterdrop")) return 1;
|
||||
if (name.equals("evaporate")) return 2;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Money related
|
||||
public static void addMoneyFromScore(int score) {
|
||||
setMoney(getRealMoney() + score * SCORE_TO_MONEY);
|
||||
|
|
|
@ -17,8 +17,6 @@
|
|||
*/
|
||||
package io.github.lonamiwebs.klooni.game;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.audio.Sound;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.math.MathUtils;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
|
@ -28,10 +26,9 @@ import java.io.DataInputStream;
|
|||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import io.github.lonamiwebs.klooni.Effect;
|
||||
import io.github.lonamiwebs.klooni.Klooni;
|
||||
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.serializer.BinSerializable;
|
||||
|
||||
// Represents the on screen board, with all the put cells
|
||||
|
@ -174,7 +171,7 @@ public class Board implements BinSerializable {
|
|||
//
|
||||
// If the piece is put on the top left corner, all the cells will be cleared.
|
||||
// If we first cleared the columns, then the rows wouldn't have been cleared.
|
||||
public int clearComplete() {
|
||||
public int clearComplete(final Effect effect) {
|
||||
int clearCount = 0;
|
||||
boolean[] clearedRows = new boolean[cellCount];
|
||||
boolean[] clearedCols = new boolean[cellCount];
|
||||
|
@ -207,7 +204,7 @@ public class Board implements BinSerializable {
|
|||
for (int i = 0; i < cellCount; ++i) {
|
||||
if (clearedRows[i]) {
|
||||
for (int j = 0; j < cellCount; ++j) {
|
||||
effects.add(Klooni.createEffect(cells[i][j], lastPutPiecePos));
|
||||
effects.add(effect.create(cells[i][j], lastPutPiecePos));
|
||||
cells[i][j].set(-1);
|
||||
}
|
||||
}
|
||||
|
@ -216,7 +213,7 @@ public class Board implements BinSerializable {
|
|||
for (int j = 0; j < cellCount; ++j) {
|
||||
if (clearedCols[j]) {
|
||||
for (int i = 0; i < cellCount; ++i) {
|
||||
effects.add(Klooni.createEffect(cells[i][j], lastPutPiecePos));
|
||||
effects.add(effect.create(cells[i][j], lastPutPiecePos));
|
||||
cells[i][j].set(-1);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -238,11 +238,11 @@ class GameScreen implements Screen, InputProcessor, BinSerializable {
|
|||
|
||||
if (result.onBoard) {
|
||||
scorer.addPieceScore(result.area);
|
||||
int bonus = scorer.addBoardScore(board.clearComplete(), board.cellCount);
|
||||
int bonus = scorer.addBoardScore(board.clearComplete(game.effect), board.cellCount);
|
||||
if (bonus > 0) {
|
||||
bonusParticleHandler.addBonus(result.pieceCenter, bonus);
|
||||
if (Klooni.soundsEnabled()) {
|
||||
game.playEffectSound();
|
||||
game.effect.playSound();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue