Made scene loader "work" for now.
This commit is contained in:
		
							
								
								
									
										23
									
								
								assets/prefabs/npc.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								assets/prefabs/npc.json
									
									
									
									
									
										Normal 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"
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
  "name": "Rosa",
 | 
			
		||||
  
 | 
			
		||||
  "assets": {
 | 
			
		||||
    "rosa_texture": {
 | 
			
		||||
    "rosa": {
 | 
			
		||||
      "type": "texture",
 | 
			
		||||
      "path": "rosa.texture"
 | 
			
		||||
    }
 | 
			
		||||
@@ -11,7 +11,7 @@
 | 
			
		||||
  "components": {
 | 
			
		||||
    "material": {
 | 
			
		||||
      "type": "SimpleTexturedMaterial",
 | 
			
		||||
      "texture": "rosa_texture"
 | 
			
		||||
      "texture": "rosa"
 | 
			
		||||
    },
 | 
			
		||||
    "meshRenderer": {
 | 
			
		||||
      "type": "MeshRenderer"
 | 
			
		||||
@@ -20,7 +20,8 @@
 | 
			
		||||
      "type": "RPGEntity"
 | 
			
		||||
    },
 | 
			
		||||
    "player": {
 | 
			
		||||
      "type": "RPGPlayer"
 | 
			
		||||
      "type": "RPGPlayer",
 | 
			
		||||
      "camera": "camera"
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -5,6 +5,10 @@
 | 
			
		||||
    "rosa": {
 | 
			
		||||
      "type": "prefab",
 | 
			
		||||
      "path": "prefabs/rosa.json"
 | 
			
		||||
    },
 | 
			
		||||
    "npc": {
 | 
			
		||||
      "type": "prefab",
 | 
			
		||||
      "path": "prefabs/npc.json"
 | 
			
		||||
    }
 | 
			
		||||
  },
 | 
			
		||||
 | 
			
		||||
@@ -20,6 +24,11 @@
 | 
			
		||||
    "rosa": {
 | 
			
		||||
      "prefab": "rosa",
 | 
			
		||||
      "position": [ 0, 0, 0 ]
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
    "npc": {
 | 
			
		||||
      "prefab": "npc",
 | 
			
		||||
      "position": [ 32, 0, 0 ]
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -9,4 +9,5 @@ target_sources(${DAWN_TARGET_NAME}
 | 
			
		||||
    SceneLoader.cpp
 | 
			
		||||
    LoaderForSceneItems.cpp
 | 
			
		||||
    PrefabLoader.cpp
 | 
			
		||||
    SceneLoadContext.cpp
 | 
			
		||||
)
 | 
			
		||||
@@ -4,3 +4,15 @@
 | 
			
		||||
// https://opensource.org/licenses/MIT
 | 
			
		||||
 | 
			
		||||
#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;
 | 
			
		||||
}
 | 
			
		||||
@@ -14,19 +14,23 @@ namespace Dawn {
 | 
			
		||||
  class AssetLoader;
 | 
			
		||||
  class SceneLoadContext;
 | 
			
		||||
 | 
			
		||||
  class PrefabLoader;
 | 
			
		||||
  class LoaderForSceneItems;
 | 
			
		||||
  class SceneLoader;
 | 
			
		||||
 | 
			
		||||
  class SceneLoadContext {
 | 
			
		||||
    public:
 | 
			
		||||
    private:
 | 
			
		||||
      std::weak_ptr<SceneLoadContext> parent;
 | 
			
		||||
      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;
 | 
			
		||||
 | 
			
		||||
      json data;
 | 
			
		||||
 | 
			
		||||
      std::shared_ptr<Scene> currentScene;
 | 
			
		||||
      std::shared_ptr<SceneItem> currentItem;
 | 
			
		||||
      std::shared_ptr<SceneComponent> currentComponent;
 | 
			
		||||
 | 
			
		||||
    public:
 | 
			
		||||
      json data;
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Gets an asset from the context.
 | 
			
		||||
       * 
 | 
			
		||||
@@ -46,6 +50,35 @@ namespace Dawn {
 | 
			
		||||
        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;
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
@@ -16,4 +16,5 @@ add_subdirectory(game)
 | 
			
		||||
# Assets
 | 
			
		||||
tool_texture(rosa rosa.png)
 | 
			
		||||
tool_copy(prefab_rosa prefabs/rosa.json)
 | 
			
		||||
tool_copy(prefab_npc prefabs/npc.json)
 | 
			
		||||
tool_copy(test_rpg_scene scenes/test_rpg_scene.json)
 | 
			
		||||
@@ -75,9 +75,6 @@ void RPGPlayer::load(std::shared_ptr<SceneLoadContext> ctx) {
 | 
			
		||||
  SceneComponent::load(ctx);
 | 
			
		||||
 | 
			
		||||
  if(ctx->data.contains("camera")) {
 | 
			
		||||
    assertMapHasKey(ctx->components, ctx->data["camera"], "Camera not found!");
 | 
			
		||||
    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!");
 | 
			
		||||
    this->camera = ctx->getComponent<Camera>(ctx->data["camera"]);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user