Skip to content

Commit

Permalink
ECS: Add layer gestion
Browse files Browse the repository at this point in the history
MINOR
  • Loading branch information
guillaumeAbel committed Sep 30, 2023
1 parent 0a78884 commit 0e920d0
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 34 deletions.
7 changes: 6 additions & 1 deletion src/Client/Raylib/Graphics/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,11 @@ namespace Raylib {

// Texture functions

Sprite::Sprite(const std::string &fileName, float width, float height)
Sprite::Sprite(
const std::string &fileName,
float width,
float height,
std::size_t id)
: _texture(LoadTexture(fileName.c_str())),
_width(width),
_height(height)
Expand All @@ -449,6 +453,7 @@ namespace Raylib {
_texture = LoadTextureFromImage(
GenImageColor(badImageSize, badImageSize, badTexture));
}
Registry::getInstance().setToDefaultLayer(id);
}

Sprite::Sprite(Image image, float width, float height)
Expand Down
7 changes: 6 additions & 1 deletion src/Client/Raylib/Graphics/Graphics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "raylib.h"
#include "Geometry.hpp"
#include "Inputs.hpp"
#include "Registry.hpp"

namespace Raylib {

Expand Down Expand Up @@ -143,7 +144,11 @@ namespace Raylib {

class Sprite {
public:
Sprite(const std::string &fileName, float width, float height);
Sprite(
const std::string &fileName,
float width,
float height,
std::size_t id);
Sprite(Image image, float width, float height);
unsigned int getId() const;
float getWidth() const;
Expand Down
9 changes: 4 additions & 5 deletions src/Client/Systems/Events/EventsSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ namespace Systems {
std::size_t /*unused*/,
std::size_t /*unused*/)
{
Registry &registry = Registry::getInstance();
Registry::components<Types::Position> arrPosition =
Registry::getInstance().getComponents<Types::Position>();

std::vector<std::size_t> playerId = Registry::getInstance()
.getComponents<Types::Player>()
.getExistingsId();
registry.getComponents<Types::Position>();
std::vector<std::size_t> playerId =
registry.getComponents<Types::Player>().getExistingsId();

for (std::size_t id : playerId) {
if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_RIGHT)) {
Expand Down
59 changes: 42 additions & 17 deletions src/Client/Systems/Graphic/GraphicSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ namespace Systems {
void
GraphicSystems::rectRenderer(std::size_t /*unused*/, std::size_t /*unused*/)
{
Registry &registry = Registry::getInstance();
Registry::components<Types::Position> arrPosition =
Registry::getInstance().getComponents<Types::Position>();
registry.getComponents<Types::Position>();
Registry::components<Types::RectangleShape> arrRect =
Registry::getInstance().getComponents<Types::RectangleShape>();
registry.getComponents<Types::RectangleShape>();
std::vector<std::size_t> rectShapeIndexes = arrRect.getExistingsId();

const float denominator = 100.0;
Expand Down Expand Up @@ -104,27 +105,50 @@ namespace Systems {
tint);
}

void GraphicSystems::spriteRenderer(
std::size_t /*unused*/,
std::size_t /*unused*/)
static void renderEntityList(std::vector<std::size_t> list)
{
Registry &registry = Registry::getInstance();
Registry::components<Raylib::Sprite> arrSprite =
Registry::getInstance().getComponents<Raylib::Sprite>();
registry.getComponents<Raylib::Sprite>();
Registry::components<Types::Rect> arrRect =
Registry::getInstance().getComponents<Types::Rect>();
registry.getComponents<Types::Rect>();
Registry::components<Types::Position> arrPosition =
Registry::getInstance().getComponents<Types::Position>();
std::vector<std::size_t> spriteIndexes = arrSprite.getExistingsId();

for (auto id : spriteIndexes) {
if (arrRect.exist(id) && arrPosition.exist(id)) {
drawSpriteWithRect(arrPosition[id], arrSprite[id], arrRect[id]);
} else if (arrPosition.exist(id)) {
drawSpriteWithoutRect(arrPosition[id], arrSprite[id]);
registry.getComponents<Types::Position>();

for (auto id : list) {
if (arrPosition.exist(id)) {
if (arrRect.exist(id)) {
drawSpriteWithRect(
arrPosition[id],
arrSprite[id],
arrRect[id]);
} else {
drawSpriteWithoutRect(arrPosition[id], arrSprite[id]);
}
}
}
}

void GraphicSystems::spriteRenderer(
std::size_t /*unused*/,
std::size_t /*unused*/)
{
Registry &registry = Registry::getInstance();
std::vector<std::vector<std::size_t>> backLayers =
registry.getBackLayers();
std::vector<std::size_t> defaultLayer = registry.getDefaultLayer();
std::vector<std::vector<std::size_t>> frontLayers =
registry.getFrontLayers();

for (auto list : backLayers) {
renderEntityList(list);
}
renderEntityList(defaultLayer);
for (auto list : frontLayers) {
renderEntityList(list);
}
}

void GraphicSystems::soundEffectPlayer(
std::size_t /*unused*/,
std::size_t /*unused*/)
Expand Down Expand Up @@ -195,10 +219,11 @@ namespace Systems {
std::size_t /*unused*/,
std::size_t /*unused*/)
{
Registry &registry = Registry::getInstance();
Registry::components<Raylib::Music> arrMusics =
Registry::getInstance().getComponents<Raylib::Music>();
registry.getComponents<Raylib::Music>();
Registry::components<Raylib::Sound> arrSounds =
Registry::getInstance().getComponents<Raylib::Sound>();
registry.getComponents<Raylib::Sound>();

for (auto &music : arrMusics) {
if (music.getPath() == musicPath
Expand Down
63 changes: 62 additions & 1 deletion src/ECS/Registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ Registry &Registry::getInstance()
return _instance;
}

void Registry::addEntity()
std::size_t Registry::addEntity()
{
for (auto function : _addComponentPlaceFunctions) {
function(*this);
}
_entitiesNb++;
return _entitiesNb - 1;
}

void Registry::removeEntity(std::size_t id)
Expand All @@ -40,6 +41,66 @@ void Registry::clear()
_entitiesNb = 0;
}

void Registry::setToBackLayers(std::size_t id, BackLayers layer)
{
removeFromDefaultLayer(id);
_backLayers[layer].push_back(id);
}

void Registry::setToDefaultLayer(std::size_t id)
{
_defaultLayer.push_back(id);
}

void Registry::setToFrontLayers(std::size_t id, FrontLayers layer)
{
_backLayers[layer].push_back(id);
}

std::vector<std::vector<std::size_t>> Registry::getBackLayers()
{
return _backLayers;
}

std::vector<std::size_t> Registry::getDefaultLayer()
{
return _defaultLayer;
}

std::vector<std::vector<std::size_t>> Registry::getFrontLayers()
{
return _frontLayers;
}

void Registry::initLayers(bool back)
{
auto max = static_cast<std::size_t>(
back ? BackLayers::BACKMAX : FrontLayers::FRONTMAX);

for (std::size_t i = 0; i < max; i++) {
std::vector<std::vector<std::size_t>> &layers =
back ? _backLayers : _frontLayers;
layers.emplace_back();
}
if (back) {
initLayers(false);
}
}

void Registry::removeFromDefaultLayer(std::size_t id)
{
auto i = _defaultLayer.end();

while (i != _defaultLayer.begin()) {
--i;
if (*i == id) {
_defaultLayer.erase(i);
break;
}
}
}

Registry::Registry() : _entitiesNb(0)
{
initLayers(true);
}
36 changes: 35 additions & 1 deletion src/ECS/Registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@
#include "SceneManager.hpp"
#include "SparseArray.hpp"

enum LayerType { BACKLAYER, FRONTLAYER, DEFAULTLAYER };

enum BackLayers { BACK = 0, BACKMAX };

/*
* FRONT is the frontest layer, so when adding a new one increment the FRONT
* value and add the new one above
*/

enum FrontLayers { FRONT = 0, FRONTMAX };

class Registry {
public:
template <class Component>
Expand All @@ -33,12 +44,27 @@ class Registry {
return castReturn<Component>();
}

void addEntity();
std::size_t addEntity();

void removeEntity(std::size_t /*id*/);

void clear();

void
setToBackLayers(std::size_t id, BackLayers layer = BackLayers::BACK);

void setToDefaultLayer(std::size_t id);

void setToFrontLayers(
std::size_t id,
FrontLayers layer = FrontLayers::FRONT);

std::vector<std::vector<std::size_t>> getBackLayers();

std::vector<std::size_t> getDefaultLayer();

std::vector<std::vector<std::size_t>> getFrontLayers();

Registry &operator=(const Registry &) = delete;
Registry(const Registry &) = delete;
void operator=(const Registry &&) = delete;
Expand All @@ -47,6 +73,10 @@ class Registry {
private:
Registry();

void initLayers(bool back);

void removeFromDefaultLayer(std::size_t id);

template <typename Component>
void checkAddSparseArray()
{
Expand Down Expand Up @@ -93,4 +123,8 @@ class Registry {
std::unordered_map<std::type_index, std::any> _data;

std::size_t _entitiesNb;

std::vector<std::vector<std::size_t>> _backLayers;
std::vector<std::size_t> _defaultLayer;
std::vector<std::vector<std::size_t>> _frontLayers;
};
16 changes: 15 additions & 1 deletion src/ECS/SparseArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ class SparseArray {
return _dense[_sparse[id]];
}

/*
* A dense sparseArrays is not sort by entities id, the begin of two
* sparseArrays could be different entities, only _sparse are
* synchronized You can only use iterator in a system dealing with only
* one component at time
*/
typename std::vector<Component>::iterator begin()
{
return _dense.begin();
Expand All @@ -78,7 +84,15 @@ class SparseArray {

bool exist(std::size_t id)
{
return id < _sparse.size() && _sparse[id] != -1;
if (id >= _sparse.size()) {
return false;
}
for (auto elemId : _revSparse) {
if (id == elemId) {
return true;
}
}
return false;
}

std::vector<std::size_t> getExistingsId()
Expand Down
39 changes: 32 additions & 7 deletions src/ECS/Systems/Systems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
namespace Systems {
void windowCollision(std::size_t /*unused*/, std::size_t /*unused*/)
{
Registry &registry = Registry::getInstance();
Registry::components<Types::Position> arrPosition =
Registry::getInstance().getComponents<Types::Position>();
registry.getComponents<Types::Position>();
Registry::components<Types::CollisionRect> arrCollisionRect =
Registry::getInstance().getComponents<Types::CollisionRect>();
registry.getComponents<Types::CollisionRect>();

std::vector<std::size_t> playerIdx = Registry::getInstance()
.getComponents<Types::Player>()
.getExistingsId();
std::vector<std::size_t> playerIdx =
registry.getComponents<Types::Player>().getExistingsId();

const float maxPercent = 100.0F;
for (std::size_t id : playerIdx) {
Expand Down Expand Up @@ -56,17 +56,42 @@ namespace Systems {

void init(std::size_t managerId, std::size_t systemId)
{
Registry::getInstance().addEntity();
std::size_t id = Registry::getInstance().addEntity();
Registry::getInstance().getComponents<Types::Position>().insertBack(
{playerData, playerData});
Registry::getInstance().getComponents<Raylib::Sprite>().insertBack(
{playerPath, playerWidth, playerHeight});
{playerPath, playerWidth, playerHeight, id});
Registry::getInstance().getComponents<Types::Rect>().insertBack(
spriteRect);
Registry::getInstance()
.getComponents<Types::CollisionRect>()
.insertBack(collisionRect);
Registry::getInstance().setToBackLayers(id);

id = Registry::getInstance().addEntity();
Registry::getInstance().getComponents<Types::Position>().insertBack(
{playerData, playerData});
Registry::getInstance().getComponents<Raylib::Sprite>().insertBack(
{playerPath, playerWidth, playerHeight, id});
Registry::getInstance().getComponents<Types::Rect>().insertBack(
spriteRect);
Registry::getInstance()
.getComponents<Types::CollisionRect>()
.insertBack(collisionRect);
Registry::getInstance().getComponents<Types::Player>().insertBack({});

id = Registry::getInstance().addEntity();
Registry::getInstance().getComponents<Types::Position>().insertBack(
{playerData, playerData + playerData + playerData});
Registry::getInstance().getComponents<Raylib::Sprite>().insertBack(
{playerPath, playerWidth, playerHeight, id});
Registry::getInstance().getComponents<Types::Rect>().insertBack(
spriteRect);
Registry::getInstance()
.getComponents<Types::CollisionRect>()
.insertBack(collisionRect);
Registry::getInstance().setToFrontLayers(id);

Registry::getInstance().getComponents<Raylib::Music>().insertBack(
{musicPath, musicVolume});
Registry::getInstance().getComponents<Raylib::Sound>().insertBack(
Expand Down

0 comments on commit 0e920d0

Please sign in to comment.