我需要帮助来优化基于图块的游戏的世界生成算法

时间:2018-10-21 07:26:07

标签: game-maker-studio-1.4

因此,我正在GameMaker:Studio 1.4中创建基于2D磁贴的自上而下游戏。这是当今世界的当前脚本:

//Generate Center Points
var centerPointCount = 500;
var centerPoint;
var centerPointX;
var centerPointY;
for (var p = 0; p < centerPointCount; p++){
    switch (irandom(3)){
        case 0: centerPoint[p] = 0; //Grass
        break;
        case 1: centerPoint[p] = 0; //Grass
        break;
        case 2: centerPoint[p] = 0; //Grass
        break;
        case 3: centerPoint[p] = 1; //Water
        break;
        default: centerPoint[p] = 0;
        break;
    }
    centerPointX[p] = irandom(499);
    centerPointY[p] = irandom(499);
}

//Set Tiles Based On Center Points
var tiles;
for (var c = 0; c < 500; c++){
    for (var r = 0; r < 500; r++){
        var centerPointDistance = 1000000000;
        var centerPointType;
        for (var p = 0; p < centerPointCount; p++){ //Find Closest Center Point
            var distancePrime = sqr(c - centerPointX[p]) + sqr(r - centerPointY[p]);
            if (distancePrime < centerPointDistance){
                centerPointType = centerPoint[p];
                centerPointDistance = distancePrime;
            }
        }
        tiles[c, r] = centerPointType; //Set Tile To Center Point Type
    }
}

//Generate Objects (Trees, Rocks, Etc.)
var objects;
for (var c = 0; c < 500; c++){
    for (var r = 0; r < 500; r++){
        switch (tiles[c, r]){ //Tile Object Is On
            case 0: //Grass
                if (random(1) <= 0.10){
                    objects[c, r] = 0; //Tree
                } else{
                    objects[c, r] = -1;
                }
            break;
            case 1: //Water
                objects[c, r] = -1;
            break;
            default:
                objects[c, r] = -1;
            break;
        }
    }
}

//Return The World Generated
var world;
world[0] = tiles;
world[1] = objects;
return world;

在Ryzen 1300x处理器上运行此代码大约需要2分钟,因此我真的需要更好地优化它,因为我也计划对其进行扩展,这将使它花费更长的时间。

0 个答案:

没有答案