java swing实战——baba is you(地图文件接口)

6. 游戏地图文件加载

​ 之前,我们将初始关卡的坐标直接写入代码中,如下图中的gameText,其中重要的数据是x,y,以及描述的信息TextLocation和PeopleLocation
在这里插入图片描述
在这里插入图片描述
现在我们提取初始关卡的关键数据:

text
baba 4,5
is1 5,5
you 6,5
flag 12,5
is2 13,5
win 14,5
wall 4,13
is3 5,13
stop 6,13
rock 12,13
is4 13,13
push 14,13
people
baba 5,9
flag 14,9
rock 9,8
rock 9,9
rock 9,10
wall 4,7
wall 5,7
wall 6,7
wall 6,7
wall 7,7
wall 8,7
wall 9,7
wall 10,7
wall 11,7
wall 12,7
wall 13,7
wall 14,7
wall 4,11
wall 5,11
wall 6,11
wall 6,11
wall 7,11
wall 8,11
wall 9,11
wall 10,11
wall 11,11
wall 12,11
wall 13,11
wall 14,11

如此,我们改为文件加载,这样我们仅需要改变文件就可以快速修改关卡

6.1 文件加载到内存中(坐标)

​ 读取文件的操作,我们放置于GameUtil中,分析上面的文件,作者准备存储为一个Map,分别放置GameText和GamePeople的坐标,同样,这两个类依然是以Map形式存储,为了方便,我们建立了一个Location类(util包下),专门存储坐标。

/**
 * 坐标加载
 */
public class Location {
    private int x, y;

    public Location(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }
}

在GameUtil建立文件读取方法

/**
 * 加载地图文件
 *
 * @param url
 * @return
 */
public static Map<String, Object> loadMap(String url)

存储结构:

Map<String, Object> res = new HashMap<>();
Map<String, Location> text = new HashMap<>();
Map<String, List<Location>> people = new HashMap<>();

在constant新建地图数据常量:

/**
 * 地图元素
 */
public interface MapConstant {
    String TEXT = "text";
    String PEOPLE = "people";

    String BABA = "baba";
    String ROCK = "rock";
    String WALL = "wall";
    String FLAG = "flag";
    String WATER = "water";

    String TILE = "tile";
    String GRASS = "grass";

    String IS1 = "is1";
    String IS2 = "is2";
    String IS3 = "is3";
    String IS4 = "is4";

    String IS5 = "is5";

    String PUSH = "push";
    String STOP = "stop";
    String YOU = "you";
    String WIN = "win";

    String SINK = "sink";
}

文件读取、字符串分隔:

/**
 * 加载地图文件
 *
 * @param url
 * @return
 */
public static Map<String, Object> loadMap(String url) throws IOException {
    Map<String, Object> res = new HashMap<>();
    Map<String, Location> text = new HashMap<>();
    Map<String, List<Location>> people = new HashMap<>();


    res.put(MapConstant.TEXT, text);
    res.put(MapConstant.PEOPLE, people);


    URL resource = GameFrame.class.getResource(url);

    InputStream resourceAsStream = GameFrame.class.getResourceAsStream(url);

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resourceAsStream));
    String line = bufferedReader.readLine();
    line = bufferedReader.readLine();

    Map<String, Location> textMap = (Map<String, Location>) res.get(MapConstant.TEXT);
    // 读取规则
    while (!MapConstant.PEOPLE.equalsIgnoreCase(line)) {
        String[] split = line.split("[\\s+ ,]");
        textMap.put(split[0], new Location(Integer.parseInt(split[1]), Integer.parseInt(split[2])));
        line = bufferedReader.readLine();
    }

    Map<String, java.util.List<Location>> peopleMap = (Map<String, java.util.List<Location>>) res.get(MapConstant.PEOPLE);
    // 读取people
    while (null != (line = bufferedReader.readLine())) {
        String[] split = line.split("[\\s+ ,]");

        List<Location> orDefault = peopleMap.getOrDefault(split[0], new ArrayList<>());
        orDefault.add(new Location(Integer.parseInt(split[1]), Integer.parseInt(split[2])));
        peopleMap.put(split[0], orDefault);
    }

    return res;
}

6.2 GameText\GamePeople初始化

我们在ui/asset下新建map包,存储我们的地图,这里我们存储为level1,level1中即为上述我们提取的数据信息在这里插入图片描述

为了之后提取坐标方便,我们在Text,People分别加入如下构造方法

public Text(Location location, String rule) {
    super(location.getX() * 24, location.getY() * 24);
    this.rule = rule;
    image = GameUtil.loadBufferedImage(rule);
}
public People(Location location, String name) {
    super(location.getX() * 24, location.getY() * 24);
    this.name = name;
    this.image = GameUtil.loadBufferedImage(name);
}

在GameText调用GameUtil读取地图,提取坐标

    public GameText() {
        this.textList = new ArrayList<>();
//        /**
//         * 初始化GameText位置
//         */
//        this.textList.add(new Text(4*24,5*24, TextLocation.BABA));
//        this.textList.add(new Text(5*24,5*24, TextLocation.IS));
//        this.textList.add(new Text(6*24,5*24, TextLocation.YOU));
//
//        this.textList.add(new Text(12*24,5*24, TextLocation.FLAG));
//        this.textList.add(new Text(13*24,5*24, TextLocation.IS));
//        this.textList.add(new Text(14*24,5*24, TextLocation.WIN));
//
//        this.textList.add(new Text(4*24,13*24, TextLocation.WALL));
//        this.textList.add(new Text(5*24,13*24, TextLocation.IS));
//        this.textList.add(new Text(6*24,13*24, TextLocation.STOP));
//
//        this.textList.add(new Text(12*24,13*24, TextLocation.ROCK));
//        this.textList.add(new Text(13*24,13*24, TextLocation.IS));
//        this.textList.add(new Text(14*24,13*24, TextLocation.PUSH));

        Map<String, Object> stringObjectMap = null;
        try {
            stringObjectMap = GameUtil.loadMap("asset/map/level1");
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String, Location> locationMap = (Map<String, Location>)stringObjectMap.get(MapConstant.TEXT);
        for (String mapConstant : locationMap.keySet()) {
            Location location = locationMap.get(mapConstant);
            switch (mapConstant) {
                case MapConstant.BABA:
                    this.textList.add(new Text(location, TextLocation.BABA));break;
                case MapConstant.FLAG:
                    this.textList.add(new Text(location, TextLocation.FLAG));break;
                case MapConstant.WALL:
                    this.textList.add(new Text(location, TextLocation.WALL));break;
                case MapConstant.ROCK:
                    this.textList.add(new Text(location, TextLocation.ROCK));break;
                case MapConstant.WIN:
                    this.textList.add(new Text(location, TextLocation.WIN));break;
                case MapConstant.PUSH:
                    this.textList.add(new Text(location, TextLocation.PUSH));break;
                case MapConstant.STOP:
                    this.textList.add(new Text(location, TextLocation.STOP));break;
                case MapConstant.YOU:
                    this.textList.add(new Text(location, TextLocation.YOU));break;
                case MapConstant.IS1:
                    this.textList.add(new Text(location, TextLocation.IS));break;
                case MapConstant.IS2:
                    this.textList.add(new Text(location, TextLocation.IS));break;
                case MapConstant.IS3:
                    this.textList.add(new Text(location, TextLocation.IS));break;
                case MapConstant.IS4:
                    this.textList.add(new Text(location, TextLocation.IS));break;
            }
        }

    }

在GamePeople中调用GameUtil读取地图,提取坐标

    public GamePeople() {
        this.peopleList = new ArrayList<>();
        this.peopleMap = new HashMap<>(); // 初始化

        /**
         * 4种可操作对象,之后可增加water等
         */
        this.peopleMap.put(PeopleLocation.BABA, new ArrayList<People>());
        this.peopleMap.put(PeopleLocation.ROCK, new ArrayList<People>());
        this.peopleMap.put(PeopleLocation.WALL, new ArrayList<People>());
        this.peopleMap.put(PeopleLocation.FLAG, new ArrayList<People>());

        Map<String, Object> stringObjectMap = null;
        try {
            stringObjectMap = GameUtil.loadMap("asset/map/level1");
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String, List<Location>> locationMap = (Map<String, List<Location>>)stringObjectMap.get(MapConstant.PEOPLE);

        for (String mapConstant : locationMap.keySet()) {
            List<Location> locations = locationMap.get(mapConstant);
            switch (mapConstant) {
                case MapConstant.BABA:
                    List<People> babaList = this.peopleMap.get(PeopleLocation.BABA);
                    for (int i = 0; i < locations.size(); i++) {
                        People baba = new People(locations.get(i), PeopleLocation.BABA);
                        babaList.add(baba);
                        this.peopleList.add(baba);
                    }
                    break;
                case MapConstant.FLAG:
                    List<People> flagList = this.peopleMap.get(PeopleLocation.FLAG);
                    for (int i = 0; i < locations.size(); i++) {
                        People flag = new People(locations.get(i), PeopleLocation.FLAG);
                        flagList.add(flag);
                        this.peopleList.add(flag);
                    }
                    break;
                case MapConstant.WALL:
                    List<People> wallList = this.peopleMap.get(PeopleLocation.WALL);
                    for (int i = 0; i < locations.size(); i++) {
                        People wall = new People(locations.get(i), PeopleLocation.WALL);
                        wallList.add(wall);
                        this.peopleList.add(wall);
                    }
                    break;
                case MapConstant.ROCK:
                    List<People> rockList = this.peopleMap.get(PeopleLocation.ROCK);
                    for (int i = 0; i < locations.size(); i++) {
                        People rock = new People(locations.get(i), PeopleLocation.ROCK);
                        rockList.add(rock);
                        this.peopleList.add(rock);
                    }
                    break;
            }
        }

//        People baba = new People(5 * 24, 9 * 24, PeopleLocation.BABA);
//        this.peopleList.add(baba);
//        this.peopleMap.get(PeopleLocation.BABA).add(baba);
//
//        People flag = new People(14 * 24, 9 * 24, PeopleLocation.FLAG);
//        this.peopleList.add(flag);
//        this.peopleMap.get(PeopleLocation.FLAG).add(flag);
//
//
//        People rock0 = new People(9 * 24, 8 * 24, PeopleLocation.ROCK);
//        People rock1 = new People(9 * 24, 9 * 24, PeopleLocation.ROCK);
//        People rock2 = new People(9 * 24, 10 * 24, PeopleLocation.ROCK);
//        this.peopleList.add(rock0);this.peopleMap.get(PeopleLocation.ROCK).add(rock0);
//        this.peopleList.add(rock1);this.peopleMap.get(PeopleLocation.ROCK).add(rock1);
//        this.peopleList.add(rock2);this.peopleMap.get(PeopleLocation.ROCK).add(rock2);
//
//
//        for (int i = 4; i < 15; i++) {
//            People wall0 = new People(i * 24, 7 * 24, PeopleLocation.WALL);
//            People wall1 = new People(i * 24, 11 * 24, PeopleLocation.WALL);
//            this.peopleList.add(wall0);this.peopleMap.get(PeopleLocation.WALL).add(wall0);
//            this.peopleList.add(wall1);;this.peopleMap.get(PeopleLocation.WALL).add(wall1);
//        }
    }

至此,我们完成地图的文件接口编写。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值