Item have a clear serialization interface

This commit is contained in:
blinry 2020-11-18 18:06:46 +01:00
parent 3167e5c76a
commit e635efa796
5 changed files with 35 additions and 26 deletions

View file

@ -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"

View file

@ -1,2 +1,3 @@
2d
time-machine
low-level

View file

@ -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

View file

@ -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

View file

@ -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)