Unity记录5.2-地图-连续地形块

汇总:Unity 记录

摘要:连续地形块的生成。

八个走向的地形-2023/08/31

  • 左右走向、2x垂直走向(从左边向下走、从右边向下走)、4x斜方向(左向上、左向下、右向上、右向下)
  • 目前想到的地表走向暂时就这些,4个出发点,左右出发点3个方向,上下出发点4个方向,去重剩7个。
  • 效果如下。

Unity_018_8Directions.png

实现代码-2023/08/30-2023/08/31

  • 前面的代码又稍微修改了下,但不重要,这里放核心的代码。
/*  ---------- direction ----------
 *  [horizontal, left]  : from left to right
 *  [horizontal, right] : from left to right
 *  [vertical, left]    : from left-up to right-down
 *  [vertical, right]   : from right-up to left-down
 *  [left, up]          : from left to up
 *  [left, down]        : from left to down
 *  [right, up]         : from right to up
 *  [right, down]       : from right to down
 */

BlockInfo _generate_1DMapBlock_plain(Vector3Int block_offsets, _BlockAround block_around, string[] directions){
    Vector3Int BSize = _game_configs.__block_size__;    // for convenience
    BlockInfo block = new();
    int min_h = Mathf.CeilToInt(BSize.y * 0.1f);        // min_h also means boundBottom_h or boundTop_h
    int max_h = BSize.y - min_h;
    int min_w = Mathf.CeilToInt(BSize.x * 0.1f);
    int max_w = BSize.x - min_w;
    block.offsets = block_offsets;
    block.directions = directions;
    block.isExist = true;
    block.up = new(-1, BSize.y);
    block.down = new(-1, 0);
    block.left = new(0, -2);
    block.right = new(BSize.x - 1, -1);
    float scale = 10f;
    // ---------- target ----------
    block.targets = new();
    if (directions[0] == "horizontal"){
        if (block_around.left.isExist && block_around.right.isExist) {
            block.left.y = block_around.left.right.y;
            block.right.y = block_around.right.left.y;
        } else if (block_around.left.isExist){
            block.left.y = block_around.left.right.y;
            block.right.y = Random.Range(min_h, max_h);
        } else if (block_around.right.isExist){
            block.left.y = Random.Range(min_h, max_h);
            block.right.y = block_around.right.left.y;
        } else{
            block.left.y = Random.Range(min_h, max_h);
            block.right.y = Random.Range(min_h, max_h);
        }
        block.targets.Add(block.left);
        block.targets.Add(block.right);
    }
    else if (directions[0] == "vertical"){
        if (directions[1] == "left"){
            if (block_around.up.isExist && block_around.down.isExist) {
                block.up.x = block_around.up.down.x;
                block.down.x = block_around.down.up.x;
            } else if (block_around.up.isExist){
                block.up.x = block_around.up.down.x;
                block.down.x = Random.Range(block.up.x, max_w);
            } else if (block_around.down.isExist){
                block.down.x = block_around.down.up.x;
                block.up.x = Random.Range(min_w, block.down.x);
            } else {
                block.up.x = Random.Range(min_w, max_w);
                block.down.x = Random.Range(block.up.x, max_w);
            }
            block.targets.Add(new(0, BSize.y));
            block.targets.Add(block.up);
            block.targets.Add(block.down);
            block.targets.Add(new(BSize.x-1, 0));
        }
        else if (directions[1] == "right"){
            if (block_around.up.isExist && block_around.down.isExist) {
                block.up.x = block_around.up.down.x;
                block.down.x = block_around.down.up.x;
            } else if (block_around.up.isExist){
                block.up.x = block_around.up.down.x;
                block.down.x = Random.Range(min_w, block.up.x);
            } else if (block_around.down.isExist){
                block.down.x = block_around.down.up.x;
                block.up.x = Random.Range(block.down.x, max_w);
            } else {
                block.up.x = Random.Range(min_w, max_w);
                block.down.x = Random.Range(min_w, block.up.x);
            }
            block.targets.Add(new(0, 0));
            block.targets.Add(block.down);
            block.targets.Add(block.up);
            block.targets.Add(new(BSize.x-1, BSize.y));
        }
    }
    else if (directions[0] == "left"){
        if (directions[1] == "up") {
            if (block_around.left.isExist && block_around.up.isExist) {
                block.left.y = block_around.left.right.y;
                block.up.x = block_around.up.down.x;
            } else if (block_around.left.isExist) {
                block.left.y = block_around.left.right.y;
                block.up.x = Random.Range(min_w, max_w);
            } else if (block_around.up.isExist) {
                block.left.y = Random.Range(min_h, max_h);
                block.up.x = block_around.up.down.x;
            } else {
                block.left.y = Random.Range(min_h, max_h);
                block.up.x = Random.Range(min_w, max_w);
            }
            block.targets.Add(block.left);
            block.targets.Add(block.up);
            block.targets.Add(new(BSize.x - 1, BSize.y));
        }
        else if (directions[1] == "down") {
            if (block_around.left.isExist && block_around.down.isExist) {
                block.left.y = block_around.left.right.y;
                block.down.x = block_around.down.up.x;
            } else if (block_around.left.isExist) {
                block.left.y = block_around.left.right.y;
                block.down.x = Random.Range(min_w, max_w);
            } else if (block_around.down.isExist) {
                block.left.y = Random.Range(min_h, max_h);
                block.down.x = block_around.down.up.x;
            } else {
                block.left.y = Random.Range(min_h, max_h);
                block.down.x = Random.Range(min_w, max_w);
            }
            block.targets.Add(block.left);
            block.targets.Add(block.down);
            block.targets.Add(new(BSize.x - 1, 0));
        }
    }
    else if (directions[0] == "right"){
        if (directions[1] == "up"){
            if (block_around.right.isExist && block_around.up.isExist) {
                block.right.y = block_around.right.left.y;
                block.up.x = block_around.up.down.x;
            } else if (block_around.right.isExist) {
                block.right.y = block_around.right.left.y;
                block.up.x = Random.Range(min_w, max_w);
            } else if (block_around.up.isExist) {
                block.right.y = Random.Range(min_h, max_h);
                block.up.x = block_around.up.down.x;
            } else{
                block.right.y = Random.Range(min_h, max_h);
                block.up.x = Random.Range(min_w, max_w);
            }
            block.targets.Add(new(0, BSize.y));
            block.targets.Add(block.up);
            block.targets.Add(block.right);
        }
        else if (directions[1] == "down"){
            if (block_around.right.isExist && block_around.down.isExist) {
                block.right.y = block_around.right.left.y;
                block.down.x = block_around.down.up.x;
            } else if (block_around.right.isExist) {
                block.right.y = block_around.right.left.y;
                block.down.x = Random.Range(min_w, max_w);
            } else if (block_around.down.isExist) {
                block.right.y = Random.Range(min_h, max_h);
                block.down.x = block_around.down.up.x;
            } else {
                block.right.y = Random.Range(min_h, max_h);
                block.down.x = Random.Range(min_w, max_w);
            }
            block.targets.Add(new(0, 0));
            block.targets.Add(block.down);
            block.targets.Add(block.right);
        }
    }

    // ---------- map ----------
    block = _fill_1DBlock_plain(scale, block);
    return block;

}

版权声明:
作者:MWHLS
链接:https://mwhls.top/4846.html
来源:无镣之涯
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
< <上一篇
下一篇>>
文章目录
关闭
目 录