Skip to content

Commit

Permalink
Merge pull request #106 from TEAM-AAAAAAAAAAAAAAAA/feat/client/connec…
Browse files Browse the repository at this point in the history
…tion_hub

Feat/client/connection hub
  • Loading branch information
Bootoyka authored Nov 13, 2022
2 parents 78bfd2c + a941604 commit 745d9be
Show file tree
Hide file tree
Showing 32 changed files with 1,036 additions and 326 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ cmake-build-debug
cmake-build-release
r-type_client
r-type_server
r-type_lobby
r-type_demo
vcpkg_installed

Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ add_subdirectory(utils)
add_subdirectory(ecs)
add_subdirectory(client)
add_subdirectory(server)
add_subdirectory(lobby)
46 changes: 29 additions & 17 deletions src/client/GetWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
#include "components/client/Controllable.hpp"
#include "components/client/Drawable.hpp"
#include "components/client/Hitbox.hpp"
#include "components/client/Connection.hpp"
#include "components/client/Parallax.hpp"
#include "components/client/Text.hpp"
#include "systems/ManageClientEvents.hpp"
#include "systems/Movement.hpp"
#include "systems/client/Animate.hpp"
#include "systems/client/Draw.hpp"
#include "systems/client/ExecuteOnce.hpp"
#include "systems/client/HandleIncomingMessages.hpp"
#include "systems/client/HandleParallaxBounds.hpp"
#include "systems/client/HandleSFMLEvents.hpp"
Expand All @@ -35,6 +35,8 @@
#include "systems/client/MenuSelect.hpp"
#include "systems/client/ScoreUpdate.hpp"
#include "systems/client/SendDirection.hpp"
#include "systems/client/PeriodicPing.hpp"
#include "systems/client/KeepAlive.hpp"

using AnimFrame = ecs::component::Animated::AnimFrame;
static const int FRAME_LIMIT = 60;
Expand Down Expand Up @@ -62,6 +64,7 @@ static void registerComponents(ecs::World &world)
world.registry.registerComponent<ecs::component::Text>();
world.registry.registerComponent<ecs::component::Activable>();
world.registry.registerComponent<ecs::component::Score>();
world.registry.registerComponent<ecs::component::Connection>();
}

static void addGameSystems(ecs::World &world)
Expand All @@ -75,14 +78,15 @@ static void addGameSystems(ecs::World &world)
world.addSystem(ecs::systems::movement);
world.addSystem(ecs::systems::HandleParallaxBounds);
world.addSystem(ecs::systems::scoreUpdate);
world.addSystem(ecs::systems::executeOnce);
world.addSystem(ecs::systems::keepAlive);
world.addSystem(ecs::systems::healthBar);
world.addSystem(ecs::systems::animate);
}

