refactor map handling and improve tile initialization logic
This commit is contained in:
@@ -1,13 +1,23 @@
|
||||
package de.miaurizius.jgame2d.core;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class Boot {
|
||||
|
||||
public static final Logger logger = Logger.getLogger("JDGame2D");
|
||||
public static JFrame window;
|
||||
public static GamePanel gamePanel = new GamePanel();
|
||||
public static GamePanel gamePanel;
|
||||
|
||||
static {
|
||||
try {
|
||||
gamePanel = new GamePanel();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
static void main() {
|
||||
generateWindow();
|
||||
|
||||
@@ -15,6 +15,8 @@ import javax.sound.sampled.FloatControl;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.logging.Level;
|
||||
@@ -38,8 +40,8 @@ public class GamePanel extends JPanel implements Runnable {
|
||||
public Graphics2D fg2;
|
||||
|
||||
// WORLD SETTINGS
|
||||
public final int maxWorldCol = 50;
|
||||
public final int maxWorldRow = 50;
|
||||
public int maxWorldCol = 50;
|
||||
public int maxWorldRow = 50;
|
||||
public Map currentMap = Map.OVERWORLD;
|
||||
|
||||
//FPS
|
||||
@@ -73,7 +75,7 @@ public class GamePanel extends JPanel implements Runnable {
|
||||
// GAME STATE
|
||||
public GameState gameState;
|
||||
|
||||
public GamePanel() {
|
||||
public GamePanel() throws IOException {
|
||||
this.setPreferredSize(new Dimension(screenWidth, screenHeight));
|
||||
this.setBackground(Color.black);
|
||||
this.setDoubleBuffered(true);
|
||||
|
||||
@@ -2,7 +2,7 @@ package de.miaurizius.jgame2d.core.enums;
|
||||
|
||||
public enum Map {
|
||||
|
||||
OVERWORLD("world3", 0),
|
||||
OVERWORLD("worldmap", 0),
|
||||
HUT("hut", 1);
|
||||
|
||||
private final String name;
|
||||
|
||||
@@ -37,18 +37,8 @@ public class AssetSetter {
|
||||
|
||||
panel.obj[Map.OVERWORLD.getIndex()][i] = new ChestObj(panel, new KeyObj(panel));
|
||||
panel.obj[Map.OVERWORLD.getIndex()][i].worldX = panel.tileSize*30;
|
||||
panel.obj[Map.OVERWORLD.getIndex()][i].worldY = panel.tileSize*28;
|
||||
panel.obj[Map.OVERWORLD.getIndex()][i].worldY = panel.tileSize*29;
|
||||
i++;
|
||||
|
||||
// panel.obj[Map.OVERWORLD.getIndex()][i] = new LanternObj(panel);
|
||||
// panel.obj[Map.OVERWORLD.getIndex()][i].worldX = panel.tileSize*18;
|
||||
// panel.obj[Map.OVERWORLD.getIndex()][i].worldY = panel.tileSize*20;
|
||||
// i++;
|
||||
//
|
||||
// panel.obj[Map.OVERWORLD.getIndex()][i] = new TentObj(panel);
|
||||
// panel.obj[Map.OVERWORLD.getIndex()][i].worldX = panel.tileSize*19;
|
||||
// panel.obj[Map.OVERWORLD.getIndex()][i].worldY = panel.tileSize*20;
|
||||
// i++;
|
||||
}
|
||||
|
||||
public void setNPC() {
|
||||
|
||||
@@ -8,6 +8,7 @@ import de.miaurizius.jgame2d.core.enums.Map;
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class TileManager {
|
||||
@@ -15,19 +16,52 @@ public class TileManager {
|
||||
GamePanel panel;
|
||||
public Tile[] tile;
|
||||
public int[][][] mapTileNum;
|
||||
ArrayList<String> filesNames = new ArrayList<>();
|
||||
ArrayList<String> collisionStatus = new ArrayList<>();
|
||||
|
||||
public TileManager(GamePanel panel) {
|
||||
public TileManager(GamePanel panel) throws IOException {
|
||||
this.panel = panel;
|
||||
tile = new Tile[50];
|
||||
mapTileNum = new int[Map.values().length][panel.maxWorldCol][panel.maxWorldRow];
|
||||
|
||||
InputStream is = new FileInputStream("assets/maps/tiledata");
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(is));
|
||||
|
||||
// READ TILE DATA FROM FILE
|
||||
try {
|
||||
String line;
|
||||
while((line = br.readLine()) != null) {
|
||||
filesNames.add(line);
|
||||
collisionStatus.add(br.readLine());
|
||||
}
|
||||
br.close();
|
||||
} catch(IOException e) {
|
||||
Boot.logger.log(Level.SEVERE, "Could not read tile data file", e);
|
||||
}
|
||||
|
||||
tile = new Tile[filesNames.size()];
|
||||
getTileImage();
|
||||
|
||||
// GETTING WORLD SIZES
|
||||
is = new FileInputStream("assets/maps/editoredmap.map");
|
||||
br = new BufferedReader(new InputStreamReader(is));
|
||||
try {
|
||||
String line = br.readLine();
|
||||
String maxTile[] = line.split(" ");
|
||||
panel.maxWorldCol = maxTile.length;
|
||||
panel.maxWorldRow = maxTile.length;
|
||||
mapTileNum = new int[Map.values().length][panel.maxWorldCol][panel.maxWorldRow];
|
||||
|
||||
br.close();
|
||||
} catch(IOException e) {
|
||||
Boot.logger.log(Level.SEVERE, "Could not read map size data", e);
|
||||
}
|
||||
|
||||
for(Map m : Map.values()) loadMap(m);
|
||||
}
|
||||
|
||||
public void initializeTile(int i, String name, boolean col) {
|
||||
try {
|
||||
tile[i] = new Tile();
|
||||
tile[i].image = ImageIO.read(new FileInputStream("assets/tiles/" + name + ".png"));
|
||||
tile[i].image = ImageIO.read(new FileInputStream("assets/tiles/" + name));
|
||||
tile[i].collision = col;
|
||||
} catch(IOException e) {
|
||||
Boot.logger.log(Level.SEVERE, "Could not load tile", e);
|
||||
@@ -35,61 +69,14 @@ public class TileManager {
|
||||
}
|
||||
|
||||
public void getTileImage() {
|
||||
// PLACEHOLDER
|
||||
initializeTile(0, "grass00", false);
|
||||
initializeTile(2, "grass00", false);
|
||||
initializeTile(3, "grass00", false);
|
||||
initializeTile(4, "grass00", false);
|
||||
initializeTile(5, "grass00", false);
|
||||
initializeTile(6, "grass00", false);
|
||||
initializeTile(7, "grass00", false);
|
||||
initializeTile(8, "grass00", false);
|
||||
initializeTile(9, "grass00", false);
|
||||
|
||||
// GRASS
|
||||
initializeTile(10, "grass00", false);
|
||||
initializeTile(11, "grass01", false);
|
||||
|
||||
// WATER
|
||||
initializeTile(12, "water00", true);
|
||||
initializeTile(13, "water01", true);
|
||||
initializeTile(14, "water02", true);
|
||||
initializeTile(15, "water03", true);
|
||||
initializeTile(16, "water04", true);
|
||||
initializeTile(17, "water05", true);
|
||||
initializeTile(18, "water06", true);
|
||||
initializeTile(19, "water07", true);
|
||||
initializeTile(20, "water08", true);
|
||||
initializeTile(21, "water09", true);
|
||||
initializeTile(22, "water10", true);
|
||||
initializeTile(23, "water11", true);
|
||||
initializeTile(24, "water12", true);
|
||||
initializeTile(25, "water13", true);
|
||||
|
||||
// ROAD
|
||||
initializeTile(26, "road00", false);
|
||||
initializeTile(27, "road01", false);
|
||||
initializeTile(28, "road02", false);
|
||||
initializeTile(29, "road03", false);
|
||||
initializeTile(30, "road04", false);
|
||||
initializeTile(31, "road05", false);
|
||||
initializeTile(32, "road06", false);
|
||||
initializeTile(33, "road07", false);
|
||||
initializeTile(34, "road08", false);
|
||||
initializeTile(35, "road09", false);
|
||||
initializeTile(36, "road10", false);
|
||||
initializeTile(37, "road11", false);
|
||||
initializeTile(38, "road12", false);
|
||||
|
||||
// WORLD
|
||||
initializeTile(39, "earth", false);
|
||||
initializeTile(40, "wall", true);
|
||||
initializeTile(41, "tree", true);
|
||||
|
||||
//
|
||||
initializeTile(42, "hut", false);
|
||||
initializeTile(43, "floor01", false);
|
||||
initializeTile(44, "table01", true);
|
||||
for(int i = 0; i < filesNames.size(); i++) {
|
||||
String fileName;
|
||||
boolean collision;
|
||||
fileName = filesNames.get(i);
|
||||
collision = Boolean.parseBoolean(collisionStatus.get(i));
|
||||
initializeTile(i, fileName, collision);
|
||||
}
|
||||
|
||||
for (Tile tile : tile) {
|
||||
if(tile == null) continue;
|
||||
|
||||
Reference in New Issue
Block a user