[开发者心得] 地图上的建筑会实时改变,如何让寻路区域动态改变

[复制链接]
500 |0
感情偏食BZ8 发表于 2023-4-26 13:40:04 | 显示全部楼层 |阅读模式
本帖最后由 感情偏食BZ8 于 2023-4-26 13:40 编辑


上述视频为建筑随机后动态寻路示例的最终实现效果


当地图上建筑随机变化时,去寻找建筑本质上就是获得随即后的location那我们无妨在使用寻路时直接把目标建筑的location作为寻路的参数放入。
看代码:

protected onUpdate(dt: number): void {
        this._timer += dt;
        if (this._timer >= 1) {
            let tempLocation = this._target.worldLocation;
            tempLocation.y = Math.random() * 2000 + 4000;
            tempLocation.x = Math.random() * 3000;
            this._target.worldLocation = tempLocation;
            this._timer = 0;
            if (this._isFollow) {
                Gameplay.moveTo(Gameplay.getCurrentPlayer().character, this._target.worldLocation, 5, () => { oTrace("success"); }, () => { oTrace("fail"); })
            }
        }
    }

我们在需要进行寻路时让寻路中的_isFollow改变状态为true。上述代码中每一秒将会调用一次随机地点并且在设置跟随后同时调用寻路,实现不管建筑随机到哪,都能够实时的去寻路。当然通常情况下建筑的随机与寻路与建筑的随机应该是分开的。

特别注意:
建筑随机的范围务必保证被寻路区域覆盖,否则将会寻路失败。
回复

使用道具 举报

热门版块
快速回复 返回顶部 返回列表