Add system to switch between path and manual movement

This system automatically checks for entities with both movement methods, and activates/deactivates them when pressing P.
This commit is contained in:
karl 2020-10-03 17:43:24 +02:00
parent cf19af1142
commit 3fb23200fb
8 changed files with 67 additions and 4 deletions

View File

@ -11,8 +11,9 @@ struct MouseLook {
float sensitivity; float sensitivity;
double yaw = 0.0; double yaw = 0.0;
double pitch = 0.0; double pitch = 0.0;
bool is_active;
}; };
#endif //ECSGAME_MOUSELOOK_H #endif //ECSGAME_MOUSELOOK_H

View File

@ -13,6 +13,8 @@ struct Movement {
glm::ivec3 moving = glm::ivec3(0, 0, 0); glm::ivec3 moving = glm::ivec3(0, 0, 0);
glm::vec3 velocity; glm::vec3 velocity;
bool is_active;
}; };
#endif //ECSGAME_MOVEMENT_H #endif //ECSGAME_MOVEMENT_H

View File

@ -25,6 +25,7 @@ struct PathMove {
} }
} }
bool is_active;
double speed; double speed;
float time_passed = 0.0; float time_passed = 0.0;
int current_point_index = 0; int current_point_index = 0;

View File

@ -63,6 +63,8 @@ class KeyboardMovementSystem : public EntitySystem, public EventSubscriber<Input
void tick(World *pWorld, float deltaTime) override { void tick(World *pWorld, float deltaTime) override {
pWorld->each<Transform, Movement>( pWorld->each<Transform, Movement>(
[&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<Movement> movement) { [&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<Movement> movement) {
if (!movement->is_active) return;
transform->add_to_origin(transform->matrix * glm::vec4((glm::vec3(movement->moving) * movement->speed * deltaTime), 0.0)); transform->add_to_origin(transform->matrix * glm::vec4((glm::vec3(movement->moving) * movement->speed * deltaTime), 0.0));
}); });
} }

View File

@ -41,6 +41,8 @@ public:
void tick(World *pWorld, float deltaTime) override { void tick(World *pWorld, float deltaTime) override {
pWorld->each<Transform, MouseLook, Camera>([&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<MouseLook> mouse, ComponentHandle<Camera> camera) { pWorld->each<Transform, MouseLook, Camera>([&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<MouseLook> mouse, ComponentHandle<Camera> camera) {
if (!mouse->is_active) return;
if(mouse->pitch > 80.0f) if(mouse->pitch > 80.0f)
mouse->pitch = 80.0f; mouse->pitch = 80.0f;
if(mouse->pitch < -80.0f) if(mouse->pitch < -80.0f)

View File

@ -77,6 +77,8 @@ class PathMoveSystem : public EntitySystem, public EventSubscriber<InputEvent> {
void tick(World *pWorld, float deltaTime) override { void tick(World *pWorld, float deltaTime) override {
pWorld->each<Transform, PathMove>( pWorld->each<Transform, PathMove>(
[&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<PathMove> pathmove) { [&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<PathMove> pathmove) {
if (!pathmove->is_active) return;
// Handle change in speed // Handle change in speed
pathmove->speed += pathmove->speed_addition * deltaTime; pathmove->speed += pathmove->speed_addition * deltaTime;
pathmove->speed = glm::clamp(pathmove->speed, 0.0, 10.0); pathmove->speed = glm::clamp(pathmove->speed, 0.0, 10.0);
@ -131,7 +133,7 @@ class PathMoveSystem : public EntitySystem, public EventSubscriber<InputEvent> {
} }
// Calculate the point on the spline // Calculate the point on the spline
glm::vec3 point = catmul(0.5f, glm::vec3 point = catmul(1.f,
p0, p0,
p1, p1,
p2, p2,

View File

@ -0,0 +1,51 @@
#ifndef __PATHMOVEMENTSWITCHSYSTEM_H__
#define __PATHMOVEMENTSWITCHSYSTEM_H__
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#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<InputEvent> {
void configure(World *pWorld) override {
myWorld = pWorld;
myWorld->subscribe<InputEvent>(this);
}
void receive(World *pWorld, const InputEvent &event) override {
if (event.key == GLFW_KEY_P) {
myWorld->each<PathMove, Movement, MouseLook>([&](Entity *ent, ComponentHandle<PathMove> pathmove, ComponentHandle<Movement> movement, ComponentHandle<MouseLook> 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__

View File

@ -19,6 +19,7 @@
#include "ECS/Systems/SineAnimationSystem.h" #include "ECS/Systems/SineAnimationSystem.h"
#include "ECS/Components/DirectionalLight.h" #include "ECS/Components/DirectionalLight.h"
#include "ECS/Components/PathMove.h" #include "ECS/Components/PathMove.h"
#include "ECS/Systems/PathMovementSwitchSystem.h"
using namespace ECS; using namespace ECS;
@ -82,14 +83,15 @@ int main() {
world->registerSystem(new MouseLookSystem(1280, 720)); world->registerSystem(new MouseLookSystem(1280, 720));
world->registerSystem(new PathMoveSystem()); world->registerSystem(new PathMoveSystem());
world->registerSystem(new SineAnimationSystem()); world->registerSystem(new SineAnimationSystem());
world->registerSystem(new PathMovementSwitchSystem());
RenderSystem* renderSystem = new RenderSystem(); RenderSystem* renderSystem = new RenderSystem();
world->registerSystem(renderSystem); world->registerSystem(renderSystem);
Entity *player = world->create(); Entity *player = world->create();
player->assign<Transform>(); player->assign<Transform>();
//player->assign<Movement>(glm::vec3(2.f, 2.f, 2.f)); player->assign<Movement>(glm::vec3(2.f, 2.f, 2.f));
//player->assign<MouseLook>(0.1); player->assign<MouseLook>(0.1);
player->assign<Camera>(70.0f, 1280, 720, 0.1f, 100.0f); player->assign<Camera>(70.0f, 1280, 720, 0.1f, 100.0f);
player->assign<PathMove>(3.0, PathMove::Path(std::vector<glm::vec3>{ player->assign<PathMove>(3.0, PathMove::Path(std::vector<glm::vec3>{
glm::vec3(0.0, 2.0, 0.0), glm::vec3(0.0, 2.0, 0.0),