Create a simple RotatingComponent

This commit is contained in:
SeanOMik 2022-09-23 00:54:18 -04:00
parent 3b51dce796
commit ab3f6c98ff
Signed by: SeanOMik
GPG Key ID: 568F326C7EB33ACB
3 changed files with 38 additions and 0 deletions

View File

@ -3,6 +3,7 @@
#include "simpleengine/ecs/component/mesh_component.h" #include "simpleengine/ecs/component/mesh_component.h"
#include <simpleengine/ecs/component/model_component.h> #include <simpleengine/ecs/component/model_component.h>
#include "simpleengine/ecs/component/transform_component.h" #include "simpleengine/ecs/component/transform_component.h"
#include <simpleengine/ecs/component/rotating_component.h>
#include "simpleengine/ecs/entity.h" #include "simpleengine/ecs/entity.h"
#include "simpleengine/gfx/light.h" #include "simpleengine/gfx/light.h"
#include "simpleengine/gfx/material.h" #include "simpleengine/gfx/material.h"
@ -184,6 +185,7 @@ int main(int argc, char *argv[]) {
// Create an Entity in the Scene and add components to it. // Create an Entity in the Scene and add components to it.
se::ecs::Entity entity = scene->create_entity(); se::ecs::Entity entity = scene->create_entity();
entity.add_component<se::ModelComponent>("examples/dev_testing/resources/dragon.obj"); entity.add_component<se::ModelComponent>("examples/dev_testing/resources/dragon.obj");
entity.add_component<se::RotatingComponent>();
auto& transform_comp = entity.add_component<se::TransformComponent>(); auto& transform_comp = entity.add_component<se::TransformComponent>();
transform_comp.translate(12.f, -4.f, 0.f); transform_comp.translate(12.f, -4.f, 0.f);

View File

@ -0,0 +1,31 @@
#pragma once
#include <glm/glm.hpp>
namespace simpleengine {
/**
* @brief A component that will rotate the transform every frame.
*
*/
class RotatingComponent {
public:
float rate;
glm::vec3 rotation_axis;
RotatingComponent() : rate(10.f), rotation_axis(glm::vec3(0, 1, 0)) {
}
RotatingComponent(float rate) : rate(rate), rotation_axis(glm::vec3(0, 1, 0)) {
}
RotatingComponent(glm::vec3 rotation_axis) : rotation_axis(rotation_axis) {
}
RotatingComponent(float rate, glm::vec3 rotation_axis) : rate(rate), rotation_axis(rotation_axis) {
}
};
}

View File

@ -2,6 +2,7 @@
#include "ecs/component/mesh_component.h" #include "ecs/component/mesh_component.h"
#include "ecs/component/model_component.h" #include "ecs/component/model_component.h"
#include "ecs/component/transform_component.h" #include "ecs/component/transform_component.h"
#include "ecs/component/rotating_component.h"
#include "ecs/entity.h" #include "ecs/entity.h"
#include "gfx/renderer.h" #include "gfx/renderer.h"
@ -25,5 +26,9 @@ namespace simpleengine {
registry.view<const TransformComponent, MeshComponent>().each([this](const TransformComponent& transform, MeshComponent& mesh_component) { registry.view<const TransformComponent, MeshComponent>().each([this](const TransformComponent& transform, MeshComponent& mesh_component) {
renderer->queue_job(gfx::RenderingJob(mesh_component.mesh, transform.transform_matrix)); renderer->queue_job(gfx::RenderingJob(mesh_component.mesh, transform.transform_matrix));
}); });
registry.view<TransformComponent, RotatingComponent>().each([this, &delta_time](TransformComponent& transform, RotatingComponent& rotating) {
transform.rotate(rotating.rate * delta_time, rotating.rotation_axis);
});
} }
} }