static void setGameHUD(ecs::World &world)
{
ecs::Entity textScore = world.registry.spawn_entity();
ecs::Entity textWave = world.registry.spawn_entity();
ecs::Entity health = world.registry.spawn_entity();
ecs::Entity healthBar = world.registry.spawn_entity();

Expand All @@ -92,6 +96,11 @@ static void setGameHUD(ecs::World &world)
world.registry.addComponent<ecs::component::Score>(textScore, {});
world.registry.addComponent<ecs::component::Activable>(textScore, {});

world.registry.addComponent<ecs::component::Position>(textWave, {utils::constant::mapWidth - 250, 10});
world.registry.addComponent<ecs::component::Size>(textWave, {40, 17});
world.registry.addComponent<ecs::component::Text>(textWave, {"Wave: ", "1"});
world.registry.addComponent<ecs::component::Activable>(textWave, {});

world.registry.addComponent<ecs::component::Position>(health, {6, 900});
world.registry.addComponent<ecs::component::Size>(health, {100, 400});
world.registry.addComponent<ecs::component::Drawable>(health, {"menu", {3475, 844, 1011, 256}});
Expand Down Expand Up @@ -181,15 +190,13 @@ static void setGameParallax(ecs::World &world)
* @param engine The engine in which you want to operate
* @return The world ready to be used
*/
ecs::World getGameWorld(const std::string &port = "8000", const std::string &host = "localhost")
ecs::World getGameWorld()
{
ecs::World world;

audio::AudioManager::stopBGM();
audio::AudioManager::loadBGM("bgm1");
audio::AudioManager::playBGM();
network::Client::setHost(host);
network::Client::setPort(port);
utils::Window::getInstance().setFramerateLimit(FRAME_LIMIT);
registerComponents(world);
setGameParallax(world);
Expand All @@ -209,6 +216,7 @@ static void addMenuSystems(ecs::World &world)
world.addSystem(ecs::systems::movement);
world.addSystem(ecs::systems::menuSelect);
world.addSystem(ecs::systems::animate);
world.addSystem(ecs::systems::HandleIncomingMessages);
}

static void setMainButtons(ecs::World &world)
Expand Down Expand Up @@ -244,18 +252,18 @@ static void setRoomTexts(ecs::World &world)
ecs::Entity textFourthRoom = world.registry.spawn_entity();
auto itRoom = utils::constant::buttonValueMap.find(utils::constant::ROOM);

world.registry.addComponent<ecs::component::Text>(textFirstRoom, {"0", "/", "4", "Player"});
world.registry.addComponent<ecs::component::Text>(textSecondRoom, {"0", "/", "4", "Player"});
world.registry.addComponent<ecs::component::Text>(textThirdRoom, {"0", "/", "4", "Player"});
world.registry.addComponent<ecs::component::Text>(textFourthRoom, {"0", "/", "4", "Player"});
world.registry.addComponent<ecs::component::Position>(textFirstRoom, {itRoom->second.posX + 100, itRoom->second.posY - 17 + itRoom->second.rectHeight / 2});
world.registry.addComponent<ecs::component::Position>(textSecondRoom, {itRoom->second.posX + 100, itRoom->second.posY + 133 + itRoom->second.rectHeight / 2});
world.registry.addComponent<ecs::component::Position>(textThirdRoom, {itRoom->second.posX + 100, itRoom->second.posY + 283 + itRoom->second.rectHeight / 2});
world.registry.addComponent<ecs::component::Position>(textFourthRoom, {itRoom->second.posX + 100, itRoom->second.posY + 433 + itRoom->second.rectHeight / 2});
world.registry.addComponent<ecs::component::Size>(textFirstRoom, {30, 0});
world.registry.addComponent<ecs::component::Size>(textSecondRoom, {30, 0});
world.registry.addComponent<ecs::component::Size>(textThirdRoom, {30, 0});
world.registry.addComponent<ecs::component::Size>(textFourthRoom, {30, 0});
world.registry.addComponent<ecs::component::Text>(textFirstRoom, {"0", "/", "4 ", ""});
world.registry.addComponent<ecs::component::Text>(textSecondRoom, {"0", "/", "4 ", ""});
world.registry.addComponent<ecs::component::Text>(textThirdRoom, {"0", "/", "4 ", ""});
world.registry.addComponent<ecs::component::Text>(textFourthRoom, {"0", "/", "4 ", ""});
world.registry.addComponent<ecs::component::Position>(textFirstRoom, {itRoom->second.posX + 80, itRoom->second.posY - 17 + itRoom->second.rectHeight / 2});
world.registry.addComponent<ecs::component::Position>(textSecondRoom, {itRoom->second.posX + 80, itRoom->second.posY + 133 + itRoom->second.rectHeight / 2});
world.registry.addComponent<ecs::component::Position>(textThirdRoom, {itRoom->second.posX + 80, itRoom->second.posY + 283 + itRoom->second.rectHeight / 2});
world.registry.addComponent<ecs::component::Position>(textFourthRoom, {itRoom->second.posX + 80, itRoom->second.posY + 433 + itRoom->second.rectHeight / 2});
world.registry.addComponent<ecs::component::Size>(textFirstRoom, {30, 20});
world.registry.addComponent<ecs::component::Size>(textSecondRoom, {30, 20});
world.registry.addComponent<ecs::component::Size>(textThirdRoom, {30, 20});
world.registry.addComponent<ecs::component::Size>(textFourthRoom, {30, 20});
world.registry.addComponent<ecs::component::Activable>(textFirstRoom, {false, false, utils::constant::ROOM_TEXT});
world.registry.addComponent<ecs::component::Activable>(textSecondRoom, {false, false, utils::constant::ROOM_TEXT});
world.registry.addComponent<ecs::component::Activable>(textThirdRoom, {false, false, utils::constant::ROOM_TEXT});
Expand All @@ -274,21 +282,25 @@ static void setRoomButtons(ecs::World &world)
world.registry.addComponent<ecs::component::Size>(firstRoom, {itRoom->second.rectHeight, itRoom->second.rectWidth});
world.registry.addComponent<ecs::component::Drawable>(firstRoom, {"menu", {itRoom->second.rectLeft, itRoom->second.rectTop, itRoom->second.defaultRectWidth, itRoom->second.defaultRectHeight}});
world.registry.addComponent<ecs::component::Activable>(firstRoom, {false, true, utils::constant::ROOM});
world.registry.addComponent<ecs::component::Connection>(firstRoom, {});

world.registry.addComponent<ecs::component::Position>(secondRoom, {itRoom->second.posX, itRoom->second.posY + 150});
world.registry.addComponent<ecs::component::Size>(secondRoom, {itRoom->second.rectHeight, itRoom->second.rectWidth});
world.registry.addComponent<ecs::component::Drawable>(secondRoom, {"menu", {itRoom->second.rectLeft, itRoom->second.rectTop, itRoom->second.defaultRectWidth, itRoom->second.defaultRectHeight}});
world.registry.addComponent<ecs::component::Activable>(secondRoom, {false, true, utils::constant::ROOM});
world.registry.addComponent<ecs::component::Connection>(secondRoom, {});

world.registry.addComponent<ecs::component::Position>(thirdRoom, {itRoom->second.posX, itRoom->second.posY + 300});
world.registry.addComponent<ecs::component::Size>(thirdRoom, {itRoom->second.rectHeight, itRoom->second.rectWidth});
world.registry.addComponent<ecs::component::Drawable>(thirdRoom, {"menu", {itRoom->second.rectLeft, itRoom->second.rectTop, itRoom->second.defaultRectWidth, itRoom->second.defaultRectHeight}});
world.registry.addComponent<ecs::component::Activable>(thirdRoom, {false, true, utils::constant::ROOM});
world.registry.addComponent<ecs::component::Connection>(thirdRoom, {});

world.registry.addComponent<ecs::component::Position>(fourthRoom, {itRoom->second.posX, itRoom->second.posY + 450});
world.registry.addComponent<ecs::component::Size>(fourthRoom, {itRoom->second.rectHeight, itRoom->second.rectWidth});
world.registry.addComponent<ecs::component::Drawable>(fourthRoom, {"menu", {itRoom->second.rectLeft, itRoom->second.rectTop, itRoom->second.defaultRectWidth, itRoom->second.defaultRectHeight}});
world.registry.addComponent<ecs::component::Activable>(fourthRoom, {false, true, utils::constant::ROOM});
world.registry.addComponent<ecs::component::Connection>(fourthRoom, {});
}

