Made scene loader "work" for now.

This commit is contained in:
2024-12-05 15:58:44 -06:00
parent a6ac4f029e
commit 223bbed232
8 changed files with 90 additions and 13 deletions

23
assets/prefabs/npc.json Normal file
View File

@ -0,0 +1,23 @@
{
"name": "Rosa",
"assets": {
"rosa": {
"type": "texture",
"path": "rosa.texture"
}
},
"components": {
"material": {
"type": "SimpleTexturedMaterial",
"texture": "rosa"
},
"meshRenderer": {
"type": "MeshRenderer"
},
"entity": {
"type": "RPGEntity"
}
}
}

View File

@ -2,7 +2,7 @@
"name": "Rosa", "name": "Rosa",
"assets": { "assets": {
"rosa_texture": { "rosa": {
"type": "texture", "type": "texture",
"path": "rosa.texture" "path": "rosa.texture"
} }
@ -11,7 +11,7 @@
"components": { "components": {
"material": { "material": {
"type": "SimpleTexturedMaterial", "type": "SimpleTexturedMaterial",
"texture": "rosa_texture" "texture": "rosa"
}, },
"meshRenderer": { "meshRenderer": {
"type": "MeshRenderer" "type": "MeshRenderer"
@ -20,7 +20,8 @@
"type": "RPGEntity" "type": "RPGEntity"
}, },
"player": { "player": {
"type": "RPGPlayer" "type": "RPGPlayer",
"camera": "camera"
} }
} }
} }

View File

@ -5,6 +5,10 @@
"rosa": { "rosa": {
"type": "prefab", "type": "prefab",
"path": "prefabs/rosa.json" "path": "prefabs/rosa.json"
},
"npc": {
"type": "prefab",
"path": "prefabs/npc.json"
} }
}, },
@ -20,6 +24,11 @@
"rosa": { "rosa": {
"prefab": "rosa", "prefab": "rosa",
"position": [ 0, 0, 0 ] "position": [ 0, 0, 0 ]
},
"npc": {
"prefab": "npc",
"position": [ 32, 0, 0 ]
} }
} }
} }

View File

@ -9,4 +9,5 @@ target_sources(${DAWN_TARGET_NAME}
SceneLoader.cpp SceneLoader.cpp
LoaderForSceneItems.cpp LoaderForSceneItems.cpp
PrefabLoader.cpp PrefabLoader.cpp
SceneLoadContext.cpp
) )

View File

@ -3,4 +3,16 @@
// This software is released under the MIT License. // This software is released under the MIT License.
// https://opensource.org/licenses/MIT // https://opensource.org/licenses/MIT
#include "SceneLoadContext.hpp" #include "SceneLoadContext.hpp"
using namespace Dawn;
std::shared_ptr<SceneItem> SceneLoadContext::getItem(const std::string &j) {
auto it = items.find(j);
if(it == items.end()) {
auto parent = this->parent.lock();
assertNotNull(parent, "Couldn't find item.");
return parent->getItem(j);
}
return it->second;
}

View File

@ -14,19 +14,23 @@ namespace Dawn {
class AssetLoader; class AssetLoader;
class SceneLoadContext; class SceneLoadContext;
class PrefabLoader;
class LoaderForSceneItems;
class SceneLoader;
class SceneLoadContext { class SceneLoadContext {
public: private:
std::weak_ptr<SceneLoadContext> parent; std::weak_ptr<SceneLoadContext> parent;
std::unordered_map<std::string, std::shared_ptr<SceneItem>> items; 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<SceneComponent>> components;
std::unordered_map<std::string, std::shared_ptr<AssetLoader>> assets; std::unordered_map<std::string, std::shared_ptr<AssetLoader>> assets;
json data;
std::shared_ptr<Scene> currentScene; std::shared_ptr<Scene> currentScene;
std::shared_ptr<SceneItem> currentItem; std::shared_ptr<SceneItem> currentItem;
std::shared_ptr<SceneComponent> currentComponent; std::shared_ptr<SceneComponent> currentComponent;
public:
json data;
/** /**
* Gets an asset from the context. * Gets an asset from the context.
* *
@ -46,6 +50,35 @@ namespace Dawn {
return asset; return asset;
} }
/**
* Gets an item from the context.
*
* @param j Name of the item to get.
* @return Item from the context.
*/
std::shared_ptr<SceneItem> getItem(const std::string &j);
/**
* Gets a component from the context.
*
* @param j Name of the component to get.
* @return Component from the context.
*/
template<class T>
std::shared_ptr<T> getComponent(const std::string &j) const {
auto it = components.find(j);
if(it == components.end()) {
auto parent = this->parent.lock();
assertNotNull(parent, "Couldn't find component.");
return parent->getComponent<T>(j);
}
auto cmp = std::dynamic_pointer_cast<T>(it->second);
assertNotNull(cmp, "Component is not of the correct type.");
return cmp;
}
friend class PrefabLoader;
friend class LoaderForSceneItems;
friend class SceneLoader;
}; };
} }

View File

@ -16,4 +16,5 @@ add_subdirectory(game)
# Assets # Assets
tool_texture(rosa rosa.png) tool_texture(rosa rosa.png)
tool_copy(prefab_rosa prefabs/rosa.json) tool_copy(prefab_rosa prefabs/rosa.json)
tool_copy(prefab_npc prefabs/npc.json)
tool_copy(test_rpg_scene scenes/test_rpg_scene.json) tool_copy(test_rpg_scene scenes/test_rpg_scene.json)

View File

@ -75,9 +75,6 @@ void RPGPlayer::load(std::shared_ptr<SceneLoadContext> ctx) {
SceneComponent::load(ctx); SceneComponent::load(ctx);
if(ctx->data.contains("camera")) { if(ctx->data.contains("camera")) {
assertMapHasKey(ctx->components, ctx->data["camera"], "Camera not found!"); this->camera = ctx->getComponent<Camera>(ctx->data["camera"]);
const auto component = ctx->components[ctx->data["camera"].get<std::string>()];
this->camera = std::dynamic_pointer_cast<Camera>(component);
assertNotNull(this->camera, "Camera not found!");
} }
} }