diff --git a/ECS/Components/MouseLook.h b/ECS/Components/MouseLook.h index ff9f27e..5749413 100644 --- a/ECS/Components/MouseLook.h +++ b/ECS/Components/MouseLook.h @@ -11,8 +11,9 @@ struct MouseLook { float sensitivity; double yaw = 0.0; - double pitch = 0.0; + + bool is_active; }; #endif //ECSGAME_MOUSELOOK_H diff --git a/ECS/Components/Movement.h b/ECS/Components/Movement.h index f8b2ae1..625d430 100644 --- a/ECS/Components/Movement.h +++ b/ECS/Components/Movement.h @@ -13,6 +13,8 @@ struct Movement { glm::ivec3 moving = glm::ivec3(0, 0, 0); glm::vec3 velocity; + + bool is_active; }; #endif //ECSGAME_MOVEMENT_H diff --git a/ECS/Components/PathMove.h b/ECS/Components/PathMove.h index 22fab58..d8dfa42 100644 --- a/ECS/Components/PathMove.h +++ b/ECS/Components/PathMove.h @@ -25,6 +25,7 @@ struct PathMove { } } + bool is_active; double speed; float time_passed = 0.0; int current_point_index = 0; diff --git a/ECS/Systems/KeyboardMovementSystem.h b/ECS/Systems/KeyboardMovementSystem.h index ccbd7ba..5297315 100644 --- a/ECS/Systems/KeyboardMovementSystem.h +++ b/ECS/Systems/KeyboardMovementSystem.h @@ -63,6 +63,8 @@ class KeyboardMovementSystem : public EntitySystem, public EventSubscribereach( [&](Entity *ent, ComponentHandle transform, ComponentHandle movement) { + if (!movement->is_active) return; + transform->add_to_origin(transform->matrix * glm::vec4((glm::vec3(movement->moving) * movement->speed * deltaTime), 0.0)); }); } diff --git a/ECS/Systems/MouseLookSystem.h b/ECS/Systems/MouseLookSystem.h index 330fe0d..ca4f3fc 100644 --- a/ECS/Systems/MouseLookSystem.h +++ b/ECS/Systems/MouseLookSystem.h @@ -41,6 +41,8 @@ public: void tick(World *pWorld, float deltaTime) override { pWorld->each([&](Entity *ent, ComponentHandle transform, ComponentHandle mouse, ComponentHandle camera) { + if (!mouse->is_active) return; + if(mouse->pitch > 80.0f) mouse->pitch = 80.0f; if(mouse->pitch < -80.0f) diff --git a/ECS/Systems/PathMoveSystem.h b/ECS/Systems/PathMoveSystem.h index 5a42a8f..273bc10 100644 --- a/ECS/Systems/PathMoveSystem.h +++ b/ECS/Systems/PathMoveSystem.h @@ -77,6 +77,8 @@ class PathMoveSystem : public EntitySystem, public EventSubscriber { void tick(World *pWorld, float deltaTime) override { pWorld->each( [&](Entity *ent, ComponentHandle transform, ComponentHandle pathmove) { + if (!pathmove->is_active) return; + // Handle change in speed pathmove->speed += pathmove->speed_addition * deltaTime; pathmove->speed = glm::clamp(pathmove->speed, 0.0, 10.0); @@ -131,7 +133,7 @@ class PathMoveSystem : public EntitySystem, public EventSubscriber { } // Calculate the point on the spline - glm::vec3 point = catmul(0.5f, + glm::vec3 point = catmul(1.f, p0, p1, p2, diff --git a/ECS/Systems/PathMovementSwitchSystem.h b/ECS/Systems/PathMovementSwitchSystem.h new file mode 100644 index 0000000..e5da081 --- /dev/null +++ b/ECS/Systems/PathMovementSwitchSystem.h @@ -0,0 +1,51 @@ +#ifndef __PATHMOVEMENTSWITCHSYSTEM_H__ +#define __PATHMOVEMENTSWITCHSYSTEM_H__ + +#include +#include + +#include + +#include "../ECS.h" +#include "../Components/PathMove.h" +#include "../Events/InputEvent.h" +#include "../Components/Movement.h" +#include "../Components/MouseLook.h" + +using namespace ECS; + +class PathMovementSwitchSystem : public EntitySystem, public EventSubscriber { + void configure(World *pWorld) override { + myWorld = pWorld; + + myWorld->subscribe(this); + } + + void receive(World *pWorld, const InputEvent &event) override { + if (event.key == GLFW_KEY_P) { + myWorld->each([&](Entity *ent, ComponentHandle pathmove, ComponentHandle movement, ComponentHandle mouselook) { + if (event.action == GLFW_PRESS) { + // Switch between them + if (pathmove->is_active) { + pathmove->is_active = false; + movement->is_active = true; + mouselook->is_active = true; + } else { + pathmove->is_active = true; + movement->is_active = false; + mouselook->is_active = false; + } + } + }); + } + } + + void unconfigure(World *pWorld) override { + pWorld->unsubscribeAll(this); + } + +private: + World *myWorld; +}; + +#endif // __PATHMOVEMENTSWITCHSYSTEM_H__ \ No newline at end of file diff --git a/main.cpp b/main.cpp index 96f5634..4086ea4 100644 --- a/main.cpp +++ b/main.cpp @@ -19,6 +19,7 @@ #include "ECS/Systems/SineAnimationSystem.h" #include "ECS/Components/DirectionalLight.h" #include "ECS/Components/PathMove.h" +#include "ECS/Systems/PathMovementSwitchSystem.h" using namespace ECS; @@ -82,14 +83,15 @@ int main() { world->registerSystem(new MouseLookSystem(1280, 720)); world->registerSystem(new PathMoveSystem()); world->registerSystem(new SineAnimationSystem()); + world->registerSystem(new PathMovementSwitchSystem()); RenderSystem* renderSystem = new RenderSystem(); world->registerSystem(renderSystem); Entity *player = world->create(); player->assign(); - //player->assign(glm::vec3(2.f, 2.f, 2.f)); - //player->assign(0.1); + player->assign(glm::vec3(2.f, 2.f, 2.f)); + player->assign(0.1); player->assign(70.0f, 1280, 720, 0.1f, 100.0f); player->assign(3.0, PathMove::Path(std::vector{ glm::vec3(0.0, 2.0, 0.0),