游戏地图边界计算

Gamemap Border Calculation

目前我正在制作一个带有地图的小游戏。 一切正常,但有些地方不清楚。

正如你在这里看到的,我再次拥有和想要的东西: https://picul.de/view/HsY

目前我的代码如下所示:

function calcTile ($x, $y, $tilemin, $tilemax,$atilemin, $atilemax){

//ActualTile 
$m  = calcNoise($x, $y);

//Above Tiles
$tl = calcNoise($x-1, $y-1);
$t  = calcNoise($x, $y-1);
$tr = calcNoise($x+1, $y-1);

//Right Tile
$r  = calcNoise($x+1, $y);

//Below Tiles
$br = calcNoise($x+1, $y+1);
$b  = calcNoise($x, $y+1);
$bl = calcNoise($x-1, $y+1);

//Left Tiles
$l  = calcNoise($x-1, $y);

    if(
        $t >= $tilemin && $t <= $tilemax &&
        $r >= $tilemin && $r <= $tilemax &&
        $b >= $tilemin && $b <= $tilemax &&
        $l >= $tilemin && $l <= $tilemax 
    ){

        $field = 50;

    } else 

//Vertical
    if(
        $t >= $atilemin && $t <= $atilemax &&
        $r >= $tilemin && $r <= $tilemax &&
        $b >= $atilemin && $b <= $atilemax &&
        $l >= $tilemin && $l <= $tilemax
    ){

        $field = 45;

    } else  

//Horizontal
    if(
        $t >= $tilemin && $t <= $tilemax &&
        $r >= $atilemin && $r <= $atilemax &&
        $b >= $tilemin && $b <= $tilemax &&
        $l >= $atilemin && $l <= $atilemax
    ){

        $field = 46;

    } else {
        ...
    }
}

所以我的新想法是用这样的模式显示:

$array = array(
        2 => array(
                '0', '1', '1',
                '0', '1', '1',
                '0', '0', '0'

        ), 

        3 => array( '1', '1', '0',
                    '1', '1', '0',
                    '0', '0', '0'
        ),

        4 => array( '0', '0', '0',
                    '1', '1', '0',
                    '1', '1', '0'
        )       
    );

$key = array_search(array('0', '1', '1',
                    '0', '1', '1',
                    '0', '0', '0'), $array);  // $key = 2;
echo $key."<br><br>";

这将是工作。

但我的问题是,同一个 Tile 可能有 2 个或更多 Patterns..

我的想法是也对数组进行排列

array(
        2 => array( array(
                '0', '1', '1',
                '0', '1', '1',
                '0', '0', '0'
             ), array(
                '0', '1', '1',
                '0', '1', '1',
                '0', '0', '1'
             )
        )

但现在的问题是我找不到东西,因为 array_search 想要数组编号:

$key = array_search(数组('0', '1', '1', '0', '1', '1', '0', '0', '0'), $array[2]);

但是我该如何解决这个问题呢?我怎样才能让他们知道这个号码?

这是目前的数字: https://picul.de/view/Hs-

1-50 另一个是测试的

或另一个问题: "Calculate" 边框/角等是否有更好的方法?

您好 丹尼尔

像这样搜​​索数组:

$tiles = [
    1 => [
        '0', '1', '1',
        '0', '1', '1',
        '0', '0', '0'
    ],
    2 => [
        '1', '1', '0',
        '1', '1', '0',
        '0', '0', '0'
    ],
    42 => [
        '0', '0', '0',
        '1', '1', '0',
        '1', '1', '0'
    ]
];

$search_tile = [
    '0', '1', '1',
    '0', '1', '1',
    '0', '0', '0'
];

$found_key = false;

foreach ($tiles as $key => $value) {
    // compare both arrays in some way
    if (implode('', $tile) == implode('', $search_tile)) {
        $found_key = $key;
    }
}

...但是如果您为游戏地图定义数据结构,则在这种情况下不需要循环。这样您还可以定义具有完全相同图案的各种图块,而不会在任何时候混淆它们。

// these are the available tiles

$tiles = [
    1 => [
        '0', '1', '1',
        '0', '1', '1',
        '0', '0', '0'
    ],
    2 => [
        '1', '1', '0',
        '1', '1', '0',
        '0', '0', '0'
    ],
    12 => [
        '0', '0', '0',
        '1', '1', '0',
        '1', '1', '0'
    ]
];



// this is a game map of 3x2 fields, with a tile mapped to each field.

$map = [
    [ /* field A0 */
        'tile' => 1,
        'other_property' => 'x'
    ],
    [ /* field A1 */
        'tile' => 12,
        'other_property' => 'y'
    ],
    [ /* field A2 */
        'tile' => 2,
        'other_property' => 'z'
    ],
    [ /* field B0 */
        'tile' => 1,
        'other_property' => 'x'
    ],
    [ /* field B1 */
        'tile' => 1,
        'other_property' => 'a'
    ],
    [ /* field B2 */
        'tile' => 12,
        'other_property' => 'b'
    ],
];



// this function returns tile number belonging to the field at X/Y

function get_tile($x, $y, $map, $map_width) {
    $index = $y * $map_width + $x;
    return $map[$index]['tile'];
}


// get the bottom right field's tile number

$tile_index = get_tile( 2, 1, $map, 3 );


// get the tile array from $tiles

$current_tile = $tiles[ $tile_index ];