Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function generate_planets(c_coords) {
- a = c_coords.x;
- b = c_coords.y;
- // left corner coordinates of chunk
- x_origin = a * chunk_size
- y_origin = b * chunk_size
- cells_n = chunk_size/(spacing*planet_width)
- // width / length of cell
- cd = chunk_size / cells_n;
- objects = []
- // set seed for chunk
- if (a >= b) {
- s = a*a+a+b;
- }
- else {
- s = a+b*b;
- }
- random_set_seed(s + seed)
- for (cx = 0; cx < cells_n - 1; cx++) {
- for (cy = 0; cy < cells_n - 1; cy++) {
- if (random(1) < chance) {
- tex = textures[irandom(array_length(textures) - 1)]
- px = x_origin + cx*cd + cd / 2 + max(0, random_range(0, spacing*planet_width - planet_width))
- py = y_origin + cy*cd + cd / 2 + max(0, random_range(0, spacing*planet_width - planet_width))
- p = instance_create_layer(px, py, "Instances", planet)
- p.sprite_index = tex
- array_push(objects, p)
- }
- }
- }
- return objects
- }
- function delete_objects_array(objects) {
- for (i = 0; i < array_length(objects); i++) {
- instance_destroy(objects[i]);
- }
- }
- function switch_chunk(c_coords, old_c_coords) {
- cdx = c_coords.x - old_c_coords.x;
- cdy = c_coords.y - old_c_coords.y;
- array_size = render_distance * 2 + 1
- // show_debug_message(cdx)
- if (cdx == 1) {
- for (yi = 0; yi < array_size; yi++) {
- delete_objects_array(oldobjects[0][yi])
- show_debug_message(oldobjects[0][yi])
- }
- // show_debug_message(oldobjects[array_size - 1][array_size - 1]);
- for (xi = 1; xi < array_size; xi++) {
- for (yi = 0; yi < array_size; yi++) {
- len = array_length(oldobjects[xi][yi])
- oldobjects[xi - 1][yi] = []
- array_copy(oldobjects[xi - 1][yi], 0, oldobjects[xi][yi],0, len)
- }
- }
- // show_debug_message(oldobjects[array_size - 1][array_size - 1]);
- for (yi = 0; yi < array_size; yi++) {
- gen_coords = {
- x : c_coords.x + render_distance,
- y : c_coords.y + yi - render_distance
- }
- oldobjects[array_size - 1][yi] = generate_planets(gen_coords)
- }
- } else if (cdx == -1) {
- // delete old planets
- for (yi = 0; yi < array_size; yi++) {
- delete_objects_array(oldobjects[array_size - 1][yi])
- show_debug_message(oldobjects[array_size - 1][yi])
- }
- for (xi = array_size - 2; xi >= 0; xi--) {
- for (yi = 0; yi < array_size; yi++) {
- len = array_length(oldobjects[xi][yi])
- oldobjects[xi + 1][yi] = []
- array_copy(oldobjects[xi + 1][yi], 0, oldobjects[xi][yi],0, len)
- }
- }
- for (yi = 0; yi < array_size; yi++) {
- gen_coords = {
- x: c_coords.x - render_distance,
- y: c_coords.y + yi - render_distance
- }
- oldobjects[0] [yi] = generate_planets(gen_coords)
- }
- } else if (cdy == 1) {
- for (xi = 0; xi < array_size; xi++) {
- delete_objects_array(oldobjects[xi][0])
- show_debug_message(oldobjects[xi][0])
- }
- // show_debug_message(oldobjects[array_size - 1][array_size - 1]);
- for (zi = 1; zi < array_size; zi++) {
- for (yi = 0; yi < array_size; yi++) {
- len = array_length(oldobjects[yi][zi])
- oldobjects[yi][zi - 1] = []
- array_copy(oldobjects[yi][zi - 1], 0, oldobjects[yi][zi],0, len)
- }
- }
- // show_debug_message(oldobjects[array_size - 1][array_size - 1]);
- for (xi = 0; xi < array_size; xi++) {
- gen_coords = {
- x : c_coords.x + xi - render_distance,
- y : c_coords.y + render_distance
- }
- oldobjects[xi][array_size - 1] = generate_planets(gen_coords)
- }
- } else if (cdy == -1) {
- for (yi = 0; yi < array_size; yi++) {
- delete_objects_array(oldobjects[yi][array_size - 1])
- show_debug_message(oldobjects[yi][array_size - 1])
- }
- for (xi = array_size - 2; xi >= 0; xi--) {
- for (yi = 0; yi < array_size; yi++) {
- len = array_length(oldobjects[yi][xi])
- oldobjects[yi][xi + 1] = []
- array_copy(oldobjects[yi][xi + 1], 0, oldobjects[yi][xi],0, len)
- }
- }
- for (yi = 0; yi < array_size; yi++) {
- gen_coords = {
- x: c_coords.x + yi - render_distance,
- y: c_coords.y - render_distance
- }
- oldobjects[yi][0] = generate_planets(gen_coords)
- }
- }
- }
- textures = [m1]
- randomize()
- seed = random_get_seed()
- chunk = {
- x : floor(player_x / chunk_size),
- y : floor(player_y / chunk_size)
- }
- oldobjects = []
- for (i = 0; i < render_distance * 2 + 1; i++) {
- for (z = 0; z < render_distance * 2 + 1; z++) {
- oldobjects[i][z] = generate_planets({
- x: chunk.x - render_distance + i,
- y: chunk.y - render_distance + z
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement