Browse Source

Component+Scene: Restructure scene .json to allow multiple components

master
Riyyi 11 months ago
parent
commit
0dd7a05d46
  1. 96
      assets/scene/scene1.json
  2. 26
      src/inferno/component/cameracomponent.cpp
  3. 3
      src/inferno/component/cameracomponent.h
  4. 22
      src/inferno/component/id-component.cpp
  5. 28
      src/inferno/component/id-component.h
  6. 21
      src/inferno/component/luascriptcomponent.cpp
  7. 7
      src/inferno/component/luascriptcomponent.h
  8. 21
      src/inferno/component/nativescriptcomponent.cpp
  9. 19
      src/inferno/component/nativescriptcomponent.h
  10. 22
      src/inferno/component/tagcomponent.cpp
  11. 4
      src/inferno/component/tagcomponent.h
  12. 116
      src/inferno/scene/scene.cpp
  13. 10
      src/inferno/scene/scene.h
  14. 4
      src/inferno/script/nativescript.h
  15. 38
      src/inferno/uid.cpp
  16. 47
      src/inferno/uid.h

96
assets/scene/scene1.json

@ -1,50 +1,68 @@
{ {
"init": "assets/lua/scene1-init.lua", "init": "assets/lua/scene1-init.lua",
"camera": { "entities": [
"name": "Camera",
"translate": [ 0.0, 0.0, 1.0 ],
"rotate": [ 0.0, 0.0, -1.0 ],
"scale": [ 1.0, 1.0, 1.0 ],
"type": "perspective",
"script": {
"type": "lua",
"name": "assets/lua/cameracontroller.lua"
}
},
"quad": [
{ {
"name": "Quad", "id": { "id": 12312312 },
"translate": [ 0.0, 0.0, 0.0 ], "tag": { "tag": "Camera" },
"rotate": [ 0.0, 0.0, 0.0 ], "transform" : {
"scale": [ 1.0, 1.0, 1.0 ], "translate": [0.0, 0.0, 1.0],
"color": [ 1.0, 1.0, 1.0, 1.0 ], "rotate": [0.0, 0.0, -1.0],
"texture": "assets/gfx/test.png" "scale": [1.0, 1.0, 1.0]
},
"camera": { "type": "perspective" },
"lua-scripts": [
{ "path": "assets/lua/cameracontroller.lua" }
]
}, },
{ {
"name": "Quad 2", "id": { "id": 564564564 },
"translate": [ 1.1, 0.0, 0.0 ], "tag": { "tag": "Quad" },
"rotate": [ 0.0, 0.0, 0.0 ], "transform" : {
"scale": [ 1.0, 1.0, 1.0 ], "translate": [ 0.0, 0.0, 0.0 ],
"color": [ 0.5, 0.6, 0.8, 1.0 ], "rotate": [ 0.0, 0.0, 0.0 ],
"texture": "assets/gfx/test.png" "scale": [ 1.0, 1.0, 1.0 ]
},
"sprite": {
"color": [ 1.0, 1.0, 1.0, 1.0 ],
"texture": "assets/gfx/test.png"
}
}, },
{ {
"name": "Quad 3", "id": { "id": 97897897 },
"translate": [ 2.2, 0.0, 0.0 ], "tag": { "tag": "Quad 2" },
"rotate": [ 0.0, 0.0, 0.0 ], "transform" : {
"scale": [ 1.0, 1.0, 1.0 ], "translate": [ 1.1, 0.0, 0.0 ],
"color": [ 1.0, 1.0, 1.0, 1.0 ], "rotate": [ 0.0, 0.0, 0.0 ],
"texture": "assets/gfx/test-inverted.png" "scale": [ 1.0, 1.0, 1.0 ]
} },
], "sprite": {
"text": [ "color": [ 0.5, 0.6, 0.8, 1.0 ],
"texture": "assets/gfx/test.png"
}
},
{
"id": { "id": 3424242 },
"tag": { "tag": "Quad 3" },
"transform" : {
"translate": [ 2.2, 0.0, 0.0 ],
"rotate": [ 0.0, 0.0, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
"sprite": {
"color": [ 1.0, 1.0, 1.0, 1.0 ],
"texture": "assets/gfx/test-inverted.png"
}
},
{ {
"name": "Text", "id": { "id": 675754 },
"content": "Hello World!", "tag": { "tag": "Text" },
"font": "assets/fnt/open-sans", "text": {
"font-size": 24, "content": "Hello World!",
"line-spacing": 1.0, "font": "assets/fnt/open-sans",
"width": 150 "font-size": 24,
"line-spacing": 1.0,
"width": 150
}
} }
] ]
} }

26
src/inferno/component/cameracomponent.cpp

@ -0,0 +1,26 @@
/*
* Copyright (C) 2023 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#include "ruc/format/print.h"
#include "ruc/json/json.h"
#include "inferno/component/cameracomponent.h"
namespace Inferno {
void fromJson(const ruc::Json& json, CameraComponent& value)
{
VERIFY(json.type() == ruc::Json::Type::Object);
if (json.exists("type")) {
value.type = json.at("type").get<std::string>() == "orthographic" ? CameraType::Orthographic : CameraType::Perspective;
}
if (json.exists("zoom-level")) {
json.at("zoom-level").getTo(value.zoomLevel);
}
}
} // namespace Inferno

3
src/inferno/component/cameracomponent.h

@ -8,6 +8,7 @@
#include "glm/ext/matrix_float4x4.hpp" // glm::mat4 #include "glm/ext/matrix_float4x4.hpp" // glm::mat4
#include "glm/ext/vector_float3.hpp" // glm::vec3 #include "glm/ext/vector_float3.hpp" // glm::vec3
#include "ruc/json/json.h"
namespace Inferno { namespace Inferno {
@ -32,4 +33,6 @@ struct CameraComponent {
glm::mat4 projection { 1.0f }; // Identity matrix glm::mat4 projection { 1.0f }; // Identity matrix
}; };
void fromJson(const ruc::Json& json, CameraComponent& value);
} // namespace Inferno } // namespace Inferno

22
src/inferno/component/id-component.cpp

@ -0,0 +1,22 @@
/*
* Copyright (C) 2023 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#include "ruc/json/json.h"
#include "inferno/component/id-component.h"
namespace Inferno {
void fromJson(const ruc::Json& json, IDComponent& value)
{
VERIFY(json.type() == ruc::Json::Type::Object);
if (json.exists("id")) {
json.at("id").getTo(value.id);
}
}
} // namespace Inferno

28
src/inferno/component/id-component.h

@ -0,0 +1,28 @@
/*
* Copyright (C) 2023 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#pragma once
#include "ruc/json/json.h"
#include "inferno/uid.h"
namespace Inferno {
struct IDComponent {
UID id;
IDComponent() = default;
IDComponent(UID id)
: id(id)
{
}
IDComponent(const IDComponent&) = default;
};
void fromJson(const ruc::Json& json, IDComponent& value);
} // namespace Inferno

21
src/inferno/component/luascriptcomponent.cpp

@ -0,0 +1,21 @@
/*
* Copyright (C) 2023 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#include "ruc/json/json.h"
#include "inferno/component/luascriptcomponent.h"
namespace Inferno {
void fromJson(const ruc::Json& json, LuaScriptComponent& value)
{
VERIFY(json.type() == ruc::Json::Type::Object);
VERIFY(json.exists("path"), "path not found");
json.at("path").getTo(value.path);
}
} // namespace Inferno

7
src/inferno/component/luascriptcomponent.h

@ -6,9 +6,11 @@
#pragma once #pragma once
#include <string> // std::string #include <string>
#include <utility> // std::move #include <utility> // std::move
#include "ruc/json/json.h"
namespace Inferno { namespace Inferno {
class LuaScript; class LuaScript;
@ -24,4 +26,7 @@ struct LuaScriptComponent {
{ {
} }
}; };
void fromJson(const ruc::Json& json, LuaScriptComponent& value);
} // namespace Inferno } // namespace Inferno

21
src/inferno/component/nativescriptcomponent.cpp

@ -0,0 +1,21 @@
/*
* Copyright (C) 2023 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#include "ruc/json/json.h"
#include "inferno/component/nativescriptcomponent.h"
namespace Inferno {
void fromJson(const ruc::Json& json, NativeScriptComponent& value)
{
VERIFY(json.type() == ruc::Json::Type::Object);
VERIFY(json.exists("name"), "name not found");
json.at("name").getTo(value.name);
}
} // namespace Inferno

19
src/inferno/component/nativescriptcomponent.h

@ -7,6 +7,7 @@
#pragma once #pragma once
#include <string> #include <string>
#include <utility> // std::move
#include "inferno/script/nativescript.h" #include "inferno/script/nativescript.h"
@ -14,17 +15,29 @@ namespace Inferno {
struct NativeScriptComponent { struct NativeScriptComponent {
NativeScript* instance { nullptr }; NativeScript* instance { nullptr };
std::string name;
NativeScript::InitializeFunction initialize { nullptr }; NativeScript::InitializeFunction initialize { nullptr };
NativeScript::DestroyFunction destroy { nullptr }; NativeScript::DestroyFunction destroy { nullptr };
// Dont allow manually setting instance during construction // Dont allow manually setting instance during construction
NativeScriptComponent() {} NativeScriptComponent() {}
NativeScriptComponent(const std::string& binding) NativeScriptComponent(const std::string& name)
: name(std::move(name))
{ {
initialize = NativeScriptBinding::the().initializeBinding(binding); bind();
destroy = NativeScriptBinding::the().destroyBinding(binding); }
void bind()
{
VERIFY(initialize == nullptr && destroy == nullptr, "NativeScript already bound");
VERIFY(name != "", "name not set");
initialize = NativeScriptBinding::the().initializeBinding(name);
destroy = NativeScriptBinding::the().destroyBinding(name);
} }
}; };
void fromJson(const ruc::Json& json, NativeScriptComponent& value);
} // namespace Inferno } // namespace Inferno

22
src/inferno/component/tagcomponent.cpp

@ -0,0 +1,22 @@
/*
* Copyright (C) 2023 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#include "ruc/json/json.h"
#include "inferno/component/tagcomponent.h"
namespace Inferno {
void fromJson(const ruc::Json& json, TagComponent& value)
{
VERIFY(json.type() == ruc::Json::Type::Object);
if (json.exists("tag")) {
json.at("tag").getTo(value.tag);
}
}
} // namespace Inferno

4
src/inferno/component/tagcomponent.h

@ -9,6 +9,8 @@
#include <string> // std::string #include <string> // std::string
#include <utility> // std::move #include <utility> // std::move
#include "ruc/json/json.h"
namespace Inferno { namespace Inferno {
struct TagComponent { struct TagComponent {
@ -23,4 +25,6 @@ struct TagComponent {
operator const std::string&() const { return tag; } operator const std::string&() const { return tag; }
}; };
void fromJson(const ruc::Json& json, TagComponent& value);
} // namespace Inferno } // namespace Inferno

116
src/inferno/scene/scene.cpp

@ -4,6 +4,7 @@
* SPDX-License-Identifier: MIT * SPDX-License-Identifier: MIT
*/ */
#include <cstddef> // size_t
#include <cstdint> // uint32_t #include <cstdint> // uint32_t
#include <limits> // std::numeric_limits #include <limits> // std::numeric_limits
@ -13,6 +14,7 @@
#include "ruc/meta/assert.h" #include "ruc/meta/assert.h"
#include "inferno/component/cameracomponent.h" #include "inferno/component/cameracomponent.h"
#include "inferno/component/id-component.h"
#include "inferno/component/luascriptcomponent.h" #include "inferno/component/luascriptcomponent.h"
#include "inferno/component/nativescriptcomponent.h" #include "inferno/component/nativescriptcomponent.h"
#include "inferno/component/spritecomponent.h" #include "inferno/component/spritecomponent.h"
@ -26,6 +28,7 @@
#include "inferno/system/scriptsystem.h" #include "inferno/system/scriptsystem.h"
#include "inferno/system/textareasystem.h" #include "inferno/system/textareasystem.h"
#include "inferno/system/transformsystem.h" #include "inferno/system/transformsystem.h"
#include "inferno/uid.h"
namespace Inferno { namespace Inferno {
@ -47,54 +50,66 @@ void Scene::initialize()
auto sceneJson = ruc::Json::parse(ruc::File("assets/scene/scene1.json").data()); auto sceneJson = ruc::Json::parse(ruc::File("assets/scene/scene1.json").data());
// Camera if (sceneJson.exists("init")) {
// TODO: load either NativeScript or LuaScript?
VERIFY(sceneJson.exists("camera"), "scene doesnt contain a camera");
auto& cameraJson = sceneJson.at("camera");
uint32_t camera = loadEntity(cameraJson);
auto& cameraComponent = addComponent<CameraComponent>(camera);
if (cameraJson.exists("type") && cameraJson.at("type").get<std::string>() == "orthographic") {
cameraComponent.type = CameraType::Orthographic;
}
if (cameraJson.exists("zoom-level") && cameraJson.at("zoom-level").type() == ruc::Json::Type::Number) {
cameraComponent.zoomLevel = cameraJson.at("zoom-level").asDouble();
} }
if (cameraJson.exists("script")) { // Entities
auto& cameraScript = cameraJson.at("script"); // -------------------------------------
if (cameraScript.exists("type") && cameraScript.exists("name")) {
auto name = cameraScript.at("name").get<std::string>();
if (cameraScript.at("type").get<std::string>() == "lua") {
addComponent<LuaScriptComponent>(camera, name);
}
else {
addComponent<NativeScriptComponent>(camera, name);
}
}
}
// Quads if (sceneJson.exists("entities")) {
const auto& entityJson = sceneJson.at("entities");
VERIFY(entityJson.type() == ruc::Json::Type::Array);
const auto& entities = entityJson.asArray();
for (size_t i = 0; i < entities.size(); ++i) {
if (sceneJson.exists("quad") && sceneJson.at("quad").type() == ruc::Json::Type::Array) { uint32_t entity = createEntity();
auto& quads = sceneJson.at("quad").asArray().elements();
for (const auto& quad : quads) {
uint32_t quadEntity = loadEntity(quad);
addComponent<SpriteComponent>(quadEntity);
auto& spriteComponent = getComponent<SpriteComponent>(quadEntity);
quad.getTo(spriteComponent);
}
}
// Text VERIFY(entities.at(i).type() == ruc::Json::Type::Object);
const auto& components = entities.at(i);
if (sceneJson.exists("text") && sceneJson.at("text").type() == ruc::Json::Type::Array) { // ID is required
auto& texts = sceneJson.at("text").asArray().elements(); VERIFY(components.exists("id"), "id not found");
for (const auto& text : texts) { auto& id = getComponent<IDComponent>(entity);
uint32_t textEntity = loadEntity(text); components.at("id").getTo(id);
addComponent<TextAreaComponent>(textEntity);
auto& textAreaComponent = getComponent<TextAreaComponent>(textEntity); if (components.exists("tag")) {
text.getTo(textAreaComponent); auto& tag = getComponent<TagComponent>(entity);
components.at("tag").getTo(tag);
}
if (components.exists("transform")) {
auto& transform = getComponent<TransformComponent>(entity);
components.at("transform").getTo(transform);
}
if (components.exists("camera")) {
auto& camera = addComponent<CameraComponent>(entity);
components.at("camera").getTo(camera);
}
if (components.exists("lua-scripts")) {
VERIFY(components.at("lua-scripts").type() == ruc::Json::Type::Array);
const auto& scripts = components.at("lua-scripts").asArray();
for (size_t j = 0; j < scripts.size(); ++j) {
auto& script = addComponent<LuaScriptComponent>(entity);
scripts.at(j).getTo(script);
}
}
if (components.exists("native-scripts")) {
VERIFY(components.at("native-scripts").type() == ruc::Json::Type::Array);
const auto& scripts = components.at("native-scripts").asArray();
for (size_t j = 0; j < scripts.size(); ++j) {
auto& script = addComponent<NativeScriptComponent>(entity);
scripts.at(j).getTo(script);
script.bind();
}
}
if (components.exists("sprite")) {
auto& sprite = addComponent<SpriteComponent>(entity);
components.at("sprite").getTo(sprite);
}
if (components.exists("text")) {
auto& text = addComponent<TextAreaComponent>(entity);
components.at("text").getTo(text);
}
} }
} }
@ -125,20 +140,15 @@ void Scene::destroy()
uint32_t Scene::createEntity(const std::string& name) uint32_t Scene::createEntity(const std::string& name)
{ {
uint32_t entity = static_cast<uint32_t>(m_registry->create()); return createEntityWithUID(UID(), name);
addComponent<TagComponent>(entity, name.empty() ? "Unnamed Entity" : name);
addComponent<TransformComponent>(entity);
return entity;
} }
uint32_t Scene::loadEntity(ruc::Json json) uint32_t Scene::createEntityWithUID(UID id, const std::string& name)
{ {
uint32_t entity = createEntity((json.exists("name")) uint32_t entity = static_cast<uint32_t>(m_registry->create());
? json.at("name").get<std::string>() addComponent<IDComponent>(entity, id);
: ""); addComponent<TagComponent>(entity, name.empty() ? "Unnamed Entity" : name);
auto& transform = getComponent<TransformComponent>(entity); addComponent<TransformComponent>(entity);
json.getTo(transform);
return entity; return entity;
} }

10
src/inferno/scene/scene.h

@ -6,6 +6,7 @@
#pragma once #pragma once
#include <cstddef> // size_t
#include <cstdint> // uint32_t #include <cstdint> // uint32_t
#include <memory> // std::shared_ptr #include <memory> // std::shared_ptr
@ -14,6 +15,8 @@
#include "ruc/format/format.h" #include "ruc/format/format.h"
#include "ruc/json/json.h" #include "ruc/json/json.h"
#include "inferno/uid.h"
namespace Inferno { namespace Inferno {
class Camera; class Camera;
@ -27,7 +30,7 @@ public:
void destroy(); void destroy();
uint32_t createEntity(const std::string& name = ""); uint32_t createEntity(const std::string& name = "");
uint32_t loadEntity(ruc::Json json); uint32_t createEntityWithUID(UID id, const std::string& name = "");
uint32_t findEntity(std::string_view name); uint32_t findEntity(std::string_view name);
void destroyEntity(uint32_t entity); void destroyEntity(uint32_t entity);
@ -49,7 +52,8 @@ public:
return m_registry->any<T...>(entt::entity { entity }); return m_registry->any<T...>(entt::entity { entity });
} }
// @Todo Should replace be allowed? could trigger memory leaks with nativescript // TODO: Should replace be allowed? could trigger memory leaks with nativescript
// TODO: Replace will make it so an entity cant have multiple scripts
template<typename T, typename... P> template<typename T, typename... P>
T& addComponent(uint32_t entity, P&&... parameters) const T& addComponent(uint32_t entity, P&&... parameters) const
{ {
@ -64,7 +68,7 @@ public:
return m_registry->remove_if_exists<T>(entt::entity { entity }); return m_registry->remove_if_exists<T>(entt::entity { entity });
} }
// @Todo Should replace be allowed? could trigger memory leaks with nativescript // TODO: Should replace be allowed? could trigger memory leaks with nativescript
template<typename T, typename... P> template<typename T, typename... P>
T& getComponent(uint32_t entity, P&&... parameters) const T& getComponent(uint32_t entity, P&&... parameters) const
{ {

4
src/inferno/script/nativescript.h

@ -55,8 +55,8 @@ public:
m_detroyBindings.emplace(binding, destroy); m_detroyBindings.emplace(binding, destroy);
} }
NativeScript::InitializeFunction initializeBinding(const std::string& binding) { return m_initializeBindings[binding]; } NativeScript::InitializeFunction initializeBinding(const std::string& name) { return m_initializeBindings[name]; }
NativeScript::DestroyFunction destroyBinding(const std::string& binding) { return m_detroyBindings[binding]; } NativeScript::DestroyFunction destroyBinding(const std::string& name) { return m_detroyBindings[name]; }
private: private:
std::unordered_map<std::string, NativeScript::InitializeFunction> m_initializeBindings; std::unordered_map<std::string, NativeScript::InitializeFunction> m_initializeBindings;

38
src/inferno/uid.cpp

@ -0,0 +1,38 @@
/*
* Copyright (C) 2023 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#include <cstdint>
#include <random>
#include "ruc/format/log.h"
#include "ruc/json/json.h"
#include "inferno/uid.h"
namespace Inferno {
static std::random_device s_seed;
static std::mt19937_64 s_engine(s_seed());
static std::uniform_int_distribution<uint64_t> s_distribution;
UID::UID()
: m_uid(s_distribution(s_engine))
{
}
UID::UID(uint64_t uid)
: m_uid(uid)
{
}
void fromJson(const ruc::Json& json, UID& value)
{
VERIFY(json.type() == ruc::Json::Type::Number);
value = UID((int64_t)json.asDouble());
}
} // namespace Inferno

47
src/inferno/uid.h

@ -0,0 +1,47 @@
/*
* Copyright (C) 2023 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#pragma once
#include <cstdint> // uint64_t
#include "ruc/json/json.h"
namespace Inferno {
class UID {
public:
UID();
UID(uint64_t uid);
// Comparison operator for std::map
bool operator<(const UID& other) const
{
return m_uid < other.m_uid;
}
operator uint64_t() const { return m_uid; }
private:
uint64_t m_uid;
};
void fromJson(const ruc::Json& json, UID& value);
} // namespace Inferno
namespace std {
// Hash function for std::unordered_map
template<>
struct hash<Inferno::UID> {
size_t operator()(const Inferno::UID& uid) const
{
return hash<uint64_t>()(static_cast<uint64_t>(uid));
}
};
} // namespace std
Loading…
Cancel
Save