static void setVolumeButtons(ecs::World &world)
Expand Down
2 changes: 1 addition & 1 deletion src/client/GetWorld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

#include "World.hpp"

ecs::World getGameWorld(const std::string& port, const std::string &host);
ecs::World getGameWorld();
ecs::World getMenuWorld();
22 changes: 12 additions & 10 deletions src/client/NetworkClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <iostream>
#include "Constant.hpp"
#include "LockedQueue.hpp"

using chrono = std::chrono::high_resolution_clock;
using chronoDuration = std::chrono::duration<double, std::milli>;
using boost::asio::ip::udp;

namespace network
Expand Down Expand Up @@ -47,6 +50,7 @@ namespace network

static inline LockedQueue<Message> &getReceivedMessages() { return getInstance()._receivedMessages; }

static inline std::chrono::high_resolution_clock::time_point getLastPing() { return getInstance()._lastPing; }
static inline bool getIsConnected() { return getInstance()._isConnected;}
static inline void setIsConnected() { getInstance()._isConnected = true;}

Expand Down Expand Up @@ -100,6 +104,11 @@ namespace network
*/
LockedQueue<Message> _receivedMessages;

/**
* The timestamp of the last received message
*/
std::chrono::high_resolution_clock::time_point _lastPing;

/**
* Handles the incoming messages by placing them into the incoming
* messages locked queue
Expand All @@ -111,6 +120,7 @@ namespace network
if (!error) {
try {
auto message = Message(_recvBuffer);
_lastPing = utils::constant::chrono::now();
if (!message.empty()) {
network::Client::setIsConnected();
_receivedMessages.push(message);
Expand All @@ -131,14 +141,6 @@ namespace network
[this](std::error_code ec, std::size_t bytesRecvd) { this->handleReceive(ec, bytesRecvd); });
}

/**
* Handles the sending of packets
* @param message the packet as an array
* @param error error code of sending
* @param bytesTransferred the size of the outgoing packet
*/
void handleSend(Message message, const std::error_code &error, std::size_t bytesTransferred) {}

/**
* Run the client's service
*/
Expand Down Expand Up @@ -185,8 +187,8 @@ namespace network
*/
std::thread _incomingService;
std::thread _outgoingService;

