2020-09-21 15:25:54 +02:00
|
|
|
extends Control
|
2020-03-18 15:23:38 +01:00
|
|
|
|
2020-03-18 20:03:17 +01:00
|
|
|
var dragged = null
|
|
|
|
|
2020-09-28 18:40:41 +02:00
|
|
|
var chapter = "bottom-up"
|
2020-09-10 12:47:18 +02:00
|
|
|
var current_level = 0
|
2020-09-01 15:14:01 +02:00
|
|
|
|
2020-09-29 14:26:20 +02:00
|
|
|
onready var terminal = $Columns/RightSide/Terminal
|
2020-09-14 19:36:58 +02:00
|
|
|
onready var input = terminal.input
|
|
|
|
onready var output = terminal.output
|
2020-09-29 14:26:20 +02:00
|
|
|
onready var goal_repository = $Columns/Repositories/GoalRepository
|
|
|
|
onready var active_repository = $Columns/Repositories/ActiveRepository
|
|
|
|
onready var level_select = $Columns/RightSide/TopStuff/Menu/LevelSelect
|
|
|
|
onready var chapter_select = $Columns/RightSide/TopStuff/Menu/ChapterSelect
|
|
|
|
onready var next_level_button = $Columns/RightSide/TopStuff/Menu/NextLevelButton
|
|
|
|
onready var level_name = $Columns/RightSide/TopStuff/LevelPanel/LevelName
|
|
|
|
onready var level_description = $Columns/RightSide/TopStuff/LevelPanel/Text/LevelDescription
|
|
|
|
onready var level_congrats = $Columns/RightSide/TopStuff/LevelPanel/Text/LevelCongrats
|
2020-09-01 21:25:24 +02:00
|
|
|
|
2020-03-18 15:23:38 +01:00
|
|
|
func _ready():
|
2020-09-29 17:52:31 +02:00
|
|
|
var args = helpers.parse_args()
|
|
|
|
|
|
|
|
if args.has("sandbox"):
|
|
|
|
var err = get_tree().change_scene("res://sandbox.tscn")
|
|
|
|
if err != OK:
|
|
|
|
helpers.crash("Could not change to sandbox scene")
|
|
|
|
return
|
|
|
|
|
2020-09-04 14:57:37 +02:00
|
|
|
# Initialize level select.
|
2020-09-28 19:02:12 +02:00
|
|
|
level_select.connect("item_selected", self, "load_level")
|
|
|
|
repopulate_levels()
|
|
|
|
level_select.select(0)
|
2020-09-04 14:57:37 +02:00
|
|
|
|
2020-09-28 18:40:41 +02:00
|
|
|
# Initialize chapter select.
|
2020-09-28 19:02:12 +02:00
|
|
|
chapter_select.connect("item_selected", self, "load_chapter")
|
|
|
|
repopulate_chapters()
|
|
|
|
chapter_select.select(0)
|
2020-09-28 18:40:41 +02:00
|
|
|
|
2020-09-04 14:57:37 +02:00
|
|
|
# Load first level.
|
|
|
|
load_level(0)
|
|
|
|
input.grab_focus()
|
|
|
|
|
2020-09-28 18:40:41 +02:00
|
|
|
func list_chapters():
|
|
|
|
var chapters = []
|
|
|
|
var dir = Directory.new()
|
|
|
|
dir.open("res://levels/")
|
|
|
|
dir.list_dir_begin()
|
|
|
|
|
|
|
|
while true:
|
|
|
|
var file = dir.get_next()
|
|
|
|
if file == "":
|
|
|
|
break
|
|
|
|
elif not file.begins_with("."):
|
|
|
|
chapters.append(file)
|
|
|
|
|
|
|
|
dir.list_dir_end()
|
|
|
|
chapters.sort()
|
|
|
|
return chapters
|
|
|
|
|
2020-09-01 19:20:51 +02:00
|
|
|
func list_levels():
|
2020-09-04 14:57:37 +02:00
|
|
|
var levels = []
|
|
|
|
var dir = Directory.new()
|
2020-09-28 18:40:41 +02:00
|
|
|
dir.open("res://levels/%s" % chapter)
|
2020-09-04 14:57:37 +02:00
|
|
|
dir.list_dir_begin()
|
2020-09-01 19:20:51 +02:00
|
|
|
|
2020-09-04 14:57:37 +02:00
|
|
|
while true:
|
|
|
|
var file = dir.get_next()
|
|
|
|
if file == "":
|
|
|
|
break
|
2020-09-22 16:12:03 +02:00
|
|
|
elif not file.begins_with(".") and file != "sequence":
|
2020-09-04 14:57:37 +02:00
|
|
|
levels.append(file)
|
2020-09-01 19:20:51 +02:00
|
|
|
|
2020-09-04 14:57:37 +02:00
|
|
|
dir.list_dir_end()
|
|
|
|
levels.sort()
|
2020-09-16 16:16:46 +02:00
|
|
|
|
2020-09-22 16:12:03 +02:00
|
|
|
var final_level_sequence = []
|
|
|
|
|
2020-09-29 14:53:00 +02:00
|
|
|
var level_sequence = Array(helpers.read_file("res://levels/%s/sequence" % chapter, "").split("\n"))
|
2020-09-16 16:16:46 +02:00
|
|
|
|
|
|
|
for level in level_sequence:
|
2020-09-22 16:12:03 +02:00
|
|
|
if level == "":
|
|
|
|
continue
|
2020-09-16 16:16:46 +02:00
|
|
|
if not levels.has(level):
|
2020-09-29 14:53:00 +02:00
|
|
|
helpers.crash("Level '%s' is specified in the sequence, but could not be found" % level)
|
2020-09-16 16:16:46 +02:00
|
|
|
levels.erase(level)
|
2020-09-22 16:12:03 +02:00
|
|
|
final_level_sequence.push_back(level)
|
2020-09-16 16:16:46 +02:00
|
|
|
|
2020-09-22 16:12:03 +02:00
|
|
|
final_level_sequence += levels
|
2020-09-16 16:16:46 +02:00
|
|
|
|
2020-09-22 16:12:03 +02:00
|
|
|
return final_level_sequence
|
2020-09-01 19:20:51 +02:00
|
|
|
|
2020-09-28 18:40:41 +02:00
|
|
|
func load_chapter(id):
|
|
|
|
var chapters = list_chapters()
|
|
|
|
chapter = chapters[id]
|
2020-09-28 19:02:12 +02:00
|
|
|
load_level(0)
|
2020-09-28 18:40:41 +02:00
|
|
|
|
2020-09-01 19:20:51 +02:00
|
|
|
func load_level(id):
|
2020-09-24 10:52:58 +02:00
|
|
|
AudioServer.set_bus_mute(AudioServer.get_bus_index("Master"), true)
|
|
|
|
|
2020-09-21 15:25:54 +02:00
|
|
|
next_level_button.hide()
|
|
|
|
level_congrats.hide()
|
|
|
|
level_description.show()
|
2020-09-10 12:47:18 +02:00
|
|
|
current_level = id
|
|
|
|
|
2020-09-04 14:57:37 +02:00
|
|
|
var levels = list_levels()
|
|
|
|
|
|
|
|
var level = levels[id]
|
2020-09-28 18:40:41 +02:00
|
|
|
var level_prefix = "res://levels/%s/" % chapter
|
2020-09-04 14:57:37 +02:00
|
|
|
|
2020-09-29 16:12:58 +02:00
|
|
|
var goal_repository_path = game.tmp_prefix_inside+"/repos/goal/"
|
|
|
|
var active_repository_path = game.tmp_prefix_inside+"/repos/active/"
|
2020-09-04 14:57:37 +02:00
|
|
|
var goal_script = level_prefix+level+"/goal"
|
|
|
|
var active_script = level_prefix+level+"/start"
|
|
|
|
|
2020-09-15 12:36:22 +02:00
|
|
|
var description_file = level_prefix+level+"/description"
|
2020-09-29 14:53:00 +02:00
|
|
|
var description = helpers.read_file(description_file, "(no description)")
|
2020-09-22 10:54:37 +02:00
|
|
|
|
|
|
|
# Surround all lines indented with four spaces with [code] tags.
|
|
|
|
var monospace_regex = RegEx.new()
|
|
|
|
monospace_regex.compile("\n (.*)\n")
|
|
|
|
description = monospace_regex.sub(description, "\n [code]$1[/code]\n", true)
|
2020-09-21 15:25:54 +02:00
|
|
|
level_description.bbcode_text = description
|
2020-09-16 16:16:46 +02:00
|
|
|
|
|
|
|
var congrats_file = level_prefix+level+"/congrats"
|
2020-09-29 14:53:00 +02:00
|
|
|
var congrats = helpers.read_file(congrats_file, "Good job, you solved the level!\n\nFeel free to try a few more things or click 'Next Level'.")
|
2020-09-21 15:25:54 +02:00
|
|
|
level_congrats.bbcode_text = congrats
|
2020-09-16 16:16:46 +02:00
|
|
|
|
2020-09-21 15:25:54 +02:00
|
|
|
level_name.text = level
|
2020-09-04 14:57:37 +02:00
|
|
|
|
2020-09-08 16:36:52 +02:00
|
|
|
# We're actually destroying stuff here.
|
2020-09-08 16:00:18 +02:00
|
|
|
# Make sure that active_repository is in a temporary directory.
|
2020-09-29 16:12:58 +02:00
|
|
|
_careful_delete(active_repository_path)
|
|
|
|
_careful_delete(goal_repository_path)
|
2020-09-15 12:36:22 +02:00
|
|
|
|
2020-09-29 14:53:00 +02:00
|
|
|
var goal_script_content = helpers.read_file(goal_script, "")
|
|
|
|
var active_script_content = helpers.read_file(active_script, "")
|
2020-09-15 12:36:22 +02:00
|
|
|
construct_repo(active_script_content +"\n"+ goal_script_content, goal_repository_path)
|
|
|
|
construct_repo(active_script_content, active_repository_path)
|
2020-09-04 14:57:37 +02:00
|
|
|
|
|
|
|
goal_repository.path = goal_repository_path
|
|
|
|
active_repository.path = active_repository_path
|
|
|
|
|
2020-09-10 12:03:46 +02:00
|
|
|
var win_script = level_prefix+level+"/win"
|
2020-09-29 16:12:58 +02:00
|
|
|
var win_script_target = game.tmp_prefix_outside+"/win"
|
2020-09-29 14:53:00 +02:00
|
|
|
var win_script_content = helpers.read_file(win_script, "exit 1\n")
|
|
|
|
helpers.write_file(win_script_target, win_script_content)
|
2020-09-14 15:35:30 +02:00
|
|
|
|
|
|
|
terminal.clear()
|
2020-09-24 10:52:58 +02:00
|
|
|
|
|
|
|
# Unmute the audio after a while, so that player can hear pop sounds for
|
|
|
|
# nodes they create.
|
|
|
|
var t = Timer.new()
|
|
|
|
t.wait_time = 3
|
|
|
|
add_child(t)
|
|
|
|
t.start()
|
|
|
|
yield(t, "timeout")
|
|
|
|
AudioServer.set_bus_mute(AudioServer.get_bus_index("Master"), false)
|
|
|
|
# FIXME: Need to clean these up when switching levels somehow.
|
2020-09-29 16:12:58 +02:00
|
|
|
|
|
|
|
func _careful_delete(path_inside):
|
|
|
|
var expected_prefix
|
|
|
|
|
|
|
|
var os = OS.get_name()
|
|
|
|
|
|
|
|
if os == "X11":
|
|
|
|
expected_prefix = "/home/%s/.local/share/git-hydra/tmp/" % OS.get_environment("USER")
|
|
|
|
elif os == "Windows":
|
|
|
|
helpers.crash("Need to figure out delete_prefix on Windows")
|
|
|
|
else:
|
|
|
|
helpers.crash("Unsupported OS: %s" % os)
|
|
|
|
|
|
|
|
if path_inside.substr(0,expected_prefix.length()) != expected_prefix:
|
|
|
|
helpers.crash("Refusing to delete directory %s that does not start with %s" % [path_inside, expected_prefix])
|
|
|
|
else:
|
|
|
|
game.global_shell.run("rm -rf '%s'" % path_inside)
|
2020-09-10 12:47:18 +02:00
|
|
|
|
2020-09-13 21:55:24 +02:00
|
|
|
func reload_level():
|
|
|
|
load_level(current_level)
|
|
|
|
|
2020-09-10 12:47:18 +02:00
|
|
|
func load_next_level():
|
2020-09-11 12:23:26 +02:00
|
|
|
current_level = (current_level + 1) % list_levels().size()
|
2020-09-10 12:47:18 +02:00
|
|
|
load_level(current_level)
|
2020-09-10 12:03:46 +02:00
|
|
|
|
2020-09-15 12:36:22 +02:00
|
|
|
func construct_repo(script_content, path):
|
2020-09-08 16:27:36 +02:00
|
|
|
# Becase in an exported game, all assets are in a .pck file, we need to put
|
|
|
|
# the script somewhere in the filesystem.
|
2020-09-15 12:36:22 +02:00
|
|
|
|
2020-09-29 16:12:58 +02:00
|
|
|
var script_path_outside = game.tmp_prefix_outside+"/git-hydra-script"
|
|
|
|
var script_path_inside = game.tmp_prefix_inside+"/git-hydra-script"
|
2020-09-29 14:53:00 +02:00
|
|
|
helpers.write_file(script_path_outside, script_content)
|
2020-09-08 16:27:36 +02:00
|
|
|
|
2020-09-09 11:48:42 +02:00
|
|
|
game.global_shell.run("mkdir " + path)
|
|
|
|
game.global_shell.cd(path)
|
|
|
|
game.global_shell.run("git init")
|
2020-09-15 12:36:22 +02:00
|
|
|
game.global_shell.run("git symbolic-ref HEAD refs/heads/main")
|
2020-09-24 10:25:38 +02:00
|
|
|
# Read stdin from /dev/null so that interactive commands don't block.
|
2020-09-29 16:12:58 +02:00
|
|
|
game.global_shell.run("bash "+script_path_inside+" </dev/null")
|
2020-09-04 14:57:37 +02:00
|
|
|
|
2020-09-11 13:12:12 +02:00
|
|
|
func show_win_status():
|
2020-09-21 15:25:54 +02:00
|
|
|
next_level_button.show()
|
|
|
|
level_description.hide()
|
|
|
|
level_congrats.show()
|
2020-09-15 16:31:41 +02:00
|
|
|
|
|
|
|
func repopulate_levels():
|
2020-09-28 19:02:12 +02:00
|
|
|
level_select.clear()
|
2020-09-15 16:31:41 +02:00
|
|
|
for level in list_levels():
|
2020-09-28 19:02:12 +02:00
|
|
|
level_select.add_item(level)
|
2020-09-28 18:40:41 +02:00
|
|
|
|
|
|
|
func repopulate_chapters():
|
2020-09-28 19:02:12 +02:00
|
|
|
chapter_select.clear()
|
2020-09-29 17:52:31 +02:00
|
|
|
for c in list_chapters():
|
|
|
|
chapter_select.add_item(c)
|