added explosion animation
This commit is contained in:
93
src/main/java/bzh/risotto/Animation.java
Normal file
93
src/main/java/bzh/risotto/Animation.java
Normal file
@@ -0,0 +1,93 @@
|
||||
package bzh.risotto;
|
||||
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Animation {
|
||||
|
||||
List<TextureRegion> imagesArray;
|
||||
|
||||
private final Vector2 textureSize;
|
||||
private final int nbFrames;
|
||||
private final double frameTime;
|
||||
|
||||
private boolean playing;
|
||||
private int curFrame;
|
||||
|
||||
private Timer imageTime;
|
||||
|
||||
private Vector2 pos;
|
||||
private boolean looping;
|
||||
|
||||
public Animation(String texturePath, Vector2 textureSize, int nbFrames, double frameTime) {
|
||||
|
||||
this.textureSize = textureSize;
|
||||
this.nbFrames = nbFrames;
|
||||
this.frameTime = frameTime;
|
||||
|
||||
this.playing = false;
|
||||
this.curFrame = -1;
|
||||
|
||||
this.imageTime = new Timer(frameTime);
|
||||
loadImages(texturePath);
|
||||
}
|
||||
|
||||
private void loadImages(String texturePath) {
|
||||
|
||||
this.imagesArray = new ArrayList<>();
|
||||
|
||||
Texture images = new Texture(texturePath);
|
||||
TextureRegion image;
|
||||
for (int i = 0; i < nbFrames; i++) {
|
||||
|
||||
image = new TextureRegion(images, i * (int) textureSize.x, i, (int) textureSize.x, (int) textureSize.y);
|
||||
|
||||
imagesArray.add(image);
|
||||
}
|
||||
}
|
||||
|
||||
public void play(Vector2 pos, boolean looping) {
|
||||
|
||||
if (playing != true) {
|
||||
this.playing = true;
|
||||
this.imageTime.start();
|
||||
this.curFrame = 1;
|
||||
this.pos = pos;
|
||||
this.looping = looping;
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
System.out.println("Stopped");
|
||||
|
||||
this.playing = false;
|
||||
this.imageTime.stop();
|
||||
this.curFrame = -1;
|
||||
}
|
||||
|
||||
public void update() {
|
||||
|
||||
if (this.playing && this.imageTime.isFinished()) {
|
||||
this.curFrame ++;
|
||||
|
||||
if (looping) {
|
||||
this.curFrame %= this.nbFrames;
|
||||
} else if (this.curFrame >= this.nbFrames) {
|
||||
this.stop();
|
||||
}
|
||||
this.imageTime.start();
|
||||
}
|
||||
}
|
||||
|
||||
public void render(SpriteBatch spriteBatch) {
|
||||
if (this.playing) {
|
||||
spriteBatch.draw(this.imagesArray.get(this.curFrame), (int) this.pos.x, (int) this.pos.y);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -79,6 +79,9 @@ public class Game implements ApplicationListener {
|
||||
}
|
||||
|
||||
private void move() {
|
||||
|
||||
minesweeper.update();
|
||||
|
||||
}
|
||||
|
||||
private void draw() {
|
||||
|
||||
@@ -41,6 +41,8 @@ public class Minesweeper {
|
||||
private int nbLeftOverMines;
|
||||
private int markedMines;
|
||||
|
||||
private Animation explosion;
|
||||
|
||||
private final Logger logger = LogManager.getLogger();
|
||||
|
||||
/**
|
||||
@@ -66,6 +68,8 @@ public class Minesweeper {
|
||||
this.nbMines = nbMines;
|
||||
this.nbLeftOverMines = nbMines;
|
||||
|
||||
this.explosion = new Animation("explosion.png", new Vector2(16, 16), 5, 0.10);
|
||||
|
||||
loadConstArrays();
|
||||
}
|
||||
|
||||
@@ -173,8 +177,10 @@ public class Minesweeper {
|
||||
|
||||
public void endGame() {
|
||||
|
||||
if (minesMap.getCell(lastTileDigged) == -1) {
|
||||
if (lastTileDigged != null && minesMap.getCell(lastTileDigged) == -1) {
|
||||
explosion.play(new Vector2(lastTileDigged.x*16, lastTileDigged.y*16), false);
|
||||
revealMines();
|
||||
lastTileDigged = null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -192,6 +198,10 @@ public class Minesweeper {
|
||||
}
|
||||
}
|
||||
|
||||
public void update() {
|
||||
this.explosion.update();
|
||||
}
|
||||
|
||||
/**
|
||||
* draw the map to the screen
|
||||
*
|
||||
@@ -200,6 +210,7 @@ public class Minesweeper {
|
||||
public void render(SpriteBatch spriteBatch) {
|
||||
this.tileMap.render(spriteBatch);
|
||||
this.overlayMap.render(spriteBatch);
|
||||
this.explosion.render(spriteBatch);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user