From cdb57ede9f3fc8302541f6364daaa03918cbebc5 Mon Sep 17 00:00:00 2001 From: bailwillharr Date: Tue, 1 Aug 2023 16:13:33 +0100 Subject: [PATCH] Slightly improve custom behaviour components --- include/scene.h | 2 + include/systems/custom_behaviour.h | 20 +++++---- src/systems/custom_behaviour.cpp | 9 +++- test/src/game.cpp | 70 ++++++++++++++++++------------ 4 files changed, 64 insertions(+), 37 deletions(-) diff --git a/include/scene.h b/include/scene.h index dfd6181..5552e04 100644 --- a/include/scene.h +++ b/include/scene.h @@ -106,6 +106,8 @@ class Scene { Application* const app_; uint32_t next_entity_id_ = 1000; + uint64_t framecount_ = 0; + /* ecs stuff */ size_t next_signature_position_ = 0; diff --git a/include/systems/custom_behaviour.h b/include/systems/custom_behaviour.h index 89a17f5..a574f79 100644 --- a/include/systems/custom_behaviour.h +++ b/include/systems/custom_behaviour.h @@ -1,6 +1,8 @@ #ifndef ENGINE_INCLUDE_CUSTOM_BEHAVIOUR_H_ #define ENGINE_INCLUDE_CUSTOM_BEHAVIOUR_H_ +#include + #include "ecs_system.h" /* This system allows for one-off custom components that execute arbitrary code @@ -8,17 +10,17 @@ namespace engine { - class CustomBehaviourSystem : public System { - public: - CustomBehaviourSystem(Scene* scene); - ~CustomBehaviourSystem(); +class CustomBehaviourSystem : public System { + public: + CustomBehaviourSystem(Scene* scene); + ~CustomBehaviourSystem(); - void OnUpdate(float ts) override; - void OnComponentInsert(uint32_t entity) override; + void OnUpdate(float ts) override; + void OnComponentInsert(uint32_t entity) override; - private: - - }; + private: + std::unordered_map entity_is_initialised_{}; +}; } // namespace engine diff --git a/src/systems/custom_behaviour.cpp b/src/systems/custom_behaviour.cpp index 625f0c9..06a5f5b 100644 --- a/src/systems/custom_behaviour.cpp +++ b/src/systems/custom_behaviour.cpp @@ -20,13 +20,20 @@ void CustomBehaviourSystem::OnUpdate(float ts) { for (uint32_t entity : entities_) { auto c = scene_->GetComponent(entity); 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); } } void CustomBehaviourSystem::OnComponentInsert(uint32_t entity) { - (void)entity; + entity_is_initialised_.emplace(entity, false); } } // namespace engine \ No newline at end of file diff --git a/test/src/game.cpp b/test/src/game.cpp index 51bd396..04212b5 100644 --- a/test/src/game.cpp +++ b/test/src/game.cpp @@ -70,7 +70,7 @@ void PlayGame(GameSettings settings) { camera_collider->is_static = false; camera_collider->is_trigger = false; camera_collider->aabb = {{-0.2f, -1.5f, -0.2f}, - {0.2f, 0.2f, 0.2f}}; // Origin is at eye level + {0.2f, 0.2f, 0.2f}}; // Origin is at eye level my_scene->AddComponent(camera); auto render_system = my_scene->GetSystem(); render_system->SetCameraEntity(camera); @@ -118,20 +118,28 @@ void PlayGame(GameSettings settings) { floor_collider->aabb = {{0.0f, 0.0f, 0.0f}, {100.0f, 0.1f, 100.0f}}; } - //engine::util::LoadMeshFromFile(my_scene, - // app.GetResourcePath("models/test_scene.dae")); + // engine::util::LoadMeshFromFile(my_scene, + // app.GetResourcePath("models/test_scene.dae")); auto cobbleHouse = engine::util::LoadMeshFromFile( my_scene, app.GetResourcePath("models/cobble_house/cobble_house.dae")); - my_scene->GetComponent(cobbleHouse)->position += glm::vec3{ - 33.0f, 0.1f, 35.0f}; + my_scene->GetComponent(cobbleHouse)->position += + glm::vec3{33.0f, 0.1f, 35.0f}; + auto cobbleCustom = my_scene->AddComponent(cobbleHouse); + cobbleCustom->onInit = [](void) { + LOG_INFO("Cobble house spin component initialised!"); + }; + cobbleCustom->onUpdate = [&](float ts) { + static auto t = my_scene->GetComponent(cobbleHouse); + t->rotation *= glm::angleAxis(ts, glm::vec3{0.0f, 0.0f, 1.0f}); + }; /* some text */ { int width, height; auto bitmap = app.GetResource("builtin.mono") - ->GetTextBitmap("ABCDEFGHIJKLMNOPQRSTUVWXYZ12345", 1080.0f, - width, height); + ->GetTextBitmap("ABCDEFGHIJKLMNOPQRSTUVWXYZ12345", + 1080.0f, width, height); uint32_t textbox = my_scene->CreateEntity("textbox"); auto textbox_renderable = @@ -148,27 +156,35 @@ void PlayGame(GameSettings settings) { (float)height / (float)width; textbox_renderable->shown = true; - my_scene->AddComponent(textbox)->onUpdate = - [&](float ts) { - (void)ts; - static float time_elapsed; - time_elapsed += ts; - if (time_elapsed >= 1.0f) { - time_elapsed = 0.0f; + auto textboxComponent = + my_scene->AddComponent(textbox); - //LOG_INFO("Creating new bitmap..."); - //auto fpsBitmap = - // app.GetResource("builtin.mono") - // ->GetTextBitmap(std::to_string(ts), 768.0f, fpsWidth, - // fpsHeight); - //LOG_INFO("Bitmap created! Loading into new texture..."); - //textbox_renderable->material->texture_ = - // std::make_unique( - // &app.render_data_, fpsBitmap->data(), fpsWidth, fpsHeight, - // engine::resources::Texture::Filtering::kBilinear); - //LOG_INFO("Texture created!"); - } - }; + textboxComponent->onInit = [](void) { + LOG_INFO("Textbox custom component initialised!"); + }; + + textboxComponent->onUpdate = [&](float ts) { + (void)ts; + static float time_elapsed; + time_elapsed += ts; + if (time_elapsed >= 1.0f) { + time_elapsed = 0.0f; + + LOG_INFO("COMPONENT UPDATE"); + + // LOG_INFO("Creating new bitmap..."); + // auto fpsBitmap = + // app.GetResource("builtin.mono") + // ->GetTextBitmap(std::to_string(ts), 768.0f, fpsWidth, + // fpsHeight); + // LOG_INFO("Bitmap created! Loading into new texture..."); + // textbox_renderable->material->texture_ = + // std::make_unique( + // &app.render_data_, fpsBitmap->data(), fpsWidth, fpsHeight, + // engine::resources::Texture::Filtering::kBilinear); + // LOG_INFO("Texture created!"); + } + }; } app.GameLoop();