mirror of
https://github.com/bailwillharr/engine.git
synced 2024-09-21 04:51:18 +00:00
Slightly improve custom behaviour components
This commit is contained in:
parent
9131eab404
commit
cdb57ede9f
@ -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;
|
||||
|
@ -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,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<uint32_t, bool> entity_is_initialised_{};
|
||||
};
|
||||
|
||||
} // namespace engine
|
||||
|
||||
|
@ -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
|
@ -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<CameraControllerComponent>(camera);
|
||||
auto render_system = my_scene->GetSystem<engine::RenderSystem>();
|
||||
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<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,27 +156,35 @@ void PlayGame(GameSettings settings) {
|
||||
(float)height / (float)width;
|
||||
textbox_renderable->shown = true;
|
||||
|
||||
my_scene->AddComponent<engine::CustomComponent>(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<engine::CustomComponent>(textbox);
|
||||
|
||||
//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_ =
|
||||
// std::make_unique<engine::resources::Texture>(
|
||||
// &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<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_ =
|
||||
// std::make_unique<engine::resources::Texture>(
|
||||
// &app.render_data_, fpsBitmap->data(), fpsWidth, fpsHeight,
|
||||
// engine::resources::Texture::Filtering::kBilinear);
|
||||
// LOG_INFO("Texture created!");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
app.GameLoop();
|
||||
|
Loading…
Reference in New Issue
Block a user