From 5c9f2e04940dd4d124d90e4ec804555528b9aaf6 Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Tue, 15 Nov 2022 14:28:10 -0500 Subject: [PATCH] Scripts running during world update --- src/gui/console.cpp | 6 ++++ src/gui/console.h | 2 ++ src/run_modes/editor/contents/script.cpp | 3 +- .../editor/panels/editor_console.cpp | 7 ++++ src/scripting/world_interface.wren | 0 src/scripting/wren_state.cpp | 8 ++++- src/utils/helpers.cpp | 24 +++++++++++++ src/utils/helpers.h | 1 + src/world/world.cpp | 34 ++++++++++++++++--- src/world/world.h | 1 + 10 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 src/scripting/world_interface.wren diff --git a/src/gui/console.cpp b/src/gui/console.cpp index 0c87762..2b84aa0 100644 --- a/src/gui/console.cpp +++ b/src/gui/console.cpp @@ -76,6 +76,12 @@ namespace lunarium } + void Console::ClearMessageHistory() + { + mMsgHistory.clear(); + } + + f32 Console::GetInputWindowHeight() const { return mInputWindowHeight; diff --git a/src/gui/console.h b/src/gui/console.h index 211976d..7a68b08 100644 --- a/src/gui/console.h +++ b/src/gui/console.h @@ -38,6 +38,8 @@ namespace lunarium virtual void Update(float dt); virtual void DoFrame(); + void ClearMessageHistory(); + bool IsFocused() const; f32 GetInputWindowHeight() const; diff --git a/src/run_modes/editor/contents/script.cpp b/src/run_modes/editor/contents/script.cpp index 1880cbf..0c297dc 100644 --- a/src/run_modes/editor/contents/script.cpp +++ b/src/run_modes/editor/contents/script.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include @@ -37,7 +38,7 @@ namespace lunarium { namespace editor std::string Script::GetScript() { - return FileLoaders::LoadTextFile(GetFileLocation()); + return File::ReadTextFile((mAssetDir / GetFileLocation()).string()); } OpRes Script::LoadRawFile() diff --git a/src/run_modes/editor/panels/editor_console.cpp b/src/run_modes/editor/panels/editor_console.cpp index b39afbd..0b5ce0f 100644 --- a/src/run_modes/editor/panels/editor_console.cpp +++ b/src/run_modes/editor/panels/editor_console.cpp @@ -59,5 +59,12 @@ namespace lunarium { namespace editor { SetVerboseFlag(mInfoVerbose); } + + ImGui::SameLine(); + + if (ImGui::Button("Clear")) + { + ClearMessageHistory(); + } } }} \ No newline at end of file diff --git a/src/scripting/world_interface.wren b/src/scripting/world_interface.wren new file mode 100644 index 0000000..e69de29 diff --git a/src/scripting/wren_state.cpp b/src/scripting/wren_state.cpp index 9fd4902..e266afd 100644 --- a/src/scripting/wren_state.cpp +++ b/src/scripting/wren_state.cpp @@ -10,6 +10,7 @@ #include "wren_state.h" #include "wren_script.h" #include +#include namespace lunarium { @@ -65,7 +66,12 @@ namespace lunarium void WrenState::WriteFN(WrenVM* vm, const char* text) { - Logger::Info(mLogCat, text); + std::string final = String::TrimEnd(text, "\n"); + + if (final.size() < 1) + return; + + Logger::Info(mLogCat, final.c_str()); } void WrenState::ErrorFN(WrenVM* vm, WrenErrorType type, const char* module, int line, const char* message) diff --git a/src/utils/helpers.cpp b/src/utils/helpers.cpp index 19d9366..b337cdd 100644 --- a/src/utils/helpers.cpp +++ b/src/utils/helpers.cpp @@ -336,4 +336,28 @@ namespace lunarium return contents; } + + + std::string File::ReadScriptFile(std::string filename) + { + std::ifstream ifs(filename); + if (!ifs.is_open()) + { + Logger::Error(LogCategory::UTILITIES, "Could not open file: %s", filename.c_str()); + return ""; + } + + std::string contents; + while (ifs) + { + contents += ifs.get(); + } + + contents.erase(contents.end() - 1); + + ifs.close(); + ifs.clear(); + + return contents; + } } \ No newline at end of file diff --git a/src/utils/helpers.h b/src/utils/helpers.h index 7f64fe5..3cf2e42 100644 --- a/src/utils/helpers.h +++ b/src/utils/helpers.h @@ -96,6 +96,7 @@ namespace lunarium { public: static std::string ReadTextFile(std::string filename); + static std::string ReadScriptFile(std::string filename); }; } diff --git a/src/world/world.cpp b/src/world/world.cpp index 066adca..956bb8d 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -17,6 +17,13 @@ #include #include #include "entity.h" +#include + +#define LOAD_ASSETS_FROM_EDITOR true +#if LOAD_ASSETS_FROM_EDITOR + #include + #include +#endif namespace lunarium { @@ -27,15 +34,16 @@ namespace lunarium // } World::World(std::string name) - : mUUID(UUID::GetNewID()), mName(name), mpActiveCamera(nullptr), mFrameBuffer(nullptr) + : mUUID(UUID::GetNewID()), mName(name), mpActiveCamera(nullptr), mFrameBuffer(nullptr), + mGetAssetsFromEditor(LOAD_ASSETS_FROM_EDITOR) { - + InitScriptState(); } void World::InitScriptState() { - + mScriptState.Initialize().LogIfFailed(LogCategory::GAME_SYSTEM, "Failed to initialize the world script state"); } void World::OnLoad() @@ -60,9 +68,25 @@ namespace lunarium void World::Update(float dt) { - auto group = mECSRegistry.group<>(entt::get); + // Update all scripts + auto group_scripts = mECSRegistry.view(); + for(auto entity: group_scripts) + { + auto& script_comp = group_scripts.get(entity); - // Render the group +#if LOAD_ASSETS_FROM_EDITOR + + editor::Script* pScript = (editor::Script*) editor::ContentManager::GetInstance().GetAsset(script_comp.ScriptID); + + WrenScript script(pScript->GetScriptFile().filename().string().c_str(), pScript->GetScript()); + + mScriptState.RunScript(&script); +#endif + } + + + // Update Transforms for any enity with a velocity + auto group = mECSRegistry.group<>(entt::get); for(auto entity: group) { auto &transform = group.get(entity); diff --git a/src/world/world.h b/src/world/world.h index 39799bd..2c0ec75 100644 --- a/src/world/world.h +++ b/src/world/world.h @@ -123,6 +123,7 @@ namespace lunarium // TEST STUFF + bool mGetAssetsFromEditor; // This is for testing until we get a proper asset manager private: // HELPERS void InitScriptState();