added diging system
This commit is contained in:
@@ -1,25 +1,39 @@
|
||||
package bzh.risotto;
|
||||
|
||||
import bzh.risotto.tilemap.Tile;
|
||||
import bzh.risotto.tilemap.TileMap;
|
||||
import bzh.risotto.tilemap.TileSet;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.maps.tiled.TiledMap;
|
||||
import com.badlogic.gdx.maps.tiled.TiledMapTileSet;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class GameMap {
|
||||
|
||||
private TileSet tileSet;
|
||||
private TileMap tileMap;
|
||||
|
||||
private List<Integer> DIRT_TILES;
|
||||
private Map<Integer, Integer> TILES_TYPE_TO_ID;
|
||||
|
||||
private Logger logger = LogManager.getLogger();
|
||||
|
||||
public GameMap() {
|
||||
|
||||
this.tileSet = new TileSet("tileset.png", new Vector2(16,16), new Vector2(6,6));
|
||||
List<List<Integer>> map = loadMap();
|
||||
this.tileMap = new TileMap(this.tileSet, map);
|
||||
this.tileMap.setTile(5,0,0);
|
||||
|
||||
loadConstArrays();
|
||||
}
|
||||
|
||||
private List<List<Integer>> loadMap() {
|
||||
@@ -41,8 +55,109 @@ public class GameMap {
|
||||
return res;
|
||||
}
|
||||
|
||||
public void dig(Vector2 worldCoord) {
|
||||
Vector2 coord = tileMap.toTileMapCoord(worldCoord);
|
||||
|
||||
// count tiles around
|
||||
int p = 0;
|
||||
int tileType = 0;
|
||||
for (int i = -1; i < 2; i ++) {
|
||||
for (int j = -1; j < 2; j ++) {
|
||||
Tile tile = tileMap.getTile((int) coord.x + j, (int) coord.y + i);
|
||||
|
||||
if (!DIRT_TILES.contains(tile.getId())) {
|
||||
tileType += (int) Math.pow(2, p);
|
||||
}
|
||||
|
||||
p++;
|
||||
}
|
||||
}
|
||||
|
||||
tileType -= 16;
|
||||
logger.debug(tileType);
|
||||
|
||||
int tileId = tileTypeToId(tileType);
|
||||
|
||||
tileMap.setTile(tileId, (int) coord.x, (int) coord.y);
|
||||
|
||||
}
|
||||
|
||||
public void render(SpriteBatch spriteBatch) {
|
||||
this.tileMap.render(spriteBatch);
|
||||
}
|
||||
|
||||
|
||||
private int tileTypeToId(int tileType) {
|
||||
switch (tileType) {
|
||||
case 0:
|
||||
return 5;
|
||||
case 2, 3, 6, 7, 71, 323, 327, 66, 322, 67, 258, 259, 263, 262, 326, 70: // down
|
||||
return 10; // down
|
||||
case 128, 192, 448, 384, 385, 449, 453, 193, 132, 196, 452, 133, 197, 388, 389 :
|
||||
return 22; // up
|
||||
case 8, 73, 72, 9, 329, 333, 328, 332, 77, 264, 268, 269, 12, 13:
|
||||
return 17; // right
|
||||
case 32, 288, 292, 36, 356, 357, 33, 97, 353, 96, 100, 101, 37, 293, 352 :
|
||||
return 15; // left
|
||||
case 160, 161, 165, 229, 224, 228, 164:
|
||||
return 21; // inner bot-left corner
|
||||
case 10, 74, 330, 334, 266, 270, 78, 14:
|
||||
return 11; // inner top-right corner
|
||||
case 34, 35, 99, 355, 290, 354, 291, 98:
|
||||
return 9; // inner top-left corner
|
||||
case 136, 137, 393, 397, 140, 141, 392, 396:
|
||||
return 23; // inner bot-right corner
|
||||
case 168, 169, 173, 172:
|
||||
return 34; // T up
|
||||
case 42, 106, 362, 298:
|
||||
return 29; // T down
|
||||
case 138, 394, 398, 142:
|
||||
return 35; // T right
|
||||
case 227, 162, 226, 163:
|
||||
return 28; // T left
|
||||
case 38, 102, 358, 359, 294, 295, 430, 39:
|
||||
return 6; // top-left corner
|
||||
case 204, 205, 461, 456, 457, 200, 201, 460:
|
||||
return 20; // bot-left corner
|
||||
case 416, 417, 481, 485, 420, 484, 421, 480:
|
||||
return 18; // bot-right corner
|
||||
case 11, 75, 79, 335, 267, 271, 351, 15:
|
||||
return 8; // top-right corner
|
||||
case 47, 111, 367, 303:
|
||||
return 7; // up-straight
|
||||
case 488, 496, 493, 492:
|
||||
return 19; // bot-straight
|
||||
case 203, 459, 463, 207:
|
||||
return 14; // left-stright
|
||||
case 422, 486, 487, 423:
|
||||
return 12; // left-stright
|
||||
case 239:
|
||||
return 32; // inner top-right
|
||||
case 431:
|
||||
return 33; // inner top-left
|
||||
case 491:
|
||||
return 26; // inner bot-left
|
||||
case 494:
|
||||
return 27; // inner bot-right
|
||||
case 495:
|
||||
return 13; // center full
|
||||
|
||||
case 170:
|
||||
return 16; // center cross
|
||||
default:
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
|
||||
private void loadConstArrays() {
|
||||
this.DIRT_TILES = new ArrayList<>();
|
||||
this.DIRT_TILES.add(0);
|
||||
this.DIRT_TILES.add(1);
|
||||
this.DIRT_TILES.add(2);
|
||||
this.DIRT_TILES.add(4);
|
||||
|
||||
this.TILES_TYPE_TO_ID = new HashMap<>();
|
||||
this.TILES_TYPE_TO_ID.put(1, 5);
|
||||
this.TILES_TYPE_TO_ID.put(2, 22);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,22 @@
|
||||
package bzh.risotto;
|
||||
|
||||
import com.badlogic.gdx.ApplicationListener;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input;
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.badlogic.gdx.utils.ScreenUtils;
|
||||
import com.badlogic.gdx.utils.viewport.FitViewport;
|
||||
import org.lwjgl.glfw.GLFWDeallocateCallback;
|
||||
|
||||
public class Minesweeper implements ApplicationListener {
|
||||
|
||||
private FitViewport viewport;
|
||||
private SpriteBatch spriteBatch;
|
||||
private OrthographicCamera camera;
|
||||
private Vector2 zoom;
|
||||
|
||||
private GameMap gameMap;
|
||||
|
||||
@@ -24,6 +29,8 @@ public class Minesweeper implements ApplicationListener {
|
||||
viewport = new FitViewport(width,height);
|
||||
spriteBatch = new SpriteBatch();
|
||||
|
||||
zoom = new Vector2(Gdx.graphics.getHeight() / height, Gdx.graphics.getWidth() / width);
|
||||
|
||||
gameMap = new GameMap();
|
||||
}
|
||||
|
||||
@@ -40,6 +47,11 @@ public class Minesweeper implements ApplicationListener {
|
||||
}
|
||||
|
||||
private void logic() {
|
||||
|
||||
if (Gdx.input.isButtonPressed(Input.Buttons.LEFT)) {
|
||||
Vector2 mousePos = getMouseWorldPos();
|
||||
gameMap.dig(mousePos);
|
||||
}
|
||||
}
|
||||
|
||||
private void move() {
|
||||
@@ -67,4 +79,14 @@ public class Minesweeper implements ApplicationListener {
|
||||
public void dispose() {
|
||||
|
||||
}
|
||||
|
||||
private Vector2 getMouseWorldPos() {
|
||||
float mouseX = Gdx.input.getX();
|
||||
float mouseY = Gdx.input.getY();
|
||||
Vector2 mouseWorldCoord = new Vector2(mouseX, mouseY);
|
||||
|
||||
mouseWorldCoord = viewport.unproject(mouseWorldCoord);
|
||||
|
||||
return mouseWorldCoord;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,9 @@ import com.badlogic.gdx.math.Vector2;
|
||||
*/
|
||||
public class Tile {
|
||||
|
||||
/** the id of the tile */
|
||||
private int id;
|
||||
|
||||
/** the texture of the tile */
|
||||
private final TextureRegion tileTexture;
|
||||
|
||||
@@ -17,10 +20,20 @@ public class Tile {
|
||||
*
|
||||
* @param tileTexture the texture of the tile
|
||||
*/
|
||||
public Tile(TextureRegion tileTexture) {
|
||||
public Tile(int id, TextureRegion tileTexture) {
|
||||
this.id = id;
|
||||
this.tileTexture = tileTexture;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the id of the tile
|
||||
*
|
||||
* @return the id of the tile
|
||||
*/
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
/**
|
||||
* render the tile
|
||||
*/
|
||||
|
||||
@@ -63,6 +63,39 @@ public class TileMap {
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the tile at position x,y
|
||||
*
|
||||
* @param x the x coordinate of the tile
|
||||
* @param y the y coordinate of the tile
|
||||
* @return the tile at position x,y
|
||||
*/
|
||||
public Tile getTile(int x, int y) {
|
||||
return this.tileList.get(y).get(x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the tile id at position x,y
|
||||
*
|
||||
* @param x the x coordinate of the tile
|
||||
* @param y the y coordinate of the tile
|
||||
* @return the tile id at position x,y
|
||||
*/
|
||||
public int getTileId(int x, int y) {
|
||||
return this.tileList.get(y).get(x).getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the tile at position x,y to id
|
||||
*
|
||||
* @param id the id of the new tile
|
||||
* @param x the x coordinate of the tile
|
||||
* @param y the y coordinate of the tile
|
||||
*/
|
||||
public void setTile(int id, int x, int y) {
|
||||
this.tileList.get(y).set(x, tileSet.getTile(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* render the map on the screen
|
||||
*/
|
||||
@@ -80,4 +113,16 @@ public class TileMap {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Vector2 toTileMapCoord(Vector2 worldCoord) {
|
||||
|
||||
Vector2 tileMapCoord = new Vector2();
|
||||
Vector2 tileSize = tileSet.getTileSize();
|
||||
|
||||
tileMapCoord.x = (int) (worldCoord.x / (tileSize.x));
|
||||
tileMapCoord.y = (int) (worldCoord.y / (tileSize.y));
|
||||
|
||||
return tileMapCoord;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ public class TileSet {
|
||||
Vector2 p = new Vector2(80,80);
|
||||
|
||||
tile = new Tile(
|
||||
|
||||
(int) (i*tilesetSize.x + j),
|
||||
new TextureRegion(
|
||||
this.tilesTexture,
|
||||
j*(int) tileSize.x,
|
||||
@@ -69,10 +69,6 @@ public class TileSet {
|
||||
)
|
||||
);
|
||||
|
||||
logger.debug("------");
|
||||
logger.debug("x:" + j*tileSize.x);
|
||||
logger.debug("y:" + i*tileSize.y);
|
||||
logger.debug(tileSize);
|
||||
tileArray.add(tile);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user