From 060bfcf6c9ab42107fd732719cfed20b8a170687 Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Wed, 11 Jan 2023 13:50:06 -0500 Subject: [PATCH] Boiler-plate code generated when new scripts are created --- docs/tasks/core.todo | 2 +- docs/tasks/editor.todo | 3 +- src/run_modes/editor/contents/script.cpp | 36 ++++++++++++++++++++---- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/docs/tasks/core.todo b/docs/tasks/core.todo index d900b8e..4a492b6 100644 --- a/docs/tasks/core.todo +++ b/docs/tasks/core.todo @@ -43,7 +43,7 @@ Physics: Scripting: - ☐ Add a Log method to the CoreAPI + ☐ Add a Log methods to the CoreAPI ✔ Allow scripts to interact (calling methods in other scripts) @done(23-01-10 16:54) - In order for a script to be registered with the World API it MUST diff --git a/docs/tasks/editor.todo b/docs/tasks/editor.todo index 0c1c9a3..b36c094 100644 --- a/docs/tasks/editor.todo +++ b/docs/tasks/editor.todo @@ -1,7 +1,6 @@ Editor: - ☐ Generate boiler-plate code when a new script is created - ☐ Include setting the entity ID + ✔ Generate boiler-plate code when a new script is created @critical @done(23-01-11 13:49) ✔ Add pure virtual ShowProperties method to EditorAsset @done(22-11-28 20:11) ✔ Script Editor Asset @done(22-11-14 18:19) diff --git a/src/run_modes/editor/contents/script.cpp b/src/run_modes/editor/contents/script.cpp index 920c87b..716d38f 100644 --- a/src/run_modes/editor/contents/script.cpp +++ b/src/run_modes/editor/contents/script.cpp @@ -20,15 +20,39 @@ namespace lunarium { namespace editor : EditorAsset(AssetType::EATYPE_SCRIPT) { std::string full_path = (asset_dir / file_location).string(); - std::ofstream ofs(asset_dir / file_location); - if (!ofs.is_open()) + + if (!std::filesystem::exists(full_path)) { - Logger::Warn(Editor::LogCat, "Could not create file \"%s\"", full_path.c_str()); - } + std::ofstream ofs(asset_dir / file_location); + if (!ofs.is_open()) + { + Logger::Warn(Editor::LogCat, "Could not create file \"%s\"", full_path.c_str()); + } + + // This is a new script file so generate the boiler-plate + std::string script_name = std::filesystem::path(full_path).stem().string(); + std::string code = "// Wren script: " + script_name + "\n"; + code += "\nimport \"CoreAPI\" for Core"; + code += "\nimport \"WorldInterface\" for WorldInterface, EntityBehavior"; + code += "\nimport \"KeyCodes\" for KeyCodes"; + code += "\n\nclass " + script_name + " is EntityBehavior {"; + code += "\n\tconstruct new(id) {"; + code += "\n\t\tsuper.EntityID = id"; + code += "\n\t}"; + code += "\n\n\tOnLoad() {"; + code += "\n\n\t}"; + code += "\n\n\tOnUnload() {"; + code += "\n\n\t}"; + code += "\n\n\tUpdate(dt) {"; + code += "\n\n\t}"; + code += "\n}"; - ofs.close(); - ofs.clear(); + ofs.write(code.c_str(), code.size()); + ofs.close(); + ofs.clear(); + + } mLocation = file_location; }