Java - Tile engine changing number in array not changing texture
Posted
by
Corey
on Game Development
See other posts from Game Development
or by Corey
Published on 2012-07-09T17:28:45Z
Indexed on
2012/07/09
21:24 UTC
Read the original article
Hit count: 329
I draw my map from a txt file. Would I have to write to the text file to notice the changes I made? Right now it changes the number in the array but the tile texture doesn't change. Do I have to do more than just change the number in the array?
public class Tiles {
public Image[] tiles = new Image[5];
public int[][] map = new int[64][64];
private Image grass, dirt, fence, mound;
private SpriteSheet tileSheet;
public int tileWidth = 32;
public int tileHeight = 32;
Player player = new Player();
public void init() throws IOException, SlickException {
tileSheet = new SpriteSheet("assets/tiles.png", tileWidth, tileHeight);
grass = tileSheet.getSprite(0, 0);
dirt = tileSheet.getSprite(7, 7);
fence = tileSheet.getSprite(2, 0);
mound = tileSheet.getSprite(2, 6);
tiles[0] = grass;
tiles[1] = dirt;
tiles[2] = fence;
tiles[3] = mound;
int x=0, y=0;
BufferedReader in = new BufferedReader(new FileReader("assets/map.dat"));
String line;
while ((line = in.readLine()) != null) {
String[] values = line.split(",");
for (String str : values) {
int str_int = Integer.parseInt(str);
map[x][y]=str_int;
//System.out.print(map[x][y] + " ");
y=y+1;
}
//System.out.println("");
x=x+1;
y = 0;
}
in.close();
}
public void update(GameContainer gc) {
}
public void render(GameContainer gc) {
for(int x = 0; x < map.length; x++) {
for(int y = 0; y < map.length; y ++) {
int textureIndex = map[y][x];
Image texture = tiles[textureIndex];
texture.draw(x*tileWidth,y*tileHeight);
}
}
}
Mouse picking
public void checkDistance(GameContainer gc) {
Input input = gc.getInput();
float mouseX = input.getMouseX();
float mouseY = input.getMouseY();
double mousetileX = Math.floor((double)mouseX/tiles.tileWidth);
double mousetileY = Math.floor((double)mouseY/tiles.tileHeight);
double playertileX = Math.floor(playerX/tiles.tileWidth);
double playertileY = Math.floor(playerY/tiles.tileHeight);
double lengthX = Math.abs((float)playertileX - mousetileX);
double lengthY = Math.abs((float)playertileY - mousetileY);
double distance = Math.sqrt((lengthX*lengthX)+(lengthY*lengthY));
if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON) && distance < 4) {
System.out.println("Clicked");
if(tiles.map[(int)mousetileX][(int)mousetileY] == 1) {
tiles.map[(int)mousetileX][(int)mousetileY] = 0;
}
}
System.out.println(tiles.map[(int)mousetileX][(int)mousetileY]);
}
© Game Development or respective owner