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:
parent
cf19af1142
commit
3fb23200fb
@ -11,8 +11,9 @@ struct MouseLook {
|
||||
float sensitivity;
|
||||
|
||||
double yaw = 0.0;
|
||||
|
||||
double pitch = 0.0;
|
||||
|
||||
bool is_active;
|
||||
};
|
||||
|
||||
#endif //ECSGAME_MOUSELOOK_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
|
||||
|
@ -25,6 +25,7 @@ struct PathMove {
|
||||
}
|
||||
}
|
||||
|
||||
bool is_active;
|
||||
double speed;
|
||||
float time_passed = 0.0;
|
||||
int current_point_index = 0;
|
||||
|
@ -63,6 +63,8 @@ class KeyboardMovementSystem : public EntitySystem, public EventSubscriber<Input
|
||||
void tick(World *pWorld, float deltaTime) override {
|
||||
pWorld->each<Transform, 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));
|
||||
});
|
||||
}
|
||||
|
@ -41,6 +41,8 @@ public:
|
||||
|
||||
void tick(World *pWorld, float deltaTime) override {
|
||||
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)
|
||||
mouse->pitch = 80.0f;
|
||||
if(mouse->pitch < -80.0f)
|
||||
|
@ -77,6 +77,8 @@ class PathMoveSystem : public EntitySystem, public EventSubscriber<InputEvent> {
|
||||
void tick(World *pWorld, float deltaTime) override {
|
||||
pWorld->each<Transform, PathMove>(
|
||||
[&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<PathMove> 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<InputEvent> {
|
||||
}
|
||||
|
||||
// Calculate the point on the spline
|
||||
glm::vec3 point = catmul(0.5f,
|
||||
glm::vec3 point = catmul(1.f,
|
||||
p0,
|
||||
p1,
|
||||
p2,
|
||||
|
51
ECS/Systems/PathMovementSwitchSystem.h
Normal file
51
ECS/Systems/PathMovementSwitchSystem.h
Normal 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__
|
6
main.cpp
6
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<Transform>();
|
||||
//player->assign<Movement>(glm::vec3(2.f, 2.f, 2.f));
|
||||
//player->assign<MouseLook>(0.1);
|
||||
player->assign<Movement>(glm::vec3(2.f, 2.f, 2.f));
|
||||
player->assign<MouseLook>(0.1);
|
||||
player->assign<Camera>(70.0f, 1280, 720, 0.1f, 100.0f);
|
||||
player->assign<PathMove>(3.0, PathMove::Path(std::vector<glm::vec3>{
|
||||
glm::vec3(0.0, 2.0, 0.0),
|
||||
|
Loading…
x
Reference in New Issue
Block a user