move player and pickup items

This commit is contained in:
bleeptrack 2020-11-17 18:31:19 +01:00
parent 17302c3f01
commit 3167e5c76a
7 changed files with 67 additions and 9 deletions

14
levels/2d/2d-test Normal file
View file

@ -0,0 +1,14 @@
title = Apples
cards = checkout commit-auto reset-hard branch-delete
[description]
Testlevel for 2d interactive files.
[setup]
echo "x = 10
y = 5
color = red" > apple
git add .
git commit -m "An apple a day"

View file

@ -140,6 +140,12 @@ clear={
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":true,"meta":false,"command":true,"pressed":false,"scancode":76,"unicode":0,"echo":false,"script":null) "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":true,"meta":false,"command":true,"pressed":false,"scancode":76,"unicode":0,"echo":false,"script":null)
] ]
} }
pickup={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":69,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":32,"unicode":0,"echo":false,"script":null)
]
}
[network] [network]

View file

@ -19,6 +19,7 @@ onready var world = $Panel/Margin/Rows/World
onready var text_edit = $Panel/TextEdit onready var text_edit = $Panel/TextEdit
onready var save_button = $Panel/TextEdit/SaveButton onready var save_button = $Panel/TextEdit/SaveButton
onready var title_label = $Panel/Margin/Rows/Title onready var title_label = $Panel/Margin/Rows/Title
onready var player = $Panel/Margin/Rows/World/You
func _ready(): func _ready():
update() update()
@ -30,11 +31,29 @@ func _input(event):
if event.is_action_pressed("save"): if event.is_action_pressed("save"):
if text_edit.visible: if text_edit.visible:
save() save()
if event.is_action_pressed("down", true):
player.move(Vector2(0,1))
if event.is_action_pressed("up", true):
player.move(Vector2(0,-1))
if event.is_action_pressed("right", true):
player.move(Vector2(1,0))
if event.is_action_pressed("left", true):
player.move(Vector2(-1,0))
if event.is_action_pressed("pickup"):
if player.held:
player.held = null
else:
for item in world.get_children():
if item.label != "":
if item.position == player.position:
player.held = item
print("player picked up item " + item.label)
func clear(): func clear():
pass pass
for item in world.get_children(): for item in world.get_children():
item.queue_free() if item.label != "":
item.queue_free()
func substr2(s): func substr2(s):
return s.substr(2) return s.substr(2)
@ -65,12 +84,14 @@ func update():
#is_visible = true #is_visible = true
var item = preload("res://scenes/item.tscn").instance() var item = preload("res://scenes/item.tscn").instance()
item.label = file_path item.label = file_path
item.file_browser = self
#item.connect("clicked", self, "item_clicked") #item.connect("clicked", self, "item_clicked")
#item.connect("deleted", self, "item_deleted") #item.connect("deleted", self, "item_deleted")
item.status = get_file_status(file_path, shell, 1) item.status = get_file_status(file_path, shell, 1)
seed(item.label.hash()) seed(item.label.hash())
item.position = Vector2(rand_range(0, world.rect_size.x), rand_range(0, world.rect_size.y)) item.position = Vector2(rand_range(0, world.rect_size.x), rand_range(0, world.rect_size.y))
randomize() randomize()
item.content = shell.run("cat " + file_path)
world.add_child(item) world.add_child(item)
#visible = is_visible #visible = is_visible

View file

@ -77,6 +77,9 @@ margin_bottom = 1064.0
size_flags_horizontal = 3 size_flags_horizontal = 3
size_flags_vertical = 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"] [node name="TextEdit" type="TextEdit" parent="Panel"]
visible = false visible = false
anchor_right = 1.0 anchor_right = 1.0
@ -121,9 +124,5 @@ __meta__ = {
[node name="PopupMenu" type="PopupMenu" parent="."] [node name="PopupMenu" type="PopupMenu" parent="."]
margin_right = 20.0 margin_right = 20.0
margin_bottom = 20.0 margin_bottom = 20.0
[node name="You" parent="." instance=ExtResource( 6 )]
position = Vector2( 175.746, 148.807 )
editable = false
[connection signal="pressed" from="Panel/TextEdit/SaveButton" to="." method="save"] [connection signal="pressed" from="Panel/TextEdit/SaveButton" to="." method="save"]
[connection signal="pressed" from="Panel/TextEdit/CloseButton" to="." method="close"] [connection signal="pressed" from="Panel/TextEdit/CloseButton" to="." method="close"]

View file

@ -93,8 +93,7 @@ func careful_delete(path_inside):
game.global_shell.cd(game.tmp_prefix) game.global_shell.cd(game.tmp_prefix)
game.global_shell.run("rm -rf '%s'" % path_inside) game.global_shell.run("rm -rf '%s'" % path_inside)
func parse(file): func parse(text):
var text = read_file(file)
var result = {} var result = {}
var current_section var current_section
@ -118,7 +117,7 @@ func parse(file):
# Parse a direct=assignment. # Parse a direct=assignment.
m = assignment_regex.search(line) m = assignment_regex.search(line)
if m: if m and current_section == null:
var key = m.get_string(1).strip_edges() var key = m.get_string(1).strip_edges()
var value = m.get_string(2).strip_edges() var value = m.get_string(2).strip_edges()
result[key] = value result[key] = value

View file

@ -6,6 +6,10 @@ export(IconStatus) var status setget _set_status
export var label: String setget _set_label export var label: String setget _set_label
var type = "file" var type = "file"
export var editable = true export var editable = true
var content setget _set_content
var held
var GRID_SIZE = 60
var file_browser
onready var label_node = $Label onready var label_node = $Label
onready var status_icon = $Status onready var status_icon = $Status
@ -51,3 +55,18 @@ func _set_status(new_status):
status_icon.texture = null status_icon.texture = null
status = new_status 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)
if held:
held.move(direction)

View file

@ -16,7 +16,7 @@ func load(path):
var dir = Directory.new() var dir = Directory.new()
if dir.file_exists(path): if dir.file_exists(path):
# This is a new-style level. # This is a new-style level.
var config = helpers.parse(path) var config = helpers.parse(helpers.read_file(path))
title = config.get("title", slug) title = config.get("title", slug)
description = config.get("description", "(no description)") description = config.get("description", "(no description)")