From 4a1a149a68e620dc4748fcf64143e075fb2d0210 Mon Sep 17 00:00:00 2001 From: Thraix Date: Thu, 13 Jul 2023 10:58:41 +0200 Subject: [PATCH] Add Scene deserialization --- CopiumEngine/CopiumEngine.vcxproj | 1 + CopiumEngine/CopiumEngine.vcxproj.filters | 3 + CopiumEngine/res/scenes/scene.meta | 121 ++++++++ CopiumEngine/src/copium/asset/AssetRef.cpp | 2 + CopiumEngine/src/copium/asset/AssetRef.h | 1 + CopiumEngine/src/copium/core/Scene.cpp | 275 +++++++++++------- CopiumEngine/src/copium/core/Scene.h | 11 +- CopiumEngine/src/copium/ecs/ComponentPool.h | 1 + .../src/copium/example/AnimationSystem.h | 16 +- CopiumEngine/src/copium/example/Components.h | 39 ++- .../copium/example/HealthComponentListener.h | 4 +- .../example/LevelGeneratorComponentListener.h | 77 +++++ .../copium/example/PlayerControllerSystem.h | 12 +- .../src/copium/example/RenderSystem.h | 4 +- .../src/copium/example/UiRenderSystem.h | 4 +- CopiumEngine/src/copium/util/MetaFile.cpp | 28 +- CopiumEngine/src/copium/util/MetaFile.h | 2 + CopiumEngine/src/copium/util/Uuid.cpp | 6 +- CopiumEngine/src/copium/util/Uuid.h | 6 +- 19 files changed, 456 insertions(+), 157 deletions(-) create mode 100644 CopiumEngine/res/scenes/scene.meta create mode 100644 CopiumEngine/src/copium/example/LevelGeneratorComponentListener.h diff --git a/CopiumEngine/CopiumEngine.vcxproj b/CopiumEngine/CopiumEngine.vcxproj index 2ab5e7e..9f19c72 100644 --- a/CopiumEngine/CopiumEngine.vcxproj +++ b/CopiumEngine/CopiumEngine.vcxproj @@ -259,6 +259,7 @@ + diff --git a/CopiumEngine/CopiumEngine.vcxproj.filters b/CopiumEngine/CopiumEngine.vcxproj.filters index 1f35fa9..f0eb3cd 100644 --- a/CopiumEngine/CopiumEngine.vcxproj.filters +++ b/CopiumEngine/CopiumEngine.vcxproj.filters @@ -521,5 +521,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/CopiumEngine/res/scenes/scene.meta b/CopiumEngine/res/scenes/scene.meta new file mode 100644 index 0000000..9b2cb5f --- /dev/null +++ b/CopiumEngine/res/scenes/scene.meta @@ -0,0 +1,121 @@ +[LevelGenerator] + +--- +[Transform] +position=0.1 -0.4 +size=0.8 0.8 + +[Texture] +texture-uuid=3e9ef1a2-59ba-2e4d-b2cd-f5efaa531af7 + +[StaticCollider] +resolve-collision=false + +[Pickup] +[Renderable] + +--- +[Transform] +position=0.1 0.1 +size=0.2 0.2 + +[Texture] +texture-uuid=f49a5284-d666-0982-95ca-cf68cc3d4f45 + +[MouseFollow] +[Renderable] + +--- +[Transform] +position=10 10 +size=0 + +[Text] +font=3e9ef1a2-59ba-2e4d-b2cd-f5efaa531af7 +text=0 fps +font-size=20 + +[FrameCount] +[UiRenderable] + +--- +[Camera] +static-bounding-box=false +ui-camera=false + +[Transform] +position=0 0 +size=2 2 + +--- +[Camera] +static-bounding-box=false +ui-camera=false + +[Transform] +position=0 0 +size=2 2 + +[Uuid] +uuid=a14e2328-ebce-5026-e5ab-495daf8c3660 + +--- +[Camera] +static-bounding-box=false +ui-camera=true + +[Transform] +position=0 0 +size=1 1 + +--- + +[Player] +camera-uuid=a14e2328-ebce-5026-e5ab-495daf8c3660 + +[Transform] +position=0 2 +size=1 1 + +[Health] +health=10 + +[Physics] +mass=0.1 + +[Texture] +texture-uuid=0964e525-22c3-4d25-d5c6-a162965f6e8d +tex-coord=0 0 +tex-size=0.25 0.25 + +[Animation] +sheet-size=4 4 +sheet-coord=0 3 +images=4 +horizontal=true +time=0.5 + +[DynamicCollider] +resolve-collision=false +collider-offset=0.4375 0 +collider-size=0.125 0.65625 + +[Uuid] +uuid=09617c73-2b1e-0a83-db81-3ae3b5971c11 + +[Renderable] + +--- +[Transform] +position=10 800 +size=1 1 + +[Text] +font=3e9ef1a2-59ba-2e4d-b2cd-f5efaa531af7 +text= +font-size=20 + +[Debug] +player-uuid=09617c73-2b1e-0a83-db81-3ae3b5971c11 + +[UiRenderable] diff --git a/CopiumEngine/src/copium/asset/AssetRef.cpp b/CopiumEngine/src/copium/asset/AssetRef.cpp index 897e58f..c7d60a1 100644 --- a/CopiumEngine/src/copium/asset/AssetRef.cpp +++ b/CopiumEngine/src/copium/asset/AssetRef.cpp @@ -10,6 +10,8 @@ namespace Copium } }; + AssetRef::AssetRef() = default; + AssetRef::AssetRef(AssetHandle handle) : handle{std::shared_ptr(new AssetHandle{handle}, AssetHandleUnloader{})} {} diff --git a/CopiumEngine/src/copium/asset/AssetRef.h b/CopiumEngine/src/copium/asset/AssetRef.h index 699fbc6..d5101f3 100644 --- a/CopiumEngine/src/copium/asset/AssetRef.h +++ b/CopiumEngine/src/copium/asset/AssetRef.h @@ -10,6 +10,7 @@ namespace Copium std::shared_ptr handle; public: + AssetRef(); AssetRef(AssetHandle handle); operator AssetHandle() const; diff --git a/CopiumEngine/src/copium/core/Scene.cpp b/CopiumEngine/src/copium/core/Scene.cpp index 832f693..244c3fa 100644 --- a/CopiumEngine/src/copium/core/Scene.cpp +++ b/CopiumEngine/src/copium/core/Scene.cpp @@ -7,16 +7,17 @@ #include "copium/ecs/Entity.h" #include "copium/ecs/System.h" #include "copium/event/MouseMoveEvent.h" +#include "copium/example/AnimationSystem.h" #include "copium/example/CameraFollowPlayerSystem.h" #include "copium/example/CameraUpdateSystem.h" #include "copium/example/ColliderSystem.h" #include "copium/example/Components.h" #include "copium/example/DebugSystem.h" -#include "copium/example/AnimationSystem.h" #include "copium/example/FrameCountSystem.h" #include "copium/example/HealthChangeSystem.h" #include "copium/example/HealthComponentListener.h" #include "copium/example/HealthDisplaySystem.h" +#include "copium/example/LevelGeneratorComponentListener.h" #include "copium/example/MouseFollowSystem.h" #include "copium/example/PhysicsSystem.h" #include "copium/example/PickupSystem.h" @@ -53,110 +54,9 @@ namespace Copium ecs->AddSystem(renderer.get(), descriptorSetRenderer.get(), &commandBuffer, &viewMatrix, &projectionMatrix); // better way to store the RenderSystem data? ecs->AddSystem(uiRenderer.get(), uiDescriptorSetRenderer.get(), &commandBuffer, &uiProjectionMatrix); ecs->SetComponentListener(); + ecs->SetComponentListener(); - // TODO: Load from scene file - for (int y = 0; y < 20; y++) - { - { - Entity entity = Entity::Create(ecs.get()); - entity.AddComponent(glm::vec2{-10.0f, -10.0f + y * 1.0}, glm::vec2{1.0f, 1.0f}); - if(y == 0 || y == 19) - entity.AddComponent(AssetRef{AssetManager::LoadAsset("stone.meta")}, glm::vec2{0.0f, 0.0f}, glm::vec2{0.25f, 1.0f}); - else - entity.AddComponent(AssetRef{AssetManager::LoadAsset("stone.meta")}, glm::vec2{0.75f, 0.0f}, glm::vec2{1.0f, 1.0f}); - entity.AddComponent(true); - entity.AddComponent(); - } - { - Entity entity = Entity::Create(ecs.get()); - entity.AddComponent(glm::vec2{10.0f, -10.0f + y * 1.0}, glm::vec2{1.0f, 1.0f}); - if(y == 0 || y == 19) - entity.AddComponent(AssetRef{AssetManager::LoadAsset("stone.meta")}, glm::vec2{0.5f, 0.0f}, glm::vec2{0.75f, 1.0f}); - else - entity.AddComponent(AssetRef{AssetManager::LoadAsset("stone.meta")}, glm::vec2{0.75f, 0.0f}, glm::vec2{1.0f, 1.0f}); - entity.AddComponent(true); - entity.AddComponent(); - } - } - for (int x = 1; x < 20; x++) - { - { - Entity entity = Entity::Create(ecs.get()); - entity.AddComponent(glm::vec2{-10.0f + x * 1.0, -10.0f}, glm::vec2{1.0f, 1.0f}); - entity.AddComponent(AssetRef{AssetManager::LoadAsset("stone.meta")}, glm::vec2{0.25f, 0.0f}, glm::vec2{0.5f, 1.0f}); - entity.AddComponent(true); - entity.AddComponent(); - } - { - Entity entity = Entity::Create(ecs.get()); - entity.AddComponent(glm::vec2{-10.0f + x * 1.0, 10.0f}, glm::vec2{1.0f, 1.0f}); - entity.AddComponent(AssetRef{AssetManager::LoadAsset("stone.meta")}, glm::vec2{0.25f, 0.0f}, glm::vec2{0.5f, 1.0f}); - entity.AddComponent(true); - entity.AddComponent(); - } - } - for (int y = 0; y < 10; y++) - { - for (int x = 0; x < 10; x++) - { - Entity entity = Entity::Create(ecs.get()); - entity.AddComponent(glm::vec2{-10.0f + x * 1.6f + 0.4f, -10.0f + y * 1.6 + 0.4f}, glm::vec2{0.8f, 0.8f}); - entity.AddComponent(glm::vec3{x * 0.1f, y * 0.1f, 1.0f}); - entity.AddComponent(false); - entity.AddComponent(); - entity.AddComponent(); - } - } - - float aspect = Vulkan::GetSwapChain().GetExtent().width / (float)Vulkan::GetSwapChain().GetExtent().height; - Entity entityFox = Entity::Create(ecs.get()); - entityFox.AddComponent(glm::vec2{-0.9f, -0.4f}, glm::vec2{0.8f, 0.8f}); - entityFox.AddComponent(AssetRef{AssetManager::LoadAsset("fox.meta")}, glm::vec2{0.0f, 0.0f}, glm::vec2{1.0f, 1.0f}); - entityFox.AddComponent(true); - entityFox.AddComponent(); - - Entity entityFontAtlas = Entity::Create(ecs.get()); - entityFontAtlas.AddComponent(glm::vec2{0.1f, -0.4f}, glm::vec2{0.8, 0.8}); - entityFontAtlas.AddComponent(AssetRef{AssetManager::LoadAsset("font.meta")}, glm::vec2{0.0f, 0.0f}, glm::vec2{1.0f, 1.0f}); - entityFontAtlas.AddComponent(false); - entityFontAtlas.AddComponent(); - entityFontAtlas.AddComponent(); - - Entity entityMouse = Entity::Create(ecs.get()); - entityMouse.AddComponent(glm::vec2(0.1), glm::vec2{0.2}); - entityMouse.AddComponent(AssetRef{AssetManager::LoadAsset("fox.meta")}, glm::vec2{0.0f, 0.0f}, glm::vec2{1.0f, 1.0f}); - entityMouse.AddComponent(); - entityMouse.AddComponent(); - - Entity entityText = Entity::Create(ecs.get()); - entityText.AddComponent(glm::vec2{-aspect * 10.0f + 0.1f, 9.4f}, glm::vec2{1.0}); - entityText.AddComponent(AssetRef{AssetManager::LoadAsset("font.meta")}, std::to_string(0) + " fps", 0.6f); - entityText.AddComponent(); - entityText.AddComponent(); - - Entity entityCamera = Entity::Create(ecs.get()); - entityCamera.AddComponent(BoundingBox(-aspect, -1.0f, aspect, 1.0f), false, false); - entityCamera.AddComponent(glm::vec2{0.0f}, glm::vec2{2.0f}); - - Entity entityUiCamera = Entity::Create(ecs.get()); - entityUiCamera.AddComponent(BoundingBox(0.0f, 0.0f, Vulkan::GetSwapChain().GetExtent().width, Vulkan::GetSwapChain().GetExtent().height), false, true); - entityUiCamera.AddComponent(glm::vec2{0.0f}, glm::vec2{1.0f}); - - Entity entityPlayer = Entity::Create(ecs.get()); - entityPlayer.AddComponent(entityCamera, false); - entityPlayer.AddComponent(10, 10); - entityPlayer.AddComponent(0.1f, glm::vec2{0.0f, 0.0f}, glm::vec2{0.0f, 0.0f}); - entityPlayer.AddComponent(glm::vec2{0.0f, 2.0f}, glm::vec2{1.0f}); - entityPlayer.AddComponent(AssetRef{AssetManager::LoadAsset("character.meta")}, glm::vec2{0.0f, 0.0f}, glm::vec2{0.25f, 1.0f}); - entityPlayer.AddComponent(4, 4, 0, 3, 4, true, 0.5f); - entityPlayer.AddComponent(false, glm::vec2{14.0f / 32.0f, 0.0f / 32.0f}, glm::vec2{4.0f / 32.0f, 21.0f / 32.0f}, glm::vec2{0.0f}); - entityPlayer.AddComponent(); - - Entity entityDebug = Entity::Create(ecs.get()); - entityDebug.AddComponent(entityPlayer); - entityDebug.AddComponent(AssetRef(AssetManager::LoadAsset("font.meta")), "", 20.0f); - entityDebug.AddComponent(glm::vec2{10.0f, Vulkan::GetSwapChain().GetExtent().height - 10.0f}, glm::vec2{1.0f}); - entityDebug.AddComponent(); + Deserialize("res/scenes/scene.meta"); } void Scene::Update() @@ -169,4 +69,171 @@ namespace Copium ecs->UpdateSystems(EventSignal{event}); return EventResult::Continue; } + + void Scene::Deserialize(const std::string& file) + { + float aspect = Vulkan::GetSwapChain().GetExtent().width / (float)Vulkan::GetSwapChain().GetExtent().height; + + std::vector metaFiles = MetaFile::ReadList(file); + + for (auto& metaFile : metaFiles) + { + Entity entity = Entity::Create(ecs.get()); + for (auto& [name, metaClass] : metaFile.GetMetaFileClasses()) + { + try + { + char* endPtr; + if (name == "Transform") + { + TransformC transform; + transform.position = ReadVec2Opt(metaClass, "position", glm::vec2{0.0f, 0.0f}); + transform.size = ReadVec2Opt(metaClass, "size", glm::vec2{1.0f, 1.0f}); + entity.AddComponent(transform); + } + else if (name == "Texture") + { + TextureC texture; + texture.asset = AssetRef{AssetManager::LoadAsset(Uuid{metaClass.GetValue("texture-uuid")})}; + texture.texCoord1 = ReadVec2Opt(metaClass, "tex-coord", glm::vec2{0.0f, 0.0f}); + texture.texCoord2 = texture.texCoord1 + ReadVec2Opt(metaClass, "tex-size", glm::vec2{1.0f, 1.0f} - texture.texCoord1); + entity.AddComponent(texture); + } + else if (name == "StaticCollider") + { + StaticColliderC staticCollider; + staticCollider.resolveCollision = ReadBoolOpt(metaClass, "resolve-collision", true); + entity.AddComponent(staticCollider); + } + else if (name == "DynamicCollider") + { + DynamicColliderC dynamicCollider; + dynamicCollider.resolveCollision = ReadBoolOpt(metaClass, "resolve-collision", true); + dynamicCollider.colliderOffset = ReadVec2Opt(metaClass, "collider-offset", glm::vec2{0.0f, 0.0f}); + dynamicCollider.colliderSize = ReadVec2Opt(metaClass, "collider-size", glm::vec2{1.0f, 1.0f}); + entity.AddComponent(dynamicCollider); + } + else if (name == "Text") + { + TextC text; + text.font = AssetRef{AssetManager::LoadAsset(Uuid{metaClass.GetValue("font")})}; + text.text = metaClass.GetValue("text"); + text.fontSize = std::strtof(metaClass.GetValue("font-size").c_str(), &endPtr); + entity.AddComponent(text); + } + else if (name == "Camera") + { + CameraC camera; + camera.staticBoundingBox = ReadBoolOpt(metaClass, "static-bounding-box", false); + camera.uiCamera = ReadBoolOpt(metaClass, "ui-camera", false); + if (camera.uiCamera) + camera.projection = BoundingBox(0, 0, Vulkan::GetSwapChain().GetExtent().width, Vulkan::GetSwapChain().GetExtent().height); + else + camera.projection = BoundingBox(-aspect, -1.0f, aspect, 1.0f); + entity.AddComponent(camera); + } + else if (name == "Uuid") + { + UuidC uuid; + uuid.uuid = Uuid{metaClass.GetValue("uuid")}; + entity.AddComponent(uuid); + } + else if (name == "Player") + { + PlayerC player; + player.camera = GetEntity(Uuid{metaClass.GetValue("camera-uuid")}); + entity.AddComponent(player); + } + else if (name == "Health") + { + HealthC health; + health.max = std::strtol(metaClass.GetValue("health").c_str(), &endPtr, 10); + health.current = health.max; + entity.AddComponent(health); + } + else if (name == "Physics") + { + PhysicsC physics; + physics.mass = std::strtof(metaClass.GetValue("mass").c_str(), &endPtr); + entity.AddComponent(physics); + } + else if (name == "Animation") + { + AnimationC animation; + animation.sheetCoord = ReadVec2Opt(metaClass, "sheet-coord", glm::ivec2{0, 0}); + animation.sheetSize = ReadVec2Opt(metaClass, "sheet-size", glm::ivec2{1, 1}); + animation.images = std::strtol(metaClass.GetValue("images").c_str(), &endPtr, 10); + animation.horizontal = ReadBoolOpt(metaClass, "horizontal", true); + animation.time = std::strtof(metaClass.GetValue("time").c_str(), &endPtr); + entity.AddComponent(animation); + } + else if (name == "Debug") + { + DebugC debug; + debug.playerEntity = GetEntity(Uuid{metaClass.GetValue("player-uuid")}); + entity.AddComponent(debug); + } + else if (name == "Renderable") + entity.AddComponent(); + else if (name == "Pickup") + entity.AddComponent(); + else if (name == "FrameCount") + entity.AddComponent(); + else if (name == "MouseFollow") + entity.AddComponent(); + else if (name == "UiRenderable") + entity.AddComponent(); + else if(name == "LevelGenerator") + entity.AddComponent(); + else + CP_WARN("Unknown component: %s", name.c_str()); + } + catch (RuntimeException& exception) { CP_ERR("Invalid %s component: %s", name.c_str(), exception.GetErrorMessage().c_str()); } + } + } + } + + Entity Scene::GetEntity(const Uuid& uuid) const + { + Entity entity{ecs.get(),ecs->Find([&](EntityId entity, const UuidC& uuidArg) { return uuid == uuidArg.uuid; })}; + CP_ASSERT(entity, "Failed to find entity with Uuid=%s", uuid.ToString().c_str()); + + return entity; + } + + glm::vec2 Scene::ReadVec2Opt(const MetaFileClass& metaClass, const std::string& key, glm::vec2 vec) + { + if (!metaClass.HasValue(key)) + return vec; + + const std::string& value = metaClass.GetValue(key); + char* endPos; + vec.x = std::strtof(value.c_str(), &endPos); + vec.y = std::strtof(endPos, &endPos); + return vec; + } + + glm::ivec2 Scene::ReadVec2Opt(const MetaFileClass& metaClass, const std::string& key, glm::ivec2 vec) + { + if (!metaClass.HasValue(key)) + return vec; + + const std::string& value = metaClass.GetValue(key); + char* endPos; + vec.x = std::strtof(value.c_str(), &endPos); + vec.y = std::strtof(endPos, &endPos); + return vec; + } + + + bool Scene::ReadBoolOpt(const MetaFileClass& metaClass, const std::string& key, bool vec) + { + if (!metaClass.HasValue(key)) + return vec; + + const std::string& value = metaClass.GetValue(key); + if (value == "true") + return true; + return false; + } } \ No newline at end of file diff --git a/CopiumEngine/src/copium/core/Scene.h b/CopiumEngine/src/copium/core/Scene.h index 83c2642..2f95d2f 100644 --- a/CopiumEngine/src/copium/core/Scene.h +++ b/CopiumEngine/src/copium/core/Scene.h @@ -1,9 +1,11 @@ #pragma once -#include "copium/renderer/Renderer.h" #include "copium/ecs/ECSManager.h" +#include "copium/ecs/Entity.h" #include "copium/event/Event.h" #include "copium/event/EventResult.h" +#include "copium/renderer/Renderer.h" +#include "copium/util/Uuid.h" #include @@ -25,5 +27,12 @@ namespace Copium Scene(CommandBuffer& commandBuffer, DescriptorPool& descriptorPool); void Update(); EventResult OnEvent(const Event& event); + private: + void Deserialize(const std::string& file); + Entity GetEntity(const Uuid& uuid) const; + + glm::vec2 ReadVec2Opt(const MetaFileClass& metaClass, const std::string& key, glm::vec2 vec); + glm::ivec2 ReadVec2Opt(const MetaFileClass& metaClass, const std::string& key, glm::ivec2 vec); + bool ReadBoolOpt(const MetaFileClass& metaClass, const std::string& key, bool vec); }; } \ No newline at end of file diff --git a/CopiumEngine/src/copium/ecs/ComponentPool.h b/CopiumEngine/src/copium/ecs/ComponentPool.h index 40df656..ca5b8d9 100644 --- a/CopiumEngine/src/copium/ecs/ComponentPool.h +++ b/CopiumEngine/src/copium/ecs/ComponentPool.h @@ -4,6 +4,7 @@ #include "copium/ecs/EntitySet.h" #include "copium/ecs/ComponentPoolBase.h" #include "copium/ecs/ComponentListener.h" +#include "copium/util/Common.h" #include diff --git a/CopiumEngine/src/copium/example/AnimationSystem.h b/CopiumEngine/src/copium/example/AnimationSystem.h index 4cb986f..719a960 100644 --- a/CopiumEngine/src/copium/example/AnimationSystem.h +++ b/CopiumEngine/src/copium/example/AnimationSystem.h @@ -16,16 +16,16 @@ namespace Copium animation.frame = (animation.frame + 1) % animation.images; } if (animation.horizontal) { - texture.texCoord1.x = (animation.sheetStartCoordX + animation.frame) / (float)animation.sheetSizeX; - texture.texCoord2.x = (animation.sheetStartCoordX + animation.frame + 1) / (float)animation.sheetSizeX; - texture.texCoord1.y = (animation.sheetStartCoordY) / (float)animation.sheetSizeY; - texture.texCoord2.y = (animation.sheetStartCoordY + 1) / (float)animation.sheetSizeY; + texture.texCoord1.x = (animation.sheetCoord.x + animation.frame) / (float)animation.sheetSize.x; + texture.texCoord2.x = (animation.sheetCoord.x + animation.frame + 1) / (float)animation.sheetSize.x; + texture.texCoord1.y = (animation.sheetCoord.y) / (float)animation.sheetSize.y; + texture.texCoord2.y = (animation.sheetCoord.y + 1) / (float)animation.sheetSize.y; } else { - texture.texCoord1.x = (animation.sheetStartCoordX) / (float)animation.sheetSizeX; - texture.texCoord2.x = (animation.sheetStartCoordX + 1) / (float)animation.sheetSizeX; - texture.texCoord1.y = (animation.sheetStartCoordY + animation.frame) / (float)animation.sheetSizeY; - texture.texCoord2.y = (animation.sheetStartCoordY + animation.frame + 1) / (float)animation.sheetSizeY; + texture.texCoord1.x = (animation.sheetCoord.x) / (float)animation.sheetSize.x; + texture.texCoord2.x = (animation.sheetCoord.x + 1) / (float)animation.sheetSize.x; + texture.texCoord1.y = (animation.sheetCoord.y + animation.frame) / (float)animation.sheetSize.y; + texture.texCoord2.y = (animation.sheetCoord.y + animation.frame + 1) / (float)animation.sheetSize.y; } } }; diff --git a/CopiumEngine/src/copium/example/Components.h b/CopiumEngine/src/copium/example/Components.h index 582abcc..c2819ab 100644 --- a/CopiumEngine/src/copium/example/Components.h +++ b/CopiumEngine/src/copium/example/Components.h @@ -2,6 +2,7 @@ #include "copium/asset/AssetRef.h" #include "copium/ecs/Entity.h" +#include "copium/util/Uuid.h" #include @@ -15,8 +16,8 @@ namespace Copium glm::vec2 size; }; - struct Renderable {}; - struct UiRenderable {}; + struct RenderableC {}; + struct UiRenderableC {}; struct ColorC { @@ -53,14 +54,16 @@ namespace Copium struct PhysicsC { float mass; - glm::vec2 force; - glm::vec2 velocity; + + glm::vec2 force{}; + glm::vec2 velocity{}; }; struct PlayerC { Entity camera; - bool grounded; + + bool grounded = false; }; struct HealthC @@ -68,8 +71,8 @@ namespace Copium int current; int max; - Entity background; - Entity foreground; + Entity background{}; + Entity foreground{}; }; struct StaticColliderC @@ -83,7 +86,7 @@ namespace Copium glm::vec2 colliderOffset; glm::vec2 colliderSize; - glm::vec2 oldPosition; + glm::vec2 oldPosition{}; }; struct PickupC @@ -98,15 +101,23 @@ namespace Copium struct AnimationC { - int sheetSizeX; - int sheetSizeY; - int sheetStartCoordX; - int sheetStartCoordY; + glm::ivec2 sheetCoord; + glm::ivec2 sheetSize; int images; bool horizontal; float time; - float timeElapsed; - int frame; + float timeElapsed = 0.0f; + int frame = 0; + }; + + struct UuidC + { + Uuid uuid; + }; + + struct LevelGeneratorC + { + std::vector entities; }; } \ No newline at end of file diff --git a/CopiumEngine/src/copium/example/HealthComponentListener.h b/CopiumEngine/src/copium/example/HealthComponentListener.h index b5ca222..229dfb1 100644 --- a/CopiumEngine/src/copium/example/HealthComponentListener.h +++ b/CopiumEngine/src/copium/example/HealthComponentListener.h @@ -16,12 +16,12 @@ namespace Copium health.background = Entity::Create(manager); health.background.AddComponent(glm::vec2{0.0f, 0.0f}, glm::vec2{0.5f, 0.05f}); health.background.AddComponent(glm::vec3{0.152f, 0.14f, 0.207f}); - health.background.AddComponent(); + health.background.AddComponent(); health.foreground = Entity::Create(manager); health.foreground.AddComponent(glm::vec2{0.0f, 0.0f}, glm::vec2{0.5f * std::clamp(health.current, 0, health.max) / (float)health.max, 0.05f}); health.foreground.AddComponent(glm::vec3{0.581f, 0.393f, 0.462f}); - health.foreground.AddComponent(); + health.foreground.AddComponent(); } void Removed(EntityId entityId, HealthC& health) override diff --git a/CopiumEngine/src/copium/example/LevelGeneratorComponentListener.h b/CopiumEngine/src/copium/example/LevelGeneratorComponentListener.h new file mode 100644 index 0000000..001ba20 --- /dev/null +++ b/CopiumEngine/src/copium/example/LevelGeneratorComponentListener.h @@ -0,0 +1,77 @@ +#pragma once + +#include "copium/asset/AssetManager.h" +#include "copium/ecs/ComponentListener.h" +#include "copium/example/Components.h" + +namespace Copium +{ + class LevelGeneratorComponentListener : public ComponentListener + { + void Added(EntityId entityId, LevelGeneratorC& levelGenerator) override + { + CP_ASSERT(levelGenerator.entities.empty(), "LevelGenerator has already generated the level"); + + for (int y = 0; y < 20; y++) + { + { + Entity entity = Entity::Create(manager); + entity.AddComponent(glm::vec2{-10.0f, -10.0f + y * 1.0}, glm::vec2{1.0f, 1.0f}); + if(y == 0 || y == 19) + entity.AddComponent(AssetRef{AssetManager::LoadAsset("stone.meta")}, glm::vec2{0.0f, 0.0f}, glm::vec2{0.25f, 1.0f}); + else + entity.AddComponent(AssetRef{AssetManager::LoadAsset("stone.meta")}, glm::vec2{0.75f, 0.0f}, glm::vec2{1.0f, 1.0f}); + entity.AddComponent(true); + entity.AddComponent(); + } + { + Entity entity = Entity::Create(manager); + entity.AddComponent(glm::vec2{10.0f, -10.0f + y * 1.0}, glm::vec2{1.0f, 1.0f}); + if(y == 0 || y == 19) + entity.AddComponent(AssetRef{AssetManager::LoadAsset("stone.meta")}, glm::vec2{0.5f, 0.0f}, glm::vec2{0.75f, 1.0f}); + else + entity.AddComponent(AssetRef{AssetManager::LoadAsset("stone.meta")}, glm::vec2{0.75f, 0.0f}, glm::vec2{1.0f, 1.0f}); + entity.AddComponent(true); + entity.AddComponent(); + } + } + for (int x = 1; x < 20; x++) + { + { + Entity entity = Entity::Create(manager); + entity.AddComponent(glm::vec2{-10.0f + x * 1.0, -10.0f}, glm::vec2{1.0f, 1.0f}); + entity.AddComponent(AssetRef{AssetManager::LoadAsset("stone.meta")}, glm::vec2{0.25f, 0.0f}, glm::vec2{0.5f, 1.0f}); + entity.AddComponent(true); + entity.AddComponent(); + } + { + Entity entity = Entity::Create(manager); + entity.AddComponent(glm::vec2{-10.0f + x * 1.0, 10.0f}, glm::vec2{1.0f, 1.0f}); + entity.AddComponent(AssetRef{AssetManager::LoadAsset("stone.meta")}, glm::vec2{0.25f, 0.0f}, glm::vec2{0.5f, 1.0f}); + entity.AddComponent(true); + entity.AddComponent(); + } + } + for (int y = 0; y < 10; y++) + { + for (int x = 0; x < 10; x++) + { + Entity entity = Entity::Create(manager); + entity.AddComponent(glm::vec2{-10.0f + x * 1.6f + 0.4f, -10.0f + y * 1.6 + 0.4f}, glm::vec2{0.8f, 0.8f}); + entity.AddComponent(glm::vec3{x * 0.1f, y * 0.1f, 1.0f}); + entity.AddComponent(false); + entity.AddComponent(); + entity.AddComponent(); + } + } + } + + void Removed(EntityId entityId, LevelGeneratorC& levelGenerator) override + { + for (auto& entity : levelGenerator.entities) + { + entity.Destroy(); + } + } + }; +} \ No newline at end of file diff --git a/CopiumEngine/src/copium/example/PlayerControllerSystem.h b/CopiumEngine/src/copium/example/PlayerControllerSystem.h index 70b4a34..e3462e3 100644 --- a/CopiumEngine/src/copium/example/PlayerControllerSystem.h +++ b/CopiumEngine/src/copium/example/PlayerControllerSystem.h @@ -30,20 +30,20 @@ namespace Copium animation.time = 0.5; if (Input::IsKeyDown(CP_KEY_A)) { force -= 1.0f; - animation.sheetStartCoordY = 0; + animation.sheetCoord.y = 0; animation.time = 0.2; } if (Input::IsKeyDown(CP_KEY_D)) { force += 1.0f; - animation.sheetStartCoordY = 1; + animation.sheetCoord.y = 1; animation.time = 0.2; } - if (force == 0.0f && animation.sheetStartCoordY == 1) - animation.sheetStartCoordY = 3; - if (force == 0.0f && animation.sheetStartCoordY == 0) - animation.sheetStartCoordY = 2; + if (force == 0.0f && animation.sheetCoord.y == 1) + animation.sheetCoord.y = 3; + if (force == 0.0f && animation.sheetCoord.y == 0) + animation.sheetCoord.y = 2; float magnitude = 75.0f; physics.force.x += force * magnitude; diff --git a/CopiumEngine/src/copium/example/RenderSystem.h b/CopiumEngine/src/copium/example/RenderSystem.h index e383207..1e94bee 100644 --- a/CopiumEngine/src/copium/example/RenderSystem.h +++ b/CopiumEngine/src/copium/example/RenderSystem.h @@ -10,7 +10,7 @@ namespace Copium { - class RenderSystem : public System + class RenderSystem : public System { private: // Find better way to store these? @@ -30,7 +30,7 @@ namespace Copium projectionMatrix{projectionMatrix} {} - void RunEntity(Entity entity, Renderable& renderable, TransformC& transform) override + void RunEntity(Entity entity, RenderableC& renderable, TransformC& transform) override { if (entity.HasComponent()) { diff --git a/CopiumEngine/src/copium/example/UiRenderSystem.h b/CopiumEngine/src/copium/example/UiRenderSystem.h index 6199fef..f228d9f 100644 --- a/CopiumEngine/src/copium/example/UiRenderSystem.h +++ b/CopiumEngine/src/copium/example/UiRenderSystem.h @@ -10,7 +10,7 @@ namespace Copium { - class UiRenderSystem : public System + class UiRenderSystem : public System { private: // Find better way to store these? @@ -26,7 +26,7 @@ namespace Copium projectionMatrix{projectionMatrix} {} - void RunEntity(Entity entity, UiRenderable& uiRenderable, TransformC& transform) + void RunEntity(Entity entity, UiRenderableC& uiRenderable, TransformC& transform) { if (entity.HasComponent()) { diff --git a/CopiumEngine/src/copium/util/MetaFile.cpp b/CopiumEngine/src/copium/util/MetaFile.cpp index 8f37b5e..8fad1f8 100644 --- a/CopiumEngine/src/copium/util/MetaFile.cpp +++ b/CopiumEngine/src/copium/util/MetaFile.cpp @@ -61,6 +61,11 @@ namespace Copium LoadMetaFile(stream); } + const std::map& MetaFile::GetMetaFileClasses() + { + return classes; + } + bool MetaFile::HasMetaClass(const std::string& className) const { return classes.find(className) != classes.end(); @@ -161,19 +166,18 @@ namespace Copium { std::vector metaFiles; std::ifstream stream{file}; - if(stream) + + CP_ASSERT(stream, "Failed to read file: %s", file.c_str()); + + MetaFile meta; + while(!stream.eof()) { - MetaFile meta; - while(!stream.eof()) - { - MetaFile meta{}; - stream >> meta; - if(meta.classes.empty()) - continue; - metaFiles.emplace_back(meta); - } - return metaFiles; + MetaFile meta{}; + stream >> meta; + if(meta.classes.empty()) + continue; + metaFiles.emplace_back(meta); } - return {}; + return metaFiles; } } diff --git a/CopiumEngine/src/copium/util/MetaFile.h b/CopiumEngine/src/copium/util/MetaFile.h index 05ce603..737e5ce 100644 --- a/CopiumEngine/src/copium/util/MetaFile.h +++ b/CopiumEngine/src/copium/util/MetaFile.h @@ -40,6 +40,8 @@ namespace Copium MetaFile(std::istream& stream); bool HasMetaClass(const std::string& className) const; + + const std::map& GetMetaFileClasses(); MetaFileClass& GetMetaClass(const std::string& className); const MetaFileClass& GetMetaClass(const std::string& className) const; const std::string& GetFilePath() const; diff --git a/CopiumEngine/src/copium/util/Uuid.cpp b/CopiumEngine/src/copium/util/Uuid.cpp index 8b16930..4e7f27f 100644 --- a/CopiumEngine/src/copium/util/Uuid.cpp +++ b/CopiumEngine/src/copium/util/Uuid.cpp @@ -55,17 +55,17 @@ namespace Copium return string; } - bool Uuid::operator==(const Uuid& rhs) + bool Uuid::operator==(const Uuid& rhs) const { return msb == rhs.msb && lsb == rhs.lsb; } - bool Uuid::operator!=(const Uuid& rhs) + bool Uuid::operator!=(const Uuid& rhs) const { return !(*this == rhs); } - bool Uuid::operator<(const Uuid& rhs) + bool Uuid::operator<(const Uuid& rhs) const { if (msb != rhs.msb) return msb < rhs.msb; diff --git a/CopiumEngine/src/copium/util/Uuid.h b/CopiumEngine/src/copium/util/Uuid.h index 2740511..379eeec 100644 --- a/CopiumEngine/src/copium/util/Uuid.h +++ b/CopiumEngine/src/copium/util/Uuid.h @@ -22,9 +22,9 @@ namespace Copium std::string ToString() const; - bool operator==(const Uuid& rhs); - bool operator!=(const Uuid& rhs); - bool operator<(const Uuid& rhs); + bool operator==(const Uuid& rhs) const; + bool operator!=(const Uuid& rhs) const; + bool operator<(const Uuid& rhs) const; friend std::ostream& operator<<(std::ostream& os, const Uuid& uuid);