Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @tool
- extends Node2D
- @export var h_tiles : int = 20
- @export var v_tiles : int = 10
- @export var h_offset : int = 0
- @export var v_offset : int = 2
- @export var MAP_SIZE : Vector2i = Vector2i(3, 3)
- @export var layer_scene : PackedScene
- @export var location_template_scene : PackedScene
- @export var locations_to_create : Array[Vector2i] = []
- @export var tutorial_levels : bool = false
- @export_tool_button("Create Locations") var create = create_tilemaps
- @export_tool_button("Create Item Data") var create_data = create_item_data
- @export_tool_button("Clear Tilemap") var clear = clear_tilemaps
- @export_tool_button("RedrawBorders") var redraw = draw_borders
- @onready var enemies : Node2D = $Enemies
- @onready var respawn_points: Node2D = $RespawnPoints
- @onready var morphers: Node2D = $Morphers
- @onready var teleports: Node2D = $Teleports
- @onready var hazards: Node2D = $Hazards
- @onready var platforms : Node2D = $Platforms
- @onready var levers : Node2D = $Levers
- @onready var items : Node2D = $Items
- @onready var powerups : Node2D = $Powerups
- @onready var doors : Node2D = $Doors
- @onready var vanishing_platforms : Node2D = $VanishingPlatforms
- @onready var signs : Node2D = $Signs
- @onready var core_levers : Node2D = $CoreLevers
- @onready var reference_tilemap_layer : LocationLayer = $Layers/Stone
- var unique_items_locations : Dictionary[Vector2i, Dictionary] = {
- }
- var unique_items_data : Array[Dictionary] = []
- func clear_tilemaps():
- for t : LocationLayer in $Layers.get_children():
- for cell in t.get_used_cells():
- t.set_cell(cell)
- for child in get_children():
- if child is Node2D and child != $Layers:
- for grandchild in child.get_children():
- grandchild.queue_free()
- func _ready() -> void:
- draw_borders()
- func draw_borders():
- $Grid.redraw()
- func create_tilemaps():
- var enemy_array : Array = enemies.get_children()
- var spawn_points_array : Array = respawn_points.get_children()
- var morphers_array : Array = morphers.get_children()
- var teleports_array : Array = teleports.get_children()
- var hazards_array : Array = hazards.get_children()
- var platforms_array : Array = platforms.get_children()
- var levers_array : Array = levers.get_children()
- var vanishing_platforms_array : Array = vanishing_platforms.get_children()
- var door_array : Array = doors.get_children()
- var signs_array : Array = signs.get_children()
- var core_levers_array : Array = core_levers.get_children()
- var thing_arrays : Dictionary[String, Array] = {
- "Enemies" : enemy_array,
- "RespawnPoints" : spawn_points_array,
- "Morphers" : morphers_array,
- "Teleports" : teleports_array,
- "Hazards" : hazards_array,
- "Platforms" : platforms_array,
- "Levers" : levers_array,
- "Doors" : door_array,
- "VanishingPlatforms" : vanishing_platforms_array,
- "Signs" : signs_array,
- "CoreLevers" : core_levers_array,
- }
- unique_items_locations = {}
- var used_cells : Array[Array] = []
- for t : LocationLayer in $Layers.get_children():
- used_cells.append(t.get_used_cells())
- for y in MAP_SIZE.y:
- for x in MAP_SIZE.x:
- var location_created : bool = false
- var location : Location = null
- var coords_offset : Vector2i = -Vector2i(x * h_tiles, y * v_tiles) + Vector2i(h_offset, v_offset)
- if locations_to_create.size() > 0 and !locations_to_create.has(Vector2i(x, y)):
- continue
- for t : LocationLayer in $Layers.get_children():
- var idx : int = t.get_index()
- if used_cells[idx].size() == 0:
- continue
- var cells_in_current_tilemap : Array[Vector2i] = used_cells[idx].filter(filter_cells.bind(x, y))
- if cells_in_current_tilemap.size() == 0:
- continue
- var layer : LocationLayer = layer_scene.instantiate()
- for cell : Vector2i in cells_in_current_tilemap:
- layer.terrain_type = t.terrain_type
- layer.dark_color = t.dark_color
- layer.set_cell(cell + coords_offset, t.get_cell_source_id(cell), t.get_cell_atlas_coords(cell), t.get_cell_alternative_tile(cell))
- layer.name = t.name
- if !location:
- location = location_template_scene.instantiate()
- location.name = "Location_" + str(x) + "_" + str(y)
- location.get_node("Layers").add_child(layer)
- layer.owner = location
- location_created = true
- if !location_created:
- location = null
- continue
- for thing : String in thing_arrays.keys():
- add_things(thing_arrays[thing], location, x, y, coords_offset, thing)
- var scene = PackedScene.new()
- var result = scene.pack(location)
- if result == OK:
- var error = ResourceSaver.save(scene, "res://scenes/new_locations_2/location_" + str(x) + "_" + str(y) +".tscn")
- if error != OK:
- push_error("An error occurred while saving the scene to disk.")
- export_data()
- func add_things(array: Array, location : Location, x : int, y: int, coords_offset : Vector2i, target_node : String):
- var things_in_location = array.filter(filter_things.bind(x, y, reference_tilemap_layer))
- for thing : Node2D in things_in_location:
- var t : Node2D = thing.duplicate()
- if t is Teleport:
- t.location = location
- t.idx = thing.get_index()
- if t is VanishingPlatform:
- t.starting_phase = thing.starting_phase
- t.position = thing.position + Vector2(coords_offset) * 16
- location.get_node(target_node).add_child(t)
- t.owner = location
- func create_item_data():
- for item : UniqueItem in items.get_children():
- var location_coords : Vector2i = Vector2i(item.position.x / 320, item.position.y / 160)
- var coords_offset : Vector2i = -Vector2i(location_coords.x * h_tiles, location_coords.y * v_tiles) + Vector2i(h_offset, v_offset)
- var entry : Dictionary = {}
- entry["location_x"] = location_coords.x
- entry["location_y"] = location_coords.y
- entry["item_type"] = item.type
- entry["position_x"] = item.position.x + coords_offset.x * 16
- entry["position_y"] = item.position.y + coords_offset.y * 16
- unique_items_data.append(entry)
- export_data()
- func export_data():
- var f : FileAccess
- if tutorial_levels:
- f = FileAccess.open("res://item_data_tutorial.json", FileAccess.WRITE)
- else:
- f = FileAccess.open("res://item_data.json", FileAccess.WRITE)
- var json_string = JSON.stringify(unique_items_data)
- f.store_string(json_string)
- func filter_cells(cell: Vector2i, x : int, y : int):
- 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
- func filter_things(thing : Node2D, x : int, y : int, t : LocationLayer):
- var coords = t.local_to_map(thing.position)
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement