Added import and export for the level designer

This commit is contained in:
2024-05-06 15:02:56 +01:00
parent ad2d467456
commit 031879cf46
18 changed files with 291 additions and 41 deletions

View File

@@ -4,10 +4,7 @@ extends Node2D
func _on_background_item_selected(index: int) -> void:
$Background.texture = load($VBoxContainer/Background.get_item_metadata(index))
func _on_save_pressed() -> void:
if $VBoxContainer/Name.text == "":
return
func get_level_object() -> Dictionary:
var data : Dictionary = {
"name": $VBoxContainer/Name.text,
"left": $VBoxContainer/Left.get_selected_text(),
@@ -21,6 +18,12 @@ func _on_save_pressed() -> void:
var brick = get_node("Bricks/Row%d/Col%d" % [row, col])
s += brick.type
data.data.push_back(s)
return data
func _on_save_pressed() -> void:
if $VBoxContainer/Name.text == "":
return
var data : Dictionary = get_level_object()
DirAccess.make_dir_recursive_absolute("user://Levels")
var f = FileAccess.open("user://Levels/%s.json" % data.name, FileAccess.WRITE)
f.store_string(JSON.stringify(data, " "))
@@ -43,17 +46,20 @@ func _on_load_pressed() -> void:
func _on_load_panel_load_file(filename: String) -> void:
if FileAccess.file_exists(filename):
var data = JSON.parse_string(FileAccess.get_file_as_string(filename))
if data != null:
$VBoxContainer/Name.text = data.name
$VBoxContainer/Left.select_by_name(data.left)
$VBoxContainer/Right.select_by_name(data.right)
$VBoxContainer/Background.select_by_name(data.background)
$Background.texture = load($VBoxContainer/Background.get_selected_filename())
for row in 18:
for col in 13:
var brick = get_node("Bricks/Row%d/Col%d" % [row, col])
var c = data.data[row].substr(col, 1)
brick.set_brick_type(c)
load_level_from_object(data)
func load_level_from_object(data : Dictionary) -> void:
if data != null:
$VBoxContainer/Name.text = data.name
$VBoxContainer/Left.select_by_name(data.left)
$VBoxContainer/Right.select_by_name(data.right)
$VBoxContainer/Background.select_by_name(data.background)
$Background.texture = load($VBoxContainer/Background.get_selected_filename())
for row in 18:
for col in 13:
var brick = get_node("Bricks/Row%d/Col%d" % [row, col])
var c = data.data[row].substr(col, 1)
brick.set_brick_type(c)
func _on_new_pressed() -> void:
@@ -66,3 +72,16 @@ func _on_new_pressed() -> void:
for col in 13:
var brick = get_node("Bricks/Row%d/Col%d" % [row, col])
brick.set_brick_type(" ")
func _on_export_pressed() -> void:
$ExportPanel.show_panel(get_level_object())
func _on_import_pressed() -> void:
$ImportPanel.show_panel()
func _on_import_panel_object_imported(data: Dictionary) -> void:
load_level_from_object(data)
pass # Replace with function body.