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

Fix/missing network send calls #80

Merged
merged 13 commits into from
Oct 12, 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
File renamed without changes.
4 changes: 4 additions & 0 deletions src/Client/Systems/Graphic/AudioSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
#include "AudioSystems.hpp"
#include "CustomTypes.hpp"
#include "Raylib.hpp"
#include "Registry.hpp"
#include "SystemManagersDirector.hpp"

namespace Systems {
void GraphicSystems::soundEffectPlayer(std::size_t /*unused*/, std::size_t /*unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
Registry::components<Raylib::Sound> arrSoundEffect =
Registry::getInstance().getComponents<Raylib::Sound>();

Expand All @@ -26,6 +28,7 @@ namespace Systems {

void GraphicSystems::musicPlayer(std::size_t /*unused*/, std::size_t /*unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
Registry::components<Raylib::Music> arrMusics =
Registry::getInstance().getComponents<Raylib::Music>();

Expand All @@ -45,6 +48,7 @@ namespace Systems {

void GraphicSystems::playSoundWithKey(std::size_t /*unused*/, std::size_t /*unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
Registry &registry = Registry::getInstance();
Registry::components<Raylib::Music> arrMusics = registry.getComponents<Raylib::Music>();
Registry::components<Raylib::Sound> arrSounds = registry.getComponents<Raylib::Sound>();
Expand Down
1 change: 1 addition & 0 deletions src/Client/Systems/Graphic/DeathSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Systems {

const std::function<void(std::size_t)> setPlayerAnimRectDeath = [](std::size_t id) {
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
Registry::components<Types::AnimRect> arrAnimRect =
Registry::getInstance().getComponents<Types::AnimRect>();

Expand Down
1 change: 1 addition & 0 deletions src/Client/Systems/Graphic/GraphicSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace Systems {
#ifndef NDEBUG
void debugCollisionRect(std::size_t /*unused*/, std::size_t /*unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
Registry::components<Types::CollisionRect> arrCollisionRect =
Registry::getInstance().getComponents<Types::CollisionRect>();
Registry::components<Types::Position> arrPosition =
Expand Down
1 change: 1 addition & 0 deletions src/Client/Systems/Graphic/ParallaxSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ namespace Systems::ParallaxSystems {

void manageParallax(std::size_t /*unused*/, std::size_t /*unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
Registry::components<Types::Position> arrPosition =
Registry::getInstance().getComponents<Types::Position>();
Registry::components<Types::Parallax> arrParallax =
Expand Down
34 changes: 33 additions & 1 deletion src/Client/Systems/Graphic/SpriteSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "SpriteSystems.hpp"
#include "CustomTypes.hpp"
#include "ECSCustomTypes.hpp"
#include "Logger.hpp"
#include "Raylib.hpp"
#include "SharedValues.hpp"
Expand Down Expand Up @@ -206,8 +207,39 @@ namespace Systems {
}
}

void GraphicSystems::createSprite(std::size_t /*unused*/, std::size_t /*unused*/)
{
auto &arrSpriteDatas = Registry::getInstance().getComponents<Types::PlayerDatas>();
auto &arrSprite = Registry::getInstance().getComponents<Raylib::Sprite>();

auto ids = arrSpriteDatas.getExistingsId();
for (auto id : ids) {
auto &spriteDatas = arrSpriteDatas[id];
Raylib::Sprite sprite(
spriteDatas.fileName,
spriteDatas.width,
spriteDatas.height,
spriteDatas.id);
arrSprite.insert(id, sprite);
switch (spriteDatas.layer) {
case BACKLAYER:
Registry::getInstance().setToBackLayers(
id,
static_cast<enum BackLayers>(spriteDatas.layerSide));
break;
case FRONTLAYER: Registry::getInstance().setToDefaultLayer(id); break;
case DEFAULTLAYER:
Registry::getInstance().setToFrontLayers(
id,
static_cast<enum FrontLayers>(spriteDatas.layerSide));
break;
}
}
arrSpriteDatas.clear();
}

std::vector<std::function<void(std::size_t, std::size_t)>> GraphicSystems::getSpriteSystems()
{
return {rectIncrementation, rectRenderer, spriteRenderer};
return {rectIncrementation, rectRenderer, spriteRenderer, createSprite};
}
} // namespace Systems
1 change: 1 addition & 0 deletions src/Client/Systems/Graphic/SpriteSystems.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Systems {
void rectIncrementation(std::size_t /*unused*/, std::size_t /*unused*/);
void rectRenderer(std::size_t /*unused*/, std::size_t /*unused*/);
void spriteRenderer(std::size_t /*unused*/, std::size_t /*unused*/);
void createSprite(std::size_t managerId, std::size_t systemId);
std::vector<std::function<void(std::size_t, std::size_t)>> getSpriteSystems();
} // namespace GraphicSystems
} // namespace Systems
21 changes: 14 additions & 7 deletions src/Client/Systems/Network/ClientNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Systems {
void receiveLifeUpdate(std::any &any, boost::asio::ip::udp::endpoint & /* unused */)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
auto msg = std::any_cast<struct msgLifeUpdate_s>(any);
Registry &registry = Registry::getInstance();
Registry::components<struct health_s> arrHealth = registry.getComponents<struct health_s>();
Expand All @@ -27,6 +28,7 @@ namespace Systems {

void receiveEnemyDeath(std::any &any, boost::asio::ip::udp::endpoint &)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
const auto enemyDeath = std::any_cast<struct msgEnemyDeath_s>(any);
Registry::components<Types::Enemy> enemies = Registry::getInstance().getComponents<Types::Enemy>();
std::vector<std::size_t> ids = enemies.getExistingsId();
Expand All @@ -39,23 +41,26 @@ namespace Systems {
}
}

void receiveEnemyNb(std::any &any, boost::asio::ip::udp::endpoint &)
void handleStartWave(std::any &any, boost::asio::ip::udp::endpoint &)
{
const auto enemyNb = std::any_cast<struct msgEnemyNb_s>(any);
Types::Enemy::setEnemyNb(enemyNb.enemyNb);
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
const auto wave = std::any_cast<struct msgStartWave_s>(any);
Types::Enemy::setEnemyNb(wave.enemyNb);
SystemManagersDirector::getInstance()
.getSystemManager(static_cast<std::size_t>(Scene::SystemManagers::GAME))
.addSystem(initWave);
Logger::info("Wave started");
}

void receivePlayerInit(std::any &any, boost::asio::ip::udp::endpoint &)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
const auto playerInit = std::any_cast<struct msgPlayerInit_s>(any);
Registry &registry = Registry::getInstance();
auto &arrPlayer = registry.getComponents<Types::Player>();
auto &arrPlayer = Registry::getInstance().getComponents<Types::Player>();

Logger::info("Player id: " + std::to_string(playerInit.playerId));
arrPlayer[0].constId = playerInit.playerId;
Logger::info("Your player id is: " + std::to_string(playerInit.playerId));
initPlayer();
arrPlayer[arrPlayer.getExistingsId().at(0)].constId = playerInit.playerId;
}

