-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTileManager.java
136 lines (133 loc) · 4.11 KB
/
TileManager.java
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//package
package tile;
//imports
import java.awt.Graphics2D;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.imageio.ImageIO;
import Main.GamePanel;
import Main.UtilityTool;
//Public class TileManager
public class TileManager {
//Make use of the GamePanel
GamePanel gp;
//Make tiles as array
public Tile[] tile;
//int mapTileNum as an array
public int mapTileNum[][];
//TileManager constructor
public TileManager(GamePanel gp) {
//this.gp=gp
this.gp=gp;
//create ten type of Tiles
tile = new Tile[10];
//instantiate mapTileNum
mapTileNum = new int[gp.maxWorldCol][gp.maxWorldRow];
//call method from constructor
getTileImage();
//call loadMap method
loadMap("/maps/Map.txt");
}//end constructor
//public void getTileImage
public void getTileImage() {
//setup the tile images
setup(0,"Desert",false);
setup(1,"Tundra",false);
setup(2,"Brick",true);
}//end method
//public void setup method
public void setup(int index, String imageName, boolean collision) {
//instantiate utilityTool
UtilityTool uTool = new UtilityTool();
//try
try {
//Code to scale the tiles
tile[index]= new Tile();
tile[index].image = ImageIO.read(getClass().getResourceAsStream("/tiles/"+ imageName +".png"));
tile[index].image = uTool.scaleImage(tile[index].image,gp.tileSize,gp.tileSize);
tile[index].collision=collision;
}//end try
//catch
catch(IOException e) {
e.printStackTrace();
}//end catch
}//end setup method
//public void loadMap method
public void loadMap(String filePath) {
//try
try {
//get map txt file
InputStream is = getClass().getResourceAsStream(filePath);
//Instantiate BufferedReader to read context of txt file
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//Declare col,row with 0
int col = 0;
int row = 0;
//while col is less than maxWorldCol and row is less than maxWorldRow
while(col<gp.maxWorldCol && row< gp.maxWorldRow) {
//read line of Map.txt
String line = br.readLine();
//while col<gp.maxWorldCol
while(col<gp.maxWorldCol) {
//Splits the numbers and put in array
String numbers[] = line.split(" ");
//Changing string to int to able to use
int num = Integer.parseInt(numbers[col]);
//mapTileNum is num
mapTileNum[col][row]= num;
//increment col
col++;
}//end while
//if col equal equal to gp.WorldCol
if(col==gp.maxWorldCol) {
//col is equal to zero
col = 0;
//row is being incremented
row++;
}//end if
}//end while loop
//close reader since we are done
br.close();
}catch(Exception e) {
}//end catch
}//end method
//public void draw method
public void draw(Graphics2D g2) {
//Declare int,worldcol,worldrow with 0
int worldCol = 0;
int worldRow = 0;
//while worldcol is less than maxworldCol and worldrow is less than maxWorldRow
while(worldCol<gp.maxWorldCol && worldRow<gp.maxWorldRow) {
//extract a tile number
int tileNum = mapTileNum[worldCol][worldRow];
//int worldX equals
int worldX=worldCol*gp.tileSize;
//int worldY equals
int worldY = worldRow*gp.tileSize;
//+gp.tileSize
//-gp.tileSize
//int screenX equals
int screenX = worldX - gp.player.worldX+gp.player.screenX;
//int screenY equals
int screenY = worldY - gp.player.worldY+gp.player.screenY;
//this if statement creates boundary to draw tiles
if(worldX+gp.tileSize>gp.player.worldX-gp.player.screenX&&
worldX-gp.tileSize<gp.player.worldX+gp.player.screenX&&
worldY+gp.tileSize>gp.player.worldY-gp.player.screenY&&
worldY-gp.tileSize<gp.player.worldY+gp.player.screenY) {
//draw the images
g2.drawImage(tile[tileNum].image,screenX,screenY,gp.tileSize,gp.tileSize,null);
}
//draw the next worldcol by incrementing
worldCol++;
//if col == gp.maxWorldCol
if(worldCol==gp.maxWorldCol) {
//worldcol equal to zero and worldrow is incremented
worldCol=0;
worldRow++;
}//end if
}//end while loop
}//end draw
}//end class