Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
9 changes: 8 additions & 1 deletion src/Client/Systems/Network/ClientNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,15 @@ namespace Systems {

void receiveNewEnemy(std::any &any, boost::asio::ip::udp::endpoint &)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
const auto newEnemy = std::any_cast<struct msgNewEnemy_s>(any);
initEnemy(enemyFile);
reviveEnemy(newEnemy.enemyInfos.id);
Types::Position pos = {
static_cast<float>(newEnemy.enemyInfos.pos.x),
static_cast<float>(newEnemy.enemyInfos.pos.y)};
struct health_s hp = newEnemy.enemyInfos.life;
Registry::getInstance().getComponents<Types::Position>().insertBack(pos);
Registry::getInstance().getComponents<struct health_s>().insertBack(hp);
}

void sendPositionRelative(std::size_t /* unused */, std::size_t /* unused */)
Expand Down
22 changes: 12 additions & 10 deletions src/ECS/ECSCustomTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,25 @@ namespace Types {

struct Enemy {
public:
Enemy(enum enemy_type_e type = enemy_type_e::CLASSIC_ENEMY) : _type(type)
Enemy(enum enemy_type_e _type = enemy_type_e::CLASSIC_ENEMY) : type(_type)
{
std::lock_guard<std::mutex> lock(_mutex);

_constId = enemy_id_s {_enemyNb};
constId = enemy_id_s {_enemyNb};
_enemyNb++;
}

[[nodiscard]] const enemy_id_s &getConstId() const
Enemy(struct enemy_id_s _constId, enum enemy_type_e _type = enemy_type_e::CLASSIC_ENEMY)
: constId(_constId),
type(_type)
{
std::lock_guard<std::mutex> lock(_mutex);

return _constId;
}

[[nodiscard]] enum enemy_type_e getType() const
[[nodiscard]] enemy_id_s getConstId() const
{
return _type;
std::lock_guard<std::mutex> lock(_mutex);

return constId;
}

static void setEnemyNb(unsigned int nb)
Expand All @@ -131,10 +132,11 @@ namespace Types {
return _enemyNb;
}

enemy_id_s constId;
enum enemy_type_e type;

private:
enemy_id_s _constId;
static unsigned int _enemyNb;
enum enemy_type_e _type;
static std::mutex _mutex;
};

Expand Down
5 changes: 5 additions & 0 deletions src/ECS/SparseArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ class SparseArray {
return _dense[_sparse[id]];
}

Component &back()
{
return _dense.back();
}

/*
* A dense sparseArrays is not sort by entities id, the begin of two
* sparseArrays could be different entities, only _sparse are
Expand Down
60 changes: 36 additions & 24 deletions src/ECS/Systems/Systems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
#include <fstream>
#include <nlohmann/json.hpp>
#include <sstream>
#include "CustomTypes.hpp"
#include "ECSCustomTypes.hpp"
#include "Registry.hpp"
#include "SystemManagersDirector.hpp"
#ifdef CLIENT
#include "CustomTypes.hpp"
#include "NitworkClient.hpp"
#include "Raylib.hpp"
#else
Expand Down Expand Up @@ -193,47 +194,48 @@ namespace Systems {
clock.restart(clockId);
}

static void initEnemyEntity(nlohmann::json_abi_v3_11_2::basic_json<> &enemyData)
static void initEnemyEntity(
nlohmann::json_abi_v3_11_2::basic_json<> &enemyData,
bool setId = false,
struct ::enemy_id_s enemyId = {0})
{
#ifdef CLIENT
std::size_t id = Registry::getInstance().addEntity();
Raylib::Sprite enemy = {enemyData["spritePath"], enemyData["width"], enemyData["height"], id};
#else
Registry::getInstance().addEntity();
#endif
Types::Position position = {Types::Position(enemyData["position"])};
Types::CollisionRect collisionRect = {Types::CollisionRect(enemyData["collisionRect"])};
Types::Rect rect = {Types::Rect(enemyData["rect"])};
struct health_s healthComp = {enemyData["health"]};
Types::Damage damageComp = {enemyData["damage"]};
Types::Enemy enemyStruct = {};
Types::Velocity velocity = {Types::Velocity(enemyData["velocity"])};

Types::Rect rect = {Types::Rect(enemyData["rect"])};
nlohmann::json animRectData = enemyData["animRect"];
nlohmann::json moveData = animRectData["move"];
nlohmann::json attackData = animRectData["attack"];
nlohmann::json deadData = animRectData["dead"];

Types::AnimRect animRect = {
Types::AnimRect animRect = {
Types::Rect(enemyData["rect"]),
moveData.get<std::vector<Types::Rect>>(),
attackData.get<std::vector<Types::Rect>>(),
deadData.get<std::vector<Types::Rect>>()};
#else
Registry::getInstance().addEntity();
#endif
Types::Position position = {Types::Position(enemyData["position"])};
Types::CollisionRect collisionRect = {Types::CollisionRect(enemyData["collisionRect"])};
struct health_s healthComp = {enemyData["health"]};
Types::Damage damageComp = {enemyData["damage"]};
Types::Enemy enemyStruct = (setId ? enemyId : ::enemy_id_s {0});
Types::Velocity velocity = {Types::Velocity(enemyData["velocity"])};

#ifdef CLIENT
Registry::getInstance().getComponents<Raylib::Sprite>().insertBack(enemy);
Registry::getInstance().getComponents<Types::Rect>().insertBack((rect));
Registry::getInstance().getComponents<Types::AnimRect>().insertBack(animRect);
#endif
Registry::getInstance().getComponents<Types::Position>().insertBack(position);
Registry::getInstance().getComponents<Types::CollisionRect>().insertBack(collisionRect);
Registry::getInstance().getComponents<Types::Rect>().insertBack((rect));
Registry::getInstance().getComponents<Types::AnimRect>().insertBack(animRect);
Registry::getInstance().getComponents<Types::Velocity>().insertBack(velocity);
Registry::getInstance().getComponents<struct health_s>().insertBack(healthComp);
Registry::getInstance().getComponents<Types::Damage>().insertBack(damageComp);
Registry::getInstance().getComponents<Types::Enemy>().insertBack(enemyStruct);
}

void initEnemy(const std::string &path)
static void initEnemy(const std::string &path)
{
nlohmann::json jsonData = openJsonData(path);

Expand All @@ -247,6 +249,15 @@ namespace Systems {

const std::string enemyFile = "assets/Json/enemyData.json";

void reviveEnemy(struct ::enemy_id_s id)
{
nlohmann::json jsonData = openJsonData(enemyFile);
if (jsonData["enemy"] == nullptr) {
return;
}
initEnemyEntity(jsonData["Enemy"][0], true, id);
}

void initWave(std::size_t managerId, std::size_t systemId)
{
static std::size_t enemyNumber = 5;
Expand Down Expand Up @@ -400,10 +411,7 @@ namespace Systems {
id,
FRONTLAYER,
static_cast<std::size_t>(FRONT));
#endif
Types::Position position = {Types::Position(jsonData["position"])};
Types::Rect rect = {Types::Rect(jsonData["rect"])};
Types::CollisionRect collisionRect = {Types::CollisionRect(jsonData["collisionRect"])};
Types::Rect rect = {Types::Rect(jsonData["rect"])};

// AnimRect
nlohmann::json animRectData = jsonData["animRect"];
Expand All @@ -417,15 +425,19 @@ namespace Systems {
attackData.get<std::vector<Types::Rect>>(),
deadData.get<std::vector<Types::Rect>>()};

#endif
Types::Position position = {Types::Position(jsonData["position"])};
Types::CollisionRect collisionRect = {Types::CollisionRect(jsonData["collisionRect"])};

// Add components to registry

#ifdef CLIENT
Registry::getInstance().getComponents<Types::Rect>().insertBack(rect);
Registry::getInstance().getComponents<Types::AnimRect>().insertBack(animRect);
Registry::getInstance().getComponents<Types::PlayerDatas>().insertBack(playerDatas);
#endif
Registry::getInstance().getComponents<Types::Position>().insertBack(position);
Registry::getInstance().getComponents<Types::Rect>().insertBack(rect);
Registry::getInstance().getComponents<Types::CollisionRect>().insertBack(collisionRect);
Registry::getInstance().getComponents<Types::AnimRect>().insertBack(animRect);
Registry::getInstance().getComponents<Types::Player>().insertBack(playerComp);
Registry::getInstance().getComponents<Types::Damage>().insertBack(damageComp);
Registry::getInstance().getComponents<struct health_s>().insertBack(healthComp);
Expand Down
2 changes: 1 addition & 1 deletion src/ECS/Systems/Systems.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <vector>

namespace Systems {
void initEnemy(const std::string &path);
void reviveEnemy(struct enemy_id_s id);
void windowCollision(std::size_t, std::size_t);
void entitiesCollision(std::size_t, std::size_t);
void deathChecker(std::size_t, std::size_t);
Expand Down
2 changes: 1 addition & 1 deletion src/Nitwork/NitworkServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ namespace Nitwork {
return;
}
_endpoints.emplace_back(endpoint);
addPlayerInitMessage(endpoint, _endpoints.size() - 1);
addPlayerInitMessage(endpoint, static_cast<n_id_t>(_endpoints.size() - 1));
}

void NitworkServer::handleReadyMsg(
Expand Down
2 changes: 1 addition & 1 deletion src/Server/Systems/Network/ServerNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace Systems {
.pos =
{static_cast<char>(static_cast<int>(arrPos[index].x)),
static_cast<char>(static_cast<int>(arrPos[index].y))},
.type = arrEnemies[index].getType(),
.type = arrEnemies[index].type,
});
}
} // namespace Systems