Advertisement
Guest User

Untitled

a guest
May 24th, 2025
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @tool
  2. extends Node2D
  3.  
  4. @export var h_tiles : int = 20
  5. @export var v_tiles : int = 10
  6. @export var h_offset : int = 0
  7. @export var v_offset : int = 2
  8.  
  9. @export var MAP_SIZE : Vector2i = Vector2i(3, 3)
  10.  
  11. @export var layer_scene : PackedScene
  12. @export var location_template_scene : PackedScene
  13.  
  14. @export var locations_to_create : Array[Vector2i] = []
  15.  
  16. @export var tutorial_levels : bool = false
  17.  
  18. @export_tool_button("Create Locations") var create = create_tilemaps
  19. @export_tool_button("Create Item Data") var create_data = create_item_data
  20. @export_tool_button("Clear Tilemap") var clear = clear_tilemaps
  21. @export_tool_button("RedrawBorders") var redraw = draw_borders
  22.  
  23. @onready var enemies : Node2D = $Enemies
  24. @onready var respawn_points: Node2D = $RespawnPoints
  25. @onready var morphers: Node2D = $Morphers
  26. @onready var teleports: Node2D = $Teleports
  27. @onready var hazards: Node2D = $Hazards
  28. @onready var platforms : Node2D = $Platforms
  29. @onready var levers : Node2D = $Levers
  30. @onready var items : Node2D = $Items
  31. @onready var powerups : Node2D = $Powerups
  32. @onready var doors : Node2D = $Doors
  33. @onready var vanishing_platforms : Node2D = $VanishingPlatforms
  34. @onready var signs : Node2D = $Signs
  35. @onready var core_levers : Node2D = $CoreLevers
  36. @onready var reference_tilemap_layer : LocationLayer = $Layers/Stone
  37.  
  38. var unique_items_locations : Dictionary[Vector2i, Dictionary] = {
  39.  
  40. }
  41. var unique_items_data : Array[Dictionary] = []
  42.  
  43.  
  44.  
  45. func clear_tilemaps():
  46.     for t : LocationLayer in $Layers.get_children():
  47.         for cell in t.get_used_cells():
  48.             t.set_cell(cell)
  49.  
  50.     for child in get_children():
  51.         if child is Node2D and child != $Layers:
  52.             for grandchild in child.get_children():
  53.                 grandchild.queue_free()
  54.  
  55.  
  56.  
  57. func _ready() -> void:
  58.     draw_borders()
  59.  
  60. func draw_borders():
  61.     $Grid.redraw()
  62.  
  63.  
  64.  
  65.  
  66. func create_tilemaps():
  67.    
  68.  
  69.     var enemy_array : Array = enemies.get_children()
  70.     var spawn_points_array : Array = respawn_points.get_children()
  71.     var morphers_array : Array = morphers.get_children()
  72.     var teleports_array : Array = teleports.get_children()
  73.     var hazards_array : Array = hazards.get_children()
  74.     var platforms_array : Array = platforms.get_children()
  75.     var levers_array : Array = levers.get_children()
  76.     var vanishing_platforms_array : Array = vanishing_platforms.get_children()
  77.     var door_array : Array = doors.get_children()
  78.     var signs_array : Array = signs.get_children()
  79.     var core_levers_array : Array = core_levers.get_children()
  80.  
  81.     var thing_arrays : Dictionary[String, Array] = {
  82.         "Enemies" : enemy_array,
  83.         "RespawnPoints" : spawn_points_array,
  84.         "Morphers" : morphers_array,
  85.         "Teleports" : teleports_array,
  86.         "Hazards" : hazards_array,
  87.         "Platforms" : platforms_array,
  88.         "Levers" : levers_array,
  89.         "Doors" : door_array,
  90.         "VanishingPlatforms" : vanishing_platforms_array,
  91.         "Signs" : signs_array,
  92.         "CoreLevers" : core_levers_array,
  93.     }
  94.  
  95.  
  96.     unique_items_locations = {}
  97.    
  98.     var used_cells : Array[Array] = []
  99.  
  100.     for t : LocationLayer in $Layers.get_children():
  101.         used_cells.append(t.get_used_cells())
  102.  
  103.     for y in MAP_SIZE.y:
  104.         for x in MAP_SIZE.x:
  105.             var location_created : bool = false
  106.             var location : Location = null
  107.             var coords_offset : Vector2i = -Vector2i(x * h_tiles, y * v_tiles) + Vector2i(h_offset, v_offset)
  108.  
  109.             if locations_to_create.size() > 0 and !locations_to_create.has(Vector2i(x, y)):
  110.                 continue
  111.            
  112.  
  113.             for t : LocationLayer in $Layers.get_children():
  114.                 var idx : int = t.get_index()
  115.                 if used_cells[idx].size() == 0:
  116.                     continue
  117.  
  118.                 var cells_in_current_tilemap : Array[Vector2i] = used_cells[idx].filter(filter_cells.bind(x, y))
  119.                 if cells_in_current_tilemap.size() == 0:
  120.                     continue
  121.                 var layer : LocationLayer = layer_scene.instantiate()
  122.                 for cell : Vector2i in cells_in_current_tilemap:
  123.                     layer.terrain_type = t.terrain_type
  124.                     layer.dark_color = t.dark_color
  125.                     layer.set_cell(cell + coords_offset, t.get_cell_source_id(cell), t.get_cell_atlas_coords(cell), t.get_cell_alternative_tile(cell))
  126.  
  127.                 layer.name = t.name
  128.                 if !location:
  129.                     location = location_template_scene.instantiate()
  130.                     location.name = "Location_" + str(x) + "_" + str(y)
  131.  
  132.                 location.get_node("Layers").add_child(layer)
  133.                 layer.owner = location
  134.                 location_created = true
  135.  
  136.             if !location_created:
  137.                 location = null
  138.                 continue
  139.  
  140.             for thing : String in thing_arrays.keys():
  141.                 add_things(thing_arrays[thing], location, x, y, coords_offset, thing)
  142.        
  143.  
  144.             var scene = PackedScene.new()
  145.  
  146.             var result = scene.pack(location)
  147.             if result == OK:
  148.                 var error = ResourceSaver.save(scene, "res://scenes/new_locations_2/location_" + str(x) + "_" + str(y) +".tscn")
  149.                 if error != OK:
  150.                     push_error("An error occurred while saving the scene to disk.")
  151.  
  152.     export_data()
  153.  
  154. func add_things(array: Array, location : Location, x : int, y: int, coords_offset : Vector2i, target_node : String):
  155.     var things_in_location = array.filter(filter_things.bind(x, y, reference_tilemap_layer))
  156.  
  157.     for thing : Node2D in things_in_location:
  158.    
  159.         var t : Node2D = thing.duplicate()
  160.  
  161.         if t is Teleport:
  162.             t.location = location
  163.             t.idx = thing.get_index()
  164.  
  165.         if t is VanishingPlatform:
  166.             t.starting_phase = thing.starting_phase
  167.            
  168.         t.position = thing.position + Vector2(coords_offset) * 16
  169.         location.get_node(target_node).add_child(t)
  170.         t.owner = location
  171.  
  172.  
  173.  
  174.  
  175. func create_item_data():
  176.     for item : UniqueItem in items.get_children():
  177.         var location_coords : Vector2i = Vector2i(item.position.x / 320, item.position.y / 160)
  178.         var coords_offset : Vector2i = -Vector2i(location_coords.x * h_tiles, location_coords.y * v_tiles) + Vector2i(h_offset, v_offset)
  179.         var entry : Dictionary = {}
  180.         entry["location_x"] = location_coords.x
  181.         entry["location_y"] = location_coords.y
  182.         entry["item_type"] = item.type
  183.         entry["position_x"] = item.position.x + coords_offset.x * 16
  184.         entry["position_y"] = item.position.y + coords_offset.y * 16
  185.  
  186.         unique_items_data.append(entry)
  187.  
  188.     export_data()
  189.  
  190. func export_data():
  191.     var f : FileAccess
  192.     if tutorial_levels:
  193.         f = FileAccess.open("res://item_data_tutorial.json", FileAccess.WRITE)
  194.     else:
  195.         f = FileAccess.open("res://item_data.json", FileAccess.WRITE)
  196.  
  197.     var json_string = JSON.stringify(unique_items_data)
  198.    
  199.     f.store_string(json_string)
  200.  
  201. func filter_cells(cell: Vector2i, x : int, y : int):
  202.     return cell.x  >= x * h_tiles - 1 and cell.x < (x + 1) * h_tiles + 1 and cell.y >= y * v_tiles - 1 and cell.y < (y + 1) * v_tiles + 1
  203.  
  204. func filter_things(thing : Node2D, x : int, y : int, t : LocationLayer):
  205.     var coords = t.local_to_map(thing.position)
  206.     return coords.x  >= x * h_tiles and coords.x < (x + 1) * h_tiles and coords.y >= y * v_tiles and coords.y < (y + 1) * v_tiles
  207.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement