cs61b proj2 phase1

1.生成room

room类定义了一个宽度、高度、在world中的坐标均为随机的room,并且用WALL包围,内填充FLOOR。
room的宽度、高度、在world中的坐标均为随机。在生成时需确保room不超出world的边界,这里使用retry来控制。

public Room(long seed) {
        rand = new Random(seed);
        setPosition(); // 确定room的左下角在world中的位置坐标(x, y)
        int x = position.getX();
        int y = position.getY();
        int retry = 10; // 随机生成的room可能超出地图范围,故设置retry
        // while循环尝试在world的(x, y)处创建room
        do {
            setHeight();
            setWidth();
            retry--;
            if (retry == 0) break;
        } while (x + width >= World.WIDTH || y + height >= World.HEIGHT);
        if (retry == 0) {
            position = new Position(-1, -1);
            return;
        }
        room = new TETile[height][width];
        roundByWall();
        fillWithFloor();
        // 以上代码构造了一个room,并且在world中的坐标也已经确定,但还未将其放置到world中
    }

在world中放置room时需注意不要重叠。

public void placeRoom() {
        Iterator<Room> iterator = rooms.iterator();
        while (iterator.hasNext()) {
            Room room = iterator.next();
            if (room.isOverlap()) { // 去除重叠
                iterator.remove();
                continue;
            }
            placeOneRoom(room);
        }
    }

    private void placeOneRoom(Room room) {
        int x = room.getPosition().getX();
        int y = room.getPosition().getY();
        int height = room.getHeight();
        int width = room.getWidth();
        TETile[][] r = room.getRoom();
        int row = height, col = 0;
        for (int Y = y; Y < y + height; Y++) {
            row--;
            col = 0;
            for (int X = x; X < x + width; X++) {
                world[X][Y] = r[row][col];
                col++;
            }
        }
    }

生成hallway连接room,生成方法参考这里
注意hallway不要像下面这样重叠。
重叠的hallway

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值