Initial Commit
This commit is contained in:
27
Scripts/Gameplay/CommandLine/Commands/cam.gd
Normal file
27
Scripts/Gameplay/CommandLine/Commands/cam.gd
Normal file
@@ -0,0 +1,27 @@
|
||||
extends Resource
|
||||
var desc: String = "Switch to a camera"
|
||||
var usage: String = "cam <id>"
|
||||
|
||||
func main(args: Array, context: Node) -> void:
|
||||
if args.size() == 0:
|
||||
context._print("Usage: cam <camera_id_or_name>")
|
||||
return
|
||||
|
||||
var input_value = args[0]
|
||||
var cam_id: int = -1
|
||||
|
||||
var int_value = int(input_value)
|
||||
if str(int_value) == input_value:
|
||||
cam_id = int_value
|
||||
else:
|
||||
cam_id = Global.cameraHelper.getID(input_value)
|
||||
|
||||
if cam_id == -1:
|
||||
context._print("Invalid camera ID or name: %s" % input_value)
|
||||
return
|
||||
|
||||
var cam_name = Global.cameraHelper.getName(cam_id)
|
||||
context._print("Camera set to %s" % cam_name.capitalize())
|
||||
|
||||
print(Global.cameraHelper.id_to_name)
|
||||
print(Global.cameraHelper.name_to_id)
|
||||
1
Scripts/Gameplay/CommandLine/Commands/cam.gd.uid
Normal file
1
Scripts/Gameplay/CommandLine/Commands/cam.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://ooovsrvdoq2a
|
||||
18
Scripts/Gameplay/CommandLine/Commands/help.gd
Normal file
18
Scripts/Gameplay/CommandLine/Commands/help.gd
Normal file
@@ -0,0 +1,18 @@
|
||||
extends Resource
|
||||
var desc: String = "Show commands"
|
||||
var usage: String = "help"
|
||||
|
||||
func main(args: Array, context: cCommandLine) -> void:
|
||||
if context.commands.size() == 0:
|
||||
context._print("No commands available.")
|
||||
return
|
||||
|
||||
context._print("Available Commands:")
|
||||
for name in context.commands.keys():
|
||||
var cmd_script = context.commands[name]
|
||||
var cmd_instance = cmd_script.new()
|
||||
|
||||
var cmdDescription = cmd_instance.desc
|
||||
var cmdUsage = "Usage: " + cmd_instance.usage
|
||||
|
||||
context._print("%s: %s\n%s" % [name, cmdDescription, cmdUsage])
|
||||
1
Scripts/Gameplay/CommandLine/Commands/help.gd.uid
Normal file
1
Scripts/Gameplay/CommandLine/Commands/help.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://mbcxt6233xib
|
||||
6
Scripts/Gameplay/CommandLine/Commands/say.gd
Normal file
6
Scripts/Gameplay/CommandLine/Commands/say.gd
Normal file
@@ -0,0 +1,6 @@
|
||||
extends Resource
|
||||
var desc: String = "Repeat your input"
|
||||
var usage: String = "say <text>"
|
||||
|
||||
func main(args: Array, context: Node) -> void:
|
||||
context._print(args)
|
||||
1
Scripts/Gameplay/CommandLine/Commands/say.gd.uid
Normal file
1
Scripts/Gameplay/CommandLine/Commands/say.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://dx43e03pno5sw
|
||||
58
Scripts/Gameplay/CommandLine/cCommandLine.gd
Normal file
58
Scripts/Gameplay/CommandLine/cCommandLine.gd
Normal file
@@ -0,0 +1,58 @@
|
||||
class_name cCommandLine
|
||||
extends CanvasLayer
|
||||
|
||||
var commands_directory = DirAccess.open("res://Scripts/Gameplay/CommandLine/Commands/")
|
||||
|
||||
@export var output_text: TextEdit
|
||||
@export var input_line: LineEdit
|
||||
var commands: Dictionary = {}
|
||||
|
||||
func _ready():
|
||||
load_commands()
|
||||
input_line.text_submitted.connect(self._on_command_entered)
|
||||
|
||||
# To make a new command, write it in ./Commands with a main() function for entry.
|
||||
# This function will automatically find and register all command files
|
||||
func load_commands():
|
||||
if not commands_directory:
|
||||
push_error("Failed to open commands folder!")
|
||||
return
|
||||
|
||||
commands_directory.list_dir_begin()
|
||||
var file_name = commands_directory.get_next()
|
||||
|
||||
while file_name != "":
|
||||
if file_name.ends_with(".gd"):
|
||||
var command_name = file_name.get_basename().to_lower()
|
||||
var script = load("res://Scripts/Gameplay/CommandLine/Commands/" + file_name)
|
||||
commands[command_name] = script
|
||||
file_name = commands_directory.get_next()
|
||||
commands_directory.list_dir_end()
|
||||
|
||||
func _print(text: String) -> void:
|
||||
var line = output_text.get_line_count() - 1
|
||||
output_text.insert_text(text + "\n", line, 0)
|
||||
|
||||
func _on_command_entered(new_text: String) -> void:
|
||||
input_line.clear()
|
||||
_print("> " + new_text)
|
||||
|
||||
# Store command and ensure its not empty
|
||||
var parts = new_text.strip_edges().split(" ")
|
||||
if parts.size() == 0:
|
||||
return
|
||||
|
||||
# Parse command and arguments
|
||||
var cmd = parts[0].to_lower()
|
||||
var args = parts.slice(1, parts.size())
|
||||
|
||||
if commands.has(cmd):
|
||||
var cmd_script = commands[cmd].new() # Instantiate the script
|
||||
if cmd_script.has_method("main"):
|
||||
# Run script
|
||||
cmd_script.main(args, self)
|
||||
else:
|
||||
_print("DEV ERROR !\nCommand '%s' has no main() function!" % cmd)
|
||||
else:
|
||||
_print("Unknown command: %s" % cmd)
|
||||
input_line.text = "" # Clear input
|
||||
1
Scripts/Gameplay/CommandLine/cCommandLine.gd.uid
Normal file
1
Scripts/Gameplay/CommandLine/cCommandLine.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://dmp22gu6t0fw2
|
||||
Reference in New Issue
Block a user