From ca6c2337d62f2e604177a3afeaab42e0f601fc44 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Fri, 16 Sep 2022 17:01:52 -0400 Subject: [PATCH] Create utility function for adding components to entity --- cmrc | 2 +- examples/dev_testing/src/main.cpp | 7 +------ .../ecs/component/model_componenet.h | 17 +++++++++++++++++ include/simpleengine/ecs/entity.h | 12 ++++++++++++ 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/cmrc b/cmrc index e386a62..a64bea5 160000 --- a/cmrc +++ b/cmrc @@ -1 +1 @@ -Subproject commit e386a629eb537d384811e598a3c96b9ca928f65e +Subproject commit a64bea50c05594c8e7cf1f08e441bb9507742e2e diff --git a/examples/dev_testing/src/main.cpp b/examples/dev_testing/src/main.cpp index c3ab15d..9c163e8 100644 --- a/examples/dev_testing/src/main.cpp +++ b/examples/dev_testing/src/main.cpp @@ -170,14 +170,9 @@ int main(int argc, char *argv[]) { se::gfx::Material material(white_texture, 1.f, 0.f, 0.f, 0.f, 0.f); - // Create a model component - se::gfx::Model model(cube_vertices, cube_indicies, std::optional(material)); - model.calculate_normals(); - se::ModelComponent model_component(model); - // Create the entity and add the model component to it. auto entity = std::make_shared(); - entity->add_component(model_component); + entity->add_component(cube_vertices, cube_indicies, material, true); entity->translate(3.5f, 0.f, 0.f); // Create a renderer and submit the entity into it. diff --git a/include/simpleengine/ecs/component/model_componenet.h b/include/simpleengine/ecs/component/model_componenet.h index 18f1815..f944f15 100644 --- a/include/simpleengine/ecs/component/model_componenet.h +++ b/include/simpleengine/ecs/component/model_componenet.h @@ -21,6 +21,23 @@ namespace simpleengine { } + ModelComponent(std::vector vertices, std::vector indicies, gfx::Material material, + bool calculate_normals = false): model(vertices, indicies, material) { + + if (calculate_normals) { + model.calculate_normals(); + } + } + + ModelComponent(std::vector vertices, std::vector indicies = std::vector(), + std::optional material = std::nullopt, bool calculate_normals = false) : + model(vertices, indicies, material) { + + if (calculate_normals) { + model.calculate_normals(); + } + } + virtual void update(const float& delta_time) override { std::cout << "Model Component update" << std::endl; } diff --git a/include/simpleengine/ecs/entity.h b/include/simpleengine/ecs/entity.h index bd061a1..54ba8a4 100644 --- a/include/simpleengine/ecs/entity.h +++ b/include/simpleengine/ecs/entity.h @@ -102,5 +102,17 @@ namespace simpleengine { assert(!has_component()); // TODO: Don't assert, give an error components.push_back(std::make_shared(component)); } + + template + std::shared_ptr add_component(Args&&... args) { + static_assert(std::is_base_of_v, "Component class must derive from simpleengine::Component"); + + // Only allow one type of the same component + assert(!has_component()); // TODO: Don't assert, give an error + auto comp = std::make_shared(std::forward(args)...); + components.push_back(comp); + + return comp; + } }; } \ No newline at end of file