From ff75af9f724d13387aa9f0aebed5919959780097 Mon Sep 17 00:00:00 2001 From: illyum Date: Thu, 12 Sep 2024 21:12:11 -0600 Subject: [PATCH] feat: pass delta to scene manager's update function --- src/scene_manager.cpp | 110 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 src/scene_manager.cpp diff --git a/src/scene_manager.cpp b/src/scene_manager.cpp new file mode 100644 index 0000000..3eae780 --- /dev/null +++ b/src/scene_manager.cpp @@ -0,0 +1,110 @@ +#include "scene_manager.h" + +#include "Logger.h" +#include "raylib.h" + +SceneManager::SceneManager(entt::registry& registry) : registry(registry), activeScene(entt::null) {} + +void SceneManager::SetActiveScene(entt::entity scene) { + if (activeScene != entt::null) { + auto& currentScene = registry.get(activeScene); + currentScene.isActive = false; + } + + activeScene = scene; + auto& newScene = registry.get(activeScene); + newScene.isActive = true; +} + +entt::entity SceneManager::CreateScene() { + entt::entity scene = registry.create(); + registry.emplace(scene, false); + return scene; +} + +void SceneManager::UpdateActiveScene(float delta) { + if (activeScene != entt::null) { + // Iterate over entities in the active scene that have an InputComponent + auto view = registry.view(); + for (auto entity : view) { + auto& inputComponent = view.get(entity); + inputComponent.Update(); // This will call the input logic + } + } +} + +void SceneManager::RenderActiveScene() { + if (activeScene != entt::null) { + // Create a multimap to store entities sorted by layer + std::multimap layeredEntities; + + // Gather all entities with a LayerComponent + auto view = registry.view(); + for (auto entity : view) { + auto& layer = view.get(entity); + layeredEntities.insert({ layer.layer, entity }); + } + + // Render all entities sorted by layer + for (auto& pair : layeredEntities) { + auto entity = pair.second; + auto& sprite = registry.get(entity); + auto& transform = registry.get(entity); + + // Render the sprite at the position defined by the TransformComponent + sprite.Render(transform); + } + } +} + +void SceneManager::UpdateUI(entt::registry& registry) { + Vector2 mousePosition = GetMousePosition(); // This assumes a raylib-like GetMousePosition, adapt as needed for your system + + auto view = registry.view(); + for (auto entity : view) { + auto& ui = view.get(entity); + auto& transform = view.get(entity); + + // Check if the mouse is over the button + bool isHovered = (mousePosition.x > transform.x && mousePosition.x < transform.x + ui.width && + mousePosition.y > transform.y && mousePosition.y < transform.y + ui.height); + + if (isHovered) { + ui.state = IsMouseButtonPressed(MOUSE_LEFT_BUTTON) ? UIState::CLICKED : UIState::HOVERED; + + // If clicked, call the onClick function + if (ui.state == UIState::CLICKED && ui.onClick) { + ui.onClick(); + } + } else { + ui.state = UIState::NORMAL; + } + } +} + +void SceneManager::RenderUI(entt::registry& registry) { + auto view = registry.view(); + + for (auto entity : view) { + auto& ui = view.get(entity); + auto& transform = view.get(entity); + + // Render the button background + Color buttonColor = DARKGRAY; + if (ui.state == UIState::HOVERED) { + buttonColor = LIGHTGRAY; + } else if (ui.state == UIState::CLICKED) { + buttonColor = GRAY; + } + + // Use the raw transform position (no adjustment to center screen) + DrawRectangle(transform.x, transform.y, ui.width, ui.height, buttonColor); + + // Render the button text (centered within the button) + int textWidth = MeasureText(ui.text.c_str(), 20); + int textX = transform.x + (ui.width / 2) - (textWidth / 2); + int textY = transform.y + (ui.height / 2) - 10; // Slight vertical adjustment + + DrawText(ui.text.c_str(), textX, textY, 20, BLACK); + } +} \ No newline at end of file