const std::string enemyFile = "assets/Json/enemyData.json";
Expand All @@ -68,6 +73,7 @@ namespace Systems {

void sendPositionRelative(std::size_t /* unused */, std::size_t /* unused */)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
constexpr std::size_t delay = 10;
static auto clockId = Registry::getInstance().getClock().create();
static std::pair<std::size_t, Types::Position> positionPlayerCached;
Expand Down Expand Up @@ -100,6 +106,7 @@ namespace Systems {

void sendPositionAbsolute(std::size_t /* unused */, std::size_t /* unused */)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
constexpr std::size_t delay = 1;
static auto clockId = Registry::getInstance().getClock().create();
Registry &registry = Registry::getInstance();
Expand Down
2 changes: 1 addition & 1 deletion src/Client/Systems/Network/ClientNetwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace Systems {
void receiveLifeUpdate(std::any &any, boost::asio::ip::udp::endpoint &endpoint);
void receiveEnemyDeath(std::any &any, boost::asio::ip::udp::endpoint &endpoint);
void receiveEnemyNb(std::any &any, boost::asio::ip::udp::endpoint &);
void handleStartWave(std::any &any, boost::asio::ip::udp::endpoint &);
void receivePlayerInit(std::any &any, boost::asio::ip::udp::endpoint &endpoint);
void receiveNewEnemy(std::any &any, boost::asio::ip::udp::endpoint &);
void sendPositionRelative(std::size_t /* unused */, std::size_t /* unused */);
Expand Down
32 changes: 32 additions & 0 deletions src/ECS/ECSCustomTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ extern "C"
{
#include "MessageTypes.h"
}
#include "Registry.hpp"

// all values are in percentage of the screen
namespace Types {
Expand Down Expand Up @@ -50,6 +51,30 @@ namespace Types {
NLOHMANN_DEFINE_TYPE_INTRUSIVE(Velocity, speedX, speedY);
};

struct PlayerDatas {
PlayerDatas(
const std::string &fileName,
float width,
float height,
std::size_t id,
enum LayerType layer,
std::size_t layerSide)
: fileName(fileName),
width(width),
height(height),
id(id),
layer(layer),
layerSide(layerSide)
{
}
std::string fileName;
float width;
float height;
std::size_t id;
enum LayerType layer;
size_t layerSide;
};

struct Player {
unsigned int constId;
};
Expand Down Expand Up @@ -99,6 +124,13 @@ namespace Types {
_enemyNb = nb;
}

static unsigned int getEnemyNb()
{
std::lock_guard<std::mutex> lock(_mutex);

return _enemyNb;
}

private:
enemy_id_s _constId;
static unsigned int _enemyNb;
Expand Down
3 changes: 3 additions & 0 deletions src/ECS/Registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <any>
#include <functional>
#include <memory>
#include <mutex>
#include <stdexcept>
#include <string>
#include <typeindex>
Expand Down Expand Up @@ -78,6 +79,8 @@ class Registry {

Logger::Logger &getLogger();

std::mutex mutex;

private:
Registry();

Expand Down
14 changes: 13 additions & 1 deletion src/ECS/SparseArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ class SparseArray {
return _revSparse;
}

void clear()
{
_dense.clear();
for (auto it = _sparse.begin(); it != _sparse.end(); it++) {
if (static_cast<int>(*it) > -1) {
(*it) = static_cast<std::size_t>(-1);
}
}
_revSparse.clear();
}

private:
void removeDenses(std::size_t sparseValue)
{
Expand All @@ -118,7 +129,8 @@ class SparseArray {
void throwIfDontExist(std::size_t id)
{
if (!exist(id)) {
throw std::runtime_error("SparseArrays: ID out of bounds!");
throw std::runtime_error(
"SparseArrays: ID out of bounds! (id: " + std::to_string(id) + ")");
}
}

Expand Down
1 change: 1 addition & 0 deletions src/ECS/Systems/Managers/SystemManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "SystemManager.hpp"
#include <algorithm>
#include "Logger.hpp"

namespace Systems {

Expand Down
Loading
Loading