views:

37

answers:

1

Alright, so I'm trying to create a simple RPG game, and my map reading code seems to be off. This is the map reading line:

Integer.parseInt(map.data.substring(Map.MAP_START + ((playerPOS - map.width)/2) - 1, Map.MAP_START + ((((playerPOS - map.width)/2) - 1) + map.dataLen)));

Now the only tiles in the map are 01 and 00, so when I see a 10 I know something is wrong:

(playerPOS - map.width) = 34 playerPOS = 50 player.x = 2 player.y = 3 blah = 0
(playerPOS - map.width) = 18 playerPOS = 34 player.x = 2 player.y = 2 blah = 10

Here is the map reading code:

public void init(GameContainer gc) throws SlickException {
        map = new Map();
        player = new Player();

        keys = new boolean[ALL_KEYS];
        for(int i = 0; i < ALL_KEYS; i++){
            keys[i] = false;
        }

        file = new File("testmap.txt");

        try {
            fin = new FileInputStream(file);

            bin = new BufferedInputStream(fin);
            StringBuilder sb = new StringBuilder();
            int ch = 0;

            while ((ch=bin.read())!=-1) {
                sb.append((char)ch);
            }

            map.data = sb.toString().replace(" ", "").replace("\n", "").replace("\r", "");
            System.out.print(map.data);

            map.width = Integer.parseInt(map.data.substring(map.dataOffs, map.dataOffs + map.dataLen));
            map.dataOffs += map.dataLen;

            map.height = Integer.parseInt(map.data.substring(map.dataOffs, map.dataOffs + map.dataLen));
            map.dataOffs += map.dataLen;
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        tiles = new Image("tiles.png");
        hero = new Image("hero.png");
    }

and here is the map file:

16 12
01 01 01 01 01 01 01 01 01 01 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00
00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
01 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

So if anymore info is needed let me know.

+2  A: 

I would recommend re-thinking the data structure for your map (perhaps a 2D array). In your init method, read the text file token-by-token (assuming each token is whitespace-separated). This is similar to what you're doing now, but you can avoid replacing the whitespace with empty strings. See http://download.oracle.com/javase/6/docs/api/java/util/StringTokenizer.html

The first token string (in your example, "16") can be converted into map.width as you're doing. Likewise, the second token becomes map.height. Then you can loop through the remaining tokens in the following manner (pseudocode):

for(int y = 0; y < map.height; ++y) {
  for (int x = 0; x < map.width; ++x) {
    mapArray[x][y] = nextTokenAsInteger;
  }
}

Then, when you desire to know what the room looks like at position (x, y), you simply access mapArray[x][y].

TreDubZedd
Thank you very much. ^_^
William