static Client &getInstance();
// static Client getInstance();
// static Client getInstance();
};
} // namespace network
11 changes: 7 additions & 4 deletions src/client/entrypoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
int main()
{
ecs::Engine engine;
// network::Message msg;
// msg.fill(0);
// network::Client::connect();
// network::Client::getOutgoingMessages().push(msg);
network::Message msg;

network::Client::setHost("localhost");
network::Client::setPort("8000");
msg.fill(0);
network::Client::connect();
network::Client::getOutgoingMessages().push(msg);
asset::AssetLoader::LoadIniFile(asset::AssetLoader::smartPath("assets", "config.ini"));
audio::AudioManager::playSFX("splash_screen");
ecs::WorldManager::setWaitingWorld(getMenuWorld);
Expand Down
2 changes: 1 addition & 1 deletion src/ecs/LockedQueue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace network
* Standard pair class that contains a string and the ID of the client that
* sent it
*/
typedef std::array<char, 16> Message;
typedef std::array<unsigned char, 16> Message;
typedef std::pair<Message, unsigned int> ClientMessage;
typedef std::pair<Message, std::vector<unsigned int>> ServerMessage;

Expand Down
1 change: 1 addition & 0 deletions src/ecs/components/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set(COMPONENTS_SRC
${SRCROOT}/Parallax.hpp
${SRCROOT}/Animated.hpp
${SRCROOT}/Text.hpp
${SRCROOT}/Connection.hpp
${SRCROOT}/Activable.hpp
PARENT_SCOPE
)
Expand Down
38 changes: 38 additions & 0 deletions src/ecs/components/client/Connection.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
** EPITECH PROJECT, by hourcadettej on 11/12/22.
** rtype
** File description:
** rtype
*/

#pragma once
#include "../../../../src/client/NetworkClient.hpp"

namespace ecs::component {
struct Connection {

Connection(int port = 8000, const std::string &host = "localhost") : _port(std::to_string(port)), _host(host), _isSet(false) {}

void setPort(int port) {
_port = std::to_string(port);
_isSet = true;
}
inline std::string getPort() const {return _port;}
inline void setHost(const std::string &host) {_host = host;}
inline std::string getHost() const {return _host;}
inline bool getIsSet() const {return _isSet;}
inline void setIsSet(bool isSet) {_isSet = isSet;}
void setClientConnection() {
network::Message msg;
msg.fill(0);
network::Client::setHost(_host);
network::Client::setPort(_port);
network::Client::connect();
network::Client::getOutgoingMessages().push(msg);
}
private:
std::string _port;
std::string _host;
bool _isSet;
};
}
7 changes: 5 additions & 2 deletions src/ecs/components/client/Text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ namespace ecs::component

struct Text {

Text(size_t marginRight = 0, const std::string &key = "nasa") : _fontKey(key), _marginRight(marginRight), _textColor({255, 255, 255, 255})
Text(size_t marginRight = 0, const std::string &key = "nasa") : _fontKey(key), _marginRight(marginRight), _textColor({255, 255, 255, 255}), _isSet(false)
{
_content.emplace_back("");
_content.emplace_back("");
_content.emplace_back("");
_content.emplace_back("");
};

Text(const std::string& s1 = "", const std::string& s2 = "", const std::string& s3 = "", const std::string& s4 = "", const std::string &key = "nasa") : _fontKey(key), _marginRight(0), _textColor({255, 255, 255, 255})
Text(const std::string& s1 = "", const std::string& s2 = "", const std::string& s3 = "", const std::string& s4 = "", const std::string &key = "nasa") : _fontKey(key), _marginRight(0), _textColor({255, 255, 255, 255}), _isSet(false)
{
_content.emplace_back(s1);
_content.emplace_back(s2);
Expand Down Expand Up @@ -63,11 +63,14 @@ namespace ecs::component
inline const sf::Font &getFont() const { return asset::AssetLoader::GetFont(_fontKey);};
inline void setMarginRight(size_t marginRight) {_marginRight = marginRight;};
inline size_t getMarginRight() const {return _marginRight;};
inline void setIsSet(bool isSet) {_isSet = isSet;}
inline bool getIsSet() {return _isSet;}

private:
std::vector<std::string> _content;
struct textColor _textColor;
std::string _fontKey;
size_t _marginRight;
bool _isSet;
};
} // namespace ecs::component
16 changes: 16 additions & 0 deletions src/ecs/components/server/ServerId.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
** EPITECH PROJECT, 2022
** RTYPE
** File description:
** ServerId
*/

#pragma once

namespace ecs::component
{
struct ServerId {
ServerId(char id) : id(id) {}
char id;
};
} // namespace ecs::component
2 changes: 2 additions & 0 deletions src/ecs/systems/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ set(SYSTEMS_SRC
${SRCROOT}/Draw.hpp
${SRCROOT}/ExecuteOnce.hpp
${SRCROOT}/HandleIncomingMessages.hpp
${SRCROOT}/PeriodicPing.hpp
${SRCROOT}/HandleSFMLEvents.hpp
${SRCROOT}/HandleSFMLKeys.hpp
${SRCROOT}/MenuSelect.hpp
${SRCROOT}/SendDirection.hpp
${SRCROOT}/HandleParallaxBounds.hpp
${SRCROOT}/KeepAlive.hpp
${SRCROOT}/Buttons.hpp
${SRCROOT}/ScoreUpdate.hpp
${SRCROOT}/HealthBar.hpp
Expand Down
Loading

0 comments on commit 745d9be

Please sign in to comment.