Add a new Evaporate effect when clearing cells

This commit is contained in:
Lonami Exo 2017-07-08 21:33:29 +02:00
parent 0713d4a5de
commit ccec6f7b81
2 changed files with 75 additions and 2 deletions

View file

@ -0,0 +1,72 @@
package io.github.lonamiwebs.klooni.effects;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import io.github.lonamiwebs.klooni.game.Cell;
public class EvaporateEffect implements IEffect {
private Vector2 pos;
private float originalX;
private float size;
private Color vanishColor;
private float vanishSize;
private float vanishElapsed;
private float driftMagnitude;
private float randomOffset;
private static final float UP_SPEED = 100.0f;
private static final float LIFETIME = 3.0f;
private static final float INV_LIFETIME = 1.0f / 3.0f;
public EvaporateEffect() {
vanishElapsed = Float.POSITIVE_INFINITY;
}
@Override
public void setInfo(Cell deadCell, Vector2 culprit) {
pos = deadCell.pos.cpy();
originalX = pos.x;
size = deadCell.size;
vanishSize = deadCell.size;
vanishColor = deadCell.getColorCopy();
driftMagnitude = Gdx.graphics.getWidth() * 0.05f;
vanishElapsed = 0;
randomOffset = MathUtils.random(MathUtils.PI2);
}
@Override
public void draw(SpriteBatch batch) {
vanishElapsed += Gdx.graphics.getDeltaTime();
// Update the size as we fade away
final float progress = vanishElapsed * INV_LIFETIME;
vanishSize = Interpolation.fade.apply(size, 0, progress);
// Fade away depending on the time
vanishColor.set(vanishColor.r, vanishColor.g, vanishColor.b, 1.0f - progress);
// Ghostly fade upwards, by doing a lerp from our current position to the wavy one
pos.x = MathUtils.lerp(
pos.x,
originalX + MathUtils.sin(randomOffset + vanishElapsed * 3f) * driftMagnitude,
0.3f
);
pos.y += UP_SPEED * Gdx.graphics.getDeltaTime();
Cell.draw(vanishColor, batch, pos.x, pos.y, vanishSize);
}
@Override
public boolean isDone() {
return vanishElapsed > LIFETIME;
}
}

View file

@ -29,6 +29,7 @@ import java.io.DataOutputStream;
import java.io.IOException;
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;
@ -212,7 +213,7 @@ public class Board implements BinSerializable {
for (int i = 0; i < cellCount; ++i) {
if (clearedRows[i]) {
for (int j = 0; j < cellCount; ++j) {
VanishEffect effect = new VanishEffect();
IEffect effect = new EvaporateEffect();
effect.setInfo(cells[i][j], lastPutPiecePos);
effects.add(effect);
cells[i][j].set(-1);
@ -224,7 +225,7 @@ public class Board implements BinSerializable {
if (clearedCols[j]) {
pan += 2f * (j - cellCount / 2) / (float)cellCount;
for (int i = 0; i < cellCount; ++i) {
VanishEffect effect = new VanishEffect();
IEffect effect = new EvaporateEffect();
effect.setInfo(cells[i][j], lastPutPiecePos);
effects.add(effect);
cells[i][j].set(-1);