First commit
This commit is contained in:
98
src/main/java/se/urmo/my2dgame/tile/TileManager.java
Normal file
98
src/main/java/se/urmo/my2dgame/tile/TileManager.java
Normal file
@ -0,0 +1,98 @@
|
||||
package se.urmo.my2dgame.tile;
|
||||
|
||||
import se.urmo.my2dgame.main.Screen;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class TileManager {
|
||||
Screen screen;
|
||||
public Tile[] tiles = new Tile[10];
|
||||
public int[][] mapTileNum;
|
||||
|
||||
public TileManager(Screen screen) {
|
||||
this.screen = screen;
|
||||
getTileImage();
|
||||
this.mapTileNum = loadMap("/maps/world01.txt");
|
||||
}
|
||||
|
||||
public int[][] loadMap(String path) {
|
||||
int[][] map = new int[screen.WORLD_MAX_ROW][screen.WORLD_MAX_COL];
|
||||
try {
|
||||
InputStream stream = Screen.class.getResourceAsStream(path);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
|
||||
int row = 0;
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null && row < screen.WORLD_MAX_ROW) {
|
||||
map[row++] = Arrays.stream(line.split(" "))
|
||||
.mapToInt(Integer::parseInt)
|
||||
.toArray();
|
||||
}
|
||||
reader.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public void getTileImage() {
|
||||
try {
|
||||
tiles[0] = new Tile();
|
||||
tiles[0].image = ImageIO.read(Screen.class.getResourceAsStream("/tiles/grass.png"));
|
||||
|
||||
tiles[1] = new Tile();
|
||||
tiles[1].image = ImageIO.read(Screen.class.getResourceAsStream("/tiles/wall.png"));
|
||||
tiles[1].collission = true;
|
||||
|
||||
tiles[2] = new Tile();
|
||||
tiles[2].image = ImageIO.read(Screen.class.getResourceAsStream("/tiles/water.png"));
|
||||
tiles[2].collission = true;
|
||||
|
||||
tiles[3] = new Tile();
|
||||
tiles[3].image = ImageIO.read(Screen.class.getResourceAsStream("/tiles/earth.png"));
|
||||
|
||||
tiles[4] = new Tile();
|
||||
tiles[4].image = ImageIO.read(Screen.class.getResourceAsStream("/tiles/tree.png"));
|
||||
tiles[4].collission = true;
|
||||
|
||||
tiles[5] = new Tile();
|
||||
tiles[5].image = ImageIO.read(Screen.class.getResourceAsStream("/tiles/sand.png"));
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g2d) {
|
||||
int worldCol = 0;
|
||||
int worldRow = 0;
|
||||
|
||||
while (worldCol < screen.WORLD_MAX_COL && worldRow < screen.WORLD_MAX_ROW) {
|
||||
int tileNum = mapTileNum[worldRow][worldCol];
|
||||
|
||||
int worldX = worldCol * screen.TILE_SIZE;
|
||||
int worldY = worldRow * screen.TILE_SIZE;
|
||||
|
||||
// Only drawImage for tiles within camera
|
||||
if (screen.isWithinScreen(worldX, worldY)) {
|
||||
int screenX = worldX - screen.player.worldX + screen.player.SCREEN_X;
|
||||
int screenY = worldY - screen.player.worldY + screen.player.SCREEN_Y;
|
||||
g2d.drawImage(tiles[tileNum].image, screenX, screenY, screen.TILE_SIZE, screen.TILE_SIZE, null);
|
||||
}
|
||||
|
||||
worldCol++;
|
||||
|
||||
if (worldCol == screen.WORLD_MAX_COL) {
|
||||
worldCol = 0;
|
||||
worldRow++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user