This repository has been archived on 2022-10-27. You can view files and clone it, but cannot push or open issues or pull requests.
2022-02-05 20:52:01 +08:00
|
|
|
_Pragma("once")
|
|
|
|
|
2022-02-07 21:38:35 +08:00
|
|
|
#include "tinyengine/pool.hpp"
|
2022-02-05 20:52:01 +08:00
|
|
|
#include "component.hpp"
|
|
|
|
|
|
|
|
class Entity;
|
|
|
|
|
|
|
|
class Context final {
|
|
|
|
public:
|
|
|
|
friend class Entity;
|
|
|
|
Entity* CreateEntity();
|
|
|
|
|
|
|
|
bool HasEntity(Entity* entity);
|
|
|
|
Pool<Entity>::ElemContainer& GetEntities();
|
|
|
|
void DestroyEntity(Entity* entity);
|
|
|
|
int GetEntityNum() const;
|
|
|
|
|
|
|
|
template <typename T, typename... Args>
|
|
|
|
T* CreateComponent(Args... args) {
|
|
|
|
ComponentId id = ComponentIdx::Get<T>();
|
|
|
|
auto it = gComponentPool.find(id);
|
|
|
|
if (it != gComponentPool.end()) {
|
|
|
|
return it->second.Create<T>(args...);
|
|
|
|
} else {
|
|
|
|
gComponentPool[id] = Pool<Component>();
|
|
|
|
return gComponentPool[id].Create<T>(args...);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void DestroyComponent(T* elem) {
|
|
|
|
DestroyComponentById(elem, ComponentIdx::Get<T>());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void DestroyComponentById(Component* elem, int id) {
|
|
|
|
auto it = gComponentPool.find(id);
|
|
|
|
if (it != gComponentPool.end()) {
|
|
|
|
gComponentPool[id].Destroy(elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Pool<Entity> entities_;
|
|
|
|
|
|
|
|
void onComponentUpdate(Entity*, ComponentId, Component*);
|
|
|
|
};
|