diff --git a/client/include/client.hpp b/client/include/client.hpp index ffd3cc3..c650242 100644 --- a/client/include/client.hpp +++ b/client/include/client.hpp @@ -136,22 +136,4 @@ void refresh_bg(registry const &, sparse_array &); namespace comp { - void new_missile(registry &, entity &, Vec &, std::queue &); - void new_enemy_missile(registry &, entity &, Vec &, std::queue &); - void new_static_enemy_missile(registry &, entity &, Vec &, std::queue &); - void new_player(registry &, entity &, const info &, std::queue &); - void new_enemy(registry &, entity &, Vec &, std::queue &); - void new_static_enemy(registry &, entity &, Vec &, std::queue &); - void new_you(registry &, entity &, const info &, std::queue &); - void new_window(registry &, size_t &, window &); - void setup_lobby(registry &, window &); - void waiting_setup_entity(registry &, window &, size_t &); - void end_setup_entity(registry &, window &); - void connect_setup_entity(registry &, window &); - PlayerAction get_action_from_key(Key); - RectButton createDefaultButton(const std::string &, const std::string &, - const sf::Vector2f &, const sf::Vector2f &); - - void update_player_controls(registry &, size_t, const KeyBindings &); - std::string get_key_name(sf::Keyboard::Key); } diff --git a/client/include/game_entities.hpp b/client/include/game_entities.hpp new file mode 100644 index 0000000..0b64009 --- /dev/null +++ b/client/include/game_entities.hpp @@ -0,0 +1,27 @@ +/* +** EPITECH PROJECT, 2025 +** R-type +** File description: +** game_entities +*/ + +#pragma once + +#include "registry.hpp" +#include "entity.hpp" +#include "client.hpp" +#include + +namespace comp { + void new_missile(registry ®, entity &enti, Vec &pos, std::queue &queue); + void new_enemy_missile(registry ®, entity &entity, Vec &pos, std::queue &queue); + void new_static_enemy_missile(registry ®, entity &entity, Vec &pos, std::queue &queue); + + void new_player(registry ®, entity &entity, const info &info, std::queue &queue); + void new_you(registry ®, entity &entity, const info &info, std::queue &queue); + + void new_enemy(registry ®, entity &entity, Vec &pos, std::queue &queue); + void new_static_enemy(registry ®, entity &entity, Vec &pos, std::queue &queue); + + void new_window(registry ®, size_t &scoreValue, comp::window &window); +} diff --git a/client/include/game_loop.hpp b/client/include/game_loop.hpp new file mode 100644 index 0000000..9f129d8 --- /dev/null +++ b/client/include/game_loop.hpp @@ -0,0 +1,16 @@ +/* +** EPITECH PROJECT, 2025 +** R-type +** File description: +** game_loop +*/ + +#pragma once + +#include "registry.hpp" +#include "args.hpp" +#include + +namespace comp { + void main_loop(std::array ®s, registry &settings, size_t &scoreValue, const args &ClientArgs); +} diff --git a/client/include/key_bindings.hpp b/client/include/key_bindings.hpp new file mode 100644 index 0000000..e33c773 --- /dev/null +++ b/client/include/key_bindings.hpp @@ -0,0 +1,28 @@ +/* +** EPITECH PROJECT, 2025 +** R-type +** File description: +** key_bindings +*/ + +#pragma once + +#include "registry.hpp" +#include "client.hpp" +#include "components/basics.hpp" +#include +#include +#include + +namespace comp { + std::string get_key_name(sf::Keyboard::Key key); + + PlayerAction get_action_from_key(Key key); + + void start_rebinding(registry ®, window &, size_t &button_id, PlayerAction action); + void bind_up(registry ®, window &win, size_t &button_id); + void bind_down(registry ®, window &win, size_t &button_id); + void bind_left(registry ®, window &win, size_t &button_id); + void bind_right(registry ®, window &win, size_t &button_id); + void bind_shoot(registry ®, window &win, size_t &button_id); +} diff --git a/client/include/movement_controls.hpp b/client/include/movement_controls.hpp new file mode 100644 index 0000000..c033216 --- /dev/null +++ b/client/include/movement_controls.hpp @@ -0,0 +1,24 @@ +/* +** EPITECH PROJECT, 2025 +** R-type +** File description: +** movement_controls +*/ + +#pragma once + +#include "registry.hpp" +#include "client.hpp" +#include "components/basics.hpp" +#include "iterator.hpp" + +namespace comp { + void move_left(registry &r, size_t id); + void move_right(registry &r, size_t id); + void move_up(registry &r, size_t id); + void move_down(registry &r, size_t id); + + void tab_press(registry &r, size_t id); + + void update_player_controls(registry ®, size_t player_idx, const KeyBindings &bindings); +} diff --git a/client/include/state_setup.hpp b/client/include/state_setup.hpp new file mode 100644 index 0000000..bef05fa --- /dev/null +++ b/client/include/state_setup.hpp @@ -0,0 +1,29 @@ +/* +** EPITECH PROJECT, 2025 +** R-type +** File description: +** state_setup +*/ + +#pragma once + +#include "registry.hpp" +#include "client.hpp" +#include "args.hpp" +#include + +namespace comp { + void setup_lobby(registry ®, window &Mainwindow); + void connect_setup_entity(registry ®, window &Mainwindow); + void waiting_setup_entity(registry ®, window &MainWindow, size_t &nb_player); + void end_setup_entity(registry ®, window &MainWindow); + + void setup_settings(registry &settings, window &Mainwindow); + void waiting_setup(registry ®, window &Mainwindow, size_t &nb_player); + void setup_connect(registry ®, window &Mainwindow); + void lobby_game(registry ®, window &Mainwindow); + void main_game(registry ®, window &Mainwindow, size_t &scoreValue); + void setup_end(registry ®, window &MainWindow); + + void main_loop(std::array ®s, registry &settings, size_t &scoreValue, const args &ClientArgs); +} diff --git a/client/include/ui_entities.hpp b/client/include/ui_entities.hpp new file mode 100644 index 0000000..74392ce --- /dev/null +++ b/client/include/ui_entities.hpp @@ -0,0 +1,33 @@ +/* +** EPITECH PROJECT, 2025 +** R-type +** File description: +** ui_entities +*/ + +#pragma once + +#include "registry.hpp" +#include "client.hpp" +#include "button/RectButton.hpp" +#include "text_field/RectTextField.hpp" +#include + +namespace comp { + RectButton createDefaultButton(const std::string &font, const std::string &label, + const sf::Vector2f &size, const sf::Vector2f &position); + + RectTextField createDefaultField(const std::string &font, const std::string &label, + const sf::Vector2f &size, const sf::Vector2f &pos); + + float get_centerx(float coord, window &Mainwindow); + float get_centery(float coord, window &Mainwindow); + + void pressed_button_action(registry ®, window &, size_t &i); + void enableSettingsButtons(registry ®, window &, size_t &); + void volume_up(registry &, window &, size_t &); + void volume_down(registry &, window &, size_t &); + + void setup_settings_button(registry ®, window &Mainwindow); + void set_bg(registry ®, window &MainWindow); +} diff --git a/client/src/create_entities.cpp b/client/src/create_entities.cpp deleted file mode 100644 index 539bcd6..0000000 --- a/client/src/create_entities.cpp +++ /dev/null @@ -1,753 +0,0 @@ -/* -** EPITECH PROJECT, 2025 -** R-type -** File description: -** client -*/ - -#include "client.hpp" -#include "components/graphics.hpp" -#include "entity.hpp" -#include "iterator.hpp" -#include "components/physics.hpp" -#include "registry.hpp" -#include "settings.hpp" -#include "text_field/RectTextField.hpp" -#include -#include -#include - -#include "button/RectButton.hpp" -#include "registry.hpp" -#include -#include -#include -#include "ecs.hpp" -#include "entity.hpp" -#include "components/basics.hpp" -#include "components/graphics.hpp" - -static assets _assets; -KeyBindings g_key_bindings; -float g_audio_volume = 50.0f; - -template -static auto add(Extra &&...extra, const auto &&f) { - return [&f, ...extra = std::forward(extra)] - (auto &&...args) mutable { f(args..., extra...); }; -} - -namespace comp { - static void move_left(registry &r, size_t id) { - r.get_components()[id]->x -= 2.5f; - } - - static void move_right(registry &r, size_t id) { - r.get_components()[id]->x += 2.5f; - } - - static void move_up(registry &r, size_t id) { - r.get_components()[id]->y -= 2.5f; - } - - static void move_down(registry &r, size_t id) { - r.get_components()[id]->y += 2.5f; - } - - static void tab_press(registry &r, size_t id) { - auto &tab_clock(r.get_components()[id]); - - if (tab_clock->paused() or tab_clock->elapsed_time() <= tab_clock->time()) - return; - tab_clock->restart(); - auto &texts(r.get_components()); - - for (auto &&[text]: zipper(texts)) text.visible = !text.visible; - } - - void new_missile(registry ®, entity &enti, Vec &pos, std::queue &queue) { - reg.add_components(enti, - velocity(3.0f, .0f), - position(pos.x, pos.y), - sprite(_assets.safe_get("assets/player_missile.png"), {64, 32}, {0, 0}), - sound(_assets.safe_get("assets/lazer-gun-432285.ogg")), - animation({{64, 32}, {0, 0}, {0, 32}}, 0, 8), - clock(0.05, true, { - { comp::clock_signals::ended, add(enti, animation::next_frame) } - }), - network(queue), - enabled()); - - auto &s = reg.get_components()[enti]; - s->setPlayingOffset(sf::seconds(.15f)); - s->setVolume(g_audio_volume); - s->play(); - } - - void new_enemy_missile(registry ®, entity &entity, Vec &pos, std::queue &queue) { - reg.add_components(entity, - velocity(-2.0f, .0f), - position(pos.x, pos.y), - sprite(_assets.safe_get("assets/r-typesheet37.gif"), {25, 30}, {153, 995}), - network(queue), - enabled()); - } - - void new_static_enemy_missile(registry ®, entity &entity, Vec &pos, std::queue &queue) { - sprite missile(_assets.safe_get("assets/r-typesheet31.gif"), {14, 14}, {165, 201}); - - missile.setScale({2.0f, 2.0f}); - reg.add_components(entity, - velocity(-2.0f, .0f), - position(pos.x, pos.y), - missile, - network(queue), - enabled()); - } - - void new_player(registry ®, entity &entity, const info &info, std::queue &queue) { - std::string asset("assets/r-typesheetShip" + std::to_string(info.color) + ".gif"); - sprite player(_assets.safe_get(asset), {21, 24}, {507, 6}); - player.setScale({2.0f, 2.0f}); - std::string player_str(info.name); - animation animation({{21, 24}, {507, 6}, {-33, 0}}, 0, 8); - - reg.add_components(entity, - position(info.pos.x, info.pos.y), - velocity(.0f, .0f), - hittable({30.0f, 40.0f}, {4.0f, 4.0f}, {{ hittable_signals::hit, - add(0, [](registry &, hittable &, size_t, size_t) { - // std::cout << "Collide!!!" << std::endl; - }) }}), - player, - animation, - clock(0.2, true, { - { comp::clock_signals::ended, add(entity, animation::next_frame) } - }), - text(player_str, _assets.safe_get("assets/arial_bold.ttf"), 18, {-int(player_str.size() * 4), -23}), - network(queue), - enabled()); - } - - void new_enemy(registry ®, entity &entity, Vec &pos, std::queue &queue) { - sprite enemy(_assets.safe_get("assets/r-typesheet37.gif"), {100, 32}, {300, 1189}); - enemy.setScale({2.0f, 2.0f}); - animation animation({{100, 32}, {298, 1087}, {0, 34}}, 0, 4); - - reg.add_components(entity, - position(pos.x, pos.y), - velocity(-1.0f, .0f), - hittable({30.0f, 40.0f}, {4.0f, 4.0f}, {{ hittable_signals::hit, - [](registry &, hittable &, size_t) { - // std::cout << "Collide!!!" << std::endl; - } }}), - enemy, - animation, - clock(0.8, true, { - { comp::clock_signals::ended, add(entity, animation::next_frame) } - }), - network(queue), - enabled()); - } - - void new_static_enemy(registry ®, entity &entity, Vec &pos, std::queue &queue) { - sprite enemy(_assets.safe_get("assets/r-typesheet31.gif"), {34, 28}, {0, 20}); - enemy.setScale({2.0f, 2.0f}); - - reg.add_components(entity, - position(pos.x, pos.y), - velocity(0.0f, .0f), - hittable({30.0f, 40.0f}, {4.0f, 4.0f}, {{ hittable_signals::hit, - [](registry &, hittable &, size_t) { - // std::cout << "Collide!!!" << std::endl; - } }}), - enemy, - network(queue), - enabled()); - } - - void new_you(registry ®, entity &entity, const info &info, std::queue &queue) { - std::string asset("assets/r-typesheetShip" + std::to_string(info.color) + ".gif"); - sprite player_s(_assets.safe_get(asset), {21, 24}, {507, 6}); - player_s.setScale({2.0f, 2.0f}); - std::string player_str(info.name); - animation animation({{21, 24}, {507, 6}, {-33, 0}}, 0, 8); - - reg.add_components(entity, - position(info.pos.x, info.pos.y), - velocity(.0f, .0f), - hittable({30.0f, 40.0f}, {4.0f, 4.0f}, {{ hittable_signals::hit, - [](registry &, hittable &, size_t) { - // std::cout << "Collide!!!" << std::endl; - } }}), - player_s, - animation, - clock(0.2, true, { - { comp::clock_signals::ended, add(entity, animation::next_frame) } - }), - player(), - text(player_str, _assets.safe_get("assets/arial_bold.ttf"), 18, {-int(player_str.size() * 4), -23}), - controllable({ - { g_key_bindings.keys[MOVE_LEFT], add(entity, move_left) }, - { g_key_bindings.keys[MOVE_RIGHT], add(entity, move_right) }, - { g_key_bindings.keys[MOVE_UP], add(entity, move_up) }, - { g_key_bindings.keys[MOVE_DOWN], add(entity, move_down) } - }), - network(queue), - enabled()); - } - - void new_window(registry ®, size_t &scoreValue, window &window) { - entity enti(reg.spawn_entity()); - entity entityScore(reg.spawn_entity()); - sprite bg(_assets.safe_get("assets/background.png"), {1134, 207}); - bg.setScale({5.2f, 5.2f}); - - std::string scoreText = "0"; - - reg.add_components(entityScore, - position(1850, 0), - text("0", _assets.safe_get("assets/arial_bold.ttf"), 18, {0, 0}), - score(scoreValue), - enabled() - ); - reg.add_components(enti, - position(.0f, .0f), - velocity(-1.0f, .0f), - clock(.3f), - window, - controllable({ - { controllable_signals::Tab, add(enti, tab_press) } - }), - bg, - enabled()); - } - - RectButton createDefaultButton(const std::string &font, const std::string &label, - const sf::Vector2f &size, const sf::Vector2f &position) { - RectButton button(size, position, _assets.safe_get(font)); - button.setFont(_assets.safe_get(font)); - button.setLabel(label, 24, sf::Color::Black); - button.setColors( - sf::Color::White, - sf::Color(220,220,220), - sf::Color(180,180,180) - ); - return button; - } - - RectTextField createDefaultField(const std::string &font, const std::string &label, - const sf::Vector2f &size, const sf::Vector2f &pos) { - RectTextField addr(_assets.safe_get(font), size, pos); - - addr.setTextSize(20); - addr.setPlaceholder(label); - addr.setMaxLength(16); - addr.setTextColor(sf::Color::Black); - return addr; - } - - void func1(registry &, window &window, size_t &) { - window->setFramerateLimit(30); - std::cout << "Settings 1 called!" << std::endl; - } - - void func2(registry &, window &window, size_t &) { - window->setFramerateLimit(60); - std::cout << "Settings 2 called!" << std::endl; - } - - void func3(registry &, window &window, size_t &) { - window->setFramerateLimit(120); - std::cout << "Settings 3 called!" << std::endl; - } - - std::string get_key_name(sf::Keyboard::Key key) { - static const std::unordered_map key_names = { - {sf::Keyboard::Key::A, "A"}, {sf::Keyboard::Key::B, "B"}, - {sf::Keyboard::Key::C, "C"}, {sf::Keyboard::Key::D, "D"}, - {sf::Keyboard::Key::E, "E"}, {sf::Keyboard::Key::F, "F"}, - {sf::Keyboard::Key::G, "G"}, {sf::Keyboard::Key::H, "H"}, - {sf::Keyboard::Key::I, "I"}, {sf::Keyboard::Key::J, "J"}, - {sf::Keyboard::Key::K, "K"}, {sf::Keyboard::Key::L, "L"}, - {sf::Keyboard::Key::M, "M"}, {sf::Keyboard::Key::N, "N"}, - {sf::Keyboard::Key::O, "O"}, {sf::Keyboard::Key::P, "P"}, - {sf::Keyboard::Key::Q, "Q"}, {sf::Keyboard::Key::R, "R"}, - {sf::Keyboard::Key::S, "S"}, {sf::Keyboard::Key::T, "T"}, - {sf::Keyboard::Key::U, "U"}, {sf::Keyboard::Key::V, "V"}, - {sf::Keyboard::Key::W, "W"}, {sf::Keyboard::Key::X, "X"}, - {sf::Keyboard::Key::Y, "Y"}, {sf::Keyboard::Key::Z, "Z"}, - {sf::Keyboard::Key::Up, "UP"}, {sf::Keyboard::Key::Down, "DOWN"}, - {sf::Keyboard::Key::Left, "LEFT"}, {sf::Keyboard::Key::Right, "RIGHT"}, - {sf::Keyboard::Key::Space, "SPACE"}, {sf::Keyboard::Key::Enter, "ENTER"}, - }; - auto it = key_names.find(key); - return it != key_names.end() ? it->second : "?"; - } - - PlayerAction get_action_from_key(Key key) { - for (size_t i = 0; i != ACTION_COUNT; ++i) - if (g_key_bindings.keys[i] == key) - return PlayerAction(i); - return ACTION_COUNT; - } - - void update_player_controls(registry ®, size_t player_idx, const KeyBindings &bindings) { - auto &controllables = reg.get_components(); - if (controllables.size() > player_idx && controllables[player_idx].has_value()) { - controllables[player_idx]->set_signals({ - {bindings.keys[MOVE_LEFT], [player_idx](registry &r) { move_left(r, player_idx); }}, - {bindings.keys[MOVE_RIGHT], [player_idx](registry &r) { move_right(r, player_idx); }}, - {bindings.keys[MOVE_UP], [player_idx](registry &r) { move_up(r, player_idx); }}, - {bindings.keys[MOVE_DOWN], [player_idx](registry &r) { move_down(r, player_idx); }} - }); - } - } - - void start_rebinding(registry ®, window &, size_t &button_id, PlayerAction action) { - auto &rebinding = reg.get_components(); - - for (auto &&[i, state, _]: indexed_zipper(rebinding, reg.get_components())) { - state.waiting_for_input = true; - state.action_to_rebind = action; - state.button_entity_id = button_id; - - auto &buttons = reg.get_components