从Slick2D中的文本文件加载平铺地图 [英] Loading Tile Maps From Text Files In Slick2D

查看:98
本文介绍了从Slick2D中的文本文件加载平铺地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Slick2D库在Java中制作一个相当简单的2D游戏引擎,并试图从文本文件中加载关卡(平铺地图).我希望能够加载多个不同的tileset,因此不想将每个数字分配给像这样的tile:

I am making a fairly simple 2D Game Engine in Java using the Slick2D Library, and am trying to load levels (tile maps) from a text file. I want to be able to load in multiple different tilesets, and therefore don't want to assign each number to a tile like this:

    1 1 1 1 1
    1 0 0 0 1
    1 0 0 0 1
    1 0 0 0 1
    1 1 1 1 1

    (Where 1 is a wall and 0 is a floor)

相反,我希望能够在tilemap中使用x和y坐标来表示在tileset上位于该tile上下的瓦片数量,如下所示:

instead, I would like to be able to use x and y coordinates in the tilemap to represent how many tiles across and down the tile is located at on the tileset, like this:

    0,0 0,0 0,0
    0,0 1,0 0,0
    0,0 0,0 0,0

    (0,0 is the tile in the upper left hand corner of the tileset, 1,0 is the second tile in the tileset)

我遇到的主要问题是读取文本文件,并将逗号前面的整数存储为Pos1(X值),将逗号后面但空格之前的所有内容存储为Pos2(Y值)

The main problem I am having with this is reading the text file and storing the integer in front of the comma as Pos1 (the X value) and everything after the comma but in front of the space as Pos2 (the Y value).

我将如何处理?我会使用常规文件读取器,缓冲读取器还是扫描器?

How would I go about this? Would I use a regular file reader, a buffered reader, or a scanner?

另外,顺便说一句,我知道Slick2D具有读取Tiled Map(.tmx)文件的内置功能,但是由于我的游戏引擎将具有内置的Tile Map编辑器,因此我想使用常规文本文件

Also, by the way, I am aware that Slick2D has built in capability of reading Tiled Map (.tmx) files, but since my game engine will have a built in tile map editor, I would like to use regular text files.

推荐答案

如果您需要文件采用该格式,以便可以在文本编辑器中打开它们,那么我将使用BufferedReader读取文件,然后读入每行都是一个字符串,并首先在每个空格处将其分隔,然后在每个逗号处进行分隔,然后解析该字符串以返回您的电话号码.

If you need your files to be in that format so that you can open them in a text editor then I would use a BufferedReader to read the file then read in each line as a string and split the line first at each space then at each comma, then parse the string to return your number.

try(BufferedReader in = new BufferedReader(new FileReader("file"))){
    String line;
    while((line=in.readLine())!=null){
        String[] values = line.split(" ");
        for(String v:values){
            String[] coords = v.split(",");
            int x = Integer.parseInt(coords[0]);
            int y = Integer.parseInt(coords[1]);
}

不过,更简单的方法可能是直接从地图编辑器直接编写int值,而不是将它们转换为字符串.如果这样做,则可以节省空间,而读取数据所需要做的只是:

However an easier way to do this may be to simply write the int values directly from your map editor instead of converting them to strings. If you do this then you would save space and all you would need to do to read the data is:

try(DataInputStream in= new DataInputStream(new BufferedInputStream(new FileInputStream(new File("file"))))){
    int numValues=in.readInt();
    for(int i=0;i<numValues;i++){
        int x = in.readInt();
        int y = in.readInt();
    }
}

然后在地图编辑器中保存地图,您只需将切片数写入文件中,然后使用writeInt()和DataOutputStream将每个切片的x和y坐标对齐.

And in your map editor to save the map you would just write the number of tiles to the file and then each of the tiles' x and y coordinates with writeInt() and a DataOutputStream.

您可能还想将地图的宽度和高度写入文件中,以便在那些不是恒定值或地图上图块的坐标不是恒定值时可以对其进行重构.

You may also want to write the width and height of the map to the file so that you can reconstruct it if those are not constant, or the coordinates of the tile on the map.

这篇关于从Slick2D中的文本文件加载平铺地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