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

Server-network: Add send bullet function into bullet creation system #68

Merged
merged 5 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 6 additions & 1 deletion src/Client/Systems/Events/EventsSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "EventsSystems.hpp"
#include "CustomTypes.hpp"
#include "NitworkClient.hpp"
#include "Raylib.hpp"
#include "Registry.hpp"
#include "SceneManager.hpp"
Expand Down Expand Up @@ -72,7 +73,7 @@ namespace Systems {
constexpr float bulletHeight = 5.0F;
Types::CollisionRect collisionRect1 = {1, 1};
Types::Velocity velocity = {0.7F, 0.0F};
Types::Missiles missileType = {Types::MissileTypes::CLASSIC};
Types::Missiles missileType = {CLASSIC};
Types::Dead deadComp = {};
Types::PlayerAllies playerAlliesComp = {};
Types::Position position = {pos.x, pos.y};
Expand All @@ -93,6 +94,10 @@ namespace Systems {
Registry::getInstance().getComponents<Types::Damage>().insertBack(damageComp);
Registry::getInstance().getComponents<Types::Dead>().insertBack(deadComp);
Registry::getInstance().setToFrontLayers(entityId);
// send bullet to server
Nitwork::NitworkClient::getInstance().addNewBulletMsg(
{static_cast<int>(position.x), static_cast<int>(position.y)},
missileType.type);
}

const std::size_t waitTimeBullet = 500;
Expand Down
4 changes: 1 addition & 3 deletions src/ECS/ECSCustomTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ extern "C"

namespace Types {

enum MissileTypes { CLASSIC };

struct CollisionRect {
float width;
float height;
Expand Down Expand Up @@ -64,7 +62,7 @@ namespace Types {
};

struct Missiles {
MissileTypes type;
missileTypes_e type;
};

struct PlayerAllies { };
Expand Down
2 changes: 2 additions & 0 deletions src/ECS/MessageTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#define PACK( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop))
#endif

enum missileTypes_e { CLASSIC };

PACK(struct health_s {
int hp;
});
Expand Down
15 changes: 15 additions & 0 deletions src/Nitwork/Nitwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#define MAGICK_START_GAME '\a'
#define MAGICK_POSITION_RELATIVE '\b'
#define MAGICK_LIFE_UPDATE '\x0b'
#define MAGICK_NEW_BULLET '\x0c'


typedef char n_magick_t;
Expand All @@ -41,6 +42,7 @@ enum n_actionType_t {
ENEMY_DEATH = 5,
POSITION_RELATIVE = 6,
POSITION_ABSOLUTE = 7,
NEW_BULLET = 8,
N_ACTION_TYPE_MAX,
};

Expand Down Expand Up @@ -130,6 +132,19 @@ PACK(struct packetPositionRelative_s {
struct msgPositionRelative_s msg;
});

/* Message New Bullet */
PACK(struct msgNewBullet_s {
n_magick_t magick;
struct position_absolute_s pos;
missileTypes_e missileType;
});

PACK(struct packetNewBullet_s {
struct header_s header;
struct action_s action;
struct msgNewBullet_s msg;
});

/* Message Position Absolute */
PACK(struct msgPositionAbsolute_s {
n_magick_t magick;
Expand Down
64 changes: 48 additions & 16 deletions src/Nitwork/NitworkClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ namespace Nitwork {
std::lock_guard<std::mutex> lock(_receivedPacketsIdsMutex);
struct packetMsgInit_s packetMsgInit = {
{
HEADER_CODE1, getIdsReceived(),
HEADER_CODE1, getIdsReceived(),
(!_receivedPacketsIds.empty()) ? _receivedPacketsIds.back() : 0,
getPacketID(),
1, HEADER_CODE2,
},
{INIT},
{MAGICK_INIT }
{MAGICK_INIT}
};
Packet packet(
packetMsgInit.header.id,
Expand All @@ -106,13 +106,13 @@ namespace Nitwork {
std::lock_guard<std::mutex> lock(_receivedPacketsIdsMutex);
struct packetMsgReady_s packetMsgReady = {
{
HEADER_CODE1, getIdsReceived(),
HEADER_CODE1, getIdsReceived(),
(!_receivedPacketsIds.empty()) ? _receivedPacketsIds.back() : 0,
getPacketID(),
1, HEADER_CODE2,
},
{READY},
{MAGICK_READY }
{MAGICK_READY}
};
Packet packet(
packetMsgReady.header.id,
Expand Down Expand Up @@ -152,28 +152,60 @@ namespace Nitwork {
addPacketToSend(_endpoint, packet);
}

void
NitworkClient::addNewBulletMsg(const struct position_absolute_s &pos, const missileTypes_e &missileType)
{
std::lock_guard<std::mutex> lock(_receivedPacketsIdsMutex);
struct packetNewBullet_s packetNewBullet = {
.header =
{
.magick1 = HEADER_CODE1,
.ids_received = getIdsReceived(),
.last_id_received = (!_receivedPacketsIds.empty()) ? _receivedPacketsIds.back() : 0,
.id = getPacketID(),
.nb_action = 1,
.magick2 = HEADER_CODE2,
},
.action =
{
.magick = NEW_BULLET,
},
.msg =
{
.magick = MAGICK_NEW_BULLET,
.pos = pos,
.missileType = missileType,
},
};
Packet packet(
packetNewBullet.header.id,
packetNewBullet.action.magick,
std::make_any<struct packetNewBullet_s>(packetNewBullet));
addPacketToSend(_endpoint, packet);
};

void NitworkClient::addPositionAbsoluteMsg(struct position_absolute_s pos)
{
std::lock_guard<std::mutex> lock(_receivedPacketsIdsMutex);
struct packetPositionAbsolute_s packetMsgPositionRelative = {
.header =
{
.magick1 = HEADER_CODE1,
.ids_received = getIdsReceived(),
.last_id_received = (!_receivedPacketsIds.empty()) ? _receivedPacketsIds.back() : 0,
.id = getPacketID(),
.nb_action = 1,
.magick2 = HEADER_CODE2,
},
.magick1 = HEADER_CODE1,
.ids_received = getIdsReceived(),
.last_id_received = (!_receivedPacketsIds.empty()) ? _receivedPacketsIds.back() : 0,
.id = getPacketID(),
.nb_action = 1,
.magick2 = HEADER_CODE2,
},
.action =
{
.magick = POSITION_RELATIVE,
},
.magick = POSITION_RELATIVE,
},
.msg =
{
.magick = MAGICK_POSITION_RELATIVE,
.pos = pos,
},
.magick = MAGICK_POSITION_RELATIVE,
.pos = pos,
},
};
Packet packet(
packetMsgPositionRelative.header.id,
Expand Down
1 change: 1 addition & 0 deletions src/Nitwork/NitworkClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace Nitwork {
void addReadyMsg();
void addPositionRelativeMsg(struct position_relative_s pos);
void addPositionAbsoluteMsg(struct position_absolute_s pos);
void addNewBulletMsg(const struct position_absolute_s &pos, const missileTypes_e &missileType);

private:
NitworkClient();
Expand Down
6 changes: 3 additions & 3 deletions src/Nitwork/NitworkServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ namespace Nitwork {
getPacketID(),
1, HEADER_CODE2},
{START_GAME},
{MAGICK_START_GAME, playerId }
{MAGICK_START_GAME, playerId}
};
Packet packet(
packetMsgStartGame.header.id,
Expand All @@ -137,8 +137,8 @@ namespace Nitwork {
.id = getPacketID(),
.nb_action = 1,
.magick2 = HEADER_CODE2},
.action = {.magick = LIFE_UPDATE },
.msgLifeUpdate = {.magick = MAGICK_LIFE_UPDATE, .playerId = playerId, .life = life }
.action = {.magick = LIFE_UPDATE},
.msgLifeUpdate = {.magick = MAGICK_LIFE_UPDATE, .playerId = playerId, .life = life}
};
Packet packet(
packetLifeUpdate.header.id,
Expand Down
Loading