Example scene loading
This commit is contained in:
@@ -19,6 +19,9 @@ Scene::Scene(
|
||||
void Scene::init() {
|
||||
Scene &selfReference = *this;
|
||||
sceneInitializer(selfReference);
|
||||
sceneInitializer = [](Scene &scene) -> void {
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
void Scene::deinit() {
|
||||
|
@@ -19,7 +19,7 @@ namespace Dawn {
|
||||
class Scene final : public std::enable_shared_from_this<Scene> {
|
||||
private:
|
||||
std::weak_ptr<Game> game;
|
||||
const std::function<void(Scene&)> sceneInitializer;
|
||||
std::function<void(Scene&)> sceneInitializer;
|
||||
std::vector<std::shared_ptr<SceneItem>> sceneItems;
|
||||
std::vector<std::shared_ptr<SceneItem>> sceneItemsToRemove;
|
||||
bool_t paused = false;
|
||||
|
@@ -70,6 +70,10 @@ std::shared_ptr<Game> SceneComponent::getGame() {
|
||||
return this->getScene()->getGame();
|
||||
}
|
||||
|
||||
void SceneComponent::load(const SceneComponentLoadContext &context) {
|
||||
// Override this method to load data from a JSON object.
|
||||
}
|
||||
|
||||
SceneComponent::~SceneComponent() {
|
||||
if(Flag::isOn<uint_fast8_t>(
|
||||
sceneComponentState,
|
||||
|
@@ -5,6 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
#include "dawn.hpp"
|
||||
#include "assert/assert.hpp"
|
||||
|
||||
#define SCENE_COMPONENT_STATE_INIT 0x01
|
||||
#define SCENE_COMPONENT_STATE_DISPOSED 0x02
|
||||
@@ -13,6 +14,25 @@ namespace Dawn {
|
||||
class Game;
|
||||
class Scene;
|
||||
class SceneItem;
|
||||
class SceneComponent;
|
||||
class AssetLoader;
|
||||
|
||||
struct SceneComponentLoadContext {
|
||||
json data;
|
||||
std::shared_ptr<Scene> scene;
|
||||
std::unordered_map<std::string, std::shared_ptr<SceneItem>> items;
|
||||
std::unordered_map<std::string, std::shared_ptr<SceneComponent>> components;
|
||||
std::unordered_map<std::string, std::shared_ptr<AssetLoader>> assets;
|
||||
|
||||
template<class T>
|
||||
std::shared_ptr<T> getAsset(const std::string &j) const {
|
||||
auto it = assets.find(j);
|
||||
assertTrue(it != assets.end(), "Asset not found.");
|
||||
auto asset = std::dynamic_pointer_cast<T>(it->second);
|
||||
assertNotNull(asset, "Asset is not of the correct type.");
|
||||
return asset;
|
||||
}
|
||||
};
|
||||
|
||||
class SceneComponent : std::enable_shared_from_this<SceneComponent> {
|
||||
private:
|
||||
@@ -75,6 +95,14 @@ namespace Dawn {
|
||||
*/
|
||||
std::shared_ptr<Game> getGame();
|
||||
|
||||
/**
|
||||
* Load data from a JSON object. This is typically done during a scene
|
||||
* load.
|
||||
*
|
||||
* @param json JSON Data that this object needs to load.
|
||||
*/
|
||||
virtual void load(const SceneComponentLoadContext &context);
|
||||
|
||||
/**
|
||||
* Disposes this scene component.
|
||||
*/
|
||||
|
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "scene/SceneItem.hpp"
|
||||
#include "scene/Scene.hpp"
|
||||
#include "util/JSON.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
@@ -65,6 +66,29 @@ void SceneItem::deinit() {
|
||||
this->components.clear();
|
||||
}
|
||||
|
||||
void SceneItem::load(const SceneComponentLoadContext &ctx) {
|
||||
// Transforms
|
||||
if(ctx.data.contains("position")) {
|
||||
this->setLocalPosition(JSON::vec3(ctx.data["position"]));
|
||||
}
|
||||
|
||||
if(ctx.data.contains("lookAt")) {
|
||||
auto &lookAt = ctx.data["lookAt"];
|
||||
glm::vec3 pos = glm::vec3(3, 3, 3);
|
||||
glm::vec3 look = glm::vec3(0, 0, 0);
|
||||
glm::vec3 up = glm::vec3(0, 1, 0);
|
||||
|
||||
if(lookAt.contains("position")) pos = JSON::vec3(lookAt["position"]);
|
||||
if(lookAt.contains("look")) look = JSON::vec3(lookAt["look"]);
|
||||
if(lookAt.contains("up")) up = JSON::vec3(lookAt["up"]);
|
||||
this->lookAt(pos, look, up);
|
||||
}
|
||||
|
||||
if(ctx.data.contains("scale")) {
|
||||
this->setLocalScale(JSON::vec3(ctx.data["scale"]));
|
||||
}
|
||||
}
|
||||
|
||||
void SceneItem::remove() {
|
||||
auto scene = getScene();
|
||||
if(!scene) return;
|
||||
|
@@ -41,6 +41,15 @@ namespace Dawn {
|
||||
*/
|
||||
void deinit();
|
||||
|
||||
/**
|
||||
* Loads this scene item from the given context. Scene items are not
|
||||
* responsible for loading their components, this is handled by the scene
|
||||
* loader.
|
||||
*
|
||||
* @param ctx Context to load this scene item from.
|
||||
*/
|
||||
void load(const SceneComponentLoadContext &ctx);
|
||||
|
||||
/**
|
||||
* Returns the scene that this scene item belongs to.
|
||||
*
|
||||
|
Reference in New Issue
Block a user