Compare commits
5 Commits
54e4b00f8c
...
fdb5ee6e1e
Author | SHA1 | Date | |
---|---|---|---|
fdb5ee6e1e | |||
7cdf73ac20 | |||
ff75af9f72 | |||
3b87746e7d | |||
d486da8994 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -10,6 +10,7 @@ build-debug/
|
||||
build-release/
|
||||
build-minsizerel/
|
||||
cmake-build-debug/
|
||||
build*/
|
||||
|
||||
# Tooling files
|
||||
output.txt
|
||||
|
@ -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})
|
||||
|
7
src/components/layer_component.h
Normal file
7
src/components/layer_component.h
Normal file
@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
struct LayerComponent {
|
||||
int layer;
|
||||
|
||||
LayerComponent(int layer = 0) : layer(layer) {}
|
||||
};
|
5
src/components/ui_component.cpp
Normal file
5
src/components/ui_component.cpp
Normal file
@ -0,0 +1,5 @@
|
||||
//
|
||||
// Created by illyum on 9/12/2024.
|
||||
//
|
||||
|
||||
#include "ui_component.h"
|
20
src/components/ui_component.h
Normal file
20
src/components/ui_component.h
Normal 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
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