mirror of
https://github.com/bailwillharr/engine.git
synced 2024-09-21 04:51:18 +00:00
Use 'Entity' type instead of uint32_t
This commit is contained in:
parent
3f013b2860
commit
604513f44d
@ -12,13 +12,19 @@ project(engine LANGUAGES CXX
|
||||
# from command: find . -regex "^\.\/.*" | sort
|
||||
set(SRC_FILES
|
||||
"src/application.cpp"
|
||||
"src/ecs_system.cpp"
|
||||
"src/ecs.cpp"
|
||||
"src/gfx_device_vulkan.cpp"
|
||||
"src/imgui/imconfig.h"
|
||||
"src/imgui/imgui.cpp"
|
||||
"src/imgui/imgui.h"
|
||||
"src/imgui/imgui_demo.cpp"
|
||||
"src/imgui/imgui_draw.cpp"
|
||||
"src/imgui/imgui_internal.h"
|
||||
"src/imgui/imgui_tables.cpp"
|
||||
"src/imgui/imgui_widgets.cpp"
|
||||
"src/imgui/imstb_rectpack.h"
|
||||
"src/imgui/imstb_textedit.h"
|
||||
"src/imgui/imstb_truetype.h"
|
||||
"src/input_manager.cpp"
|
||||
"src/libs/stb_image.cpp"
|
||||
"src/libs/stb_truetype.cpp"
|
||||
@ -55,7 +61,7 @@ set(INCLUDE_FILES
|
||||
"include/components/mesh_renderable.h"
|
||||
"include/components/ui_renderable.h"
|
||||
"include/components/transform.h"
|
||||
"include/ecs_system.h"
|
||||
"include/ecs.h"
|
||||
"include/engine_api.h"
|
||||
"include/event_system.h"
|
||||
"include/gfx.h"
|
||||
|
@ -7,6 +7,8 @@
|
||||
#include <glm/mat4x4.hpp>
|
||||
#include <glm/vec3.hpp>
|
||||
|
||||
#include "ecs.h"
|
||||
|
||||
namespace engine {
|
||||
|
||||
struct TransformComponent {
|
||||
@ -17,7 +19,7 @@ struct TransformComponent {
|
||||
glm::vec3 scale;
|
||||
|
||||
std::string tag;
|
||||
uint32_t parent;
|
||||
Entity parent;
|
||||
|
||||
bool is_static;
|
||||
};
|
||||
|
@ -1,5 +1,5 @@
|
||||
#ifndef ENGINE_INCLUDE_ECS_SYSTEM_H_
|
||||
#define ENGINE_INCLUDE_ECS_SYSTEM_H_
|
||||
#ifndef ENGINE_INCLUDE_ECS_H_
|
||||
#define ENGINE_INCLUDE_ECS_H_
|
||||
|
||||
#include <bitset>
|
||||
#include <cassert>
|
||||
@ -13,7 +13,9 @@ namespace engine {
|
||||
|
||||
class Scene;
|
||||
|
||||
constexpr size_t kMaxComponents = 64;
|
||||
using Entity = uint32_t; // ECS entity
|
||||
|
||||
constexpr size_t kMaxComponents = 10;
|
||||
|
||||
class IComponentArray {
|
||||
public:
|
||||
@ -23,7 +25,7 @@ class IComponentArray {
|
||||
template <typename T>
|
||||
class ComponentArray : public IComponentArray {
|
||||
public:
|
||||
void InsertData(uint32_t entity, T component) {
|
||||
void InsertData(Entity entity, T component) {
|
||||
if (component_array_.size() < entity + 1) {
|
||||
component_array_.resize(entity + 1);
|
||||
}
|
||||
@ -31,11 +33,11 @@ class ComponentArray : public IComponentArray {
|
||||
component_array_.at(entity) = component;
|
||||
}
|
||||
|
||||
void DeleteData(uint32_t entity) {
|
||||
void DeleteData(Entity entity) {
|
||||
(void)entity; // TODO
|
||||
}
|
||||
|
||||
T* GetData(uint32_t entity) {
|
||||
T* GetData(Entity entity) {
|
||||
assert(entity < component_array_.size());
|
||||
return &component_array_[entity];
|
||||
}
|
||||
@ -53,15 +55,15 @@ class System {
|
||||
|
||||
virtual void OnUpdate(float ts) = 0;
|
||||
|
||||
virtual void OnComponentInsert(uint32_t) {}
|
||||
virtual void OnComponentRemove(uint32_t) {}
|
||||
virtual void OnComponentInsert(Entity) {}
|
||||
virtual void OnComponentRemove(Entity) {}
|
||||
|
||||
Scene* const scene_;
|
||||
|
||||
std::bitset<kMaxComponents> signature_;
|
||||
|
||||
// entities that contain the needed components
|
||||
std::set<uint32_t> entities_{};
|
||||
std::set<Entity> entities_{};
|
||||
};
|
||||
|
||||
} // namespace engine
|
@ -9,7 +9,7 @@
|
||||
|
||||
#include <glm/vec3.hpp>
|
||||
|
||||
#include "ecs_system.h"
|
||||
#include "ecs.h"
|
||||
#include "event_system.h"
|
||||
|
||||
namespace engine {
|
||||
@ -31,10 +31,10 @@ class Scene {
|
||||
|
||||
/* ecs stuff */
|
||||
|
||||
uint32_t CreateEntity(const std::string& tag, uint32_t parent = 0,
|
||||
Entity CreateEntity(const std::string& tag, Entity parent = 0,
|
||||
const glm::vec3& pos = glm::vec3{0.0f, 0.0f, 0.0f});
|
||||
|
||||
uint32_t GetEntity(const std::string& tag, uint32_t parent = 0);
|
||||
Entity GetEntity(const std::string& tag, Entity parent = 0);
|
||||
|
||||
size_t GetComponentSignaturePosition(size_t hash);
|
||||
|
||||
@ -54,13 +54,13 @@ class Scene {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T* GetComponent(uint32_t entity) {
|
||||
T* GetComponent(Entity entity) {
|
||||
auto array = GetComponentArray<T>();
|
||||
return array->GetData(entity);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T* AddComponent(uint32_t entity) {
|
||||
T* AddComponent(Entity entity) {
|
||||
size_t hash = typeid(T).hash_code();
|
||||
|
||||
auto array = GetComponentArray<T>();
|
||||
@ -107,7 +107,7 @@ class Scene {
|
||||
|
||||
private:
|
||||
Application* const app_;
|
||||
uint32_t next_entity_id_ = 1; // 0 is not a valid entity
|
||||
Entity next_entity_id_ = 1; // 0 is not a valid entity
|
||||
|
||||
uint64_t framecount_ = 0;
|
||||
|
||||
@ -117,7 +117,7 @@ class Scene {
|
||||
// maps component hashes to signature positions
|
||||
std::unordered_map<size_t, size_t> component_signature_positions_{};
|
||||
// maps entity ids to their signatures
|
||||
std::unordered_map<uint32_t, std::bitset<kMaxComponents>> signatures_{};
|
||||
std::unordered_map<Entity, std::bitset<kMaxComponents>> signatures_{};
|
||||
// maps component hashes to their arrays
|
||||
std::unordered_map<size_t, std::unique_ptr<IComponentArray>>
|
||||
component_arrays_{};
|
||||
|
@ -7,7 +7,7 @@
|
||||
#include <glm/mat4x4.hpp>
|
||||
|
||||
#include "components/collider.h"
|
||||
#include "ecs_system.h"
|
||||
#include "ecs.h"
|
||||
|
||||
namespace engine {
|
||||
|
||||
@ -17,11 +17,11 @@ class PhysicsSystem : public System {
|
||||
|
||||
void OnUpdate(float ts) override;
|
||||
|
||||
void OnComponentInsert(uint32_t entity) override;
|
||||
void OnComponentInsert(Entity entity) override;
|
||||
|
||||
struct CollisionEvent {
|
||||
bool is_collision_enter; // false == collision exit
|
||||
uint32_t collided_entity; // the entity that this entity collided with
|
||||
Entity collided_entity; // the entity that this entity collided with
|
||||
glm::vec3 normal; // the normal of the surface this entity collided with;
|
||||
// ignored on collision exit
|
||||
glm::vec3 point; // where the collision was detected
|
||||
@ -30,12 +30,12 @@ class PhysicsSystem : public System {
|
||||
private:
|
||||
// dynamic arrays to avoid realloc on every frame
|
||||
// entity, aabb, is_trigger
|
||||
std::vector<std::tuple<uint32_t, AABB, bool>> static_aabbs_{};
|
||||
std::vector<std::tuple<uint32_t, AABB, bool>> dynamic_aabbs_{};
|
||||
std::vector<std::tuple<Entity, AABB, bool>> static_aabbs_{};
|
||||
std::vector<std::tuple<Entity, AABB, bool>> dynamic_aabbs_{};
|
||||
|
||||
struct PossibleCollision {
|
||||
PossibleCollision(uint32_t static_entity, AABB static_aabb,
|
||||
bool static_trigger, uint32_t dynamic_entity,
|
||||
PossibleCollision(Entity static_entity, AABB static_aabb,
|
||||
bool static_trigger, Entity dynamic_entity,
|
||||
AABB dynamic_aabb, bool dynamic_trigger)
|
||||
: static_entity(static_entity),
|
||||
static_aabb(static_aabb),
|
||||
@ -44,15 +44,15 @@ class PhysicsSystem : public System {
|
||||
dynamic_aabb(dynamic_aabb),
|
||||
dynamic_trigger(dynamic_trigger) {}
|
||||
|
||||
uint32_t static_entity;
|
||||
Entity static_entity;
|
||||
AABB static_aabb;
|
||||
bool static_trigger;
|
||||
uint32_t dynamic_entity;
|
||||
Entity dynamic_entity;
|
||||
AABB dynamic_aabb;
|
||||
bool dynamic_trigger;
|
||||
};
|
||||
std::vector<PossibleCollision> possible_collisions_{};
|
||||
std::vector<std::pair<uint32_t, CollisionEvent>>
|
||||
std::vector<std::pair<Entity, CollisionEvent>>
|
||||
collision_infos_{}; // target entity, event info
|
||||
};
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
#include "ecs_system.h"
|
||||
#include "ecs.h"
|
||||
|
||||
/* This system allows for one-off custom components that execute arbitrary code
|
||||
* It is similar to Unity's 'MonoBehavior' system */
|
||||
@ -16,10 +16,10 @@ class CustomBehaviourSystem : public System {
|
||||
~CustomBehaviourSystem();
|
||||
|
||||
void OnUpdate(float ts) override;
|
||||
void OnComponentInsert(uint32_t entity) override;
|
||||
void OnComponentInsert(Entity entity) override;
|
||||
|
||||
private:
|
||||
std::unordered_map<uint32_t, bool> entity_is_initialised_{};
|
||||
std::unordered_map<Entity, bool> entity_is_initialised_{};
|
||||
};
|
||||
|
||||
} // namespace engine
|
||||
|
@ -3,7 +3,7 @@
|
||||
|
||||
#include <glm/mat4x4.hpp>
|
||||
|
||||
#include "ecs_system.h"
|
||||
#include "ecs.h"
|
||||
#include "scene.h"
|
||||
#include "gfx.h"
|
||||
|
||||
@ -29,7 +29,7 @@ class MeshRenderSystem : public System {
|
||||
const RenderList* GetStaticRenderList() const { return &static_render_list_; }
|
||||
const RenderList* GetDynamicRenderList() const { return &dynamic_render_list_; }
|
||||
|
||||
void OnComponentInsert(uint32_t entity) override;
|
||||
void OnComponentInsert(Entity entity) override;
|
||||
void OnUpdate(float ts) override;
|
||||
|
||||
private:
|
||||
|
@ -1,7 +1,7 @@
|
||||
#ifndef ENGINE_INCLUDE_SYSTEMS_TRANSFORM_H_
|
||||
#define ENGINE_INCLUDE_SYSTEMS_TRANSFORM_H_
|
||||
|
||||
#include "ecs_system.h"
|
||||
#include "ecs.h"
|
||||
|
||||
namespace engine {
|
||||
|
||||
@ -12,7 +12,7 @@ namespace engine {
|
||||
|
||||
void OnUpdate(float ts) override;
|
||||
|
||||
uint32_t GetChildEntity(uint32_t parent, const std::string& tag);
|
||||
Entity GetChildEntity(Entity parent, const std::string& tag);
|
||||
|
||||
};
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
#include <glm/mat4x4.hpp>
|
||||
|
||||
#include "ecs_system.h"
|
||||
#include "ecs.h"
|
||||
#include "scene.h"
|
||||
#include "gfx.h"
|
||||
|
||||
@ -13,7 +13,7 @@ namespace engine {
|
||||
UIRenderSystem(Scene* scene);
|
||||
~UIRenderSystem();
|
||||
|
||||
void OnComponentInsert(uint32_t entity) override;
|
||||
void OnComponentInsert(Entity entity) override;
|
||||
void OnUpdate(float ts) override;
|
||||
|
||||
private:
|
||||
|
@ -8,7 +8,7 @@
|
||||
namespace engine {
|
||||
namespace util {
|
||||
|
||||
uint32_t LoadMeshFromFile(Scene* parent, const std::string& path, bool is_static = false);
|
||||
Entity LoadMeshFromFile(Scene* parent, const std::string& path, bool is_static = false);
|
||||
|
||||
} // namespace util
|
||||
} // namespace engine
|
||||
|
@ -1,4 +1,4 @@
|
||||
#include "ecs_system.h"
|
||||
#include "ecs.h"
|
||||
|
||||
#include "scene.h"
|
||||
|
@ -35,9 +35,9 @@ Scene::Scene(Application* app) : app_(app) {
|
||||
|
||||
Scene::~Scene() {}
|
||||
|
||||
uint32_t Scene::CreateEntity(const std::string& tag, uint32_t parent,
|
||||
Entity Scene::CreateEntity(const std::string& tag, Entity parent,
|
||||
const glm::vec3& pos) {
|
||||
uint32_t id = next_entity_id_++;
|
||||
Entity id = next_entity_id_++;
|
||||
|
||||
signatures_.emplace(id, std::bitset<kMaxComponents>{});
|
||||
|
||||
@ -53,7 +53,7 @@ uint32_t Scene::CreateEntity(const std::string& tag, uint32_t parent,
|
||||
return id;
|
||||
}
|
||||
|
||||
uint32_t Scene::GetEntity(const std::string& tag, uint32_t parent) {
|
||||
Entity Scene::GetEntity(const std::string& tag, Entity parent) {
|
||||
return GetSystem<TransformSystem>()->GetChildEntity(parent, tag);
|
||||
}
|
||||
|
||||
|
@ -71,7 +71,7 @@ namespace engine {
|
||||
scene_->event_system()->RegisterEventType<CollisionEvent>();
|
||||
}
|
||||
|
||||
void PhysicsSystem::OnComponentInsert(uint32_t entity)
|
||||
void PhysicsSystem::OnComponentInsert(Entity entity)
|
||||
{
|
||||
(void)entity;
|
||||
const size_t size = entities_.size();
|
||||
@ -91,7 +91,7 @@ namespace engine {
|
||||
possible_collisions_.clear();
|
||||
collision_infos_.clear();
|
||||
|
||||
for (uint32_t entity : entities_) {
|
||||
for (Entity entity : entities_) {
|
||||
const auto t = scene_->GetComponent<TransformComponent>(entity);
|
||||
const auto c = scene_->GetComponent<ColliderComponent>(entity);
|
||||
|
||||
|
@ -17,7 +17,7 @@ CustomBehaviourSystem::CustomBehaviourSystem(Scene* scene)
|
||||
CustomBehaviourSystem::~CustomBehaviourSystem() {}
|
||||
|
||||
void CustomBehaviourSystem::OnUpdate(float ts) {
|
||||
for (uint32_t entity : entities_) {
|
||||
for (Entity entity : entities_) {
|
||||
auto c = scene_->GetComponent<CustomComponent>(entity);
|
||||
assert(c != nullptr);
|
||||
bool& entity_initialised = entity_is_initialised_.at(entity);
|
||||
@ -31,7 +31,7 @@ void CustomBehaviourSystem::OnUpdate(float ts) {
|
||||
}
|
||||
}
|
||||
|
||||
void CustomBehaviourSystem::OnComponentInsert(uint32_t entity)
|
||||
void CustomBehaviourSystem::OnComponentInsert(Entity entity)
|
||||
{
|
||||
entity_is_initialised_.emplace(entity, false);
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ void MeshRenderSystem::RebuildStaticRenderList() {
|
||||
list_needs_rebuild_ = false;
|
||||
}
|
||||
|
||||
void MeshRenderSystem::OnComponentInsert(uint32_t entity) {
|
||||
void MeshRenderSystem::OnComponentInsert(Entity entity) {
|
||||
(void)entity;
|
||||
list_needs_rebuild_ = true;
|
||||
}
|
||||
@ -42,7 +42,7 @@ void MeshRenderSystem::BuildRenderList(RenderList& render_list,
|
||||
|
||||
std::unordered_map<const gfx::Pipeline*, int> render_orders;
|
||||
|
||||
for (uint32_t entity : entities_) {
|
||||
for (Entity entity : entities_) {
|
||||
auto transform = scene_->GetComponent<engine::TransformComponent>(entity);
|
||||
|
||||
if (transform->is_static != with_static_entities) continue;
|
||||
|
@ -13,7 +13,7 @@ TransformSystem::TransformSystem(Scene* scene)
|
||||
void TransformSystem::OnUpdate(float ts) {
|
||||
(void)ts;
|
||||
|
||||
for (uint32_t entity : entities_) {
|
||||
for (Entity entity : entities_) {
|
||||
auto t = scene_->GetComponent<TransformComponent>(entity);
|
||||
|
||||
glm::mat4 transform;
|
||||
@ -37,9 +37,9 @@ void TransformSystem::OnUpdate(float ts) {
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t TransformSystem::GetChildEntity(uint32_t parent,
|
||||
Entity TransformSystem::GetChildEntity(Entity parent,
|
||||
const std::string& tag) {
|
||||
for (uint32_t entity : entities_) {
|
||||
for (Entity entity : entities_) {
|
||||
auto t = scene_->GetComponent<TransformComponent>(entity);
|
||||
if (t->parent == parent) {
|
||||
if (t->tag == tag) {
|
||||
|
@ -11,7 +11,7 @@ namespace engine {
|
||||
|
||||
UIRenderSystem::~UIRenderSystem() {}
|
||||
|
||||
void UIRenderSystem::OnComponentInsert(uint32_t entity) {
|
||||
void UIRenderSystem::OnComponentInsert(Entity entity) {
|
||||
(void)entity;
|
||||
}
|
||||
|
||||
|
@ -31,7 +31,7 @@ namespace engine::util {
|
||||
const std::map<int, std::shared_ptr<resources::Texture>>& textures,
|
||||
const std::vector<std::shared_ptr<resources::Mesh>>& meshes,
|
||||
const std::vector<unsigned int>& meshTextureIndices,
|
||||
aiNode* parentNode, Scene* scene, uint32_t parentObj, bool is_static)
|
||||
aiNode* parentNode, Scene* scene, Entity parentObj, bool is_static)
|
||||
{
|
||||
|
||||
// convert to glm column major
|
||||
@ -99,7 +99,7 @@ namespace engine::util {
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t LoadMeshFromFile(Scene* parent, const std::string& path, bool is_static)
|
||||
Entity LoadMeshFromFile(Scene* parent, const std::string& path, bool is_static)
|
||||
{
|
||||
Assimp::Importer importer;
|
||||
|
||||
@ -231,7 +231,7 @@ namespace engine::util {
|
||||
indices));
|
||||
}
|
||||
|
||||
uint32_t obj = parent->CreateEntity(scene->GetShortFilename(path.c_str()));
|
||||
Entity obj = parent->CreateEntity(scene->GetShortFilename(path.c_str()));
|
||||
|
||||
buildGraph(textures, meshes, meshMaterialIndices, scene->mRootNode, parent, obj, is_static);
|
||||
|
||||
|
@ -7,6 +7,7 @@
|
||||
|
||||
#include "application.h"
|
||||
#include "components/transform.h"
|
||||
#include "ecs.h"
|
||||
#include "input_manager.h"
|
||||
#include "log.h"
|
||||
#include "scene.h"
|
||||
@ -19,7 +20,7 @@ CameraControllerSystem::CameraControllerSystem(engine::Scene* scene)
|
||||
|
||||
void CameraControllerSystem::OnUpdate(float ts) {
|
||||
if (t == nullptr || c == nullptr) {
|
||||
for (uint32_t entity : entities_) {
|
||||
for (engine::Entity entity : entities_) {
|
||||
t = scene_->GetComponent<engine::TransformComponent>(entity);
|
||||
c = scene_->GetComponent<CameraControllerComponent>(entity);
|
||||
break;
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include <glm/vec3.hpp>
|
||||
|
||||
#include "components/transform.h"
|
||||
#include "ecs_system.h"
|
||||
#include "ecs.h"
|
||||
|
||||
struct CameraControllerComponent {
|
||||
static constexpr float kWalkSpeed = 4.0f;
|
||||
|
@ -86,7 +86,7 @@ void PlayGame(GameSettings settings) {
|
||||
|
||||
/* skybox */
|
||||
{
|
||||
uint32_t skybox = my_scene->CreateEntity("skybox");
|
||||
engine::Entity skybox = my_scene->CreateEntity("skybox");
|
||||
|
||||
auto skybox_renderable =
|
||||
my_scene->AddComponent<engine::MeshRenderableComponent>(skybox);
|
||||
@ -105,7 +105,7 @@ void PlayGame(GameSettings settings) {
|
||||
|
||||
/* floor */
|
||||
{
|
||||
uint32_t floor = engine::util::LoadMeshFromFile(
|
||||
engine::Entity floor = engine::util::LoadMeshFromFile(
|
||||
my_scene, app.GetResourcePath("models/terrain.dae"), true);
|
||||
|
||||
auto floor_transform =
|
||||
@ -134,7 +134,7 @@ void PlayGame(GameSettings settings) {
|
||||
|
||||
/* some text */
|
||||
{
|
||||
uint32_t textbox =
|
||||
engine::Entity textbox =
|
||||
my_scene->CreateEntity("textbox", 0, glm::vec3{0.0f, 0.8f, 0.0f});
|
||||
auto textboxComponent =
|
||||
my_scene->AddComponent<engine::CustomComponent>(textbox);
|
||||
@ -153,7 +153,11 @@ void PlayGame(GameSettings settings) {
|
||||
}
|
||||
|
||||
/* teapot */
|
||||
my_scene->GetComponent<engine::TransformComponent>(engine::util::LoadMeshFromFile(my_scene, app.GetResourcePath("models/teapot.dae"), true))->position += glm::vec3{10.0f, 10.0f, 10.0f};
|
||||
my_scene
|
||||
->GetComponent<engine::TransformComponent>(
|
||||
engine::util::LoadMeshFromFile(
|
||||
my_scene, app.GetResourcePath("models/teapot.dae"), true))
|
||||
->position += glm::vec3{10.0f, 10.0f, 10.0f};
|
||||
|
||||
app.GameLoop();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user