Scripts running during world update

master
Joey Pollack 3 years ago
parent c507273ac4
commit 5c9f2e0494

@ -76,6 +76,12 @@ namespace lunarium
} }
void Console::ClearMessageHistory()
{
mMsgHistory.clear();
}
f32 Console::GetInputWindowHeight() const f32 Console::GetInputWindowHeight() const
{ {
return mInputWindowHeight; return mInputWindowHeight;

@ -38,6 +38,8 @@ namespace lunarium
virtual void Update(float dt); virtual void Update(float dt);
virtual void DoFrame(); virtual void DoFrame();
void ClearMessageHistory();
bool IsFocused() const; bool IsFocused() const;
f32 GetInputWindowHeight() const; f32 GetInputWindowHeight() const;

@ -10,6 +10,7 @@
#include <editor/editor.h> #include <editor/editor.h>
#include <editor/editor_helpers.h> #include <editor/editor_helpers.h>
#include <utils/helpers.h>
#include <utils/logger.h> #include <utils/logger.h>
#include <fstream> #include <fstream>
@ -37,7 +38,7 @@ namespace lunarium { namespace editor
std::string Script::GetScript() std::string Script::GetScript()
{ {
return FileLoaders::LoadTextFile(GetFileLocation()); return File::ReadTextFile((mAssetDir / GetFileLocation()).string());
} }
OpRes Script::LoadRawFile() OpRes Script::LoadRawFile()

@ -59,5 +59,12 @@ namespace lunarium { namespace editor
{ {
SetVerboseFlag(mInfoVerbose); SetVerboseFlag(mInfoVerbose);
} }
ImGui::SameLine();
if (ImGui::Button("Clear"))
{
ClearMessageHistory();
}
} }
}} }}

@ -10,6 +10,7 @@
#include "wren_state.h" #include "wren_state.h"
#include "wren_script.h" #include "wren_script.h"
#include <utils/logger.h> #include <utils/logger.h>
#include <utils/helpers.h>
namespace lunarium namespace lunarium
{ {
@ -65,7 +66,12 @@ namespace lunarium
void WrenState::WriteFN(WrenVM* vm, const char* text) 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) void WrenState::ErrorFN(WrenVM* vm, WrenErrorType type, const char* module, int line, const char* message)

@ -336,4 +336,28 @@ namespace lunarium
return contents; 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;
}
} }

@ -96,6 +96,7 @@ namespace lunarium
{ {
public: public:
static std::string ReadTextFile(std::string filename); static std::string ReadTextFile(std::string filename);
static std::string ReadScriptFile(std::string filename);
}; };
} }

@ -17,6 +17,13 @@
#include <renderer/frame_buffer.h> #include <renderer/frame_buffer.h>
#include <renderer/orthographic_camera.h> #include <renderer/orthographic_camera.h>
#include "entity.h" #include "entity.h"
#include <scripting/wren_script.h>
#define LOAD_ASSETS_FROM_EDITOR true
#if LOAD_ASSETS_FROM_EDITOR
#include <editor/contents/content_manager.h>
#include <editor/contents/script.h>
#endif
namespace lunarium namespace lunarium
{ {
@ -27,15 +34,16 @@ namespace lunarium
// } // }
World::World(std::string name) 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() void World::InitScriptState()
{ {
mScriptState.Initialize().LogIfFailed(LogCategory::GAME_SYSTEM, "Failed to initialize the world script state");
} }
void World::OnLoad() void World::OnLoad()
@ -60,9 +68,25 @@ namespace lunarium
void World::Update(float dt) void World::Update(float dt)
{ {
auto group = mECSRegistry.group<>(entt::get<VelocityComponent, TransformComponent>); // Update all scripts
auto group_scripts = mECSRegistry.view<ScriptComponent>();
for(auto entity: group_scripts)
{
auto& script_comp = group_scripts.get<ScriptComponent>(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<VelocityComponent, TransformComponent>);
for(auto entity: group) for(auto entity: group)
{ {
auto &transform = group.get<TransformComponent>(entity); auto &transform = group.get<TransformComponent>(entity);

@ -123,6 +123,7 @@ namespace lunarium
// TEST STUFF // TEST STUFF
bool mGetAssetsFromEditor; // This is for testing until we get a proper asset manager
private: // HELPERS private: // HELPERS
void InitScriptState(); void InitScriptState();

Loading…
Cancel
Save