Advertisement
esfo

planet generation setup controller

Jan 16th, 2023 (edited)
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function generate_planets(c_coords) {
  2.     a = c_coords.x;
  3.     b = c_coords.y;
  4.    
  5.     // left corner coordinates of chunk
  6.     x_origin = a * chunk_size
  7.     y_origin = b * chunk_size
  8.     cells_n = chunk_size/(spacing*planet_width)
  9.     // width / length of cell
  10.     cd = chunk_size / cells_n;
  11.  
  12.     objects = []
  13.    
  14.     // set seed for chunk
  15.     if (a >= b) {
  16.         s = a*a+a+b;
  17.     }
  18.     else {
  19.         s = a+b*b;
  20.     }
  21.     random_set_seed(s + seed)
  22.    
  23.    
  24.    
  25.     for (cx = 0; cx < cells_n - 1; cx++) {
  26.         for (cy = 0; cy < cells_n - 1; cy++) {
  27.             if (random(1) < chance) {
  28.                 tex = textures[irandom(array_length(textures) - 1)]
  29.                 px = x_origin + cx*cd + cd / 2 + max(0, random_range(0, spacing*planet_width - planet_width))
  30.                 py = y_origin + cy*cd + cd / 2 + max(0, random_range(0, spacing*planet_width - planet_width))
  31.                 p = instance_create_layer(px, py, "Instances", planet)
  32.                 p.sprite_index = tex
  33.                 array_push(objects, p)
  34.             }
  35.         }
  36.     }
  37.     return objects
  38. }
  39.  
  40.  
  41. function delete_objects_array(objects) {
  42.     for (i = 0; i < array_length(objects); i++) {
  43.         instance_destroy(objects[i]);
  44.     }
  45. }
  46.  
  47.  
  48. function switch_chunk(c_coords, old_c_coords) {
  49.     cdx = c_coords.x - old_c_coords.x;
  50.     cdy = c_coords.y - old_c_coords.y;
  51.    
  52.     array_size = render_distance * 2 + 1
  53.    
  54.     // show_debug_message(cdx)
  55.     if (cdx == 1) {
  56.        
  57.         for (yi = 0; yi < array_size; yi++) {
  58.             delete_objects_array(oldobjects[0][yi])
  59.             show_debug_message(oldobjects[0][yi])
  60.         }
  61.        
  62.         // show_debug_message(oldobjects[array_size - 1][array_size - 1]);
  63.         for (xi = 1; xi < array_size; xi++) {
  64.             for (yi = 0; yi < array_size; yi++) {
  65.                 len = array_length(oldobjects[xi][yi])
  66.                 oldobjects[xi - 1][yi] = []
  67.                 array_copy(oldobjects[xi - 1][yi], 0, oldobjects[xi][yi],0, len)
  68.             }
  69.         }
  70.         // show_debug_message(oldobjects[array_size - 1][array_size - 1]);
  71.        
  72.         for (yi = 0; yi < array_size; yi++) {
  73.             gen_coords = {
  74.                 x : c_coords.x + render_distance,
  75.                 y : c_coords.y + yi - render_distance
  76.             }
  77.             oldobjects[array_size - 1][yi] = generate_planets(gen_coords)  
  78.         }
  79.     } else if (cdx == -1) {
  80.         // delete old planets
  81.         for (yi = 0; yi < array_size; yi++) {
  82.             delete_objects_array(oldobjects[array_size - 1][yi])
  83.             show_debug_message(oldobjects[array_size - 1][yi])
  84.         }
  85.        
  86.         for (xi = array_size - 2; xi >= 0; xi--) {
  87.             for (yi = 0; yi < array_size; yi++) {
  88.                 len = array_length(oldobjects[xi][yi])
  89.                 oldobjects[xi + 1][yi] = []
  90.                 array_copy(oldobjects[xi + 1][yi], 0, oldobjects[xi][yi],0, len)
  91.             }
  92.         }
  93.        
  94.         for (yi = 0; yi < array_size; yi++) {
  95.             gen_coords = {
  96.                 x: c_coords.x - render_distance,
  97.                 y: c_coords.y + yi - render_distance
  98.             }
  99.             oldobjects[0] [yi] = generate_planets(gen_coords)
  100.         }
  101.     } else if (cdy == 1) {
  102.         for (xi = 0; xi < array_size; xi++) {
  103.             delete_objects_array(oldobjects[xi][0])
  104.             show_debug_message(oldobjects[xi][0])
  105.         }
  106.        
  107.         // show_debug_message(oldobjects[array_size - 1][array_size - 1]);
  108.         for (zi = 1; zi < array_size; zi++) {
  109.             for (yi = 0; yi < array_size; yi++) {
  110.                 len = array_length(oldobjects[yi][zi])
  111.                 oldobjects[yi][zi - 1] = []
  112.                 array_copy(oldobjects[yi][zi - 1], 0, oldobjects[yi][zi],0, len)
  113.             }
  114.         }
  115.         // show_debug_message(oldobjects[array_size - 1][array_size - 1]);
  116.        
  117.         for (xi = 0; xi < array_size; xi++) {
  118.             gen_coords = {
  119.                 x : c_coords.x + xi - render_distance,
  120.                 y : c_coords.y + render_distance
  121.             }
  122.             oldobjects[xi][array_size - 1] = generate_planets(gen_coords)  
  123.         }
  124.     } else if (cdy == -1) {
  125.         for (yi = 0; yi < array_size; yi++) {
  126.             delete_objects_array(oldobjects[yi][array_size - 1])
  127.             show_debug_message(oldobjects[yi][array_size - 1])
  128.         }
  129.        
  130.         for (xi = array_size - 2; xi >= 0; xi--) {
  131.             for (yi = 0; yi < array_size; yi++) {
  132.                 len = array_length(oldobjects[yi][xi])
  133.                 oldobjects[yi][xi + 1] = []
  134.                 array_copy(oldobjects[yi][xi + 1], 0, oldobjects[yi][xi],0, len)
  135.             }
  136.         }
  137.        
  138.         for (yi = 0; yi < array_size; yi++) {
  139.             gen_coords = {
  140.                 x: c_coords.x + yi - render_distance,
  141.                 y: c_coords.y - render_distance
  142.             }
  143.             oldobjects[yi][0]  = generate_planets(gen_coords)
  144.         }
  145.     }
  146. }
  147.  
  148. textures = [m1]
  149.  
  150. randomize()
  151. seed = random_get_seed()
  152.  
  153. chunk = {
  154.     x : floor(player_x / chunk_size),
  155.     y : floor(player_y / chunk_size)
  156. }
  157.  
  158.  
  159. oldobjects = []
  160. for (i = 0; i < render_distance * 2 + 1; i++) {
  161.     for (z = 0; z < render_distance * 2 + 1; z++) {
  162.         oldobjects[i][z] = generate_planets({
  163.         x: chunk.x - render_distance + i,
  164.         y: chunk.y - render_distance + z
  165.         })
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement