2017-01-06 1 views
0

モーメントで私はマップを使って小さなゲームをします。 すべて動作しますが、何かが不明です。ゲームマップボーダー計算

あなたは私が持っていると、再び何をしたいことをここに見ることができます。 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>"; 

これは作業になります。

しかし、私の問題は、同じタイルの2つの以上のパターンがあることが可能であろう、..です

私の考えでは、アレイにも

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(array( '0'、 '1'、 '1'、 '0'、 '1'、 '1' 、 '0'、 '0'、 '0')、$ array [2]);

この問題はどのように解決できますか?どのように私は彼らにこの番号を知らせることができますか?

これは、数値は、現時点では、次のとおりです。 https://picul.de/view/Hs-

1-50他の一つであり、テストの

それとも別の質問: は/コーナーなど国境を「計算」するためのより良い方法はありますか?

挨拶 ダニエル

答えて

0

は、このような配列検索:

$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 ];