漂亮的数字在关联数组中波动

时间:2013-03-28 18:04:06

标签: javascript algorithm

我实际上是为游戏制作'地面指标引擎'。我基本上做的是将真实地图数据复制到另一个关联数组中,并将其映射到一些指标属性,如生育率或湿度,这是一个示例代码:

this.metricsMap = {};
this.generatedFertilities = {};

for (var i = 0; i < this.groundMap.data.length; i++) {
    this.generatedFertilities[i] = this.groundMap.data[i].map(function () {
        // TODO
    });
}

for (var i = 0; i < this.groundMap.data.length; i++) {
    this.metricsMap[i] = this.generatedFertilities[i].map(function (res) {
        return {
            ownedBy: "",
            fertility: res,
            humidity: 50
        }
    });
}

地图数据是一个关联数组[32] [32]。我想生成具有一致性和“数字波”的相应“生育矩阵”,这里是我想要尝试在较小的关联数组上做的大图:

[68, 69, 70, 71, 72, 73, 74, 75, 74, 73],
[69, 70, 71, 72, 73, 74, 75, 74, 73, 72],
[70, 71, 72, 73, 74, 75, 74, 73, 72, 71],
[71, 72, 73, 74, 75, 74, 73, 72, 71, 60],
[72, 73, 74, 75, 74, 73, 72, 71, 60, 59],
[73, 74, 75, 74, 73, 72, 71, 60, 59, 58],
[74, 75, 74, 73, 72, 71, 60, 59, 58; 57],
[75, 74, 73, 72, 71, 60, 59, 58; 57, 56],
[74, 73, 72, 71, 60, 59, 58; 57, 56, 55],
[73, 72, 71, 60, 59, 58; 57, 56, 55, 54]

我们可以设想更多'模式',例如矩阵中间有更多的浓度。

我尝试了很多这样的事情:

var baseRand = Math.floor(Math.random() * 100) + 1;
for (var i = 0; i < this.groundMap.data.length; i++) {
    var counter = 0;
    this.generatedFertilities[i] = this.groundMap.data[i].map(function () {
        counter++;

        // think of i like the y axis and counter like the x axis
        if(i % 8 == 0){
            if(counter < 8){
                return Math.abs(Math.floor(baseRand++));
            } else {
                return Math.abs(Math.floor(baseRand--));
            }
        } else {
            if(counter > 8){
                return Math.abs(Math.floor(baseRand--));
            } else {
                return Math.abs(Math.floor(baseRand++));
            }
        }
    });
}

但是这并不能让我在某个地方,而我背后的数学也遇到了麻烦。你们怎么设计这样的算法?

1 个答案:

答案 0 :(得分:0)

我不确定我是否正确理解你的问题。你是否与真实的地图数据联系在一起?

如果没有,也许Perlin Noise可以帮到你。这种噪声函数可以很容易地用于游戏中的地形生成。您只需将颜色值映射到高度值。