Compare commits
2 Commits
a9512b4af8
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b50ac3ef44 | ||
|
|
41fa146b12 |
@@ -1,5 +1,6 @@
|
||||
package bzh.risotto;
|
||||
|
||||
import bzh.risotto.core.Game;
|
||||
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
|
||||
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package bzh.risotto;
|
||||
package bzh.risotto.core;
|
||||
|
||||
import bzh.risotto.utils.Timer;
|
||||
import com.badlogic.gdx.ApplicationListener;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input;
|
||||
@@ -79,6 +80,9 @@ public class Game implements ApplicationListener {
|
||||
}
|
||||
|
||||
private void move() {
|
||||
|
||||
minesweeper.update();
|
||||
|
||||
}
|
||||
|
||||
private void draw() {
|
||||
@@ -1,4 +1,4 @@
|
||||
package bzh.risotto;
|
||||
package bzh.risotto.core;
|
||||
|
||||
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
@@ -1,5 +1,7 @@
|
||||
package bzh.risotto;
|
||||
package bzh.risotto.core;
|
||||
|
||||
import bzh.risotto.graphics.Animation;
|
||||
import bzh.risotto.utils.Utils;
|
||||
import bzh.risotto.tilemap.Tile;
|
||||
import bzh.risotto.tilemap.TileMap;
|
||||
import bzh.risotto.tilemap.TileSet;
|
||||
@@ -41,6 +43,8 @@ public class Minesweeper {
|
||||
private int nbLeftOverMines;
|
||||
private int markedMines;
|
||||
|
||||
private Animation explosion;
|
||||
|
||||
private final Logger logger = LogManager.getLogger();
|
||||
|
||||
/**
|
||||
@@ -54,19 +58,21 @@ public class Minesweeper {
|
||||
|
||||
TileSet tileSet = new TileSet("tileset.png", new Vector2(16,16), new Vector2(6,10));
|
||||
|
||||
this.minesMap = new MinesMap(new Vector2(10, 10), nbMines);
|
||||
|
||||
List<List<Integer>> map = loadMap(size);
|
||||
this.tileMap = new TileMap(tileSet, map);
|
||||
this.tileMap.setTile(5,0,0);
|
||||
|
||||
tileSet = new TileSet("ui.png", new Vector2(16,16), new Vector2(5,3));
|
||||
this.overlayMap = new TileMap(tileSet, Utils.emptyMap(size));
|
||||
|
||||
this.minesMap = new MinesMap(new Vector2(10, 10), nbMines);
|
||||
|
||||
this.nbMines = nbMines;
|
||||
this.nbLeftOverMines = nbMines;
|
||||
|
||||
this.explosion = new Animation("explosion.png", new Vector2(16, 16), 5, 0.10);
|
||||
loadConstArrays();
|
||||
|
||||
digFirstTile();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,6 +100,26 @@ public class Minesweeper {
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dig the first tile to start the game on an empty tile
|
||||
*/
|
||||
private void digFirstTile() {
|
||||
// dig empty tile to start game
|
||||
List<Vector2> emptyTiles = new ArrayList<>();
|
||||
Vector2 pos;
|
||||
for (int i = 0; i < size.x; i++) {
|
||||
for (int j = 0; j < size.y; j++) {
|
||||
pos = new Vector2(j, i);
|
||||
if (this.minesMap.getCell(pos) == 0) {
|
||||
emptyTiles.add(pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Vector2 digPos = emptyTiles.get((int) (Math.random() * emptyTiles.size()));
|
||||
dig(tileMap.toWorldMapCoord(digPos));
|
||||
}
|
||||
|
||||
/**
|
||||
* Dig the gamemap at the coord
|
||||
* convert grass tiles to dirt tiles, and make them visualy connect
|
||||
@@ -141,6 +167,34 @@ public class Minesweeper {
|
||||
|
||||
int nbMines = minesMap.getCell(coord);
|
||||
this.overlayMap.setTile((nbMines+5), (int) coord.x, (int) coord.y);
|
||||
|
||||
if (minesMap.getCell(coord) == 0) {
|
||||
digEmptyArround(coord);
|
||||
}
|
||||
}
|
||||
|
||||
private void digEmptyArround(Vector2 pos) {
|
||||
|
||||
for (int i = -1; i < 2; i ++) {
|
||||
for (int j = -1; j < 2; j ++) {
|
||||
Tile tile = null;
|
||||
|
||||
Vector2 tilePos = new Vector2(pos.x + j, pos.y + i);
|
||||
|
||||
// prevent out of bound error while checking border tiles
|
||||
try {
|
||||
tile = tileMap.getTile((int) tilePos.x, (int) tilePos.y);
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
|
||||
//logger.info("Out of world check", e);
|
||||
}
|
||||
|
||||
// if not digged and mines arround is 0, dig
|
||||
if (tile != null && dirtTiles.contains(tile.getId())) {
|
||||
dig(tileMap.toWorldMapCoord(tilePos));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void mark(Vector2 worldCoord) {
|
||||
@@ -173,8 +227,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 +248,10 @@ public class Minesweeper {
|
||||
}
|
||||
}
|
||||
|
||||
public void update() {
|
||||
this.explosion.update();
|
||||
}
|
||||
|
||||
/**
|
||||
* draw the map to the screen
|
||||
*
|
||||
@@ -200,6 +260,7 @@ public class Minesweeper {
|
||||
public void render(SpriteBatch spriteBatch) {
|
||||
this.tileMap.render(spriteBatch);
|
||||
this.overlayMap.render(spriteBatch);
|
||||
this.explosion.render(spriteBatch);
|
||||
}
|
||||
|
||||
/**
|
||||
94
src/main/java/bzh/risotto/graphics/Animation.java
Normal file
94
src/main/java/bzh/risotto/graphics/Animation.java
Normal file
@@ -0,0 +1,94 @@
|
||||
package bzh.risotto.graphics;
|
||||
|
||||
import bzh.risotto.utils.Timer;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -131,4 +131,22 @@ public class TileMap {
|
||||
return tileMapCoord;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* convert tilemap coord to world coord
|
||||
*
|
||||
* @param tileMapdCoord world coordinate to convert
|
||||
* @return tilemap coordinate
|
||||
*/
|
||||
public Vector2 toWorldMapCoord(Vector2 tileMapCoord) {
|
||||
|
||||
Vector2 worldCoord = new Vector2();
|
||||
Vector2 tileSize = tileSet.getTileSize();
|
||||
|
||||
worldCoord.x = (int) (tileMapCoord.x * tileSize.x);
|
||||
worldCoord.y = (int) (tileMapCoord.y * tileSize.y);
|
||||
|
||||
return worldCoord;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package bzh.risotto;
|
||||
package bzh.risotto.utils;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@@ -1,4 +1,4 @@
|
||||
package bzh.risotto;
|
||||
package bzh.risotto.utils;
|
||||
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
|
||||
Reference in New Issue
Block a user