Move all ECS stuff into their own files / dirs
This commit is contained in:
parent
0d9f16939c
commit
3529254a29
@ -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})
|
||||
|
||||
|
24
ECS/Components/Movement.h
Normal file
24
ECS/Components/Movement.h
Normal file
@ -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
|
18
ECS/Components/Position.h
Normal file
18
ECS/Components/Position.h
Normal file
@ -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
|
13
ECS/Events/InputEvent.h
Normal file
13
ECS/Events/InputEvent.h
Normal file
@ -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
|
29
ECS/Systems/GravitySystem.h
Normal file
29
ECS/Systems/GravitySystem.h
Normal file
@ -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<Position>([&](Entity *ent, ComponentHandle<Position> position) {
|
||||
position->y += gravityAmount * deltaTime;
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
float gravityAmount;
|
||||
};
|
||||
|
||||
#endif //ECSGAME_GRAVITYSYSTEM_H
|
81
ECS/Systems/KeyboardMovementSystem.h
Normal file
81
ECS/Systems/KeyboardMovementSystem.h
Normal file
@ -0,0 +1,81 @@
|
||||
//
|
||||
// Created by karl on 04.01.20.
|
||||
//
|
||||
|
||||
#ifndef ECSGAME_KEYBOARDMOVEMENTSYSTEM_H
|
||||
#define ECSGAME_KEYBOARDMOVEMENTSYSTEM_H
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "../ECS.h"
|
||||
#include "../Components/Position.h"
|
||||
#include "../Events/InputEvent.h"
|
||||
#include "../Components/Movement.h"
|
||||
|
||||
using namespace ECS;
|
||||
|
||||
class KeyboardMovementSystem : 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_W) {
|
||||
myWorld->each<Movement>([&](Entity *ent, ComponentHandle<Movement> 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<Movement>([&](Entity *ent, ComponentHandle<Movement> 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<Movement>([&](Entity *ent, ComponentHandle<Movement> 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<Movement>([&](Entity *ent, ComponentHandle<Movement> 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<Position, Movement>(
|
||||
[&](Entity *ent, ComponentHandle<Position> position, ComponentHandle<Movement> 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
|
28
ECS/Systems/PositionDebugSystem.h
Normal file
28
ECS/Systems/PositionDebugSystem.h
Normal file
@ -0,0 +1,28 @@
|
||||
//
|
||||
// Created by karl on 04.01.20.
|
||||
//
|
||||
|
||||
#ifndef ECSGAME_POSITIONDEBUGSYSTEM_H
|
||||
#define ECSGAME_POSITIONDEBUGSYSTEM_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "../ECS.h"
|
||||
#include "../Components/Position.h"
|
||||
|
||||
using namespace ECS;
|
||||
|
||||
class PositionDebugOutputSystem : public EntitySystem {
|
||||
public:
|
||||
void tick(World *pWorld, float deltaTime) override {
|
||||
pWorld->each<Position>([&](Entity *ent, ComponentHandle<Position> position) {
|
||||
std::cout << ent->getEntityId() << ": "
|
||||
<< position->x << ", "
|
||||
<< position->y << ", "
|
||||
<< position->z
|
||||
<< std::endl;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
#endif //ECSGAME_POSITIONDEBUGSYSTEM_H
|
121
main.cpp
121
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<Position>([&](Entity *ent, ComponentHandle<Position> position) {
|
||||
position->y += gravityAmount * deltaTime;
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
float gravityAmount;
|
||||
};
|
||||
|
||||
class PositionDebugOutputSystem : public EntitySystem {
|
||||
public:
|
||||
void tick(World *pWorld, float deltaTime) override {
|
||||
pWorld->each<Position>([&](Entity *ent, ComponentHandle<Position> position) {
|
||||
std::cout << ent->getEntityId() << ": "
|
||||
<< position->x << ", "
|
||||
<< position->y << ", "
|
||||
<< position->z
|
||||
<< std::endl;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
class KeyboardMovementSystem : public EntitySystem, public EventSubscriber<InputEvent> {
|
||||
void configure(World *pWorld) override {
|
||||
pWorld->subscribe<InputEvent>(this);
|
||||
}
|
||||
|
||||
void receive(World *pWorld, const InputEvent &event) override {
|
||||
if (event.key == GLFW_KEY_W) {
|
||||
world->each<Movement>([&](Entity *ent, ComponentHandle<Movement> 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<Movement>([&](Entity *ent, ComponentHandle<Movement> 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<Movement>([&](Entity *ent, ComponentHandle<Movement> 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<Movement>([&](Entity *ent, ComponentHandle<Movement> 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<Position, Movement>(
|
||||
[&](Entity *ent, ComponentHandle<Position> position, ComponentHandle<Movement> 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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user