Skip to content

Commit

Permalink
ECS: Add function dense SparseArrays
Browse files Browse the repository at this point in the history
MINOR
  • Loading branch information
guillaumeAbel committed Sep 30, 2023
1 parent 5dfe603 commit 058c46c
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 127 deletions.
4 changes: 1 addition & 3 deletions src/Client/SceneManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ int SceneManager::run()
auto &director = Systems::SystemManagersDirector::getInstance();

try {
Registry::getInstance().initCustomSparseArrays(
_scenesCustomIndexes.at(_currentScene));
while (!_stop && !Raylib::windowShouldClose()) {
Raylib::beginDrawing();
Raylib::clearBackground(Raylib::DarkGray);
Expand All @@ -87,7 +85,7 @@ int SceneManager::run()
void SceneManager::changeScene(Scene scene)
{
_currentScene = scene;
Registry::getInstance().clear(_scenesCustomIndexes.at(_currentScene));
Registry::getInstance().clear();
Systems::SystemManagersDirector::getInstance().resetChanges();
}

Expand Down
6 changes: 0 additions & 6 deletions src/Client/SceneManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include <functional>
#include <vector>

enum CustomIndex { BULLET, PLAYER, ENNEMY };

enum ReturnValue { OK = 0, ERROR = 84 };

enum Scene { MENU, MAIN_GAME, SCENE_MAX };
Expand All @@ -37,10 +35,6 @@ class SceneManager {
std::vector<SystemManagers> {EVENTS, GAME, DISPLAY},
std::vector<SystemManagers> {EVENTS, GAME, DISPLAY}
};
const std::array<std::vector<CustomIndex>, 2> _scenesCustomIndexes = {
std::vector<CustomIndex> {PLAYER },
std::vector<CustomIndex> { PLAYER, BULLET, ENNEMY}
};

// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
static bool _init;
Expand Down
4 changes: 3 additions & 1 deletion src/Client/Systems/Events/EventsSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ namespace Systems {
Registry::components<Types::Position> arrPosition =
Registry::getInstance().getComponents<Types::Position>();

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

for (std::size_t id : playerId) {
if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_RIGHT)) {
Expand Down
11 changes: 4 additions & 7 deletions src/Client/Systems/Graphic/GraphicSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ namespace Systems {
(position.y * static_cast<float>(Raylib::getScreenHeight()))
/ denominator;

float width = (rectangle.width
* static_cast<float>(Raylib::getScreenWidth()))
float width =
(rectangle.width * static_cast<float>(Raylib::getScreenWidth()))
/ denominator;
float height = (rectangle.height
* static_cast<float>(Raylib::getScreenHeight()))
Expand Down Expand Up @@ -118,10 +118,7 @@ namespace Systems {

for (auto id : spriteIndexes) {
if (arrRect.exist(id) && arrPosition.exist(id)) {
drawSpriteWithRect(
arrPosition[id],
arrSprite[id],
arrRect[id]);
drawSpriteWithRect(arrPosition[id], arrSprite[id], arrRect[id]);
} else if (arrPosition.exist(id)) {
drawSpriteWithoutRect(arrPosition[id], arrSprite[id]);
}
Expand Down Expand Up @@ -182,7 +179,7 @@ namespace Systems {
Registry::components<Raylib::Text> arrText =
Registry::getInstance().getComponents<Raylib::Text>();

for (auto &textIt: arrText) {
for (auto &textIt : arrText) {
drawTextResponsive(textIt);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/ECS/ECSCustomTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ namespace Types {
float y;
};

struct Player {
};
struct Player { };

} // namespace Types
30 changes: 2 additions & 28 deletions src/ECS/Registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,40 +32,14 @@ void Registry::removeEntity(std::size_t id)
}
}

void Registry::clear(std::vector<CustomIndex> indexes)
void Registry::clear()
{
_data.clear();
_addComponentPlaceFunctions.clear();
_removeComponentFunctions.clear();
_entitiesNb = 0;
_customSparseArrays.clear();
initCustomSparseArrays(indexes);
}

std::size_t Registry::getEntitiesNb() const
Registry::Registry() : _entitiesNb(0)
{
return (_entitiesNb);
}

void Registry::initCustomSparseArrays(std::vector<CustomIndex> indexes)
{
for (auto index : indexes) {
addCustomSparseIndex(static_cast<std::size_t>(index));
}
}

Registry::Registry() : _entitiesNb(0), _maxCustomId(0)
{
}

void Registry::addCustomSparseIndex(std::size_t id)
{
if (_customSparseArrays.find(id) != _customSparseArrays.end()) {
throw std::runtime_error(
"addCustomSparseIndex: id" + std::to_string(id) + " already exist");
};
_customSparseArrays[id] = SparseArray<std::size_t>();
if (id > _maxCustomId) {
_maxCustomId = id;
}
}
39 changes: 1 addition & 38 deletions src/ECS/Registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,39 +37,7 @@ class Registry {

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

void clear(std::vector<CustomIndex>);

template <class Component>
components<Component> getCustomSparseArray(std::size_t id)
{
if (_customSparseArrays.find(id) == _customSparseArrays.end()) {
throw std::runtime_error(
"getCustomSparseArray ID not in :" + std::to_string(id));
}
try {
components<Component> castedComponent =
std::any_cast<components<Component>>(
_customSparseArrays[id]);

return castedComponent;
} catch (const std::bad_any_cast &e) {
throw std::runtime_error("Bad cast: " + std::string(e.what()));
}
}

template <class Component>
std::size_t addCustomSparseArray()
{
_maxCustomId++;
std::size_t id = _maxCustomId;

_customSparseArrays[id] = SparseArray<Component>();
return (id);
}

std::size_t getEntitiesNb() const;

void initCustomSparseArrays(std::vector<CustomIndex> indexes);
void clear();

Registry &operator=(const Registry &) = delete;
Registry(const Registry &) = delete;
Expand Down Expand Up @@ -114,8 +82,6 @@ class Registry {
_data[typeid(Component)]);
}

void addCustomSparseIndex(std::size_t id);

// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
static Registry _instance;
// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
Expand All @@ -126,8 +92,5 @@ class Registry {
_removeComponentFunctions;
std::unordered_map<std::type_index, std::any> _data;

std::unordered_map<std::size_t, std::any> _customSparseArrays;
std::size_t _maxCustomId;

std::size_t _entitiesNb;
};
29 changes: 19 additions & 10 deletions src/ECS/SparseArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,30 @@
#pragma once

#include <iterator>
#include <optional>
#include <vector>

template <typename Component>
class SparseArray {
public:
void add()
{
_sparse.push_back(-1);
_sparse.push_back(std::size_t(-1));
}

void insertBack(const Component &value)
{
insert(_sparse.size() - 1, value);
}
void insert(size_t id, const Component &value) {
if (id >= sparse.size()) {
throw std::runtime_error("SparseArrays::insert: ID out of bounds!");

void insert(size_t id, const Component &value)
{
if (id >= _sparse.size()) {
throw std::runtime_error(
"SparseArrays::insert: ID out of bounds!");
}

if (_sparse[id] > -1) {
_dense[_sparse[id]] = value;
_dense[_sparse[id]] = value;
_revSparse[_sparse[id]] = id;
return;
}
Expand All @@ -37,10 +40,12 @@ class SparseArray {
_dense.push_back(value);
_revSparse.push_back(id);
}

void erase(std::size_t id)
{
if (id >= sparse.size()) {
throw std::runtime_error("SparseArrays::erase: ID out of bounds!");
if (id >= _sparse.size()) {
throw std::runtime_error(
"SparseArrays::erase: ID out of bounds!");
}
if (_sparse[id] != -1) {
auto it = _dense.begin();
Expand All @@ -54,6 +59,7 @@ class SparseArray {
std::advance(it, id);
_sparse.erase(it);
};

Component &operator[](size_t id)
{
throwIfDontExist(id);
Expand All @@ -65,13 +71,16 @@ class SparseArray {
return _dense.begin();
}

typename std::vector<std::optional<Component>>::iterator end()
typename std::vector<Component>::iterator end()
{
return _dense.end();
}
bool exist(std::size_t id) {

bool exist(std::size_t id)
{
return id < _sparse.size() && _sparse[id] != -1;
}

std::vector<std::size_t> getExistingsId()
{
return _revSparse;
Expand Down
57 changes: 25 additions & 32 deletions src/ECS/Systems/Systems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ namespace Systems {
Registry::components<Types::CollisionRect> arrCollisionRect =
Registry::getInstance().getComponents<Types::CollisionRect>();

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

const float maxPercent = 100.0F;
for (std::size_t id : playerIdx) {
Expand All @@ -30,17 +32,11 @@ namespace Systems {
if (arrPosition[id].y < 0) {
arrPosition[id].y = 0;
}
if (arrPosition[id].x
+ arrCollisionRect[id].width
> maxPercent) {
arrPosition[id].x =
maxPercent - arrCollisionRect[id].width;
if (arrPosition[id].x + arrCollisionRect[id].width > maxPercent) {
arrPosition[id].x = maxPercent - arrCollisionRect[id].width;
}
if (arrPosition[id].y
+ arrCollisionRect[id].height
> maxPercent) {
arrPosition[id].y = maxPercent
- arrCollisionRect[id].height;
if (arrPosition[id].y + arrCollisionRect[id].height > maxPercent) {
arrPosition[id].y = maxPercent - arrCollisionRect[id].height;
}
}
}
Expand All @@ -61,29 +57,26 @@ namespace Systems {
void init(std::size_t managerId, std::size_t systemId)
{
Registry::getInstance().addEntity();
Registry::getInstance().getComponents<Types::Position>().insertBack({
playerData,
playerData});
Registry::getInstance().getComponents<Raylib::Sprite>().insertBack({
playerPath,
playerWidth,
playerHeight});
Registry::getInstance().getComponents<Types::Position>().insertBack(
{playerData, playerData});
Registry::getInstance().getComponents<Raylib::Sprite>().insertBack(
{playerPath, playerWidth, playerHeight});
Registry::getInstance().getComponents<Types::Rect>().insertBack(
spriteRect);
Registry::getInstance().getComponents<Types::CollisionRect>().insertBack(
collisionRect);
Registry::getInstance().getComponents<Types::Player>().insertBack();
Registry::getInstance().getComponents<Raylib::Music>().insertBack({
musicPath,
musicVolume});
Registry::getInstance().getComponents<Raylib::Sound>().insertBack({
soundPath,
soundVolume});
Registry::getInstance().getComponents<Raylib::Text>().insertBack({
"Press space to play music, enter to play sound",
textPos,
fontScale,
Raylib::DarkBlue});
Registry::getInstance()
.getComponents<Types::CollisionRect>()
.insertBack(collisionRect);
Registry::getInstance().getComponents<Types::Player>().insertBack({});
Registry::getInstance().getComponents<Raylib::Music>().insertBack(
{musicPath, musicVolume});
Registry::getInstance().getComponents<Raylib::Sound>().insertBack(
{soundPath, soundVolume});
Registry::getInstance().getComponents<Raylib::Text>().insertBack(
{"Press SPACE to play music, ENTER to play sound, J to reset "
"scene, ARROWS to move",
textPos,
fontScale,
Raylib::DarkBlue});
SystemManagersDirector::getInstance()
.getSystemManager(managerId)
.removeSystem(systemId);
Expand Down

0 comments on commit 058c46c

Please sign in to comment.