feat: pass delta to scene manager's update function
This commit is contained in:
parent
3b87746e7d
commit
ff75af9f72
110
src/scene_manager.cpp
Normal file
110
src/scene_manager.cpp
Normal file
@ -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<SceneComponent>(activeScene);
|
||||
currentScene.isActive = false;
|
||||
}
|
||||
|
||||
activeScene = scene;
|
||||
auto& newScene = registry.get<SceneComponent>(activeScene);
|
||||
newScene.isActive = true;
|
||||
}
|
||||
|
||||
entt::entity SceneManager::CreateScene() {
|
||||
entt::entity scene = registry.create();
|
||||
registry.emplace<SceneComponent>(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<InputComponent>();
|
||||
for (auto entity : view) {
|
||||
auto& inputComponent = view.get<InputComponent>(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<int, entt::entity> layeredEntities;
|
||||
|
||||
// Gather all entities with a LayerComponent
|
||||
auto view = registry.view<LayerComponent, SpriteComponent, TransformComponent>();
|
||||
for (auto entity : view) {
|
||||
auto& layer = view.get<LayerComponent>(entity);
|
||||
layeredEntities.insert({ layer.layer, entity });
|
||||
}
|
||||
|
||||
// Render all entities sorted by layer
|
||||
for (auto& pair : layeredEntities) {
|
||||
auto entity = pair.second;
|
||||
auto& sprite = registry.get<SpriteComponent>(entity);
|
||||
auto& transform = registry.get<TransformComponent>(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<UIComponent, TransformComponent>();
|
||||
for (auto entity : view) {
|
||||
auto& ui = view.get<UIComponent>(entity);
|
||||
auto& transform = view.get<TransformComponent>(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<UIComponent, TransformComponent>();
|
||||
|
||||
for (auto entity : view) {
|
||||
auto& ui = view.get<UIComponent>(entity);
|
||||
auto& transform = view.get<TransformComponent>(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);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user