Archived
1
Fork 0
This repository has been archived on 2025-04-12. You can view files and clone it, but cannot push or open issues or pull requests.
graph/include/ecs.h
2018-12-21 06:32:37 -05:00

136 lines
3.5 KiB
C++

#pragma once
#include <cstdint>
#include <map>
#include <array>
#include <glm/glm.hpp>
#include <type_traits>
struct InfoComponent {
std::string name, tag;
};
struct TransformComponent {
glm::vec3 position = glm::vec3(0);
};
struct MeshAsset;
struct MaterialAsset;
struct MeshComponent {
MeshAsset* mesh = nullptr;
MaterialAsset* material = nullptr;
};
enum class LightType {
Point,
Directional
};
struct LightComponent {
LightType type = LightType::Point;
glm::vec3 color = glm::vec3(1);
glm::mat4 matrix = glm::mat4(1.0f);
};
struct CameraComponent {
float fov = 75.0f, near = 0.1f, far = 100.0f;
float focusDistance = 0.0f;
float aperture = 0.0f;
glm::vec3 target = glm::vec3(0);
};
using EntityID = uint64_t;
struct World;
namespace ECS {
// components
inline std::map<EntityID, InfoComponent*> infos;
inline std::map<EntityID, TransformComponent*> transforms;
inline std::map<EntityID, MeshComponent*> meshes;
inline std::map<EntityID, LightComponent*> lights;
inline std::map<EntityID, CameraComponent*> cameras;
inline std::map<EntityID, ::World*> worlds;
inline EntityID lastID = 1;
static inline EntityID createEntity(World* world) {
EntityID newID = lastID++;
worlds[newID] = world;
return newID;
}
static inline void destroyEntity(const EntityID id) {
}
template<class T>
static inline T* addComponent(const EntityID id) {
T* t = new T();
if constexpr(std::is_same<T, InfoComponent>::value) {
infos[id] = t;
} else if constexpr(std::is_same<T, TransformComponent>::value) {
transforms[id] = t;
} else if constexpr(std::is_same<T, MeshComponent>::value) {
meshes[id] = t;
} else if constexpr(std::is_same<T, LightComponent>::value) {
lights[id] = t;
} else if constexpr(std::is_same<T, CameraComponent>::value) {
cameras[id] = t;
}
return t;
}
template<class T>
static inline T* getComponent(const EntityID id) {
if constexpr(std::is_same<T, InfoComponent>::value) {
return infos[id];
} else if constexpr(std::is_same<T, TransformComponent>::value) {
return transforms[id];
} else if constexpr(std::is_same<T, MeshComponent>::value) {
return meshes[id];
} else if constexpr(std::is_same<T, LightComponent>::value) {
return lights[id];
} else if constexpr(std::is_same<T, CameraComponent>::value) {
return cameras[id];
}
}
template<class T>
static inline auto getWorldComponents(World* world) {
std::vector<EntityID> entities;
for (auto it = worlds.begin(); it != worlds.end(); ++it) {
if(it->second == world) {
entities.push_back(it->first);
}
}
std::vector<std::tuple<EntityID, T*>> components;
for(auto ent : entities) {
T* t = getComponent<T>(ent);
if(t != nullptr)
components.push_back(std::tuple(ent, t));
}
return components;
}
static inline std::vector<EntityID> getWorldEntities(World* world) {
std::vector<EntityID> entities;
for (auto it = worlds.begin(); it != worlds.end(); ++it) {
if(it->second == world)
entities.push_back(it->first);
}
return entities;
}
template<class T>
static inline void removeComponent(const EntityID id) {
delete transforms[id];
}
};