From e635efa796c3e5544492768418bda2f359b52e73 Mon Sep 17 00:00:00 2001 From: blinry Date: Wed, 18 Nov 2020 18:06:46 +0100 Subject: [PATCH] Item have a clear serialization interface --- levels/2d/2d-test | 7 ++++--- levels/sequence | 1 + scenes/file_browser.gd | 5 +++-- scenes/file_browser.tscn | 6 +----- scenes/item.gd | 42 +++++++++++++++++++++++++--------------- 5 files changed, 35 insertions(+), 26 deletions(-) diff --git a/levels/2d/2d-test b/levels/2d/2d-test index 268bdd5..d229d1f 100644 --- a/levels/2d/2d-test +++ b/levels/2d/2d-test @@ -7,8 +7,9 @@ Testlevel for 2d interactive files. [setup] -echo "x = 10 -y = 5 -color = red" > apple +echo "x = 100 +y = 50" > apple +echo "x = 200 +y = 50" > pear git add . git commit -m "An apple a day" diff --git a/levels/sequence b/levels/sequence index f159582..f8da63e 100644 --- a/levels/sequence +++ b/levels/sequence @@ -1,2 +1,3 @@ +2d time-machine low-level diff --git a/scenes/file_browser.gd b/scenes/file_browser.gd index b86713e..940b3e7 100644 --- a/scenes/file_browser.gd +++ b/scenes/file_browser.gd @@ -19,13 +19,15 @@ onready var world = $Panel/Margin/Rows/World onready var text_edit = $Panel/TextEdit onready var save_button = $Panel/TextEdit/SaveButton onready var title_label = $Panel/Margin/Rows/Title -onready var player = $Panel/Margin/Rows/World/You +#onready var player = $Panel/Margin/Rows/World/You +var player func _ready(): update() _set_mode(mode) _set_title(title) $PopupMenu.add_item("New file", 1) + #player.file_browser = self func _input(event): if event.is_action_pressed("save"): @@ -91,7 +93,6 @@ func update(): seed(item.label.hash()) item.position = Vector2(rand_range(0, world.rect_size.x), rand_range(0, world.rect_size.y)) randomize() - item.content = shell.run("cat " + file_path) world.add_child(item) #visible = is_visible diff --git a/scenes/file_browser.tscn b/scenes/file_browser.tscn index daa9eff..69da164 100644 --- a/scenes/file_browser.tscn +++ b/scenes/file_browser.tscn @@ -1,9 +1,8 @@ -[gd_scene load_steps=5 format=2] +[gd_scene load_steps=4 format=2] [ext_resource path="res://scenes/file_browser.gd" type="Script" id=1] [ext_resource path="res://fonts/default.tres" type="DynamicFont" id=2] [ext_resource path="res://styles/theme.tres" type="Theme" id=3] -[ext_resource path="res://scenes/item.tscn" type="PackedScene" id=6] [node name="FileBrowser" type="Control" groups=[ "editors", @@ -77,9 +76,6 @@ margin_bottom = 1064.0 size_flags_horizontal = 3 size_flags_vertical = 3 -[node name="You" parent="Panel/Margin/Rows/World" instance=ExtResource( 6 )] -editable = false - [node name="TextEdit" type="TextEdit" parent="Panel"] visible = false anchor_right = 1.0 diff --git a/scenes/item.gd b/scenes/item.gd index 0a6c6aa..95a6475 100644 --- a/scenes/item.gd +++ b/scenes/item.gd @@ -6,7 +6,9 @@ export(IconStatus) var status setget _set_status export var label: String setget _set_label var type = "file" export var editable = true -var content setget _set_content + +var attributes + var held var GRID_SIZE = 60 var file_browser @@ -17,10 +19,27 @@ onready var status_icon = $Status func _ready(): _set_label(label) _set_status(status) - if not editable: - type = "nothing" + + read_from_file() +# if not editable: +# type = "nothing" #$PopupMenu.add_item("Delete file", 0) +func read_from_file(): + print(file_browser) + attributes = helpers.parse(file_browser.shell.run("cat '%s'" % label)) + position.x = int(attributes["x"]) + position.y = int(attributes["y"]) + +func write_to_file(): + attributes["x"] = str(position.x) + attributes["y"] = str(position.y) + + var content = "" + for key in attributes: + content += "%s = %s\n" % [key, attributes[key]] + file_browser.shell.run("echo \"%s\" > %s" % [content, label]) + func _set_label(new_label): label = new_label if label_node: @@ -55,18 +74,9 @@ func _set_status(new_status): status_icon.texture = null status = new_status - -func _set_content(new_content): - content = new_content - var attributes = helpers.parse(content) - if attributes.has("x") and attributes.has("y"): - position.x = int(attributes["x"]) * GRID_SIZE - position.y = int(attributes["y"]) * GRID_SIZE -func move(direction): - position += direction * GRID_SIZE - if label != "": - file_browser.shell.run("echo \"x = "+ String(position.x / GRID_SIZE) +"\ny = "+ String(position.y / GRID_SIZE) +"\" > " + label) +func move(diff): + position += diff + write_to_file() if held: - held.move(direction) - + held.move(diff)