Begin adding custom behaviour system

This commit is contained in:
bailwillharr 2023-05-25 18:35:32 +01:00
parent ae8091f2a4
commit e57be9a0e7
9 changed files with 140 additions and 63 deletions

View File

@ -25,6 +25,7 @@ set(SRC_FILES
"src/scene.cpp" "src/scene.cpp"
"src/scene_manager.cpp" "src/scene_manager.cpp"
"src/systems/collisions.cpp" "src/systems/collisions.cpp"
"src/systems/custom_behaviour.cpp"
"src/systems/render.cpp" "src/systems/render.cpp"
"src/systems/transform.cpp" "src/systems/transform.cpp"
"src/util/files.cpp" "src/util/files.cpp"
@ -43,6 +44,7 @@ set(SRC_FILES
set(INCLUDE_FILES set(INCLUDE_FILES
"include/application.h" "include/application.h"
"include/components/collider.h" "include/components/collider.h"
"include/components/custom.h"
"include/components/renderable.h" "include/components/renderable.h"
"include/components/transform.h" "include/components/transform.h"
"include/ecs_system.h" "include/ecs_system.h"
@ -64,6 +66,7 @@ set(INCLUDE_FILES
"include/scene.h" "include/scene.h"
"include/scene_manager.h" "include/scene_manager.h"
"include/systems/collisions.h" "include/systems/collisions.h"
"include/systems/custom_behaviour.h"
"include/systems/render.h" "include/systems/render.h"
"include/systems/transform.h" "include/systems/transform.h"
"include/util.h" "include/util.h"

View File

@ -0,0 +1,14 @@
#ifndef ENGINE_INCLUDE_COMPONENTS_CUSTOM_H_
#define ENGINE_INCLUDE_COMPONENTS_CUSTOM_H_
#include <functional>
namespace engine {
struct CustomComponent {
std::function<void(float)> onUpdate; // void onUpdate(float ts);
};
} // namespace engine
#endif

View File

@ -0,0 +1,24 @@
#ifndef ENGINE_INCLUDE_CUSTOM_BEHAVIOUR_H_
#define ENGINE_INCLUDE_CUSTOM_BEHAVIOUR_H_
#include "ecs_system.h"
/* This system allows for one-off custom components that execute arbitrary code
* It is similar to Unity's 'MonoBehavior' system */
namespace engine {
class CustomBehaviourSystem : public System {
public:
CustomBehaviourSystem(Scene* scene);
~CustomBehaviourSystem();
void OnUpdate(float ts) override;
private:
};
} // namespace engine
#endif

View File

@ -1602,7 +1602,7 @@ namespace engine {
LOG_INFO("GPU Memory Statistics:"); LOG_INFO("GPU Memory Statistics:");
for (uint32_t i = 0; i < memProps.memoryProperties.memoryHeapCount; i++) { for (uint32_t i = 0; i < memProps.memoryProperties.memoryHeapCount; i++) {
const VmaStatistics& statistics = pStats.memoryType[i].statistics; const VmaStatistics& statistics = pStats.memoryHeap[i].statistics;
VkMemoryHeap heap = memProps.memoryProperties.memoryHeaps[i]; VkMemoryHeap heap = memProps.memoryProperties.memoryHeaps[i];
LOG_INFO("Memory heap {}", i); LOG_INFO("Memory heap {}", i);
if (heap.flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) { if (heap.flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) {

View File

@ -82,7 +82,7 @@ std::unique_ptr<std::vector<uint8_t>> Font::GetTextBitmap(
bitmap->at(i * 4 + 0) = 0x00; bitmap->at(i * 4 + 0) = 0x00;
bitmap->at(i * 4 + 1) = 0x00; bitmap->at(i * 4 + 1) = 0x00;
bitmap->at(i * 4 + 2) = 0x00; bitmap->at(i * 4 + 2) = 0x00;
bitmap->at(i * 4 + 3) = 0xFF; bitmap->at(i * 4 + 3) = 0x00;
} }
int top_left_x = 0; int top_left_x = 0;

View File

@ -3,69 +3,65 @@
#include "components/transform.h" #include "components/transform.h"
#include "components/renderable.h" #include "components/renderable.h"
#include "components/collider.h" #include "components/collider.h"
#include "components/custom.h"
#include "systems/transform.h" #include "systems/transform.h"
#include "systems/render.h" #include "systems/render.h"
#include "systems/collisions.h" #include "systems/collisions.h"
#include "systems/custom_behaviour.h"
namespace engine { namespace engine {
Scene::Scene(Application* app) Scene::Scene(Application* app) : app_(app) {
: app_(app) // event system
{ event_system_ = std::make_unique<EventSystem>();
// event system
event_system_ = std::make_unique<EventSystem>();
// ecs configuration: // ecs configuration:
RegisterComponent<TransformComponent>(); RegisterComponent<TransformComponent>();
RegisterComponent<RenderableComponent>(); RegisterComponent<RenderableComponent>();
RegisterComponent<ColliderComponent>(); RegisterComponent<ColliderComponent>();
RegisterComponent<CustomComponent>();
// Order here matters:
RegisterSystem<TransformSystem>();
RegisterSystem<PhysicsSystem>();
RegisterSystem<RenderSystem>();
}
Scene::~Scene()
{
}
uint32_t Scene::CreateEntity(const std::string& tag, uint32_t parent)
{
uint32_t id = next_entity_id_++;
signatures_.emplace(id, std::bitset<kMaxComponents>{});
auto t = AddComponent<TransformComponent>(id);
t->position = {0.0f, 0.0f, 0.0f};
t->rotation = {};
t->scale = {1.0f, 1.0f, 1.0f};
t->tag = tag;
t->parent = parent;
return id;
}
uint32_t Scene::getEntity(const std::string& tag, uint32_t parent)
{
return GetSystem<TransformSystem>()->GetChildEntity(parent, tag);
}
size_t Scene::GetComponentSignaturePosition(size_t hash)
{
return component_signature_positions_.at(hash);
}
void Scene::Update(float ts)
{
for (auto& [name, system] : systems_) {
system->OnUpdate(ts);
}
event_system_->DespatchEvents(); // clears event queue
}
// Order here matters:
RegisterSystem<TransformSystem>();
RegisterSystem<PhysicsSystem>();
RegisterSystem<RenderSystem>();
RegisterSystem<CustomBehaviourSystem>();
} }
Scene::~Scene() {}
uint32_t Scene::CreateEntity(const std::string& tag, uint32_t parent) {
uint32_t id = next_entity_id_++;
signatures_.emplace(id, std::bitset<kMaxComponents>{});
auto t = AddComponent<TransformComponent>(id);
t->position = {0.0f, 0.0f, 0.0f};
t->rotation = {};
t->scale = {1.0f, 1.0f, 1.0f};
t->tag = tag;
t->parent = parent;
return id;
}
uint32_t Scene::getEntity(const std::string& tag, uint32_t parent) {
return GetSystem<TransformSystem>()->GetChildEntity(parent, tag);
}
size_t Scene::GetComponentSignaturePosition(size_t hash) {
return component_signature_positions_.at(hash);
}
void Scene::Update(float ts) {
for (auto& [name, system] : systems_) {
system->OnUpdate(ts);
}
event_system_->DespatchEvents(); // clears event queue
}
} // namespace engine

View File

@ -0,0 +1,27 @@
#include "systems/custom_behaviour.h"
#include <typeinfo>
#include "components/custom.h"
#include "components/transform.h"
#include "scene.h"
namespace engine {
CustomBehaviourSystem::CustomBehaviourSystem(Scene* scene)
: System(scene, {typeid(TransformComponent).hash_code(),
typeid(CustomComponent).hash_code()}) {
// constructor here
}
CustomBehaviourSystem::~CustomBehaviourSystem() {}
void CustomBehaviourSystem::OnUpdate(float ts) {
for (uint32_t entity : entities_) {
auto c = scene_->GetComponent<CustomComponent>(entity);
assert(c != nullptr);
c->onUpdate(ts);
}
}
} // namespace engine

View File

@ -17,6 +17,10 @@ set(GAME_SOURCES
) )
if (WIN32) if (WIN32)
option(ENGINETEST_BUILD_WIN32_APP "Build as a standalone win32 app (no console window)" ON)
endif()
if (WIN32 AND ENGINETEST_BUILD_WIN32_APP)
add_executable(${PROJECT_NAME} WIN32 ${GAME_SOURCES} "game.rc") add_executable(${PROJECT_NAME} WIN32 ${GAME_SOURCES} "game.rc")
else() else()
add_executable(${PROJECT_NAME} ${GAME_SOURCES}) add_executable(${PROJECT_NAME} ${GAME_SOURCES})

View File

@ -3,6 +3,7 @@
#include "application.h" #include "application.h"
#include "camera_controller.hpp" #include "camera_controller.hpp"
#include "components/collider.h" #include "components/collider.h"
#include "components/custom.h"
#include "components/renderable.h" #include "components/renderable.h"
#include "components/transform.h" #include "components/transform.h"
#include "input_manager.h" #include "input_manager.h"
@ -129,8 +130,8 @@ void PlayGame(GameSettings settings) {
floor_collider->aabb = {{0.0f, 0.0f, 0.0f}, {10000.0f, 1.0f, 10000.0f}}; floor_collider->aabb = {{0.0f, 0.0f, 0.0f}, {10000.0f, 1.0f, 10000.0f}};
} }
// engine::util::LoadMeshFromFile( //engine::util::LoadMeshFromFile(
// my_scene, app.GetResourcePath("models/astronaut/astronaut.dae")); // my_scene, app.GetResourcePath("models/astronaut/astronaut.dae"));
/* skybox */ /* skybox */
{ {
@ -150,7 +151,8 @@ void PlayGame(GameSettings settings) {
{ {
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("The", 768.0f, width, height); ->GetTextBitmap("ABCDEFGHIJKLMNOPQRSTUVWXYZ12345", 768.0f,
width, height);
uint32_t textbox = my_scene->CreateEntity("textbox"); uint32_t textbox = my_scene->CreateEntity("textbox");
auto textbox_renderable = auto textbox_renderable =
@ -161,8 +163,15 @@ void PlayGame(GameSettings settings) {
textbox_renderable->material->texture_ = textbox_renderable->material->texture_ =
std::make_unique<engine::resources::Texture>( std::make_unique<engine::resources::Texture>(
&app.render_data_, bitmap->data(), width, height, &app.render_data_, bitmap->data(), width, height,
engine::resources::Texture::Filtering::kOff); engine::resources::Texture::Filtering::kBilinear);
textbox_renderable->mesh = GenSphereMesh(app.gfxdev(), 1.0f, 8); textbox_renderable->mesh = GenSphereMesh(app.gfxdev(), 1.0f, 5);
my_scene->GetComponent<engine::TransformComponent>(textbox)->scale.y =
(float)height / (float)width;
my_scene->AddComponent<engine::CustomComponent>(textbox)->onUpdate =
[](float ts) {
/* LOG_INFO("Time step: {}", ts); */
};
} }
app.GameLoop(); app.GameLoop();