Compare commits

..

5 Commits

6 changed files with 151 additions and 0 deletions

1
.gitignore vendored
View File

@ -10,6 +10,7 @@ build-debug/
build-release/
build-minsizerel/
cmake-build-debug/
build*/
# Tooling files
output.txt

View File

@ -36,6 +36,10 @@ FetchContent_Declare(
raylib
URL https://github.com/raysan5/raylib/archive/refs/tags/5.0.zip
)
set(BUILD_EXAMPLES OF CACHE BOOL "" FORCE)
set(SUPPORT_EVENTS_WAITING ON CACHE BOOL "" FORCE)
set(SUPPORT_BUSY_WAIT_LOOP OFF CACHE BOOL "" FORCE)
if (BUILD_SHARED_LIBS)
set(RAYLIB_LIBTYPE SHARED)
endif()
@ -109,6 +113,10 @@ set(ENGINE_SOURCES
src/components/input_component.cpp
src/components/input_component.h
src/components/health_component.h
src/components/ui_component.cpp
src/components/ui_component.h
src/scene_manager.cpp
src/components/layer_component.h
)
add_library(IsoEngine ${ENGINE_SOURCES})

View File

@ -0,0 +1,7 @@
#pragma once
struct LayerComponent {
int layer;
LayerComponent(int layer = 0) : layer(layer) {}
};

View File

@ -0,0 +1,5 @@
//
// Created by illyum on 9/12/2024.
//
#include "ui_component.h"

View File

@ -0,0 +1,20 @@
#pragma once
#include <functional>
#include <string>
enum class UIState {
NORMAL,
HOVERED,
CLICKED
};
struct UIComponent {
std::string text;
float width;
float height;
std::function<void()> onClick;
UIState state = UIState::NORMAL;
UIComponent(std::string text, float width, float height, std::function<void()> onClick)
: text(std::move(text)), width(width), height(height), onClick(std::move(onClick)) {}
};

110
src/scene_manager.cpp Normal file
View 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);
}
}