diff --git a/src/Client/Systems/ClientSystems.cpp b/src/Client/Systems/ClientSystems.cpp index 91357a1a..1359b434 100644 --- a/src/Client/Systems/ClientSystems.cpp +++ b/src/Client/Systems/ClientSystems.cpp @@ -8,10 +8,11 @@ #include "ClientSystems.hpp" #include #include "CustomTypes.hpp" -#include "Registry.hpp" #include "Raylib.hpp" +#include "Registry.hpp" namespace Systems { + void GraphicSystems::rectRenderer(std::size_t /*unused*/) { Registry::components arrPosition = @@ -29,17 +30,19 @@ namespace Systems { Types::Position &position = positionIt->value(); Types::RectangleShape &rectangle = rectIt->value(); - float x = (position.x * static_cast(Raylib::getScreenWidth())) - / denominator; - float y = (position.y * static_cast(Raylib::getScreenHeight())) - / denominator; + float x = + (position.x * static_cast(Raylib::getScreenWidth())) + / denominator; + float y = + (position.y * static_cast(Raylib::getScreenHeight())) + / denominator; - float width = - (rectangle.width * static_cast(Raylib::getScreenWidth())) - / denominator; - float height = - (rectangle.height * static_cast(Raylib::getScreenHeight())) - / denominator; + float width = (rectangle.width + * static_cast(Raylib::getScreenWidth())) + / denominator; + float height = (rectangle.height + * static_cast(Raylib::getScreenHeight())) + / denominator; DrawRectangle( static_cast(x), @@ -53,65 +56,85 @@ namespace Systems { } } + void EventsSystems::playerMovement(std::size_t /*unused*/) + { + Registry::components arrPosition = + Registry::getInstance().getComponents(); + + Registry::components arrPlayer = + Registry::getInstance().getComponents(); + + auto positionIt = arrPosition.begin(); + auto playerIt = arrPlayer.begin(); + + while (positionIt != arrPosition.end() && playerIt != arrPlayer.end()) { + if (playerIt->has_value() && positionIt->has_value() + && playerIt->value().isMine) { + if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_RIGHT)) { + positionIt->value().x += 1; + } + if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_LEFT)) { + positionIt->value().x -= 1; + } + if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_UP)) { + positionIt->value().y -= 1; + } + if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_DOWN)) { + positionIt->value().y += 1; + } + } + positionIt++; + playerIt++; + } + } + static void - drawSpriteWithoutRect(Types::Position &position, Types::Sprite &sprite) + drawSpriteWithoutRect(Types::Position &position, Raylib::Sprite &sprite) { - float scale = 1.0F; - float rotation = 0; - auto tint = WHITE; - Vector2 spritePos = {0, 0}; - const float denominator = 100.0; + float scale = 1.0F; + float rotation = 0; + Raylib::Color tint = Raylib::White; + Raylib::Vector2 spritePos = {0, 0}; + const float denominator = 100.0; - float x = - (position.x * static_cast(GetScreenWidth())) / denominator; - float y = - (position.y * static_cast(GetScreenHeight())) / denominator; + float x = (position.x * static_cast(Raylib::getScreenWidth())) + / denominator; + float y = (position.y * static_cast(Raylib::getScreenHeight())) + / denominator; - scale = (sprite.width * static_cast(GetScreenWidth())) - / denominator / static_cast(sprite.sprite.width); + scale = + (sprite.getWidth() * static_cast(Raylib::getScreenWidth())) + / denominator / static_cast(sprite.getTextureWidth()); spritePos = {x, y}; - while (positionIt != arrPosition.end() && playerIt != arrPlayer.end()) { - if (playerIt->has_value() && positionIt->has_value() - && playerIt->value().isMine) { - if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_RIGHT)) { - positionIt->value().x += 1; - } - if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_LEFT)) { - positionIt->value().x -= 1; - } - if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_UP)) { - positionIt->value().y -= 1; - } - if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_DOWN)) { - positionIt->value().y += 1; - DrawTextureEx(sprite.sprite, spritePos, rotation, scale, tint); + sprite.drawEx(spritePos, rotation, scale, tint); } static void drawSpriteWithRect( Types::Position &position, - Types::Sprite &sprite, + Raylib::Sprite &sprite, Types::Rect &rect) { - Vector2 origin = {0, 0}; + Raylib::Vector2 origin = {0, 0}; float rotation = 0; - auto tint = WHITE; + Raylib::Color tint = Raylib::White; const float denominator = 100.0; - float x = - (position.x * static_cast(GetScreenWidth())) / denominator; - float y = - (position.y * static_cast(GetScreenHeight())) / denominator; + float x = (position.x * static_cast(Raylib::getScreenWidth())) + / denominator; + float y = (position.y * static_cast(Raylib::getScreenHeight())) + / denominator; float width = - (sprite.width * static_cast(GetScreenWidth())) / denominator; - float height = (sprite.height * static_cast(GetScreenHeight())) + (sprite.getWidth() * static_cast(Raylib::getScreenWidth())) + / denominator; + float height = + (sprite.getHeight() * static_cast(Raylib::getScreenHeight())) / denominator; - DrawTexturePro( - sprite.sprite, - Rectangle(rect.x, rect.y, rect.width, rect.height), - Rectangle(x, y, width, height), + sprite.drawPro( + Raylib::Rectangle(rect.x, rect.y, rect.width, rect.height), + Raylib::Rectangle(x, y, width, height), origin, rotation, tint); @@ -119,8 +142,8 @@ namespace Systems { void GraphicSystems::spriteRenderer(std::size_t /*unused*/) { - Registry::components arrSprite = - Registry::getInstance().getComponents(); + Registry::components arrSprite = + Registry::getInstance().getComponents(); Registry::components arrRect = Registry::getInstance().getComponents(); Registry::components arrPosition = @@ -162,79 +185,23 @@ namespace Systems { } } - static void - drawSpriteWithoutRect(Types::Position &position, Raylib::Sprite &sprite) - { - float scale = 1.0F; - float rotation = 0; - Raylib::Color tint = Raylib::White; - Raylib::Vector2 spritePos = {0, 0}; - const float denominator = 100.0; - - float x = (position.x * static_cast(Raylib::getScreenWidth())) / denominator; - float y = - (position.y * static_cast(Raylib::getScreenHeight())) / denominator; - - scale = (sprite.getWidth() * static_cast(Raylib::getScreenWidth())) / denominator - / static_cast(sprite.getTextureWidth()); - spritePos = {x, y}; - - sprite.drawEx(spritePos, rotation, scale, tint); - } - - static void drawSpriteWithRect( - Types::Position &position, - Raylib::Sprite &sprite, - Types::Rect &rect) + void GraphicSystems::musicPlayer(std::size_t /*unused*/) { - Raylib::Vector2 origin = {0, 0}; - float rotation = 0; - Raylib::Color tint = Raylib::White; - const float denominator = 100.0; - - float x = (position.x * static_cast(Raylib::getScreenWidth())) / denominator; - float y = - (position.y * static_cast(Raylib::getScreenHeight())) / denominator; - - float width = - (sprite.getWidth() * static_cast(Raylib::getScreenWidth())) / denominator; - float height = - (sprite.getHeight() * static_cast(Raylib::getScreenHeight())) / denominator; - - sprite.drawPro( - Raylib::Rectangle(rect.x, rect.y, rect.width, rect.height), - Raylib::Rectangle(x, y, width, height), - origin, - rotation, - tint); - } + Registry::components arrMusics = + Registry::getInstance().getComponents(); - void GraphicSystems::spriteRenderer(std::size_t /*unused*/) - { - Registry::components arrSprite = - Registry::getInstance().getComponents(); - Registry::components arrRect = - Registry::getInstance().getComponents(); - Registry::components arrPosition = - Registry::getInstance().getComponents(); - - auto positionIt = arrPosition.begin(); - auto spriteIt = arrSprite.begin(); - auto rectIt = arrRect.begin(); - - while (positionIt != arrPosition.end() && spriteIt != arrSprite.end()) { - if (positionIt->has_value() && spriteIt->has_value() - && rectIt->has_value()) { - drawSpriteWithRect( - positionIt->value(), - spriteIt->value(), - rectIt->value()); - } else if (positionIt->has_value() && spriteIt->has_value()) { - drawSpriteWithoutRect(positionIt->value(), spriteIt->value()); + for (auto &music : arrMusics) { + if (!music.has_value()) { + continue; + } + if (music.value().needToPlay) { + PlayMusicStream(music.value().music); + music.value().needToPlay = false; + music.value().isPlaying = true; + } + if (music.value().isPlaying) { + UpdateMusicStream(music.value().music); } - positionIt++; - spriteIt++; - rectIt++; } } @@ -278,67 +245,12 @@ namespace Systems { } } - void EventsSystems::playerMovement(std::size_t /*unused*/) - { - Registry::components arrPosition = - Registry::getInstance().getComponents(); - - Registry::components arrPlayer = - Registry::getInstance().getComponents(); - - auto positionIt = arrPosition.begin(); - auto playerIt = arrPlayer.begin(); - - while (positionIt != arrPosition.end() && playerIt != arrPlayer.end()) { - if (playerIt->has_value() && positionIt->has_value() - && playerIt->value().isMine) { - if (IsKeyDown(KEY_RIGHT)) { - positionIt->value().x += 1; - } - if (IsKeyDown(KEY_LEFT)) { - positionIt->value().x -= 1; - } - if (IsKeyDown(KEY_UP)) { - positionIt->value().y -= 1; - } - if (IsKeyDown(KEY_DOWN)) { - positionIt->value().y += 1; - } - } - positionIt++; - playerIt++; - } - } - - void GraphicSystems::musicPlayer(std::size_t /*unused*/) - { - Registry::components arrMusics = - Registry::getInstance().getComponents(); - - for (auto &music : arrMusics) { - if (!music.has_value()) { - continue; - } - if (music.value().needToPlay) { - PlayMusicStream(music.value().music); - music.value().needToPlay = false; - music.value().isPlaying = true; - } - if (music.value().isPlaying) { - UpdateMusicStream(music.value().music); - } - } - } - const std::vector> GraphicSystems::graphicSystems { rectRenderer, spriteRenderer, textRenderer, musicPlayer, - soundEffectPlayer - }; + soundEffectPlayer}; - const std::vector> - EventsSystems::eventSystems {playerMovement}; } // namespace Systems diff --git a/src/Client/Systems/CustomTypes.hpp b/src/Client/Systems/CustomTypes.hpp index 4c558a7f..59df414f 100644 --- a/src/Client/Systems/CustomTypes.hpp +++ b/src/Client/Systems/CustomTypes.hpp @@ -19,6 +19,11 @@ namespace Types { float height; }; + struct Position { + float x; + float y; + }; + struct SoundEffect { SoundEffect(std::string soundPath) : sound(LoadSound(soundPath.c_str())), diff --git a/src/main_client.cpp b/src/main_client.cpp index b978be56..feb1c814 100644 --- a/src/main_client.cpp +++ b/src/main_client.cpp @@ -5,66 +5,8 @@ ** main */ -#include "CustomTypes.hpp" -#include "GameManager.hpp" -#include "GraphicManager.hpp" -#include "Registry.hpp" -#include "SystemEventsManager.hpp" -#include "Raylib.hpp" int main() { - Systems::GameManager &gameManager = Systems::GameManager::getInstance(); - Systems::GraphicManager &graphicManager = - Systems::GraphicManager::getInstance(); - Systems::SystemEventsManager &systemEventsManager = - Systems::SystemEventsManager::getInstance(); - - Registry::components arrRectShape = - Registry::getInstance().getComponents(); - - Registry::components arrPosition = - Registry::getInstance().getComponents(); - - Registry::components arrCollisionRect = - Registry::getInstance().getComponents(); - - Registry::components arrSprite = - Registry::getInstance().getComponents(); - - Registry::components arrRect = - Registry::getInstance().getComponents(); - - Registry::components arrPlayer = - Registry::getInstance().getComponents(); - - const Types::CollisionRect playerCollisionRect = {10, 20}; - const Types::Rect playerRect = {2.0F, 5.0F, 30.5F, 25.2F}; - const Types::RectangleShape rectShape = {10, 10}; - const Types::Position playerPosition = {0, 0}; - const Types::Position squarePosition = {-5, 45}; - const Raylib::Sprite playerSprite = { - "assets/R-TypeSheet/r-typesheet18.gif", - 10, - 20}; - - // add rectangle shape entity of 10% of the screen at the middle - Registry::getInstance().addEntity(); - arrRectShape.back() = rectShape; - arrPosition.back() = squarePosition; - - // add player entity test - Registry::getInstance().addEntity(); - arrPosition.back() = playerPosition; - arrSprite.back() = playerSprite; - arrRect.back() = playerRect; - Types::Player myPlayer = {true}; - arrPlayer.back() = myPlayer; - arrCollisionRect.back() = playerCollisionRect; - - while (true) { - systemEventsManager.updateSystems(); - gameManager.updateSystems(); - graphicManager.updateSystems(); - } + return 0; }