Advertisement
Neverknow247

Nebula Planet Generation Godot

Nov 27th, 2024
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 5.88 KB | Source Code | 0 0
  1. extends Node
  2. class_name Planet_Generator
  3.  
  4. var stats = Stats
  5. var rng = RandomNumberGenerator.new()
  6.  
  7. var all_rooms = All_Rooms.new()
  8. var all_enemies = All_Enemies.new()
  9. var all_upgrades = All_Upgrades.new()
  10. var all_items = All_Items.new()
  11.  
  12. #var rooms
  13. var planet = []
  14. var planet_detailed = []
  15. var planet_room = {
  16.     "location":Vector2.ZERO,
  17.     "north_door":false,
  18.     "south_door":false,
  19.     "east_door":false,
  20.     "west_door":false,
  21.     "room_type":"n/a",
  22.     "planet_type":"n/a",
  23.     "room_number":0,
  24.     "enemies":[],
  25.     "items":[],
  26.     "upgrade":null,
  27. }
  28. var door_names = ["north_door","south_door","east_door","west_door"]
  29. var starting_room = Vector2.ZERO
  30. var final_room = Vector2.ZERO
  31.  
  32. #planet types:
  33. #0 - null
  34. #1 - landing (top, left, right)
  35. #2 - corridor (left, right)
  36. #3 - drop (top, left, right, bottom)
  37. #4 - other (n/a)
  38.  
  39. var directions = [Vector2(0,-1),Vector2(0,1),Vector2(-1,0),Vector2(1,0)]
  40. var directions_sans_up = [Vector2(0,1),Vector2(-1,0),Vector2(1,0)]
  41. func generate_planet(size,planet_type):
  42.     rng.seed = stats.save_data.run_data.current_planet.planet_seed
  43.     var rooms
  44.     if size == 0:
  45.         rooms = rng.randi_range(5,9)
  46.     else:
  47.         rooms = rng.randi_range(10,20)
  48.     if planet_type == "first_planet":
  49.         rooms = 4
  50.     starting_room = Vector2.ZERO
  51.     planet.push_front(starting_room)
  52.     var current_room = starting_room+Vector2(0,1)
  53.     planet.push_front(current_room)
  54.     var counter = 0
  55.     while planet.size() < rooms:
  56.         var possible_directions
  57.         if current_room.y<=1:
  58.             possible_directions = directions_sans_up.duplicate()
  59.         else:
  60.             possible_directions = directions.duplicate()
  61.         var next_room = Vector2.ZERO
  62.         var good_room = false
  63.         while !good_room:
  64.             if possible_directions.size() == 0:
  65.                 counter+=1
  66.                 current_room = planet[counter]
  67.                 if current_room.y<=1:
  68.                     possible_directions = directions_sans_up.duplicate()
  69.                 else:
  70.                     possible_directions = directions.duplicate()
  71.             next_room = current_room+possible_directions.pop_at(rng.randi_range(0,possible_directions.size()-1))
  72.             if next_room not in planet:
  73.                 planet.push_front(next_room)
  74.                 good_room = true
  75.                 current_room = next_room
  76.                 counter = 0
  77.     final_room = planet[0]
  78.     for room in planet:
  79.         if starting_room.distance_to(room)>starting_room.distance_to(final_room):
  80.             final_room = room
  81.     for room in planet:
  82.         var new_room = planet_room.duplicate()
  83.         new_room["location"] = room
  84.         new_room["planet_type"] = planet_type
  85.         if room == starting_room:
  86.             new_room["room_type"] = "starting_room"
  87.         elif room == final_room:
  88.             new_room["room_type"] = "final_room"
  89.            
  90.         for i in directions.size():
  91.             if room+directions[i] in planet:
  92.                 new_room[door_names[i]] = true
  93.         planet_detailed.push_front(new_room)
  94.     for room in planet_detailed:
  95.         if room["room_type"] == "n/a":
  96.             if room["south_door"]:
  97.                 room["room_type"] = "drop"
  98.             elif room["east_door"] && room["west_door"]:
  99.                 room["room_type"] = "corridor"
  100.             else:
  101.                 room["room_type"] = "landing"
  102.         room["room_number"] = choose_room(room["room_type"],room["planet_type"])
  103.         room["enemies"] = generate_enemy_data(room["room_type"],room["planet_type"],room["room_number"])
  104.         room["upgrade"] = generate_upgrade_data(room["room_type"],room["planet_type"],room["room_number"])
  105.         room["items"] = generate_item_data(room["room_type"],room["planet_type"],room["room_number"])
  106.        
  107.     #print(planet)
  108.     #print(JSON.stringify(planet_detailed,"\t",false))
  109.  
  110. func choose_room(room_type,planet_type):
  111.     return rng.randi_range(0,all_rooms.rooms[planet_type][room_type].size()-1)
  112.  
  113. func generate_enemy_data(room_type,planet_type,room_number):
  114.     var data = []
  115.     if room_type == "starting_room" or room_type == "final_room":
  116.         return data
  117.     var room = all_rooms.rooms[planet_type][room_type][room_number].instantiate()
  118.     var enemy_positions = room.get_node("enemies").get_children()
  119.     var enemy_numbers = rng.randi_range(0,enemy_positions.size()-1)
  120.     for i in enemy_numbers:
  121.         var new_enemy = {
  122.             "position":Vector2.ZERO,
  123.             "type_index":0
  124.         }
  125.         var enemy_position_index = rng.randi_range(0,enemy_positions.size()-1)
  126.         var enemy_type_index = rng.randi_range(0,all_enemies.enemies[stats.save_data.run_data.current_planet.biome].size()-1)
  127.         new_enemy["position"] = enemy_positions[enemy_position_index].position
  128.         new_enemy["type_index"] = enemy_type_index
  129.         data.append(new_enemy)
  130.         enemy_positions.pop_at(enemy_position_index)
  131.     return data
  132.  
  133. func generate_upgrade_data(room_type,planet_type,room_number):
  134.     var data = {
  135.         "position":Vector2.ZERO,
  136.         "upgrade_type":"",
  137.     }
  138.     if room_type != "final_room":
  139.         return null
  140.     var room = all_rooms.rooms[planet_type][room_type][room_number].instantiate()
  141.     data["position"] = room.get_node("upgrade").position
  142.     if planet_type == "first_planet":
  143.         data["upgrade_type"] = "wall_climbing_unlocked"
  144.         return data
  145.     else:
  146.         var chance = rng.randi_range(1,10)
  147.         if chance == 10:
  148.             var upgrade_list = all_upgrades.list_available_unlockables()
  149.             if upgrade_list.size() == 0:
  150.                 return null
  151.             else:
  152.                 data["upgrade_type"] = upgrade_list[rng.randi_range(0,upgrade_list.size()-1)]
  153.                 print(data)
  154.                 return data
  155.         else:
  156.             return null
  157.  
  158. func generate_item_data(room_type,planet_type,room_number):
  159.     var data = []
  160.     if room_type == "starting_room" or room_type == "final_room":
  161.         return data
  162.     var room = all_rooms.rooms[planet_type][room_type][room_number].instantiate()
  163.     var item_positions = room.get_node("items").get_children()
  164.     var item_numbers = rng.randi_range(0,item_positions.size()-1)
  165.     for i in item_numbers:
  166.         var new_item = {
  167.             "position":Vector2.ZERO,
  168.             "item_type":""
  169.         }
  170.         var item_position_index = rng.randi_range(0,item_positions.size()-1)
  171.         var items = all_items.return_item_list()
  172.         var item_index = rng.randi_range(0,items.size()-1)
  173.         new_item["position"] = item_positions[item_position_index].position
  174.         new_item["item_type"] = items[item_index]
  175.         data.append(new_item)
  176.         item_positions.pop_at(item_position_index)
  177.     return data
  178.  
  179.  
  180.  
  181.  
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement