diff --git a/CMakeLists.txt b/CMakeLists.txt index f114b12..f251511 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ set(CMAKE_CXX_STANDARD 17) find_package(OpenGL REQUIRED) find_package(glfw3 REQUIRED) -add_executable(ecsgame Util/glad.c Util/OBJ_Loader.h Rendering/Shader.cpp Rendering/Shader.h main.cpp) +add_executable(ecsgame Util/glad.c Util/OBJ_Loader.h Rendering/Shader.cpp Rendering/Shader.h main.cpp ECS/Components/Position.h ECS/Components/Movement.h ECS/Events/InputEvent.h ECS/Systems/GravitySystem.h ECS/Systems/PositionDebugSystem.h ECS/Systems/KeyboardMovementSystem.h) include_directories(${OPENGL_INCLUDE_DIRS}) diff --git a/ECS/Components/Movement.h b/ECS/Components/Movement.h new file mode 100644 index 0000000..ed0f431 --- /dev/null +++ b/ECS/Components/Movement.h @@ -0,0 +1,24 @@ +// +// Created by karl on 04.01.20. +// + +#ifndef ECSGAME_MOVEMENT_H +#define ECSGAME_MOVEMENT_H + +struct Movement { + Movement(float speedX, float speedY, float speedZ) : speedX(speedX), speedY(speedY), speedZ(speedZ) {} + + float speedX; + float speedY; + float speedZ; + + int movingX = 0; + int movingY = 0; + int movingZ = 0; + + float velocityX = 0.0f; + float velocityY = 0.0f; + float velocityZ = 0.0f; +}; + +#endif //ECSGAME_MOVEMENT_H diff --git a/ECS/Components/Position.h b/ECS/Components/Position.h new file mode 100644 index 0000000..d5f592d --- /dev/null +++ b/ECS/Components/Position.h @@ -0,0 +1,18 @@ +// +// Created by karl on 04.01.20. +// + +#ifndef ECSGAME_POSITION_H +#define ECSGAME_POSITION_H + +struct Position { + Position(float x, float y, float z) : x(x), y(y), z(z) {} + + Position() : x(0.f), y(0.f), z(0.f) {} + + float x; + float y; + float z; +}; + +#endif //ECSGAME_POSITION_H diff --git a/ECS/Events/InputEvent.h b/ECS/Events/InputEvent.h new file mode 100644 index 0000000..38b5c57 --- /dev/null +++ b/ECS/Events/InputEvent.h @@ -0,0 +1,13 @@ +// +// Created by karl on 04.01.20. +// + +#ifndef ECSGAME_INPUTEVENT_H +#define ECSGAME_INPUTEVENT_H + +struct InputEvent { + int key; + int action; +}; + +#endif //ECSGAME_INPUTEVENT_H diff --git a/ECS/Systems/GravitySystem.h b/ECS/Systems/GravitySystem.h new file mode 100644 index 0000000..d0e1162 --- /dev/null +++ b/ECS/Systems/GravitySystem.h @@ -0,0 +1,29 @@ +// +// Created by karl on 04.01.20. +// + +#ifndef ECSGAME_GRAVITYSYSTEM_H +#define ECSGAME_GRAVITYSYSTEM_H + +#include "../ECS.h" +#include "../Components/Position.h" + +using namespace ECS; + +class GravitySystem : public EntitySystem { +public: + explicit GravitySystem(float amount) { + gravityAmount = amount; + } + + void tick(World *pWorld, float deltaTime) override { + pWorld->each([&](Entity *ent, ComponentHandle position) { + position->y += gravityAmount * deltaTime; + }); + } + +private: + float gravityAmount; +}; + +#endif //ECSGAME_GRAVITYSYSTEM_H diff --git a/ECS/Systems/KeyboardMovementSystem.h b/ECS/Systems/KeyboardMovementSystem.h new file mode 100644 index 0000000..326dc55 --- /dev/null +++ b/ECS/Systems/KeyboardMovementSystem.h @@ -0,0 +1,81 @@ +// +// Created by karl on 04.01.20. +// + +#ifndef ECSGAME_KEYBOARDMOVEMENTSYSTEM_H +#define ECSGAME_KEYBOARDMOVEMENTSYSTEM_H + +#include +#include + +#include + +#include "../ECS.h" +#include "../Components/Position.h" +#include "../Events/InputEvent.h" +#include "../Components/Movement.h" + +using namespace ECS; + +class KeyboardMovementSystem : 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_W) { + myWorld->each([&](Entity *ent, ComponentHandle movement) { + if (event.action == GLFW_PRESS) { + movement->movingZ = -1; + } else if (event.action == GLFW_RELEASE) { + movement->movingZ = 0; + } + }); + } else if (event.key == GLFW_KEY_S) { + myWorld->each([&](Entity *ent, ComponentHandle movement) { + if (event.action == GLFW_PRESS) { + movement->movingZ = 1; + } else if (event.action == GLFW_RELEASE) { + movement->movingZ = 0; + } + }); + } else if (event.key == GLFW_KEY_A) { + myWorld->each([&](Entity *ent, ComponentHandle movement) { + if (event.action == GLFW_PRESS) { + movement->movingX = 1; + } else if (event.action == GLFW_RELEASE) { + movement->movingX = 0; + } + }); + } else if (event.key == GLFW_KEY_D) { + myWorld->each([&](Entity *ent, ComponentHandle movement) { + if (event.action == GLFW_PRESS) { + movement->movingX = -1; + } else if (event.action == GLFW_RELEASE) { + movement->movingX = 0; + } + }); + } + std::cout << "MyEvent was emitted!" << std::endl; + } + + void tick(World *pWorld, float deltaTime) override { + pWorld->each( + [&](Entity *ent, ComponentHandle position, ComponentHandle movement) { + position->x += movement->movingX * movement->speedX * deltaTime; + position->y += movement->movingY * movement->speedY * deltaTime; + position->z += movement->movingZ * movement->speedZ * deltaTime; + }); + } + + void unconfigure(World *pWorld) override { + pWorld->unsubscribeAll(this); + } + +private: + World *myWorld; +}; + +#endif //ECSGAME_KEYBOARDMOVEMENTSYSTEM_H diff --git a/ECS/Systems/PositionDebugSystem.h b/ECS/Systems/PositionDebugSystem.h new file mode 100644 index 0000000..5fd0a66 --- /dev/null +++ b/ECS/Systems/PositionDebugSystem.h @@ -0,0 +1,28 @@ +// +// Created by karl on 04.01.20. +// + +#ifndef ECSGAME_POSITIONDEBUGSYSTEM_H +#define ECSGAME_POSITIONDEBUGSYSTEM_H + +#include + +#include "../ECS.h" +#include "../Components/Position.h" + +using namespace ECS; + +class PositionDebugOutputSystem : public EntitySystem { +public: + void tick(World *pWorld, float deltaTime) override { + pWorld->each([&](Entity *ent, ComponentHandle position) { + std::cout << ent->getEntityId() << ": " + << position->x << ", " + << position->y << ", " + << position->z + << std::endl; + }); + } +}; + +#endif //ECSGAME_POSITIONDEBUGSYSTEM_H diff --git a/main.cpp b/main.cpp index a4389f0..686c332 100644 --- a/main.cpp +++ b/main.cpp @@ -5,6 +5,10 @@ #include "Rendering/Shader.h" #include "ECS/ECS.h" +#include "ECS/Events/InputEvent.h" +#include "ECS/Systems/GravitySystem.h" +#include "ECS/Systems/PositionDebugSystem.h" +#include "ECS/Systems/KeyboardMovementSystem.h" using namespace ECS; @@ -12,123 +16,6 @@ using namespace ECS; World *world = World::createWorld(); -struct Position { - Position(float x, float y, float z) : x(x), y(y), z(z) {} - - Position() : x(0.f), y(0.f), z(0.f) {} - - float x; - float y; - float z; -}; - -struct Movement { - Movement(float speedX, float speedY, float speedZ) : speedX(speedX), speedY(speedY), speedZ(speedZ) {} - - float speedX; - float speedY; - float speedZ; - - int movingX = 0; - int movingY = 0; - int movingZ = 0; - - float velocityX = 0.0f; - float velocityY = 0.0f; - float velocityZ = 0.0f; -}; - -struct InputEvent { - int key; - int action; -}; - -class GravitySystem : public EntitySystem { -public: - explicit GravitySystem(float amount) { - gravityAmount = amount; - } - - void tick(World *pWorld, float deltaTime) override { - pWorld->each([&](Entity *ent, ComponentHandle position) { - position->y += gravityAmount * deltaTime; - }); - } - -private: - float gravityAmount; -}; - -class PositionDebugOutputSystem : public EntitySystem { -public: - void tick(World *pWorld, float deltaTime) override { - pWorld->each([&](Entity *ent, ComponentHandle position) { - std::cout << ent->getEntityId() << ": " - << position->x << ", " - << position->y << ", " - << position->z - << std::endl; - }); - } -}; - -class KeyboardMovementSystem : public EntitySystem, public EventSubscriber { - void configure(World *pWorld) override { - pWorld->subscribe(this); - } - - void receive(World *pWorld, const InputEvent &event) override { - if (event.key == GLFW_KEY_W) { - world->each([&](Entity *ent, ComponentHandle movement) { - if (event.action == GLFW_PRESS) { - movement->movingZ = -1; - } else if (event.action == GLFW_RELEASE) { - movement->movingZ = 0; - } - }); - } else if (event.key == GLFW_KEY_S) { - world->each([&](Entity *ent, ComponentHandle movement) { - if (event.action == GLFW_PRESS) { - movement->movingZ = 1; - } else if (event.action == GLFW_RELEASE) { - movement->movingZ = 0; - } - }); - } else if (event.key == GLFW_KEY_A) { - world->each([&](Entity *ent, ComponentHandle movement) { - if (event.action == GLFW_PRESS) { - movement->movingX = 1; - } else if (event.action == GLFW_RELEASE) { - movement->movingX = 0; - } - }); - } else if (event.key == GLFW_KEY_D) { - world->each([&](Entity *ent, ComponentHandle movement) { - if (event.action == GLFW_PRESS) { - movement->movingX = -1; - } else if (event.action == GLFW_RELEASE) { - movement->movingX = 0; - } - }); - } - std::cout << "MyEvent was emitted!" << std::endl; - } - - void tick(World *pWorld, float deltaTime) override { - pWorld->each( - [&](Entity *ent, ComponentHandle position, ComponentHandle movement) { - position->x += movement->movingX * movement->speedX * deltaTime; - position->y += movement->movingY * movement->speedY * deltaTime; - position->z += movement->movingZ * movement->speedZ * deltaTime; - }); - } - - void unconfigure(World *pWorld) override { - pWorld->unsubscribeAll(this); - } -}; - - static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) { if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GLFW_TRUE);