Browse Source

Scene: Load camera from JSON

master
Riyyi 2 years ago
parent
commit
892be89728
  1. 49
      assets/scene/scene1.json
  2. 52
      src/inferno/scene/scene.cpp
  3. 3
      src/inferno/scene/scene.h

49
assets/scene/scene1.json

@ -0,0 +1,49 @@
{
"init": "assets/lua/scene1-init.lua",
"camera": {
"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",
"translate": [ 0.0, 0.0, 0.0 ],
"rotate": [ 0.0, 0.0, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ],
"color": [ 1.0, 1.0, 1.0, 1.0 ],
"texture": "assets/gfx/test.png"
},
{
"name": "Quad 2",
"translate": [ 1.1, 0.0, 0.0 ],
"rotate": [ 0.0, 0.0, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ],
"color": [ 0.5, 0.6, 0.8, 1.0 ],
"texture": "assets/gfx/test.png"
},
{
"name": "Quad 3",
"translate": [ 2.2, 0.0, 0.0 ],
"rotate": [ 0.0, 0.0, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ],
"color": [ 1.0, 1.0, 1.0, 1.0 ],
"texture": "assets/gfx/test-inverted.png"
}
],
"text": [
{
"content": "HelloWorld!",
"font": "assets/fnt/dejavu-sans",
"font-size": 0,
"width": 150,
"lines": 3
}
]
}

52
src/inferno/scene/scene.cpp

@ -4,7 +4,9 @@
* SPDX-License-Identifier: MIT
*/
#include "ruc/file.h"
#include "ruc/format/log.h"
#include "ruc/json/json.h"
#include "ruc/meta/assert.h"
#include "inferno/component/cameracomponent.h"
@ -13,6 +15,7 @@
#include "inferno/component/spritecomponent.h"
#include "inferno/component/tagcomponent.h"
#include "inferno/component/textareacomponent.h"
#include "inferno/component/transformcomponent.h"
#include "inferno/scene/scene.h"
#include "inferno/script/cameracontroller.h"
#include "inferno/script/nativescript.h"
@ -43,20 +46,36 @@ void Scene::initialize()
m_texture = TextureManager::the().load("assets/gfx/test.png");
m_texture2 = TextureManager::the().load("assets/gfx/test-inverted.png");
// Load scene .json
// -------------------------------------
auto sceneJson = ruc::Json::parse(ruc::File("assets/scene/scene1.json").data());
VERIFY(sceneJson.exists("camera"), "scene doesnt contain a camera");
auto& cameraJson = sceneJson.at("camera");
uint32_t camera = loadEntity(cameraJson);
auto cameraType = CameraType::Perspective;
if (cameraJson.exists("type") && cameraJson.at("type").get<std::string>() == "orthographic") {
cameraType = CameraType::Orthographic;
}
addComponent<CameraComponent>(camera, cameraType);
if (cameraJson.exists("script")) {
auto& cameraScript = cameraJson.at("script");
if (cameraScript.exists("type") && cameraScript.exists("name")) {
if (cameraScript.at("type").get<std::string>() == "lua") {
addComponent<LuaScriptComponent>(camera, cameraScript.at("name").get<std::string>());
}
else {
addComponent<NativeScriptComponent>(camera).bind<CameraController>();
}
}
}
// Construct entities
// ---------------------------------
uint32_t camera = createEntity("Camera Entity");
auto& cameraTransform = getComponent<TransformComponent>(camera);
// cameraTransform.rotate.z = 0.0f;
// cameraTransform.translate.z = -1.0f;
// addComponent<CameraComponent>(camera, CameraType::Orthographic);
cameraTransform.rotate.z = -1.0f;
cameraTransform.translate.z = 1.0f;
addComponent<CameraComponent>(camera, CameraType::Perspective);
// addComponent<NativeScriptComponent>(camera).bind<CameraController>();
addComponent<LuaScriptComponent>(camera, "assets/lua/cameracontroller.lua");
uint32_t quad = createEntity("Quad");
addComponent<SpriteComponent>(quad, glm::vec4 { 1.0f, 1.0f, 1.0f, 1.0f }, m_texture);
@ -114,6 +133,17 @@ void Scene::destroyEntity(uint32_t entity)
m_registry->destroy(entt::entity { entity });
}
uint32_t Scene::loadEntity(ruc::Json json)
{
uint32_t entity = createEntity((json.exists("name"))
? json.at("name").get<std::string>()
: "");
auto& transform = getComponent<TransformComponent>(entity);
json.getTo(transform);
return entity;
}
glm::mat4 Scene::cameraProjectionView()
{
return CameraSystem::the().projectionView();

3
src/inferno/scene/scene.h

@ -12,6 +12,7 @@
#include "entt/entity/registry.hpp" // entt::entity, entt::registry
#include "glm/ext/matrix_float4x4.hpp" // glm::mat4
#include "ruc/format/format.h"
#include "ruc/json/json.h"
namespace Inferno {
@ -28,6 +29,8 @@ public:
uint32_t createEntity(const std::string& name = "");
void destroyEntity(uint32_t entity);
uint32_t loadEntity(ruc::Json json);
glm::mat4 cameraProjectionView();
void validEntity(uint32_t entity) const;

Loading…
Cancel
Save