diff --git a/CMakeLists.txt b/CMakeLists.txt index 5cc80c6c..8574a23a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -127,6 +127,8 @@ set(SOURCES sources/Component/IntroAnimation/IntroAnimationComponent.cpp sources/System/IntroAnimation/IntroAnimationSystem.hpp sources/System/IntroAnimation/IntroAnimationSystem.cpp + sources/System/Renderer/CameraSystem.cpp + sources/System/Renderer/CameraSystem.cpp sources/Runner/SplashScreenScene.cpp sources/Runner/TitleScreenScene.cpp sources/Runner/MainMenuScene.cpp diff --git a/sources/Runner/GameScene.cpp b/sources/Runner/GameScene.cpp index 51a0221f..601e991c 100644 --- a/sources/Runner/GameScene.cpp +++ b/sources/Runner/GameScene.cpp @@ -47,7 +47,6 @@ namespace BBM return scene.addEntity("player") .addComponent() .addComponent("assets/player/player.iqm", true) - .addComponent() .addComponent() .addComponent() .addComponent() diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 2e29e4b2..72a49ce8 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -33,6 +33,7 @@ #include "System/Gravity/GravitySystem.hpp" #include "System/BumperTimer/BumperTimerSystem.hpp" #include "System/Music/MusicSystem.hpp" +#include "System/Renderer/CameraSystem.hpp" #include "System/Lobby/LobbySystem.hpp" #include "Component/Lobby/LobbyComponent.hpp" @@ -85,7 +86,8 @@ namespace BBM .addSystem() .addSystem() .addSystem() - .addSystem(); + .addSystem() + .addSystem(); } void Runner::enableRaylib(WAL::Wal &wal) diff --git a/sources/System/Renderer/CameraSystem.cpp b/sources/System/Renderer/CameraSystem.cpp new file mode 100644 index 00000000..73134bb1 --- /dev/null +++ b/sources/System/Renderer/CameraSystem.cpp @@ -0,0 +1,79 @@ +// +// Created by Tom Augier on 05/06/2021 +// + +#include "CameraSystem.hpp" +#include "Entity/Entity.hpp" +#include "Component/Tag/TagComponent.hpp" + +namespace BBM +{ + CameraSystem::CameraSystem(WAL::Wal &wal) + : System(wal) + {} + + bool CameraSystem::introAnimation(WAL::ViewEntity &entity, bool restart) + { + auto &pos = entity.get(); + static Vector3f posTarget(8, 25, 7); + static bool hasEnded = false; + + if (restart) { + hasEnded = false; + return (false); + } + if (pos.position.distance(posTarget) < 2 || hasEnded) { + hasEnded = true; + return (true); + } + + auto &cam = entity.get(); + + pos.position += (posTarget - pos.position) / 100; + return (false); + } + + void CameraSystem::onUpdate(WAL::ViewEntity &entity, + std::chrono::nanoseconds dtime) + { + if (!introAnimation(entity)) + return; + auto &pos = entity.get(); + auto &cam = entity.get(); + Vector3f newCameraPos = Vector3f(-1, -1, -1); + std::vector playerPos; + float maxDist = 0; + float lowerXDist = 0; + float lowerZDist = 0; + + for (auto &[entity, pos, _] : this->_wal.getScene()->view>()) { + if (!entity.hasComponent()) + entity.addComponent(); + playerPos.emplace_back(pos.position); + } + if (playerPos.size() == 0) + introAnimation(entity, true); + if (playerPos.size() == 1) + newCameraPos = playerPos[0]; + for (int i = 0; i < playerPos.size(); i++) + for (int j = 0; j < playerPos.size(); j++) { + if (maxDist < playerPos[i].distance(playerPos[j])) { + maxDist = playerPos[i].distance(playerPos[j]); + newCameraPos = (playerPos[i] + playerPos[j]) / 2; + } + if (lowerXDist < std::abs((playerPos[i].x - playerPos[j].x))) + lowerXDist = std::abs((playerPos[i].x - playerPos[j].x)); + if (lowerZDist < std::abs((playerPos[i].z - playerPos[j].z))) + lowerZDist = std::abs((playerPos[i].z - playerPos[j].z)); + } + maxDist += (lowerXDist + lowerZDist) / 2; + if (maxDist < 14) + maxDist = 14; + if (maxDist > 25) + maxDist = 25; + cam.target += (newCameraPos.abs() - pos.position.abs()) / 10; + newCameraPos.y = maxDist; + newCameraPos.z -= 1; + pos.position += (newCameraPos.abs() - pos.position.abs()) / 10; + } +} \ No newline at end of file diff --git a/sources/System/Renderer/CameraSystem.hpp b/sources/System/Renderer/CameraSystem.hpp new file mode 100644 index 00000000..ce3a804f --- /dev/null +++ b/sources/System/Renderer/CameraSystem.hpp @@ -0,0 +1,36 @@ +// +// Created by Tom Augier on 05/06/2021 +// + +#pragma once + +#include "System/System.hpp" +#include "Window.hpp" +#include "Component/Renderer/CameraComponent.hpp" +#include "Component/Position/PositionComponent.hpp" +#include +#include "Wal.hpp" + +namespace BBM +{ + class CameraSystem : public WAL::System + { + public: + //! @inherit + void onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds) override; + + //! @brief introduciton animation when entering gameScene + bool introAnimation(WAL::ViewEntity &entity, bool restart = false); + + //! @brief ctor + CameraSystem(WAL::Wal &wal); + //! @brief Default copy ctor + CameraSystem(const CameraSystem &) = default; + //! @brief Default dtor + ~CameraSystem() override = default; + //! @brief A CameraManager screen system can't be assigned. + CameraSystem &operator=(const CameraSystem &) = delete; + }; +} + + \ No newline at end of file diff --git a/sources/System/Renderer/RenderSystem.cpp b/sources/System/Renderer/RenderSystem.cpp index 483bb9f6..f72f850b 100644 --- a/sources/System/Renderer/RenderSystem.cpp +++ b/sources/System/Renderer/RenderSystem.cpp @@ -80,69 +80,12 @@ namespace BBM this->_window.endDrawing(); } - bool RenderSystem::introAnimation(WAL::ViewEntity &entity, bool restart) - { - auto &pos = entity.get(); - static Vector3f posTarget(8, 25, 7); - static bool hasEnded = false; - - if (restart) { - hasEnded = false; - return (false); - } - if (pos.position.distance(posTarget) < 1 || hasEnded) { - hasEnded = true; - return (true); - } - - auto &cam = entity.get(); - - pos.position += (posTarget - pos.position) / 100; - this->_camera.setPosition(pos.position); - this->_camera.setTarget(cam.target); - return (false); - } - - void RenderSystem::onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds dtime) { - if (!introAnimation(entity)) - return; auto &pos = entity.get(); auto &cam = entity.get(); - Vector3f newCameraPos = Vector3f(-1, -1, -1); - std::vector playerPos; - float maxDist = 0; - float lowerXDist = 0; - float lowerZDist = 0; - - for (auto &[entity, pos, _] : this->_wal.getScene()->view>()) - playerPos.emplace_back(pos.position); - if (playerPos.size() == 0) - introAnimation(entity, true); - if (playerPos.size() == 1) - newCameraPos = playerPos[0]; - for (int i = 0; i < playerPos.size(); i++) - for (int j = 0; j < playerPos.size(); j++) { - if (maxDist < playerPos[i].distance(playerPos[j])) { - maxDist = playerPos[i].distance(playerPos[j]); - newCameraPos = (playerPos[i] + playerPos[j]) / 2; - } - if (lowerXDist < std::abs((playerPos[i].x - playerPos[j].x))) - lowerXDist = std::abs((playerPos[i].x - playerPos[j].x)); - if (lowerZDist < std::abs((playerPos[i].z - playerPos[j].z))) - lowerZDist = std::abs((playerPos[i].z - playerPos[j].z)); - } - maxDist += (lowerXDist + lowerZDist) / 2; - if (maxDist < 14) - maxDist = 14; - if (maxDist > 25) - maxDist = 25; - cam.target += (newCameraPos.abs() - pos.position.abs()) / 10; - newCameraPos.y = maxDist; - newCameraPos.z -= 1; - pos.position += (newCameraPos.abs() - pos.position.abs()) / 10; + _camera.setTarget(cam.target); _camera.setPosition(pos.position); } diff --git a/sources/System/Renderer/RenderSystem.hpp b/sources/System/Renderer/RenderSystem.hpp index fcb40970..7ef2c801 100644 --- a/sources/System/Renderer/RenderSystem.hpp +++ b/sources/System/Renderer/RenderSystem.hpp @@ -43,9 +43,6 @@ namespace BBM //! @param entity entity to draw bounding box of void drawBoundingBox(const WAL::Entity &entity, const PositionComponent &posComponent, const Drawable3DComponent &drawable) const; - - //! @brief introduciton animation when entering gameScene - bool introAnimation(WAL::ViewEntity &entity, bool restart = false); //! @brief ctor RenderSystem(WAL::Wal &wal, RAY::Window &window, bool debugMode = false);