-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTileMapManagerHelp.java
More file actions
111 lines (88 loc) · 3.58 KB
/
Copy pathTileMapManagerHelp.java
File metadata and controls
111 lines (88 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.imageio.ImageIO;
public class TileMapManagerHelp {
private GamePanel gp;
public Tile[] tile;
public int map[][];
private util u = new util();
public TileMapManagerHelp(GamePanel gp){
this.gp = gp;
tile = new Tile[10];
map = new int[gp.getWorldCol()][gp.getWorldRow()];
getTileImage();
loadMap("maps//map2.txt");
}
public void getTileImage(){
setUp(0, "images//tiles//l1//tile_A.png", true);
setUp(1, "images//tiles//l1//tile_B.png", false);
setUp(2, "images//tiles//l1//tile_C.png", false);
setUp(3, "images//tiles//l1//tile_D.png", false);
setUp(4, "images//tiles//l1//tile_E.png", true);
setUp(5, "images//tiles//l1//tile_F.png", false);
setUp(6, "images//tiles//l1//tile_G.png", false);
}
public void setUp(int index, String path, boolean collision){
try{
tile[index] = new Tile();
tile[index].tileImage = ImageIO.read(getClass().getResourceAsStream(path));
tile[index].tileImage = u.scaledImage(tile[index].tileImage, gp.getTileSize(), gp.getTileSize());
tile[index].setCollision(collision);
}
catch(IOException e){
e.printStackTrace();
}
}
public void loadMap(String filename){
try{
InputStream in = getClass().getResourceAsStream(filename);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
int col = 0;
int row = 0;
while(col < gp.getWorldCol() && row < gp.getWorldRow()){
String line = br.readLine();
while(col < gp.getWorldCol()){
String number[] = line.split(" ");
int num = Integer.parseInt(number[col]);
map[col][row] = num;
//System.out.println("WorldCol: " + col + " WorldRow: " + row + " Tile: " + num + " WorldCol: " + gp.getWorldCol() + " WorldRow: " + gp.getWorldRow());
col++;
}
if(col == gp.getWorldCol()){
col = 0;
row++;
}
}
br.close();
}
catch(IOException e){
e.printStackTrace();
}
}
public void draw(Graphics2D g2){
int WorldCol = 0;
int WorldRow = 0;
while(WorldCol < gp.getWorldCol() && WorldRow < gp.getWorldRow()){
int tileIndex = map[WorldCol][WorldRow];
int worldXvar = gp. getTileSize() * WorldCol;
int worldYvar = gp. getTileSize() * WorldRow;
int ScreenX = worldXvar - gp.player.Worldx + gp.player.screenX;
int ScreenY = worldYvar - gp.player.Worldy + gp.player.screenY;
if(worldXvar + gp. getTileSize() > gp.player.Worldx - gp.player.screenX &&
worldXvar - gp. getTileSize() < gp.player.Worldx + gp.player.screenX &&
worldYvar + gp. getTileSize() > gp.player.Worldy - gp.player.screenY &&
worldYvar - gp. getTileSize() < gp.player.Worldy + gp.player.screenY){
g2.drawImage(tile[tileIndex].tileImage, ScreenX, ScreenY, null);
}
WorldCol++;
if(WorldCol == gp.getWorldCol()){
WorldCol = 0;
WorldRow++;
}
}
}
}