Skip to content

Commit

Permalink
feat: dynamic data on gameobjects at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMcAvoy committed Nov 4, 2024
1 parent 44b74f2 commit b29e399
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*.{cpp,h,hpp}]
indent_style = space
indent_size = 2
tab_width = 2
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ Come back later, sorry! Jenjin is not ready for real usage.
- [x] Transparency on sprites
- [x] Icons everywhere
- [x] Code editor in the editor
- [x] Dynamic data attached to game objects at runtime
- [ ] Improved meshing algorithm that gives same input meshes all one mesh reference instead of making multiple mesh references and data for them.
- [ ] Dynamic data attached to game objects at runtime
- [ ] Hierarchy and `.Parent` in Lua along with `workspace` (e.g. `Workspace.MyGameObject.Parent` == `Workspace`)
- [ ] Console window in the editor showing output from the engine
- [ ] Autocompletion in the editor
- [ ] Allow user to create UI from Lua
- [ ] Better Explorer, allowing right click and creation of new scripts if on the scripts context menu

## TODO (Future)
- [ ] Serialization/Deserialization of scenes to better formats
Expand Down
15 changes: 15 additions & 0 deletions engine/include/jenjin/datastore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <sol/sol.hpp>

#include <string>
#include <unordered_map>

namespace Jenjin {
struct DataStore {
std::unordered_map<std::string, sol::object> entries;

void DynamicSet(std::string key, sol::stack_object value);
sol::object DynamicGet(std::string key);
};
} // namespace Jenjin
9 changes: 7 additions & 2 deletions engine/include/jenjin/gameobject.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#pragma once

#include "jenjin/datastore.h"
#include "jenjin/mesh.h"

#include <glm/glm.hpp>
#include <spdlog/spdlog.h>

#include <sol/sol.hpp>

#include <string>

namespace Jenjin {
Expand Down Expand Up @@ -32,6 +35,8 @@ class GameObject {

bool mixColor = false;

DataStore dataStore;

// TODO: hierarchy
#ifdef false
GameObject *parent = nullptr;
Expand All @@ -43,14 +48,14 @@ class GameObject {
glm::vec2 GetPosition() { return transform.position; }
glm::vec2 GetScale() { return transform.scale; }
float GetRotation() { return transform.rotation; }
glm::vec3 GetColor() { return color; }
glm::vec3 GetColor() { return color; }

// Setters
void SetName(std::string name) { this->name = name; }
void SetPosition(glm::vec2 position) { transform.position = position; }
void SetScale(glm::vec2 scale) { transform.scale = scale; }
void SetRotation(float rotation) { transform.rotation = rotation; }
void SetColor(glm::vec3 color) { this->color = color; }
void SetColor(glm::vec3 color) { this->color = color; }

// Pointer getters
std::string *GetNamePointer() { return &name; }
Expand Down
4 changes: 4 additions & 0 deletions engine/include/jenjin/luamanager.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "jenjin/datastore.h"

#include <sol/forward.hpp>
#include <sol/sol.hpp>
#include <unordered_map>
Expand Down Expand Up @@ -28,6 +30,8 @@ class LuaManager {
// Getters
sol::state *GetLuaState() { return &lua; }

DataStore dataStore;

private:
sol::state lua;

Expand Down
23 changes: 23 additions & 0 deletions engine/src/datastore.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "jenjin/datastore.h"

using namespace Jenjin;

void DataStore::DynamicSet(std::string key, sol::stack_object value) {
auto it = entries.find(key);
if (it == entries.cend()) {
entries.insert(it, {std::move(key), std::move(value)});
} else {
std::pair<const std::string, sol::object> &kvp = *it;
sol::object &entry = kvp.second;
entry = sol::object(std::move(value));
}
}

sol::object DataStore::DynamicGet(std::string key) {
auto it = entries.find(key);
if (it == entries.cend()) {
return sol::lua_nil;
}

return it->second;
}
8 changes: 6 additions & 2 deletions engine/src/editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ void Manager::viewport(Jenjin::Scene *scene) {
}

ImGui::GetWindowDrawList()->AddImage(
(void *)(intptr_t)renderTexture, ImVec2(ImGui::GetCursorScreenPos()),
(ImTextureID)renderTexture, ImVec2(ImGui::GetCursorScreenPos()),
ImVec2(ImGui::GetCursorScreenPos().x + size.x,
ImGui::GetCursorScreenPos().y + size.y),
ImVec2(0, 1), ImVec2(1, 0));
Expand Down Expand Up @@ -537,11 +537,15 @@ void Manager::code(Jenjin::Scene *scene) {
// TODO: completions on tab
if (ImGui::InputTextMultiline("##Code", codeBuffer, sizeof(codeBuffer),
ImVec2(ImGui::GetWindowWidth() - 20,
ImGui::GetWindowHeight() - 40 - 32),
ImGui::GetWindowHeight() - 40 - 32 - 24),
ImGuiInputTextFlags_AllowTabInput)) {
showUnsaved = true;
}

ImGui::BeginChild("InfoChild", ImVec2(0, 0), true);
ImGui::Text("%s", codeFile);
ImGui::EndChild();

ImGui::End();
}

Expand Down
29 changes: 28 additions & 1 deletion engine/src/luamanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,20 @@ void Bindings(LuaManager *lm, sol::state &lua) {
&GameObject::SetScale, "SetRotation", &GameObject::SetRotation,
"GetColor", &GameObject::GetColor, "SetColor", &GameObject::SetColor,
"Translate", &GameObject::Translate, "Scale", &GameObject::Scale,
"Rotate", &GameObject::Rotate);
"Rotate", &GameObject::Rotate,

sol::meta_function::index,
[](GameObject &gobj, const std::string &key) {
return gobj.dataStore.DynamicGet(key);
},

sol::meta_function::new_index,
[](GameObject &gobj, const std::string &key, sol::stack_object value) {
gobj.dataStore.DynamicSet(key, std::move(value));
},

sol::meta_function::length,
[](GameObject &gobj) { return gobj.dataStore.entries.size(); });

lua.new_usertype<GameObject::Transform>(
"Transform",
Expand Down Expand Up @@ -431,6 +444,20 @@ void Bindings(LuaManager *lm, sol::state &lua) {
}
});

// Globals table (this->dataStore), it needs to be bound to a `globals`
// variable in the lua script and have a metatable that allows indexing and
// new indexing of the table
lua.new_usertype<DataStore>(
"DataStore", sol::no_constructor, sol::meta_function::index,
[](DataStore &ds, const std::string &key) { return ds.DynamicGet(key); },
sol::meta_function::new_index,
[](DataStore &ds, const std::string &key, sol::stack_object value) {
ds.DynamicSet(key, std::move(value));
},
sol::meta_function::length,
[](DataStore &ds) { return ds.entries.size(); });
lua.set("globals", lm->dataStore);

auto helpers_tb = lua.create_named_table("helpers");
helpers_tb.set_function(
"CreateQuad", [&]() { return Jenjin::Helpers::CreateQuad(2.0f, 2.0f); });
Expand Down
2 changes: 1 addition & 1 deletion megasource

0 comments on commit b29e399

Please sign in to comment.