mirror of
https://github.com/bailwillharr/engine.git
synced 2024-09-21 04:51:18 +00:00
Begin adding custom behaviour system
This commit is contained in:
parent
ae8091f2a4
commit
e57be9a0e7
@ -25,6 +25,7 @@ set(SRC_FILES
|
||||
"src/scene.cpp"
|
||||
"src/scene_manager.cpp"
|
||||
"src/systems/collisions.cpp"
|
||||
"src/systems/custom_behaviour.cpp"
|
||||
"src/systems/render.cpp"
|
||||
"src/systems/transform.cpp"
|
||||
"src/util/files.cpp"
|
||||
@ -43,6 +44,7 @@ set(SRC_FILES
|
||||
set(INCLUDE_FILES
|
||||
"include/application.h"
|
||||
"include/components/collider.h"
|
||||
"include/components/custom.h"
|
||||
"include/components/renderable.h"
|
||||
"include/components/transform.h"
|
||||
"include/ecs_system.h"
|
||||
@ -64,6 +66,7 @@ set(INCLUDE_FILES
|
||||
"include/scene.h"
|
||||
"include/scene_manager.h"
|
||||
"include/systems/collisions.h"
|
||||
"include/systems/custom_behaviour.h"
|
||||
"include/systems/render.h"
|
||||
"include/systems/transform.h"
|
||||
"include/util.h"
|
||||
|
14
include/components/custom.h
Normal file
14
include/components/custom.h
Normal 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
|
24
include/systems/custom_behaviour.h
Normal file
24
include/systems/custom_behaviour.h
Normal 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
|
@ -1602,7 +1602,7 @@ namespace engine {
|
||||
LOG_INFO("GPU Memory Statistics:");
|
||||
|
||||
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];
|
||||
LOG_INFO("Memory heap {}", i);
|
||||
if (heap.flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) {
|
||||
|
@ -82,7 +82,7 @@ std::unique_ptr<std::vector<uint8_t>> Font::GetTextBitmap(
|
||||
bitmap->at(i * 4 + 0) = 0x00;
|
||||
bitmap->at(i * 4 + 1) = 0x00;
|
||||
bitmap->at(i * 4 + 2) = 0x00;
|
||||
bitmap->at(i * 4 + 3) = 0xFF;
|
||||
bitmap->at(i * 4 + 3) = 0x00;
|
||||
}
|
||||
|
||||
int top_left_x = 0;
|
||||
|
@ -3,15 +3,15 @@
|
||||
#include "components/transform.h"
|
||||
#include "components/renderable.h"
|
||||
#include "components/collider.h"
|
||||
#include "components/custom.h"
|
||||
#include "systems/transform.h"
|
||||
#include "systems/render.h"
|
||||
#include "systems/collisions.h"
|
||||
#include "systems/custom_behaviour.h"
|
||||
|
||||
namespace engine {
|
||||
|
||||
Scene::Scene(Application* app)
|
||||
: app_(app)
|
||||
{
|
||||
Scene::Scene(Application* app) : app_(app) {
|
||||
// event system
|
||||
event_system_ = std::make_unique<EventSystem>();
|
||||
|
||||
@ -20,19 +20,18 @@ namespace engine {
|
||||
RegisterComponent<TransformComponent>();
|
||||
RegisterComponent<RenderableComponent>();
|
||||
RegisterComponent<ColliderComponent>();
|
||||
RegisterComponent<CustomComponent>();
|
||||
|
||||
// Order here matters:
|
||||
RegisterSystem<TransformSystem>();
|
||||
RegisterSystem<PhysicsSystem>();
|
||||
RegisterSystem<RenderSystem>();
|
||||
}
|
||||
RegisterSystem<CustomBehaviourSystem>();
|
||||
}
|
||||
|
||||
Scene::~Scene()
|
||||
{
|
||||
}
|
||||
Scene::~Scene() {}
|
||||
|
||||
uint32_t Scene::CreateEntity(const std::string& tag, uint32_t parent)
|
||||
{
|
||||
uint32_t Scene::CreateEntity(const std::string& tag, uint32_t parent) {
|
||||
uint32_t id = next_entity_id_++;
|
||||
|
||||
signatures_.emplace(id, std::bitset<kMaxComponents>{});
|
||||
@ -47,25 +46,22 @@ namespace engine {
|
||||
t->parent = parent;
|
||||
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t Scene::getEntity(const std::string& tag, uint32_t parent)
|
||||
{
|
||||
uint32_t Scene::getEntity(const std::string& tag, uint32_t parent) {
|
||||
return GetSystem<TransformSystem>()->GetChildEntity(parent, tag);
|
||||
}
|
||||
}
|
||||
|
||||
size_t Scene::GetComponentSignaturePosition(size_t hash)
|
||||
{
|
||||
size_t Scene::GetComponentSignaturePosition(size_t hash) {
|
||||
return component_signature_positions_.at(hash);
|
||||
}
|
||||
}
|
||||
|
||||
void Scene::Update(float ts)
|
||||
{
|
||||
void Scene::Update(float ts) {
|
||||
for (auto& [name, system] : systems_) {
|
||||
system->OnUpdate(ts);
|
||||
}
|
||||
|
||||
event_system_->DespatchEvents(); // clears event queue
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} // namespace engine
|
||||
|
27
src/systems/custom_behaviour.cpp
Normal file
27
src/systems/custom_behaviour.cpp
Normal 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
|
@ -17,6 +17,10 @@ set(GAME_SOURCES
|
||||
)
|
||||
|
||||
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")
|
||||
else()
|
||||
add_executable(${PROJECT_NAME} ${GAME_SOURCES})
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include "application.h"
|
||||
#include "camera_controller.hpp"
|
||||
#include "components/collider.h"
|
||||
#include "components/custom.h"
|
||||
#include "components/renderable.h"
|
||||
#include "components/transform.h"
|
||||
#include "input_manager.h"
|
||||
@ -129,7 +130,7 @@ void PlayGame(GameSettings settings) {
|
||||
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"));
|
||||
|
||||
/* skybox */
|
||||
@ -150,7 +151,8 @@ void PlayGame(GameSettings settings) {
|
||||
{
|
||||
int width, height;
|
||||
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");
|
||||
auto textbox_renderable =
|
||||
@ -161,8 +163,15 @@ void PlayGame(GameSettings settings) {
|
||||
textbox_renderable->material->texture_ =
|
||||
std::make_unique<engine::resources::Texture>(
|
||||
&app.render_data_, bitmap->data(), width, height,
|
||||
engine::resources::Texture::Filtering::kOff);
|
||||
textbox_renderable->mesh = GenSphereMesh(app.gfxdev(), 1.0f, 8);
|
||||
engine::resources::Texture::Filtering::kBilinear);
|
||||
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();
|
||||
|
Loading…
Reference in New Issue
Block a user