Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/rb 23 refactor ecs #6

Merged
merged 6 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.15)

set(PROJECT_NAME poc)
set(PROJECT_NAME client)

set(CMAKE_CXX_STANDARD 20)

Expand All @@ -16,7 +16,21 @@ add_executable(

add_subdirectory(deps)

add_subdirectory(src/poc)
if(MSVC)
target_compile_options(
${PROJECT_NAME}
PRIVATE
/W4
)
else()
target_compile_options(
${PROJECT_NAME}
PRIVATE
-Wall -Wextra -pedantic
)
endif()

add_subdirectory(src)

if (WIN32)
add_custom_command(
Expand Down
6 changes: 3 additions & 3 deletions src/poc/src/CMakeLists.txt → src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
cmake_minimum_required(VERSION 3.15)

add_subdirectory(system)
add_subdirectory(EventManager)

target_include_directories(
${PROJECT_NAME}
PRIVATE
Expand All @@ -14,3 +11,6 @@ target_sources(
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
)

add_subdirectory(Client)
add_subdirectory(ECS)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.27)
cmake_minimum_required(VERSION 3.15)

target_include_directories(
${PROJECT_NAME}
Expand All @@ -10,5 +10,6 @@ target_sources(
${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/SystemManager.cpp
)

add_subdirectory(Systems)
15 changes: 15 additions & 0 deletions src/Client/Systems/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.15)

target_include_directories(
${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
)

target_sources(
${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/ClientSystems.cpp
)

add_subdirectory(Managers)
40 changes: 40 additions & 0 deletions src/Client/Systems/ClientSystems.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
** EPITECH PROJECT, 2023
** R-Bus
** File description:
** Systems implementation
*/

#include <iostream>
#include "raylib.h"
#include "ClientSystems.hpp"
#include "Registry.hpp"
#include "CustomTypes.hpp"

void GraphicSystems::pixelRenderer(std::size_t)
{
Registry::array<Pixel> arrPixel = Registry::getInstance().getComponents<Pixel>();
for (auto begin = arrPixel.begin(); begin != arrPixel.end(); begin++) {
for (int i = 0; i < 50; i++) {
for (int j = 0; j < 50 ; j++) {
DrawPixel(begin->x + i, begin->y + j, PURPLE);
}
}
}
}

void EventsSystems::playerMovement(std::size_t)
{
Registry::array<Pixel> arrPixel = Registry::getInstance().getComponents<Pixel>();

for (auto &pixel : arrPixel) {
if (IsKeyDown(KEY_RIGHT))
pixel.x += 1;
if (IsKeyDown(KEY_LEFT))
pixel.x -= 1;
if (IsKeyDown(KEY_UP))
pixel.y -= 1;
if (IsKeyDown(KEY_DOWN))
pixel.y += 1;
}
}
16 changes: 16 additions & 0 deletions src/Client/Systems/ClientSystems.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
** EPITECH PROJECT, 2023
** R-Bus
** File description:
** Systems
*/

#pragma once

namespace GraphicSystems {
void pixelRenderer(std::size_t);
}

namespace EventsSystems {
void playerMovement(std::size_t);
}
14 changes: 14 additions & 0 deletions src/Client/Systems/Managers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.15)

target_include_directories(
${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
)

target_sources(
${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/GraphicManager.cpp
${CMAKE_CURRENT_SOURCE_DIR}/SystemEventsManager.cpp
)
54 changes: 54 additions & 0 deletions src/Client/Systems/Managers/GraphicManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
** EPITECH PROJECT, 2023
** R-Bus
** File description:
** GraphicManager
*/

#include <iostream>
#include "raylib.h"
#include "GraphicManager.hpp"
#include "CustomTypes.hpp"
#include "ClientSystems.hpp"

namespace Systems {
GraphicManager GraphicManager::_instance = GraphicManager();

GraphicManager::GraphicManager()
{
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "Poc ECS");
SetTargetFPS(60);

addSystem(GraphicSystems::pixelRenderer);
}

GraphicManager::~GraphicManager()
{
CloseWindow();
}

GraphicManager &GraphicManager::getInstance()
{
return _instance;
}

void GraphicManager::updateSystems()
{
BeginDrawing();
ClearBackground(RAYWHITE);
ASystemManager::updateSystems();
EndDrawing();
}

void GraphicManager::addSystem(std::function<void(std::size_t)> sys)
{
ASystemManager::addSystem(sys);
}

void GraphicManager::removeSystem(std::size_t id)
{
ASystemManager::removeSystem(id);
}
}
24 changes: 24 additions & 0 deletions src/Client/Systems/Managers/GraphicManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
** EPITECH PROJECT, 2023
** R-Bus
** File description:
** GraphicManager
*/

#pragma once

#include "ASystemManager.hpp"

namespace Systems {
class GraphicManager : public ASystemManager {
public:
static GraphicManager &getInstance();
void updateSystems();
void addSystem(std::function<void(std::size_t)>);
void removeSystem(std::size_t);
private:
GraphicManager();
~GraphicManager();
static GraphicManager _instance;
};
}
40 changes: 40 additions & 0 deletions src/Client/Systems/Managers/SystemEventsManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
** EPITECH PROJECT, 2023
** R-Bus
** File description:
** SystemEventsManager
*/

#include <iostream>
#include "SystemEventsManager.hpp"
#include "CustomTypes.hpp"
#include "ClientSystems.hpp"

namespace Systems {
SystemEventsManager SystemEventsManager::_instance = SystemEventsManager();

SystemEventsManager::SystemEventsManager()
{
addSystem(EventsSystems::playerMovement);
}

SystemEventsManager &SystemEventsManager::getInstance()
{
return _instance;
}

void SystemEventsManager::updateSystems()
{
ASystemManager::updateSystems();
}

void SystemEventsManager::addSystem(std::function<void(std::size_t)> sys)
{
ASystemManager::addSystem(sys);
}

void SystemEventsManager::removeSystem(std::size_t id)
{
ASystemManager::removeSystem(id);
}
}
23 changes: 23 additions & 0 deletions src/Client/Systems/Managers/SystemEventsManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
** EPITECH PROJECT, 2023
** R-Bus
** File description:
** SystemEventsManager
*/

#pragma once

#include "ASystemManager.hpp"

namespace Systems {
class SystemEventsManager : public ASystemManager {
public:
static SystemEventsManager &getInstance();
void updateSystems();
void addSystem(std::function<void(std::size_t)>);
void removeSystem(std::size_t);
private:
SystemEventsManager();
static SystemEventsManager _instance;
};
}
16 changes: 16 additions & 0 deletions src/ECS/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.15)

add_subdirectory(EventManager)
add_subdirectory(Systems)

target_include_directories(
${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
)

target_sources(
${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/Registry.cpp
)
File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions src/ECS/Registry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
** EPITECH PROJECT, 2023
** R-Bus
** File description:
** Registry
*/

#include "Registry.hpp"

Registry Registry::_instance = Registry();

Registry &Registry::getInstance() {
return _instance;
}

23 changes: 17 additions & 6 deletions src/poc/src/Registry.hpp → src/ECS/Registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,48 @@
#include <typeinfo>
#include <typeindex>
#include <unordered_map>
#include "SparseArray.hpp"
#include <memory>
#include <iostream>
#include "SparseArray.hpp"

class Registry {

public:
template <class Component>
using array = std::shared_ptr<SparseArray<Component>>;
using array = SparseArray<Component> &;

static Registry &getInstance();

template <class Component>
array<Component> registerComponent()
{
if (_data.find(typeid(Component)) == _data.end()) {
_data[typeid(Component)] = std::make_shared<SparseArray<Component>>();
_data[typeid(Component)] = SparseArray<Component>();
}
return castReturn<Component>();
};
}

template <class Component>
array<Component> getComponents()
{
return castReturn<Component>();
};
}

template <class Component>
array<Component> const &getComponents() const
{
return castReturn<Component>();
};
}
private:
Registry() = default;

template<class Component>
array<Component> castReturn()
{
return std::any_cast<array<Component>>(_data[typeid(Component)]);
}

static Registry _instance;
Registry& operator=(const Registry&) = delete;
std::unordered_map<std::type_index, std::any> _data;
};
File renamed without changes.
16 changes: 16 additions & 0 deletions src/ECS/Systems/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.15)

target_include_directories(
${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
)

target_sources(
${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/Systems.cpp
)

add_subdirectory(Managers)
Loading
Loading