diff --git a/assets/Audio/Musics/home.mp3 b/assets/Audio/Musics/home.mp3 new file mode 100644 index 00000000..e654a083 Binary files /dev/null and b/assets/Audio/Musics/home.mp3 differ diff --git a/assets/Json/createLobby.json b/assets/Json/createLobby.json index 8dfff103..2894c830 100644 --- a/assets/Json/createLobby.json +++ b/assets/Json/createLobby.json @@ -51,6 +51,67 @@ } ] }, + { + "type": "button", + "callback": "changeParallax", + "spritePath": "assets/Menu/button.png", + "width": 1500, + "height" : 750, + "rect": { + "x": 51.0, + "y": 51.0, + "width": 1265.0, + "height": 305.0 + }, + "position" : { + "x": 8000, + "y": 500 + }, + "collisionRect": { + "width": 1500, + "height": 750, + "offsetX": 0, + "offsetY": 0 + }, + "animRect": [ + { + "type": "hover", + "time": 80, + "noIncr": true, + "list": [ + { + "x" : 1416.0, + "y": 49.0, + "width": 1267.0, + "height": 307.0 + } + ] + }, + { + "type": "selected", + "time": 80, + "noIncr": true, + "list": [ + { + "x" : 2759.0, + "y": 31.0, + "width": 1303.0, + "height": 340.0 + } + ] + } + ] + }, + { + "type": "text", + "color": "red", + "size": 1.5, + "text": "PARALLAX_1", + "position" : { + "x": 8300, + "y": 800 + } + }, { "type": "text", "text": "Go Back", @@ -284,6 +345,16 @@ ] } ] + }, + { + "type": "text", + "color": "red", + "size": 2, + "text": "CREATE LOBBY", + "position" : { + "x": 4150, + "y": 500 + } } ] } diff --git a/assets/Json/createServer.json b/assets/Json/createServer.json new file mode 100644 index 00000000..7f523dc8 --- /dev/null +++ b/assets/Json/createServer.json @@ -0,0 +1,312 @@ +{ + "createServerMenu": [ + { + "type": "button", + "spritePath": "assets/Menu/button.png", + "width": 1600, + "height": 1000, + "callback": "goToMenu", + "rect": { + "x": 0.0, + "y": 0.0, + "width": 1366.0, + "height": 406.0 + }, + "position": { + "x": 100, + "y": 500 + }, + "collisionRect": { + "width": 1600, + "height": 1000, + "offsetX": 0, + "offsetY": 0 + }, + "animRect": [ + { + "type": "hover", + "time": 80, + "noIncr": true, + "list": [ + { + "x": 1366.0, + "y": 0.0, + "width": 1366.0, + "height": 406.0 + } + ] + }, + { + "type": "selected", + "time": 80, + "noIncr": true, + "list": [ + { + "x" : 2732.0, + "y": 0.0, + "width": 1366.0, + "height": 406.0 + } + ] + } + ] + }, + { + "type": "button", + "callback": "changeParallax", + "spritePath": "assets/Menu/button.png", + "width": 1500, + "height" : 750, + "rect": { + "x": 51.0, + "y": 51.0, + "width": 1265.0, + "height": 305.0 + }, + "position" : { + "x": 8000, + "y": 500 + }, + "collisionRect": { + "width": 1500, + "height": 750, + "offsetX": 0, + "offsetY": 0 + }, + "animRect": [ + { + "type": "hover", + "time": 80, + "noIncr": true, + "list": [ + { + "x" : 1416.0, + "y": 49.0, + "width": 1267.0, + "height": 307.0 + } + ] + }, + { + "type": "selected", + "time": 80, + "noIncr": true, + "list": [ + { + "x" : 2759.0, + "y": 31.0, + "width": 1303.0, + "height": 340.0 + } + ] + } + ] + }, + { + "type": "text", + "color": "red", + "size": 1.5, + "text": "PARALLAX_1", + "position" : { + "x": 8300, + "y": 800 + } + }, + { + "type": "text", + "text": "Go Back", + "color": "red", + "size": 2, + "position": { + "x": 525, + "y": 875 + } + }, + { + "type": "text", + "color": "red", + "size": 2, + "text": "YOU CAN CREATE A NEW SERVER HERE", + "position" : { + "x": 2670, + "y": 1700 + } + }, + { + "type": "text", + "color": "red", + "size": 2, + "text": "JUST INDICATE US THE PORT OF YOUR SERVER", + "position" : { + "x": 2250, + "y": 2000 + } + }, + + { + "type": "text", + "color": "red", + "size": 2, + "text": "PORT (ex: 4242)", + "position" : { + "x": 2450, + "y": 3650 + } + }, + { + "name": "port", + "type": "input", + "font": "assets/Fonts/soliden/SolidenTrial-Black.otf", + "spritePath": "assets/Menu/input-box.png", + "text": "4242", + "color": "white", + "textSize": 3, + "maxChar": 8, + "width": 6000, + "height": 800, + "textPosition": { + "x": 3050, + "y": 4180 + }, + "position" : { + "x": 2000, + "y": 4000 + }, + "rect": { + "x": 0.0, + "y": 0.0, + "width": 1670.0, + "height": 278.0 + }, + "collisionRect": { + "width": 6000, + "height": 800, + "offsetX": 0, + "offsetY": 0 + }, + "animRect": [ + { + "type": "hover", + "time": 80, + "noIncr": true, + "list": [ + { + "x" : 1697.0, + "y": 31.0, + "width": 1618.0, + "height": 221.0 + } + ] + }, + { + "type": "selected", + "time": 80, + "noIncr": true, + "list": [ + { + "x" : 3348.0, + "y": 14.0, + "width": 1651.0, + "height": 252.0 + } + ] + } + ] + }, + { + "type": "text", + "color": "white", + "size": 2, + "text": "CREATE", + "position" : { + "x": 4200, + "y": 6500 + } + }, + { + "type": "button", + "spritePath": "assets/Menu/button.png", + "callback": "createServer", + "width": 2500, + "height" : 1250, + "rect": { + "x": 0.0, + "y": 0.0, + "width": 1366.0, + "height": 406.0 + }, + "position": { + "x": 3400, + "y": 6000 + }, + "collisionRect": { + "width": 2500, + "height": 1250, + "offsetX": 0, + "offsetY": 0 + }, + "animRect": [ + { + "type": "hover", + "time": 80, + "noIncr": true, + "list": [ + { + "x": 1366.0, + "y": 0.0, + "width": 1366.0, + "height": 406.0 + } + ] + }, + { + "type": "selected", + "time": 80, + "noIncr": true, + "list": [ + { + "x" : 3348.0, + "y": 14.0, + "width": 1651.0, + "height": 252.0 + } + ] + }, + { + "type": "selected", + "time": 80, + "noIncr": true, + "list": [ + { + "x": 2732.0, + "y": 0.0, + "width": 1366.0, + "height": 406.0 + } + ] + } + ] + }, + { + "type": "text", + "color": "red", + "size": 2, + "text": "CREATE SERVER", + "position" : { + "x": 3800, + "y": 500 + } + } + ], + "errorMessage": { + "type": "text", + "color": "red", + "size": 2, + "text": "SERVER ALREADY CREATED", + "position" : { + "x": 3200, + "y": 8500 + } + } + } + \ No newline at end of file diff --git a/assets/Json/menu.json b/assets/Json/menu.json index 6ae48a1e..d1d73312 100644 --- a/assets/Json/menu.json +++ b/assets/Json/menu.json @@ -4,8 +4,8 @@ "type": "button", "callback": "initConnection", "spritePath": "assets/Menu/button.png", - "width": 3000, - "height" : 1500, + "width": 1500, + "height" : 750, "rect": { "x": 51.0, "y": 51.0, @@ -13,12 +13,185 @@ "height": 305.0 }, "position" : { - "x": 3100, + "x": 3800, "y": 6500 }, "collisionRect": { - "width": 3000, - "height": 1500, + "width": 1500, + "height": 750, + "offsetX": 0, + "offsetY": 0 + }, + "animRect": [ + { + "type": "hover", + "time": 80, + "noIncr": true, + "list": [ + { + "x" : 1416.0, + "y": 49.0, + "width": 1267.0, + "height": 307.0 + } + ] + }, + { + "type": "selected", + "time": 80, + "noIncr": true, + "list": [ + { + "x" : 2759.0, + "y": 31.0, + "width": 1303.0, + "height": 340.0 + } + ] + } + ] + }, + { + "type": "text", + "text": "EXIT ('ESC')", + "color": "red", + "size": 1.5, + "position": { + "x": 450, + "y": 875 + } + }, + { + "type": "button", + "spritePath": "assets/Menu/button.png", + "width": 1600, + "height": 1000, + "callback": "exitGame", + "rect": { + "x": 0.0, + "y": 0.0, + "width": 1366.0, + "height": 406.0 + }, + "position": { + "x": 100, + "y": 500 + }, + "collisionRect": { + "width": 1600, + "height": 1000, + "offsetX": 0, + "offsetY": 0 + }, + "animRect": [ + { + "type": "hover", + "time": 80, + "noIncr": true, + "list": [ + { + "x": 1366.0, + "y": 0.0, + "width": 1366.0, + "height": 406.0 + } + ] + }, + { + "type": "selected", + "time": 80, + "noIncr": true, + "list": [ + { + "x" : 2732.0, + "y": 0.0, + "width": 1366.0, + "height": 406.0 + } + ] + } + ] + }, + { + "type": "button", + "callback": "changeParallax", + "spritePath": "assets/Menu/button.png", + "width": 1500, + "height" : 750, + "rect": { + "x": 51.0, + "y": 51.0, + "width": 1265.0, + "height": 305.0 + }, + "position" : { + "x": 8000, + "y": 500 + }, + "collisionRect": { + "width": 1500, + "height": 750, + "offsetX": 0, + "offsetY": 0 + }, + "animRect": [ + { + "type": "hover", + "time": 80, + "noIncr": true, + "list": [ + { + "x" : 1416.0, + "y": 49.0, + "width": 1267.0, + "height": 307.0 + } + ] + }, + { + "type": "selected", + "time": 80, + "noIncr": true, + "list": [ + { + "x" : 2759.0, + "y": 31.0, + "width": 1303.0, + "height": 340.0 + } + ] + } + ] + }, + { + "type": "text", + "color": "red", + "size": 1.5, + "text": "PARALLAX_1", + "position" : { + "x": 8300, + "y": 800 + } + }, + { + "type": "button", + "callback": "goCreateServer", + "spritePath": "assets/Menu/button.png", + "width": 1500, + "height" : 750, + "rect": { + "x": 51.0, + "y": 51.0, + "width": 1265.0, + "height": 305.0 + }, + "position" : { + "x": 3800, + "y": 7700 + }, + "collisionRect": { + "width": 1500, + "height": 750, "offsetX": 0, "offsetY": 0 }, @@ -194,11 +367,31 @@ { "type": "text", "color": "red", - "size": 3, + "size": 2, "text": "CONNECT", "position" : { - "x": 3850, - "y": 7000 + "x": 4100, + "y": 6750 + } + }, + { + "type": "text", + "color": "red", + "size": 2, + "text": "CREATE", + "position" : { + "x": 4150, + "y": 7950 + } + }, + { + "type": "text", + "color": "red", + "size": 2, + "text": "MENU", + "position" : { + "x": 4150, + "y": 500 } } ], @@ -206,5 +399,9 @@ "imgPath": "assets/Menu/loading.gif", "width": 100, "height": 100 + }, + "mainTheme": { + "path": "assets/Audio/Musics/home.mp3", + "volume": 0.65 } } diff --git a/assets/Json/selectLobby.json b/assets/Json/selectLobby.json index 27ac8651..545289c6 100644 --- a/assets/Json/selectLobby.json +++ b/assets/Json/selectLobby.json @@ -16,6 +16,67 @@ "height": 1518.0 } }, + { + "type": "button", + "callback": "changeParallax", + "spritePath": "assets/Menu/button.png", + "width": 1500, + "height" : 750, + "rect": { + "x": 51.0, + "y": 51.0, + "width": 1265.0, + "height": 305.0 + }, + "position" : { + "x": 8000, + "y": 500 + }, + "collisionRect": { + "width": 1500, + "height": 750, + "offsetX": 0, + "offsetY": 0 + }, + "animRect": [ + { + "type": "hover", + "time": 80, + "noIncr": true, + "list": [ + { + "x" : 1416.0, + "y": 49.0, + "width": 1267.0, + "height": 307.0 + } + ] + }, + { + "type": "selected", + "time": 80, + "noIncr": true, + "list": [ + { + "x" : 2759.0, + "y": 31.0, + "width": 1303.0, + "height": 340.0 + } + ] + } + ] + }, + { + "type": "text", + "color": "red", + "size": 1.5, + "text": "PARALLAX_1", + "position" : { + "x": 8300, + "y": 800 + } + }, { "type": "button", "spritePath": "assets/Menu/button.png", @@ -259,6 +320,77 @@ "x": 7350, "y": 7250 } + }, + { + "type": "button", + "spritePath": "assets/Menu/button.png", + "width": 1600, + "height": 1000, + "callback": "goToMenu", + "rect": { + "x": 0.0, + "y": 0.0, + "width": 1366.0, + "height": 406.0 + }, + "position": { + "x": 100, + "y": 500 + }, + "collisionRect": { + "width": 1600, + "height": 1000, + "offsetX": 0, + "offsetY": 0 + }, + "animRect": [ + { + "type": "hover", + "time": 80, + "noIncr": true, + "list": [ + { + "x": 1366.0, + "y": 0.0, + "width": 1366.0, + "height": 406.0 + } + ] + }, + { + "type": "selected", + "time": 80, + "noIncr": true, + "list": [ + { + "x" : 2732.0, + "y": 0.0, + "width": 1366.0, + "height": 406.0 + } + ] + } + ] + }, + { + "type": "text", + "text": "Go Back", + "color": "red", + "size": 2, + "position": { + "x": 525, + "y": 875 + } + }, + { + "type": "text", + "color": "red", + "size": 2, + "text": "SELECT LOBBY", + "position" : { + "x": 4150, + "y": 500 + } } ], "inputBoxes" : { diff --git a/src/Client/Systems/Menus/CMakeLists.txt b/src/Client/Systems/Menus/CMakeLists.txt index 8bad22f1..768dcd0d 100644 --- a/src/Client/Systems/Menus/CMakeLists.txt +++ b/src/Client/Systems/Menus/CMakeLists.txt @@ -3,3 +3,4 @@ cmake_minimum_required(VERSION 3.15) add_subdirectory(Menu) add_subdirectory(SelectLobby) add_subdirectory(CreateLobby) +add_subdirectory(CreateServer) diff --git a/src/Client/Systems/Menus/CreateServer/CMakeLists.txt b/src/Client/Systems/Menus/CreateServer/CMakeLists.txt new file mode 100644 index 00000000..2b08f1e1 --- /dev/null +++ b/src/Client/Systems/Menus/CreateServer/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.15) + +target_include_directories( + ${PROJECT_NAME_CLIENT} + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR} +) + +target_sources( + ${PROJECT_NAME_CLIENT} + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/CreateServerSystems.cpp +) diff --git a/src/Client/Systems/Menus/CreateServer/CreateServerSystems.cpp b/src/Client/Systems/Menus/CreateServer/CreateServerSystems.cpp new file mode 100644 index 00000000..d4dbe47d --- /dev/null +++ b/src/Client/Systems/Menus/CreateServer/CreateServerSystems.cpp @@ -0,0 +1,40 @@ +/* +** EPITECH PROJECT, 2023 +** R-Bus +** File description: +** CreateServerSystems +*/ + +#include "CreateServerSystems.hpp" +#include "B-luga/SceneManager.hpp" +#include "B-luga/SystemManagers/SystemManagersDirector.hpp" +#include "Menu.hpp" +#include "Parallax.hpp" +#include "ResourcesManager.hpp" +#include "init.hpp" + +namespace Systems::CreateServerSystems { + void initCreateServer(std::size_t managerId, std::size_t systemId) + { + if (Scene::SceneManager::getInstance().getCurrentScene() != CREATE_SERVER_SCENE) { + SystemManagersDirector::getInstance().getSystemManager(managerId).removeSystem(systemId); + return; + } + try { + Parallax::initParalax(); + nlohmann::json jsonData = Json::getInstance().getDataByJsonType( + ResourcesManager::getPathByJsonType(JsonType::CREATE_SERVER), + "createServerMenu"); + ::Menu::MenuBuilder::getInstance().initMenuSceneEntity( + Json::getInstance().getDatasFromList(jsonData)); + } catch (std::runtime_error &err) { + Logger::info(err.what()); + } + SystemManagersDirector::getInstance().getSystemManager(managerId).removeSystem(systemId); + } + + std::vector> getCreateServerSystems() + { + return {initCreateServer}; + } +} // namespace Systems::CreateServerSystems diff --git a/src/Client/Systems/Menus/CreateServer/CreateServerSystems.hpp b/src/Client/Systems/Menus/CreateServer/CreateServerSystems.hpp new file mode 100644 index 00000000..f3d503df --- /dev/null +++ b/src/Client/Systems/Menus/CreateServer/CreateServerSystems.hpp @@ -0,0 +1,16 @@ +/* +** EPITECH PROJECT, 2023 +** R-Bus +** File description: +** CreateServerSystems +*/ + +#pragma once + +#include +#include + +namespace Systems::CreateServerSystems { + std::vector> + getCreateServerSystems(); +} \ No newline at end of file diff --git a/src/Client/Systems/Menus/Menu/ButtonCallbacks.cpp b/src/Client/Systems/Menus/Menu/ButtonCallbacks.cpp index 61d2aeca..d14ede49 100644 --- a/src/Client/Systems/Menus/Menu/ButtonCallbacks.cpp +++ b/src/Client/Systems/Menus/Menu/ButtonCallbacks.cpp @@ -10,7 +10,11 @@ #include "B-luga-graphics/Raylib/Raylib.hpp" #include "B-luga/Logger.hpp" #include "B-luga/SceneManager.hpp" +#include "B-luga/SystemManagers/SystemManagersDirector.hpp" +#include "Menu.hpp" #include "NitworkClient.hpp" +#include "Parallax.hpp" +#include "ResourcesManager.hpp" #include "SelectLobbySystems.hpp" #include "init.hpp" @@ -155,6 +159,7 @@ namespace Menu { void sendReadyPacket() { auto idsButton = Registry::getInstance().getEntitiesByComponents({typeid(Types::Button)}); + auto arrButton = Registry::getInstance().getComponents(); Nitwork::NitworkClient::getInstance().addReadyMsg(); for (auto &id : idsButton) { @@ -168,5 +173,91 @@ namespace Menu { } } } + + void goCreateServer() + { + Scene::SceneManager::getInstance().changeScene(CREATE_SERVER_SCENE); + } + + void goMenu() + { + Scene::SceneManager::getInstance().changeScene(MENU); + } + + void exitGame() + { + Scene::SceneManager::getInstance().stop(); + } + + static const std::size_t MAX_TEXT_OF_SCENE = 9; + + void createServer() + { + auto arrInputBox = Registry::getInstance().getComponents(); + auto idsText = Registry::getInstance().getEntitiesByComponents({typeid(Raylib::TextShared)}); + auto ids = Registry::getInstance().getEntitiesByComponents({typeid(Types::InputBox)}); + + if (Nitwork::NitworkClient::getInstance().serverAlreadyCreated() + && idsText.size() < MAX_TEXT_OF_SCENE) { + try { + nlohmann::json jsonData = Json::getInstance().getDataByJsonType( + ResourcesManager::getPathByJsonType(JsonType::CREATE_SERVER), + "errorMessage"); + ::Menu::MenuBuilder::getInstance().initMenuEntity(jsonData); + } catch (std::runtime_error &err) { + Logger::warn(err.what()); + } + return; + } + for (auto id : ids) { + if (arrInputBox[id].name == "port") { + Nitwork::NitworkClient::getInstance().createForkedServer(arrInputBox[id].text); + } + } + } + + static const std::size_t MAX_PARALLAX = 2; + + void changeParallax() + { + auto ids = Registry::getInstance().getEntitiesByComponents({typeid(Raylib::TextShared)}); + auto idsPara = Registry::getInstance().getEntitiesByComponents({typeid(Types::Parallax)}); + auto arrText = Registry::getInstance().getComponents(); + auto arrParallax = Registry::getInstance().getComponents(); + + if (Systems::Parallax::ActualParallax::getInstance()._actualParallaxNbr < MAX_PARALLAX) { + Systems::Parallax::ActualParallax::getInstance()._actualParallaxNbr += 1; + } else { + Systems::Parallax::ActualParallax::getInstance()._actualParallaxNbr = 1; + } + for (auto id : idsPara) { + Registry::getInstance().addToRemove(id); + } + switch (Systems::Parallax::ActualParallax::getInstance().getActualParallaxType()) { + case JsonType::DEFAULT_PARALLAX: + Systems::Parallax::ActualParallax::getInstance().setActualParralaxType( + JsonType::PARALLAX_2); + break; + case JsonType::PARALLAX_2: + Systems::Parallax::ActualParallax::getInstance().setActualParralaxType( + JsonType::DEFAULT_PARALLAX); + break; + default: + Systems::Parallax::ActualParallax::getInstance().setActualParralaxType( + JsonType::DEFAULT_PARALLAX); + break; + } + for (auto id : ids) { + if (arrText[id]->getCurrentText().find("PARALLAX") != std::string::npos) { + std::string paraName = + "PARALLAX_" + + std::to_string( + Systems::Parallax::ActualParallax::getInstance()._actualParallaxNbr); + + arrText[id]->setCurrentText(paraName); + } + } + Systems::Parallax::initParalax(); + } } // namespace Callback } // namespace Menu diff --git a/src/Client/Systems/Menus/Menu/ButtonCallbacks.hpp b/src/Client/Systems/Menus/Menu/ButtonCallbacks.hpp index f539ff67..2604f0c1 100644 --- a/src/Client/Systems/Menus/Menu/ButtonCallbacks.hpp +++ b/src/Client/Systems/Menus/Menu/ButtonCallbacks.hpp @@ -23,7 +23,12 @@ namespace Menu { GO_NEXT, GO_SELECT_LOBBY, CREATE_NORMAL, - SEND_READY + SEND_READY, + GO_CREATE_SERVER, + CREATE_SERVER, + GO_MENU, + CHANGE_PARALLAX, + EXIT_GAME }; NLOHMANN_JSON_SERIALIZE_ENUM( @@ -37,7 +42,12 @@ namespace Menu { {GO_NEXT, "goNextPage" }, {GO_SELECT_LOBBY, "goToSelectLobby" }, {CREATE_NORMAL, "onButtonCreateLobbyNormalClicked"}, - {SEND_READY, "sendReadyPacket" } + {SEND_READY, "sendReadyPacket" }, + {GO_CREATE_SERVER, "goCreateServer" }, + {CREATE_SERVER, "createServer" }, + {GO_MENU, "goToMenu" }, + {CHANGE_PARALLAX, "changeParallax" }, + {EXIT_GAME, "exitGame" }, }); void initConnection(); @@ -58,17 +68,37 @@ namespace Menu { void sendReadyPacket(); + void goCreateServer(); + + void goMenu(); + + void createServer(); + + void changeParallax(); + + void exitGame(); + const std::unordered_map> callbacks = { - {CallbackType::DEFAULT_CALLBACK, &defaultCallBack }, - {CallbackType::INIT_CONNECTION, &initConnection }, - {CallbackType::CONNECT, &connectLobbySelected }, - {CallbackType::CREATE, &onButtonGotoCreateLobbyClicked }, - {CallbackType::GO_SELECT_LOBBY, &gotToSelectLobby }, - {CallbackType::GO_BACK, &goBackPage }, - {CallbackType::GO_NEXT, &goNextPage }, - {CallbackType::CREATE_NORMAL, &onButtonCreateLobbyNormalClicked}, - {CallbackType::SEND_READY, &sendReadyPacket } + {CallbackType::DEFAULT_CALLBACK, &defaultCallBack}, + {CallbackType::INIT_CONNECTION, &initConnection}, + {CallbackType::CONNECT, &connectLobbySelected}, + {CallbackType::CREATE, &onButtonGotoCreateLobbyClicked}, + {CallbackType::GO_SELECT_LOBBY, &gotToSelectLobby}, + {CallbackType::GO_BACK, &goBackPage}, + {CallbackType::GO_NEXT, &goNextPage}, + {CallbackType::CREATE_NORMAL, &onButtonCreateLobbyNormalClicked}, + {CallbackType::SEND_READY, &sendReadyPacket}, + {CallbackType::GO_CREATE_SERVER, &goCreateServer}, + {CallbackType::CREATE_SERVER, &createServer}, + {CallbackType::EXIT_GAME, &exitGame}, + { + CallbackType::GO_MENU, + &goMenu, + }, + { + CallbackType::CHANGE_PARALLAX, + &changeParallax, + }, }; - } // namespace Callback } // namespace Menu diff --git a/src/Client/Systems/Menus/Menu/Menu.cpp b/src/Client/Systems/Menus/Menu/Menu.cpp index 23fd2578..2e676237 100644 --- a/src/Client/Systems/Menus/Menu/Menu.cpp +++ b/src/Client/Systems/Menus/Menu/Menu.cpp @@ -128,10 +128,12 @@ namespace Menu { static std::size_t initButtonFromSprite(nlohmann::json &elem, std::function &callback) { - Types::Button button(callback); - std::size_t id = Registry::getInstance().addEntity(); initFromSprite(elem); + std::string name = Json::isDataExist(elem, "name") + ? Json::getInstance().getDataFromJson(elem, "name") + : ""; + Types::Button button(callback, name); Types::Position position = { Types::Position(Json::getInstance().getDataFromJson(elem, "position"))}; Types::CollisionRect collisionRect = {Types::CollisionRect( diff --git a/src/Client/Systems/Menus/Menu/MenuSystems.cpp b/src/Client/Systems/Menus/Menu/MenuSystems.cpp index bbdca0bf..f12f17e0 100644 --- a/src/Client/Systems/Menus/Menu/MenuSystems.cpp +++ b/src/Client/Systems/Menus/Menu/MenuSystems.cpp @@ -188,6 +188,23 @@ namespace Systems { } } + static void initMainTheme() + { + Json &json = Json::getInstance(); + + nlohmann::json jsonData = json.getDataByJsonType( + ResourcesManager::getPathByJsonType(JsonType::MENU), + "mainTheme"); + if (json.isDataExist(jsonData, "path") && json.isDataExist(jsonData, "volume")) { + std::string musicPath = json.getDataFromJson(jsonData, "path"); + float volume = json.getDataFromJson(jsonData, "volume"); + Raylib::MusicShared music = Raylib::Music::fromFile(musicPath, volume); + music->setNeedToPlay(true); + Registry::getInstance().addEntity(); + Registry::getInstance().getComponents().insertBack(music); + } + } + void initMenu(std::size_t managerId, std::size_t systemId) { if (Scene::SceneManager::getInstance().getCurrentScene() != MENU) { @@ -195,7 +212,9 @@ namespace Systems { return; } try { + initMainTheme(); Parallax::initParalax(); + Logger::info("Init Parallax"); nlohmann::json jsonData = Json::getInstance().getDataByJsonType( ResourcesManager::getPathByJsonType(JsonType::MENU), "menu"); @@ -216,6 +235,7 @@ namespace Systems { Scene::SceneManager::getInstance().changeScene(SELECT_LOBBY); break; case SELECT_LOBBY: Scene::SceneManager::getInstance().changeScene(MENU); break; + case CREATE_SERVER_SCENE: Scene::SceneManager::getInstance().changeScene(MENU); break; case GAME: Nitwork::NitworkClient::getInstance().disconnectLobby(); Types::WaveInfos::getInstance().reset(); @@ -241,6 +261,8 @@ namespace Systems { std::vector textures = Json::getInstance().getDatasByKey( {ResourcesManager::getPathByJsonType(JsonType::ENEMIES), ResourcesManager::getPathByJsonType(JsonType::DEFAULT_PARALLAX), + ResourcesManager::getPathByJsonType(JsonType::MENU), + ResourcesManager::getPathByJsonType(JsonType::PARALLAX_2), ResourcesManager::getPathByJsonType(JsonType::BULLETS), ResourcesManager::getPathByJsonType(JsonType::DEFAULT_PLAYER)}, "spritePath"); @@ -295,6 +317,23 @@ namespace Systems { } } + void resetParallaxTextForParallax(std::size_t /*unused*/, std::size_t /*unused*/) + { + auto ids = Registry::getInstance().getEntitiesByComponents({typeid(Raylib::TextShared)}); + auto arrText = Registry::getInstance().getComponents(); + + for (auto id : ids) { + if (arrText[id]->getCurrentText().find("PARALLAX") != std::string::npos) { + std::string paraName = + "PARALLAX_" + + std::to_string( + Systems::Parallax::ActualParallax::getInstance()._actualParallaxNbr); + + arrText[id]->setCurrentText(paraName); + } + } + } + void initTextures(std::size_t managerId, std::size_t systemId) { static int step = 0; @@ -347,7 +386,8 @@ namespace Systems { checkTextInput, checkInputDeletion, initSceneGame, - quitScene}; + quitScene, + resetParallaxTextForParallax}; } } // namespace Menu } // namespace Systems diff --git a/src/Client/Systems/Network/ClientNetwork.cpp b/src/Client/Systems/Network/ClientNetwork.cpp index 0595a7a3..2e06cbab 100644 --- a/src/Client/Systems/Network/ClientNetwork.cpp +++ b/src/Client/Systems/Network/ClientNetwork.cpp @@ -388,17 +388,6 @@ namespace Systems { } } - void quitGame(std::size_t /* unused */, std::size_t /* unused */) - { - if (Raylib::KeyboardInput::isKeyPressed(Raylib::KeyboardKey::KB_ESCAPE)) { - if (Scene::SceneManager::getInstance().getCurrentScene() == static_cast(GAME)) { - Nitwork::NitworkClient::getInstance().disconnectLobby(); - Types::WaveInfos::getInstance().reset(); - Scene::SceneManager::getInstance().changeScene(static_cast(SELECT_LOBBY)); - } - } - } - void receiveEndGame(std::any & /*unused*/, boost::asio::ip::udp::endpoint & /*unused*/) { auto &director = SystemManagersDirector::getInstance(); @@ -409,6 +398,6 @@ namespace Systems { std::vector> getNetworkSystems() { - return {quitGame, sendInitPacket, sendPositionRelative, sendPositionAbsolute}; + return {sendInitPacket, sendPositionRelative, sendPositionAbsolute}; } } // namespace Systems diff --git a/src/Client/Systems/Parallax/Parallax.cpp b/src/Client/Systems/Parallax/Parallax.cpp index b04e9422..20b63f2c 100644 --- a/src/Client/Systems/Parallax/Parallax.cpp +++ b/src/Client/Systems/Parallax/Parallax.cpp @@ -1,10 +1,12 @@ +#include "Parallax.hpp" #include #include "B-luga-graphics/GraphicsSystems.hpp" #include "B-luga/Json.hpp" -#include "ResourcesManager.hpp" namespace Systems::Parallax { + std::size_t ActualParallax::_actualParallaxNbr = 1; + static void initParallaxEntity(nlohmann::basic_json<> &elem, const int maxOffsideParallax = 0) { std::size_t id = Registry::getInstance().addEntity(); @@ -42,7 +44,8 @@ namespace Systems::Parallax { { std::lock_guard lock(Registry::getInstance().mutex); std::vector> parallaxData = Json::getInstance().getDataByJsonType( - ResourcesManager::getPathByJsonType(JsonType::DEFAULT_PARALLAX), + ResourcesManager::getPathByJsonType( + Parallax::ActualParallax::getInstance().getActualParallaxType()), "parallax"); for (auto &elem : parallaxData) { diff --git a/src/Client/Systems/Parallax/Parallax.hpp b/src/Client/Systems/Parallax/Parallax.hpp index 7a6e80b7..0e71b230 100644 --- a/src/Client/Systems/Parallax/Parallax.hpp +++ b/src/Client/Systems/Parallax/Parallax.hpp @@ -1,5 +1,37 @@ #pragma once +#include "ResourcesManager.hpp" + namespace Systems::Parallax { + class ActualParallax { + public: + static ActualParallax &getInstance() + { + static auto instance = ActualParallax(); + return instance; + } + + JsonType getActualParallaxType() + { + return _actualParallax; + } + + void setActualParralaxType(JsonType data) + { + _actualParallax = data; + } + + static std::size_t _actualParallaxNbr; + + private: + ActualParallax() + { + _actualParallax = JsonType::DEFAULT_PARALLAX; + } + ~ActualParallax() = default; + + JsonType _actualParallax; + }; + void initParalax(); } // namespace Systems::Parallax \ No newline at end of file diff --git a/src/Game/ResourcesManager.hpp b/src/Game/ResourcesManager.hpp index 0c7558b9..6e3ef564 100644 --- a/src/Game/ResourcesManager.hpp +++ b/src/Game/ResourcesManager.hpp @@ -10,6 +10,7 @@ enum class JsonType : std::size_t { DEFAULT_PLAYER = 0, DEFAULT_PARALLAX, + PARALLAX_2, WAVE, BULLETS, MENU, @@ -17,13 +18,15 @@ enum class JsonType : std::size_t { CREATE_LOBBY, HUD, ENEMIES, - MAXTYPE + CREATE_SERVER, + MAXTYPE, }; namespace ResourcesManagerValues { const std::vector paths = { "assets/Json/playerData.json", "assets/Json/parallaxData.json", + "assets/Json/parallaxData2.json", "assets/Json/waves.json", "assets/Json/bullets.json", "assets/Json/menu.json", @@ -31,6 +34,7 @@ namespace ResourcesManagerValues { "assets/Json/createLobby.json", "assets/Json/HUD.json", "assets/Json/enemies.json", + "assets/Json/createServer.json", }; } diff --git a/src/Nitwork/NitworkClient.cpp b/src/Nitwork/NitworkClient.cpp index 2e7c1724..c76b7939 100644 --- a/src/Nitwork/NitworkClient.cpp +++ b/src/Nitwork/NitworkClient.cpp @@ -7,10 +7,13 @@ #if defined(_WIN32) #define _CRT_SECURE_NO_WARNINGS + #include +#else #endif -#include "NitworkClient.hpp" #include "B-luga/Logger.hpp" #include "B-luga/Registry.hpp" +#include "NitworkClient.hpp" +#include "ResourcesManager.hpp" namespace Nitwork { NitworkClient::NitworkClient() : _resolver(_context) @@ -418,6 +421,83 @@ namespace Nitwork { addPacketToSend(packet); } + bool NitworkClient::serverAlreadyCreated() + { + if (_serverPids.size() > 0) { + return true; + } + return (false); + } + + void NitworkClient::createForkedServer(const std::string &port) + { + if (_serverPids.size() > 0) { + return; + } +#ifdef _WIN32 + std::basic_ostringstream cmdline; + cmdline << _T(ResourcesManager::convertPath("./r-type_server.exe").c_str()) << _T(" 0 ") + << _T(" ") << _T(port); + + Logger::fatal("cmdline: " + cmdline.str()); + STARTUPINFO si = {sizeof(si)}; + PROCESS_INFORMATION pi; + #ifdef UNICODE + TCHAR *cmd = _wcsdup(cmdline.str().c_str()); + #else + TCHAR *cmd = _strdup(cmdline.str().c_str()); + #endif + + if (!CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) { + Logger::error("Error: CreateProcess failed"); + free(cmd); + return; + } + free(cmd); + _serverPids.emplace_back(pi.dwProcessId); +#else + pid_t c_pid = fork(); + + if (c_pid == -1) { + Logger::error("Error: fork failed"); + return; + } + if (c_pid == 0) { + if (execl( + ResourcesManager::convertPath("r-type_server").c_str(), + ResourcesManager::convertPath("r-type_server").c_str(), + "0", + port.c_str(), + NULL) + == -1) { + Logger::error("Error: execl failed"); + return; + } + Logger::info("Server created"); + } else { + _serverPids.emplace_back(c_pid); + } +#endif + } + + void NitworkClient::stop() + { +#ifdef _WIN32 + for (auto pid : _serverPids) { + HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid); + if (hProcess != NULL) { + TerminateProcess(hProcess, 0); + CloseHandle(hProcess); + } + } +#else + for (auto pid : _serverPids) { + kill(pid, SIGINT); + } +#endif + ANitwork::stop(); + } + void NitworkClient::addConnectLobbyMsg() { std::lock_guard lock(_receivedPacketsIdsMutex); diff --git a/src/Nitwork/NitworkClient.hpp b/src/Nitwork/NitworkClient.hpp index 268bc6ee..d6c02af4 100644 --- a/src/Nitwork/NitworkClient.hpp +++ b/src/Nitwork/NitworkClient.hpp @@ -117,6 +117,22 @@ namespace Nitwork { void addCreateLobbyMsg(const std::string &name, enum gameType_e gameType, unsigned int maxNbPlayer); + /** + * @brief Check if the server is created by the client + */ + bool serverAlreadyCreated(); + + /** + * @brief Create a server + * @param port port of the server + */ + void createForkedServer(const std::string &port); + + /** + * @brief Stop the server + */ + void stop() final; + /* Private connection methods */ private: @@ -217,6 +233,8 @@ namespace Nitwork { */ boost::asio::ip::udp::endpoint _serverEndpoint; + std::vector _serverPids; // pid of the servers + // clang-format off /** * @brief Map that will be used to handle the actions, in order to send or receive them diff --git a/src/Nitwork/NitworkMainServer.cpp b/src/Nitwork/NitworkMainServer.cpp index e3c08513..1ea95a98 100644 --- a/src/Nitwork/NitworkMainServer.cpp +++ b/src/Nitwork/NitworkMainServer.cpp @@ -197,7 +197,6 @@ namespace Nitwork { << _T(maxNbPlayer) << _T(" ") << _T(gameType) << _T(" ") << _T(winName.c_str()) << _T(" ") << _T(ownerIp.c_str()) << _T(" ") << _T(ownerPort); - Logger::fatal("cmdline: " + cmdline.str()); STARTUPINFO si = {sizeof(si)}; PROCESS_INFORMATION pi; #ifdef UNICODE diff --git a/src/Nitwork/NitworkServer.cpp b/src/Nitwork/NitworkServer.cpp index 60416e55..655500bd 100644 --- a/src/Nitwork/NitworkServer.cpp +++ b/src/Nitwork/NitworkServer.cpp @@ -267,7 +267,7 @@ namespace Nitwork { auto &director = Systems::SystemManagersDirector::getInstance(); std::lock_guard lock(director.mutex); director.getSystemManager(static_cast(SystemManagers::GAME_LOGIC)) - .addSystem(Systems::waveHandler); // le sheeiiiiiitan VERIF + .addSystem(Systems::waveHandler); } void diff --git a/src/init.cpp b/src/init.cpp index 62de4a3f..cc1b914c 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -9,6 +9,7 @@ #include "B-luga-graphics/GraphicsSystems.hpp" #include "ClientNetwork.hpp" #include "CreateLobbySystems.hpp" + #include "CreateServerSystems.hpp" #include "EventsSystems.hpp" #include "MenuSystems.hpp" #include "SelectLobbySystems.hpp" @@ -39,11 +40,12 @@ void initScenes() std::map>()>> systems = { #ifdef CLIENT - {SystemManagers::EVENTS, &Systems::EventsSystems::getEventsSystems }, - {SystemManagers::CLIENTNETWORK, &Systems::getNetworkSystems }, - {SystemManagers::MENU_SYSTEMS, &Systems::Menu::getMenuSystems }, - {SystemManagers::NETWORK_MENU, &Systems::SelectLobbySystems::getLobbySystems}, - {SystemManagers::NETWORK_CREATE_LOBBY, &Systems::CreateLobby::getCreateLobbySystems }, + {SystemManagers::EVENTS, &Systems::EventsSystems::getEventsSystems }, + {SystemManagers::CLIENTNETWORK, &Systems::getNetworkSystems }, + {SystemManagers::MENU_SYSTEMS, &Systems::Menu::getMenuSystems }, + {SystemManagers::NETWORK_MENU, &Systems::SelectLobbySystems::getLobbySystems }, + {SystemManagers::NETWORK_CREATE_LOBBY, &Systems::CreateLobby::getCreateLobbySystems }, + {SystemManagers::NETWORK_CREATE_SERVER, &Systems::CreateServerSystems::getCreateServerSystems}, #endif }; @@ -56,11 +58,18 @@ void initScenes() {SystemManagers::GRAPHICS, SystemManagers::MENU_SYSTEMS, SystemManagers::ECSSYSTEMS}); auto menu = enumListTosizet( {SystemManagers::GRAPHICS, SystemManagers::MENU_SYSTEMS, SystemManagers::ECSSYSTEMS}); - auto selectLobby = enumListTosizet( + auto createServer = enumListTosizet({ + SystemManagers::MENU_SYSTEMS, + SystemManagers::GRAPHICS, + SystemManagers::NETWORK_CREATE_SERVER, + SystemManagers::ECSSYSTEMS, + SystemManagers::CLIENTNETWORK, + }); + auto selectLobby = enumListTosizet( {SystemManagers::MENU_SYSTEMS, - SystemManagers::NETWORK_MENU, - SystemManagers::ECSSYSTEMS, - SystemManagers::GRAPHICS}); + SystemManagers::NETWORK_MENU, + SystemManagers::ECSSYSTEMS, + SystemManagers::GRAPHICS}); auto createLobby = enumListTosizet( {SystemManagers::MENU_SYSTEMS, SystemManagers::NETWORK_CREATE_LOBBY, @@ -73,7 +82,7 @@ void initScenes() SystemManagers::CLIENTNETWORK, SystemManagers::GRAPHICS, SystemManagers::MENU_SYSTEMS}); - sceneManager.setScenes({loadingScreen, menu, selectLobby, createLobby, mainScene}); + sceneManager.setScenes({loadingScreen, menu, createServer, selectLobby, createLobby, mainScene}); #else auto scene = enumListTosizet({SystemManagers::ECSSYSTEMS, SystemManagers::GAME_LOGIC}); diff --git a/src/init.hpp b/src/init.hpp index d9d06c5f..6031ae2a 100644 --- a/src/init.hpp +++ b/src/init.hpp @@ -8,9 +8,10 @@ enum SystemManagers { GRAPHICS, MENU_SYSTEMS, NETWORK_MENU, - NETWORK_CREATE_LOBBY + NETWORK_CREATE_LOBBY, + NETWORK_CREATE_SERVER }; -enum Scenes { LOADING_SCREEN, MENU, SELECT_LOBBY, CREATE_LOBBY_SCENE, GAME }; +enum Scenes { LOADING_SCREEN, MENU, CREATE_SERVER_SCENE, SELECT_LOBBY, CREATE_LOBBY_SCENE, GAME }; void initScenes();