Advertisement
Guest User

Untitled

a guest
May 28th, 2015
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.11 KB | None | 0 0
  1. /*
  2.      * Метод генерации карты мира
  3.      */
  4.    
  5.     public function _generate_world() {
  6.        
  7.         $arr_coords = array();
  8.        
  9.         $blocks_x = 49;
  10.         $blocks_y = 49;
  11.         $all_blocks = $blocks_x * $blocks_y;
  12.        
  13.         $def_x = -24;
  14.         $def_y = 24;
  15.         $s = 0;
  16.        
  17.         $coord_x = NULL;
  18.         $coord_y = NULL;
  19.        
  20.         $i = 0;
  21.         while($i < $all_blocks) {
  22.            
  23.             if($s < 49) {
  24.                 $coord_x = $def_x + $s;
  25.                 $coord_y = $def_y;
  26.                 $arr_coords[$coord_x.'&'.$coord_y] = 0;
  27.                
  28.             }
  29.            
  30.             else {
  31.                 $coord_x = $def_x;
  32.                 $coord_y = $def_y = $def_y - 1;
  33.                 $arr_coords[$coord_x.'&'.$coord_y] = 0;
  34.                 $s = 0;
  35.             }
  36.            
  37.             $i++;
  38.             $s++;
  39.            
  40.         }
  41.        
  42.         $rand_keys = array_rand($arr_coords, 250);
  43.        
  44.         foreach($rand_keys as $key) {
  45.             $x = rand(0, 100);
  46.             $zone = null;
  47.             if($x > 0 && $x < 60) { $zone = 1; }
  48.             elseif($x > 60 && $x < 85) { $zone = 2;}
  49.             elseif($x > 85 && $x < 95) { $zone = 3;}
  50.             else { $zone = 4; }
  51.             $arr_coords[$key] = $zone;
  52.         }
  53.        
  54.         $i = 0;
  55.         while($i < 100) {
  56.             foreach($arr_coords as $key => $value) {
  57.                 if($value != 0) {
  58.                     $xy_coords = explode('&', $key);
  59.                     $x_coord = $xy_coords[0];
  60.                     $y_coord = $xy_coords[1];
  61.                     $left_coords = ($x_coord - 1) . '&' . $y_coord;
  62.                     $top_coords = $x_coord . '&' . ($y_coord + 1);
  63.                     $right_coords = ($x_coord + 1) . '&' . $y_coord;
  64.                     $bottom_coords = $x_coord . '&' . ($y_coord - 1);
  65.                    
  66.                     if(isset($arr_coords[$left_coords]) && $arr_coords[$left_coords] == 0) { $arr_coords[$left_coords] = $value; }
  67.                     if(isset($arr_coords[$top_coords]) && $arr_coords[$top_coords] == 0) { $arr_coords[$top_coords] = $value; }
  68.                     if(isset($arr_coords[$right_coords]) && $arr_coords[$right_coords] == 0) { $arr_coords[$right_coords] = $value; }
  69.                     if(isset($arr_coords[$bottom_coords]) && $arr_coords[$bottom_coords] == 0) { $arr_coords[$bottom_coords] = $value; }
  70.                 }
  71.             }
  72.             $i++;
  73.         }
  74.        
  75.         foreach($arr_coords as $key => $value) {
  76.            
  77.             echo '<div style="width: 25px; height: 25px; float: left; border: 1px solid black;';
  78.             if($value == 1) { echo 'background: #00FA9A;">';}
  79.             elseif($value == 2) { echo 'background: green;">';}
  80.             elseif($value == 3) { echo 'background: blue;">';}
  81.             elseif($value == 4) { echo 'background: gray;">';}
  82.             else { echo 'background: #ffffff;">';}
  83.            
  84.                 echo $value;
  85.            
  86.             echo '</div>';
  87.            
  88.            
  89.         }
  90.        
  91.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement