I have tilemaps represented by values from 0 to 4. How can I make the player collide with tiles represented by 3 and stop the player from passing through it ? Here is my code for the player and tilemap:
public class tilemap {
private int x;
private int y;
private int tilesize;
private int[][] map;
private int mapwidth;
private int mapheight;
public static Image img;
public static int camx,camy;
public String sl;
public tilemap(String s,int tilesize){
this.tilesize = tilesize;
try{
BufferedReader br = new BufferedReader(new FileReader(s));
mapwidth = Integer.parseInt(br.readLine());
mapheight = Integer.parseInt(br.readLine());
map = new int[mapheight][mapwidth];
String del = " ";
for(int row = 0; row < mapheight; row++){
String line = br.readLine();
String[] tokens = line.split(del);
for(int col = 0; col < mapwidth; col++){
map[row][col] = Integer.parseInt(tokens[col]);
}
}
}catch(Exception e){}
}
public void update(){
}
public void draw(Graphics g){
for(int row = 0; row < mapheight; row++){
for(int col = 0; col < mapwidth; col++){
int rc = map[row][col];
if(rc == 0){
g.setColor(Color.green);
}
if(rc == 1){
g.setColor(Color.yellow);
}
if(rc == 2){
g.setColor(Color.GRAY);
}
if(rc == 3){
g.setColor(new Color(127,51,0));
}
if(rc == 4){
g.setColor(Color.BLUE);
}
g.fillRect(x + col * tilesize - camx, y + row * tilesize - camy,tilesize,tilesize);
}
}
}
}
public class player {
public static boolean camo = false, walkb = false, walkl = false,
walkr = false, walkw = false;
public static int x = 431, y = 335, anim = 0;
public static String name = "Jack";
public static Image img;
public static int cx, cy;
public static int an = 0, mxan = 2;
public static int an1 = 0, mxan1 = 2;
public static int an2 = 0, mxan2 = 2;
public static int an3 = 0, mxan3 = 2;
public static void tick() {
if (walkb) {
// y+=2;
tilemap.camy += 2;
if (an <= mxan) {
an++;
}
} else if (walkw) {
// y-=2;
tilemap.camy -= 2;
if (an3 <= mxan3) {
an3++;
}
} else if (walkr) {
// x+=2;
tilemap.camx += 2;
if (an1 <= mxan1) {
an1++;
}
} else if (walkl) {
// x-=2;
tilemap.camx -= 2;
if (an2 <= mxan2) {
an2++;
}
}
if (an >= mxan) {
an = 0;
}
if (an1 >= mxan1) {
an1 = 0;
}
if (an2 >= mxan2) {
an2 = 0;
}
if (an3 >= mxan3) {
an3 = 0;
}
}
public static void render(Graphics g) {
g.setColor(Color.black);
g.setFont(new Font("italic", Font.BOLD, 12));
g.drawString("" + name, x + 10, y - 10);
/** player **/
if (anim == 0) {
if (an1 == 0) {
ImageIcon i3 = new ImageIcon("res/player.png");
img = i3.getImage();
g.drawImage(img, x, y, null);
} else if (an1 == 1) {
ImageIcon i3 = new ImageIcon("res/player.1.png");
img = i3.getImage();
g.drawImage(img, x, y, null);
}
} else if (anim == 1) {
if (an2 == 0) {
ImageIcon i3 = new ImageIcon("res/player2.png");
img = i3.getImage();
g.drawImage(img, x, y, null);
} else if (an2 == 1) {
ImageIcon i3 = new ImageIcon("res/player2.1.png");
img = i3.getImage();
g.drawImage(img, x, y, null);
}
} else if (anim == 2) {
if (an3 == 0) {
ImageIcon i3 = new ImageIcon("res/player3.png");
img = i3.getImage();
g.drawImage(img, x, y, null);
} else if (an3 == 1) {
ImageIcon i3 = new ImageIcon("res/player3.1.png");
img = i3.getImage();
g.drawImage(img, x, y, null);
}
} else if (anim == 3) {
if (an == 0) {
ImageIcon i3 = new ImageIcon("res/player4.png");
img = i3.getImage();
g.drawImage(img, x, y, null);
} else if (an == 1) {
ImageIcon i3 = new ImageIcon("res/player4.1.png");
img = i3.getImage();
g.drawImage(img, x, y, null);
}
}
}
}