diff --git a/src/main.cpp b/src/main.cpp index 4e2bc25..d629fc0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,8 @@ /************************************************************************** * EngineSquared - Vehicle Usage Example * - * This example demonstrates how to use the Graphic and Physics plugins together for vehicle simulation. + * This example demonstrates how to use the Graphic and Physics plugins together + * for vehicle simulation. **************************************************************************/ #include "Engine.hpp" @@ -12,93 +13,124 @@ #include "Input.hpp" #include "Object.hpp" #include "Physics.hpp" -#include "Sound.hpp" #include "RenderingPipeline.hpp" +#include "Sound.hpp" #include "plugin/PluginWindow.hpp" #include "resource/Window.hpp" #include "component/PlayerVehicle.hpp" #include "resource/CameraControlSystemManager.hpp" -#include "scenes/LoadCourse.hpp" -#include "scenes/CreateVehicle.hpp" #include "scenes/CreateLight.hpp" -#include "system/VehicleInput.hpp" +#include "scenes/CreateVehicle.hpp" +#include "scenes/LoadCourse.hpp" #include "system/ChildFollowParentSystem.hpp" -#include "utils/OrbitalChaseCameraBehavior.hpp" #include "system/EngineAudioSystem.hpp" +#include "system/VehicleInput.hpp" +#include "utils/OrbitalChaseCameraBehavior.hpp" -void EscapeKeySystem(Engine::Core &core) -{ - auto &inputManager = core.GetResource(); +void EscapeKeySystem(Engine::Core &core) { + auto &inputManager = core.GetResource(); - if (inputManager.IsKeyPressed(GLFW_KEY_ESCAPE)) - { - core.Stop(); - } + if (inputManager.IsKeyPressed(GLFW_KEY_ESCAPE)) { + core.Stop(); + } } -void Setup(Engine::Core &core) -{ - // Option to lock the cursor to the window - auto &window = core.GetResource(); - // window.MaskCursor(); +void Setup(Engine::Core &core) { + // Option to lock the cursor to the window + auto &window = core.GetResource(); + // window.MaskCursor(); - //CreateCheckeredFloor(core); - LoadCourse(core); - auto vehicle = CreateVehicle(core); - auto light = CreateLight(core); + // CreateCheckeredFloor(core); + LoadCourse(core); + auto vehicle = CreateVehicle(core); + auto light = CreateLight(core); - auto camera = core.CreateEntity(); + auto camera = core.CreateEntity(); - camera.AddComponent(glm::vec3(0.0f, 1.0f, -10.0f)); - camera.AddComponent(); - camera.GetComponents().farPlane = 10000.0f; + camera.AddComponent( + glm::vec3(0.0f, 1.0f, -10.0f)); + camera.AddComponent(); + camera.GetComponents().farPlane = 10000.0f; - auto &cameraManager = core.GetResource(); - cameraManager.SetActiveCamera(camera); - cameraManager.SetMovementSpeed(3.0f); + auto &cameraManager = + core.GetResource(); + cameraManager.SetActiveCamera(camera); + cameraManager.SetMovementSpeed(3.0f); - core.RegisterSystem(EscapeKeySystem); + core.RegisterSystem(EscapeKeySystem); - core.RegisterSystem(VehicleInput); - core.RegisterSystem(ChildFollowParentSystem); + core.RegisterSystem(VehicleInput); + core.RegisterSystem( + ChildFollowParentSystem); - auto chaseBehavior = std::make_shared(core, vehicle); - cameraManager.SetBehavior(chaseBehavior); + auto chaseBehavior = + std::make_shared(core, vehicle); + cameraManager.SetBehavior(chaseBehavior); - auto &fixedTimeScheduler = core.GetScheduler(); - fixedTimeScheduler.SetTickRate(1.0f / 120.0f); + auto &fixedTimeScheduler = + core.GetScheduler(); + fixedTimeScheduler.SetTickRate(1.0f / 120.0f); - auto &cameraControlSystemManager = core.GetResource(); - cameraControlSystemManager.SetCameraControlSystemScheduler(core); + auto &cameraControlSystemManager = + core.GetResource(); + cameraControlSystemManager + .SetCameraControlSystemScheduler( + core); - // Register engine sound and attach audio component to vehicle entity - auto &soundMgr = core.GetResource(); - soundMgr.RegisterSound("engine_low", "asset/sounds/911_RSR30_1_in_on_high.wav", true); - Log::Info("Engine sound registered: engine_low"); + // Register engine sound and attach audio component to vehicle entity + auto &soundMgr = core.GetResource(); + soundMgr.RegisterSound("engine_low", + "asset/sounds/911_RSR30_1_in_on_high.wav", true); + Log::Info("Engine sound registered: engine_low"); - vehicle.AddComponent(); - Log::Info("EngineAudioComponent added to vehicle"); + vehicle.AddComponent(); + Log::Info("EngineAudioComponent added to vehicle"); - // Drive audio updates on regular Update scheduler - core.RegisterSystem(EngineAudioSystem); + // Drive audio updates on regular Update scheduler + core.RegisterSystem(EngineAudioSystem); } class GraphicExampleError : public std::runtime_error { - public: - using std::runtime_error::runtime_error; +public: + using std::runtime_error::runtime_error; }; -int main(void) -{ - spdlog::set_level(spdlog::level::info); - Engine::Core core; +int main(void) { + spdlog::set_level(spdlog::level::info); + Engine::Core core; + + core.AddPlugins(); - core.AddPlugins(); + core.RegisterSystem(Setup); + + core.RegisterSystem([](Engine::Core &core) { + auto view1 = + core.GetRegistry() + .view(); + if (!view1.front()) { + throw GraphicExampleError( + "No entity with PlayerVehicle and VehicleController found."); + } + Engine::Entity playerVehicle{core, *view1.begin()}; + const auto &vehicleTransform = + playerVehicle.GetComponents(); + + auto view2 = core.GetRegistry().view(); + if (!view2.front()) { + throw GraphicExampleError( + "No entity with PlayerVehicle and VehicleController found."); + } + Engine::Entity playerDirectionalLight{core, *view2.begin()}; + auto &directionalLightTransform = + playerDirectionalLight.GetComponents(); - core.RegisterSystem(Setup); + directionalLightTransform.SetPosition(vehicleTransform.GetPosition() + + glm::vec3(3.35, 7.12, -5.14) * 5.f); + }); - core.RunCore(); + core.RunCore(); - return 0; + return 0; } diff --git a/src/scenes/CreateLight.cpp b/src/scenes/CreateLight.cpp index 34ffcd7..1a6fff8 100644 --- a/src/scenes/CreateLight.cpp +++ b/src/scenes/CreateLight.cpp @@ -4,19 +4,28 @@ #include -Engine::Entity CreateLight(Engine::Core &core) -{ - auto pointLight = core.CreateEntity(); - pointLight.AddComponent(glm::vec3(-2.0f, 7.0f, -1.0f)); - pointLight.AddComponent( - Object::Component::PointLight{.color = glm::vec3(1.0f, 1.0f, 1.0f), - .intensity = 3.0f, - .radius = 100.0f, - .falloff = 1.0f}); +Engine::Entity CreateLight(Engine::Core &core) { + auto pointLight = core.CreateEntity(); + pointLight.AddComponent( + glm::vec3(-2.0f, 7.0f, -1.0f)); + pointLight.AddComponent( + Object::Component::PointLight{.color = glm::vec3(1.0f, 1.0f, 1.0f), + .intensity = 3.0f, + .radius = 100.0f, + .falloff = 1.0f}); - auto ambientLight = core.CreateEntity(); - ambientLight.AddComponent( - Object::Component::AmbientLight{.color = glm::vec3(0.4f, 0.4f, 0.4f)}); + auto ambientLight = core.CreateEntity(); + ambientLight.AddComponent( + Object::Component::AmbientLight{.color = glm::vec3{0.1f}}); - return pointLight; -} \ No newline at end of file + auto directionalLight = core.CreateEntity(); + directionalLight + .AddComponent(glm::vec3(-5, 3, 1.5)) + .SetRotation(0.408217877, -0.234569684, 0.109381631, 0.875426114); + directionalLight.AddComponent( + {.color = glm::vec4(0.5f, 0.35f, 0.05f, 1.0f), + .projection = + glm::orthoLH_ZO(-40.0f, 40.0f, -40.0f, 40.0f, 0.1f, 60.0f)}); + + return pointLight; +}