From 30b36bd9f84ee4fcc76e59c744a76c954f0d48c4 Mon Sep 17 00:00:00 2001 From: karl Date: Wed, 15 Jan 2020 17:16:55 +0100 Subject: [PATCH] Add basic sine animation component + system for it --- CMakeLists.txt | 2 +- ECS/Components/SineAnimation.h | 15 +++++++++++++++ ECS/Systems/MouseLookSystem.h | 8 ++++---- ECS/Systems/SineAnimationSystem.h | 28 ++++++++++++++++++++++++++++ main.cpp | 18 ++++++++++++++++++ 5 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 ECS/Components/SineAnimation.h create mode 100644 ECS/Systems/SineAnimationSystem.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 538bda4..3927e74 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ find_package(OpenGL REQUIRED) find_package(glfw3 REQUIRED) find_package(glm REQUIRED) -add_executable(ecsgame Util/glad.c Util/OBJ_Loader.h Rendering/Shader.cpp Rendering/Shader.h main.cpp ECS/Components/Transform.h ECS/Components/Movement.h ECS/Events/InputEvent.h ECS/Events/MouseMoveEvent.h ECS/Systems/GravitySystem.h ECS/Systems/PositionDebugSystem.h ECS/Systems/KeyboardMovementSystem.h ECS/Components/Camera.h ECS/Systems/RenderSystem.h ECS/Components/Mesh.h ECS/Systems/MouseLookSystem.h ECS/Components/MouseLook.h ECS/Components/ObjMesh.h Util/stb_setup.cpp ECS/Components/Texture.h ECS/Components/LODObjMesh.h ECS/Components/SineAnimation.h ECS/Systems/SineAnimationSystem.h) +add_executable(ecsgame Util/glad.c Util/OBJ_Loader.h Rendering/Shader.cpp Rendering/Shader.h main.cpp ECS/Components/Transform.h ECS/Components/Movement.h ECS/Events/InputEvent.h ECS/Events/MouseMoveEvent.h ECS/Systems/GravitySystem.h ECS/Systems/PositionDebugSystem.h ECS/Systems/KeyboardMovementSystem.h ECS/Components/Camera.h ECS/Systems/RenderSystem.h ECS/Components/Mesh.h ECS/Systems/MouseLookSystem.h ECS/Components/MouseLook.h ECS/Components/ObjMesh.h Util/stb_setup.cpp ECS/Components/Texture.h ECS/Components/LODObjMesh.h ECS/Components/SineAnimation.h ECS/Systems/SineAnimationSystem.h ECS/Components/DirectionalLight.h) include_directories(${OPENGL_INCLUDE_DIRS}) diff --git a/ECS/Components/SineAnimation.h b/ECS/Components/SineAnimation.h new file mode 100644 index 0000000..38badc0 --- /dev/null +++ b/ECS/Components/SineAnimation.h @@ -0,0 +1,15 @@ +// +// Created by karl on 15.01.20. +// + +#ifndef ECSGAME_SINEANIMATION_H +#define ECSGAME_SINEANIMATION_H + +struct SineAnimation { + SineAnimation(const glm::vec3 &maxDistance, float speedScale) : maxDistance(maxDistance), speedScale(speedScale) {} + + glm::vec3 maxDistance; + float speedScale; +}; + +#endif //ECSGAME_SINEANIMATION_H diff --git a/ECS/Systems/MouseLookSystem.h b/ECS/Systems/MouseLookSystem.h index f89ea16..5f9df2e 100644 --- a/ECS/Systems/MouseLookSystem.h +++ b/ECS/Systems/MouseLookSystem.h @@ -41,10 +41,10 @@ public: void tick(World *pWorld, float deltaTime) override { pWorld->each([&](Entity *ent, ComponentHandle transform, ComponentHandle mouse, ComponentHandle camera) { - if(mouse->pitch > 89.0f) - mouse->pitch = 89.0f; - if(mouse->pitch < -89.0f) - mouse->pitch = -89.0f; + if(mouse->pitch > 50.0f) + mouse->pitch = 50.0f; + if(mouse->pitch < -50.0f) + mouse->pitch = -50.0f; glm::mat4x4 newTransform = glm::mat4x4(1.0); diff --git a/ECS/Systems/SineAnimationSystem.h b/ECS/Systems/SineAnimationSystem.h new file mode 100644 index 0000000..6783df8 --- /dev/null +++ b/ECS/Systems/SineAnimationSystem.h @@ -0,0 +1,28 @@ +// +// Created by karl on 15.01.20. +// + +#ifndef ECSGAME_SINEANIMATIONSYSTEM_H +#define ECSGAME_SINEANIMATIONSYSTEM_H + +#include "../ECS.h" +#include "../Components/Transform.h" +#include "../Components/SineAnimation.h" + +using namespace ECS; + +class SineAnimationSystem : public EntitySystem { +public: + void tick(World *pWorld, float deltaTime) override { + passedTime += deltaTime; + + pWorld->each([&](Entity *ent, ComponentHandle transform, ComponentHandle anim) { + transform->translate(anim->maxDistance * glm::sin(passedTime * anim->speedScale) * deltaTime); + }); + } + +private: + float passedTime = 0.0; +}; + +#endif //ECSGAME_SINEANIMATIONSYSTEM_H diff --git a/main.cpp b/main.cpp index f932818..b067bd3 100644 --- a/main.cpp +++ b/main.cpp @@ -14,6 +14,8 @@ #include "ECS/Systems/MouseLookSystem.h" #include "ECS/Components/ObjMesh.h" #include "ECS/Components/Texture.h" +#include "ECS/Components/SineAnimation.h" +#include "ECS/Systems/SineAnimationSystem.h" using namespace ECS; @@ -75,6 +77,7 @@ int main() { // world->registerSystem(new PositionDebugOutputSystem()); world->registerSystem(new KeyboardMovementSystem()); world->registerSystem(new MouseLookSystem(640, 480)); + world->registerSystem(new SineAnimationSystem()); RenderSystem* renderSystem = new RenderSystem(); world->registerSystem(renderSystem); @@ -90,6 +93,7 @@ int main() { monkey->assign(); monkey->assign(std::vector{ObjMesh("Resources/Monkey.obj", ObjMesh::Settings(0.0, 8.0)), ObjMesh("Resources/MonkeySimple.obj", ObjMesh::Settings(8.0, 100.0))}); monkey->assign("Resources/Marble.jpg", Texture::Settings(true, false)); + monkey->assign(glm::vec3(0.0, 0.3, 0.0), 0.5); monkey->get()->translate(glm::vec3(0.0f, 2.0f, -6.0f)); Entity *wall1 = world->create(); @@ -104,6 +108,20 @@ int main() { wall2->assign("Resources/Glass.png", Texture::Settings(true, true)); wall2->get()->translate(glm::vec3(0.0f, 0.0f, -10.0f)); + Entity *wall3 = world->create(); + wall3->assign(); + wall3->assign(ObjMesh("Resources/Wall.obj", ObjMesh::Settings())); + wall3->assign("Resources/Glass.png", Texture::Settings(true, true)); + wall3->get()->translate(glm::vec3(4.0f, 0.0f, -6.0f)); + wall3->get()->rotate(90.0, glm::vec3(0.0, 1.0, 0.0)); + + Entity *wall4 = world->create(); + wall4->assign(); + wall4->assign(ObjMesh("Resources/Wall.obj", ObjMesh::Settings())); + wall4->assign("Resources/Glass.png", Texture::Settings(true, true)); + wall4->get()->translate(glm::vec3(-4.0f, 0.0f, -6.0f)); + wall4->get()->rotate(90.0, glm::vec3(0.0, 1.0, 0.0)); + Entity *ground = world->create(); ground->assign(); ground->assign(ObjMesh("Resources/Ground.obj", ObjMesh::Settings()));