Slightly improve custom behaviour components

This commit is contained in:
Bailey Harrison 2023-08-01 16:13:33 +01:00
parent 9131eab404
commit cdb57ede9f
4 changed files with 64 additions and 37 deletions

View File

@ -106,6 +106,8 @@ class Scene {
Application* const app_; Application* const app_;
uint32_t next_entity_id_ = 1000; uint32_t next_entity_id_ = 1000;
uint64_t framecount_ = 0;
/* ecs stuff */ /* ecs stuff */
size_t next_signature_position_ = 0; size_t next_signature_position_ = 0;

View File

@ -1,6 +1,8 @@
#ifndef ENGINE_INCLUDE_CUSTOM_BEHAVIOUR_H_ #ifndef ENGINE_INCLUDE_CUSTOM_BEHAVIOUR_H_
#define ENGINE_INCLUDE_CUSTOM_BEHAVIOUR_H_ #define ENGINE_INCLUDE_CUSTOM_BEHAVIOUR_H_
#include <unordered_map>
#include "ecs_system.h" #include "ecs_system.h"
/* This system allows for one-off custom components that execute arbitrary code /* This system allows for one-off custom components that execute arbitrary code
@ -17,7 +19,7 @@ namespace engine {
void OnComponentInsert(uint32_t entity) override; void OnComponentInsert(uint32_t entity) override;
private: private:
std::unordered_map<uint32_t, bool> entity_is_initialised_{};
}; };
} // namespace engine } // namespace engine

View File

@ -20,13 +20,20 @@ void CustomBehaviourSystem::OnUpdate(float ts) {
for (uint32_t entity : entities_) { for (uint32_t entity : entities_) {
auto c = scene_->GetComponent<CustomComponent>(entity); auto c = scene_->GetComponent<CustomComponent>(entity);
assert(c != nullptr); assert(c != nullptr);
bool& entity_initialised = entity_is_initialised_.at(entity);
if (entity_initialised == false) {
if (c->onInit == nullptr) throw std::runtime_error("CustomComponent::onInit not set! Entity: " + std::to_string(entity));
if (c->onUpdate == nullptr) throw std::runtime_error("CustomComponent::onUpdate not set! Entity: " + std::to_string(entity));
c->onInit();
entity_initialised = true;
}
c->onUpdate(ts); c->onUpdate(ts);
} }
} }
void CustomBehaviourSystem::OnComponentInsert(uint32_t entity) void CustomBehaviourSystem::OnComponentInsert(uint32_t entity)
{ {
(void)entity; entity_is_initialised_.emplace(entity, false);
} }
} // namespace engine } // namespace engine

View File

@ -123,15 +123,23 @@ void PlayGame(GameSettings settings) {
auto cobbleHouse = engine::util::LoadMeshFromFile( auto cobbleHouse = engine::util::LoadMeshFromFile(
my_scene, app.GetResourcePath("models/cobble_house/cobble_house.dae")); my_scene, app.GetResourcePath("models/cobble_house/cobble_house.dae"));
my_scene->GetComponent<engine::TransformComponent>(cobbleHouse)->position += glm::vec3{ my_scene->GetComponent<engine::TransformComponent>(cobbleHouse)->position +=
33.0f, 0.1f, 35.0f}; glm::vec3{33.0f, 0.1f, 35.0f};
auto cobbleCustom = my_scene->AddComponent<engine::CustomComponent>(cobbleHouse);
cobbleCustom->onInit = [](void) {
LOG_INFO("Cobble house spin component initialised!");
};
cobbleCustom->onUpdate = [&](float ts) {
static auto t = my_scene->GetComponent<engine::TransformComponent>(cobbleHouse);
t->rotation *= glm::angleAxis(ts, glm::vec3{0.0f, 0.0f, 1.0f});
};
/* some text */ /* some text */
{ {
int width, height; int width, height;
auto bitmap = app.GetResource<engine::resources::Font>("builtin.mono") auto bitmap = app.GetResource<engine::resources::Font>("builtin.mono")
->GetTextBitmap("ABCDEFGHIJKLMNOPQRSTUVWXYZ12345", 1080.0f, ->GetTextBitmap("ABCDEFGHIJKLMNOPQRSTUVWXYZ12345",
width, height); 1080.0f, width, height);
uint32_t textbox = my_scene->CreateEntity("textbox"); uint32_t textbox = my_scene->CreateEntity("textbox");
auto textbox_renderable = auto textbox_renderable =
@ -148,14 +156,22 @@ void PlayGame(GameSettings settings) {
(float)height / (float)width; (float)height / (float)width;
textbox_renderable->shown = true; textbox_renderable->shown = true;
my_scene->AddComponent<engine::CustomComponent>(textbox)->onUpdate = auto textboxComponent =
[&](float ts) { my_scene->AddComponent<engine::CustomComponent>(textbox);
textboxComponent->onInit = [](void) {
LOG_INFO("Textbox custom component initialised!");
};
textboxComponent->onUpdate = [&](float ts) {
(void)ts; (void)ts;
static float time_elapsed; static float time_elapsed;
time_elapsed += ts; time_elapsed += ts;
if (time_elapsed >= 1.0f) { if (time_elapsed >= 1.0f) {
time_elapsed = 0.0f; time_elapsed = 0.0f;
LOG_INFO("COMPONENT UPDATE");
// LOG_INFO("Creating new bitmap..."); // LOG_INFO("Creating new bitmap...");
// auto fpsBitmap = // auto fpsBitmap =
// app.GetResource<engine::resources::Font>("builtin.mono") // app.GetResource<engine::resources::Font>("builtin.mono")