107 lines
3.6 KiB
Java
107 lines
3.6 KiB
Java
package de.miaurizius.jgame2d.tile;
|
|
|
|
import de.miaurizius.jgame2d.core.Boot;
|
|
import de.miaurizius.jgame2d.core.GamePanel;
|
|
|
|
import javax.imageio.ImageIO;
|
|
import java.awt.*;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.*;
|
|
import java.util.logging.Level;
|
|
|
|
public class TileManager {
|
|
|
|
GamePanel panel;
|
|
public Tile[] tile;
|
|
public int[][] mapTileNum;
|
|
|
|
public TileManager(GamePanel panel) {
|
|
this.panel = panel;
|
|
tile = new Tile[10];
|
|
mapTileNum = new int[panel.maxWorldCol][panel.maxWorldRow];
|
|
getTileImage();
|
|
loadMap("testworld");
|
|
}
|
|
|
|
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].collision = col;
|
|
} catch(IOException e) {
|
|
Boot.logger.log(Level.SEVERE, "Could not load tile", e);
|
|
}
|
|
}
|
|
|
|
public void getTileImage() {
|
|
initializeTile(0, "grass", false);
|
|
initializeTile(1, "wall", true);
|
|
initializeTile(2, "water", true);
|
|
initializeTile(3, "earth", false);
|
|
initializeTile(4, "tree", true);
|
|
initializeTile(5, "sand", false);
|
|
|
|
BufferedImage scaledImage;
|
|
for (Tile tile : tile) {
|
|
if(tile == null) continue;
|
|
scaledImage = new BufferedImage(panel.tileSize, panel.tileSize, tile.image.getType());
|
|
Graphics2D g2d = scaledImage.createGraphics();
|
|
g2d.drawImage(tile.image, 0, 0, panel.tileSize, panel.tileSize, null);
|
|
tile.image = scaledImage;
|
|
}
|
|
}
|
|
|
|
public void loadMap(String map) {
|
|
try {
|
|
InputStream stream = new FileInputStream("assets/maps/"+map+".map");
|
|
BufferedReader bReader = new BufferedReader(new InputStreamReader(stream));
|
|
int col = 0;
|
|
int row = 0;
|
|
while(col < panel.maxWorldCol && row < panel.maxWorldRow) {
|
|
String line = bReader.readLine();
|
|
while(col < panel.maxWorldCol) {
|
|
String[] numbers = line.split(" ");
|
|
int num = Integer.parseInt(numbers[col]);
|
|
mapTileNum[col][row] = num;
|
|
col++;
|
|
}
|
|
if(col == panel.maxWorldCol) {
|
|
col = 0;
|
|
row++;
|
|
}
|
|
}
|
|
bReader.close();
|
|
} catch(Exception e) {
|
|
Boot.logger.log(Level.SEVERE, "Could not load map", e);
|
|
}
|
|
}
|
|
|
|
public void draw(Graphics2D graphics2D) {
|
|
int worldCol = 0;
|
|
int worldRow = 0;
|
|
|
|
while(worldCol < panel.maxWorldCol && worldRow < panel.maxWorldRow) {
|
|
int tileNum = mapTileNum[worldCol][worldRow];
|
|
|
|
int worldX = worldCol * panel.tileSize;
|
|
int worldY = worldRow * panel.tileSize;
|
|
int screenX = worldX - panel.player.worldX + panel.player.screenX;
|
|
int screenY = worldY - panel.player.worldY + panel.player.screenY;
|
|
|
|
if(worldX + panel.tileSize > panel.player.worldX - panel.player.screenX &&
|
|
worldX - panel.tileSize < panel.player.worldX + panel.player.screenX &&
|
|
worldY + panel.tileSize > panel.player.worldY - panel.player.screenY &&
|
|
worldY - panel.tileSize < panel.player.worldY + panel.player.screenY
|
|
) graphics2D.drawImage(tile[tileNum].image, screenX, screenY, panel.tileSize, panel.tileSize, null);
|
|
|
|
worldCol++;
|
|
|
|
if(worldCol == panel.maxWorldCol) {
|
|
worldCol = 0;
|
|
worldRow++;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|