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_;
uint32_t next_entity_id_ = 1000;
uint64_t framecount_ = 0;
/* ecs stuff */
size_t next_signature_position_ = 0;

View File

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

View File

@ -20,13 +20,20 @@ void CustomBehaviourSystem::OnUpdate(float ts) {
for (uint32_t entity : entities_) {
auto c = scene_->GetComponent<CustomComponent>(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

View File

@ -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,
// 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<engine::TransformComponent>(cobbleHouse)->position += glm::vec3{
33.0f, 0.1f, 35.0f};
my_scene->GetComponent<engine::TransformComponent>(cobbleHouse)->position +=
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 */
{
int width, height;
auto bitmap = app.GetResource<engine::resources::Font>("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,25 +156,33 @@ void PlayGame(GameSettings settings) {
(float)height / (float)width;
textbox_renderable->shown = true;
my_scene->AddComponent<engine::CustomComponent>(textbox)->onUpdate =
[&](float ts) {
auto textboxComponent =
my_scene->AddComponent<engine::CustomComponent>(textbox);
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("Creating new bitmap...");
//auto fpsBitmap =
LOG_INFO("COMPONENT UPDATE");
// LOG_INFO("Creating new bitmap...");
// auto fpsBitmap =
// app.GetResource<engine::resources::Font>("builtin.mono")
// ->GetTextBitmap(std::to_string(ts), 768.0f, fpsWidth,
// fpsHeight);
//LOG_INFO("Bitmap created! Loading into new texture...");
//textbox_renderable->material->texture_ =
// LOG_INFO("Bitmap created! Loading into new texture...");
// textbox_renderable->material->texture_ =
// std::make_unique<engine::resources::Texture>(
// &app.render_data_, fpsBitmap->data(), fpsWidth, fpsHeight,
// engine::resources::Texture::Filtering::kBilinear);
//LOG_INFO("Texture created!");
// LOG_INFO("Texture created!");
}
};
}