Compare commits
2 Commits
4166103b68
...
42b1c2aeb8
Author | SHA1 | Date | |
---|---|---|---|
42b1c2aeb8 | |||
eb7bb26804 |
@ -1,19 +0,0 @@
|
|||||||
#ifndef POLYGUN_ECS_BASE_COMPONENT_HPP
|
|
||||||
#define POLYGUN_ECS_BASE_COMPONENT_HPP
|
|
||||||
|
|
||||||
#include "ecs_types.hpp"
|
|
||||||
|
|
||||||
namespace polygun::ecs {
|
|
||||||
struct BaseComponent {
|
|
||||||
BaseComponent() = default;
|
|
||||||
~BaseComponent() = default;
|
|
||||||
|
|
||||||
inline const EntityID get_id() const { return m_entity_id; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
EntityID m_entity_id = -1;
|
|
||||||
friend class EntityManager;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // POLYGUN_ECS_BASE_COMPONENT_HPP
|
|
@ -1,36 +0,0 @@
|
|||||||
#ifndef POLYGUN_ECS_BASE_SYSTEM_HPP
|
|
||||||
#define POLYGUN_ECS_BASE_SYSTEM_HPP
|
|
||||||
|
|
||||||
#include "ecs_types.hpp"
|
|
||||||
|
|
||||||
namespace polygun::ecs {
|
|
||||||
|
|
||||||
class BaseSystem {
|
|
||||||
public:
|
|
||||||
BaseSystem() = default;
|
|
||||||
virtual ~BaseSystem() = default;
|
|
||||||
|
|
||||||
inline const Signature get_signature() const { return m_signature; }
|
|
||||||
|
|
||||||
inline void erase_entity(const EntityID entity) { m_entities.erase(entity); }
|
|
||||||
inline void push_entity(const EntityID entity) { m_entities.insert(entity); }
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
inline void add_component_signature() { m_signature.insert(CompType<T>()); }
|
|
||||||
|
|
||||||
inline const bool is_empty() const { return (m_entities.size() == 0);}
|
|
||||||
inline const bool has_entity(EntityID entity) const { return (m_entities.count(entity) > 0);}
|
|
||||||
|
|
||||||
virtual void stop() {}
|
|
||||||
virtual void start() {}
|
|
||||||
virtual void render() {}
|
|
||||||
virtual void update() {}
|
|
||||||
virtual void awake() {}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
Signature m_signature;
|
|
||||||
std::set<EntityID> m_entities;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // POLYGUN_ECS_BASE_SYSTEM_HPP
|
|
@ -1,60 +0,0 @@
|
|||||||
#ifndef POLYGUN_ECS_BASE_COMPONENT_HPP
|
|
||||||
#define POLYGUN_ECS_BASE_COMPONENT_HPP
|
|
||||||
|
|
||||||
#include "base_component.hpp"
|
|
||||||
#include "game/core.hpp"
|
|
||||||
|
|
||||||
namespace polygun::ecs {
|
|
||||||
|
|
||||||
using Constructor = std::function<FactoryType()>;
|
|
||||||
|
|
||||||
class ComponentFactory {
|
|
||||||
|
|
||||||
public:
|
|
||||||
~ComponentFactory() = default;
|
|
||||||
ComponentFactory(const ComponentFactory&) = delete;
|
|
||||||
ComponentFactory& operator=(const ComponentFactory&) = delete;
|
|
||||||
static ComponentFactory& ref() {
|
|
||||||
static ComponentFactory reference;
|
|
||||||
return reference;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto create_component(const char* type_name) {
|
|
||||||
auto it = m_registry_funcs.find(type_name);
|
|
||||||
assert(it != m_registry_funcs.end() && "Component Type not found in registry");
|
|
||||||
auto component = it->second();
|
|
||||||
return component;
|
|
||||||
}
|
|
||||||
|
|
||||||
void register_type(ComponentTypeID comp_type, const char* type_name, Constructor callback) {
|
|
||||||
m_registry_funcs[type_name] = callback;
|
|
||||||
m_registry_types[type_name] = comp_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
const ComponentTypeID get_type_id(const char* type_name) {
|
|
||||||
assert(m_registry_types.find(type_name) != m_registry_types.end() && "Typename out of range");
|
|
||||||
return m_registry_types.at(type_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
ComponentFactory() = default;
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::map<const char*, Constructor> m_registry_funcs;
|
|
||||||
std::map<const char*, ComponentTypeID> m_registry_types;
|
|
||||||
};
|
|
||||||
|
|
||||||
static ComponentFactory& CompFactory = ComponentFactory::ref();
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
class Registrar {
|
|
||||||
public:
|
|
||||||
Registrar(const char* type_name) {
|
|
||||||
const ComponentTypeID comp_type = CompType<T>();
|
|
||||||
CompFactory.register_type(comp_type, type_name, []()->FactoryType{ return std::move(std::make_shared<T>()); });
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // POLYGUN_ECS_BASE_COMPONENT_HPP
|
|
@ -1,53 +0,0 @@
|
|||||||
#ifndef POLYGUN_ECS_COMP_LIST_HPP
|
|
||||||
#define POLYGUN_ECS_COMP_LIST_HPP
|
|
||||||
|
|
||||||
#include "ecs_types.hpp"
|
|
||||||
|
|
||||||
namespace polygun::ecs {
|
|
||||||
|
|
||||||
class ICompList {
|
|
||||||
public:
|
|
||||||
ICompList() = default;
|
|
||||||
virtual ~ICompList() = default;
|
|
||||||
virtual void erase(const EntityID entity) { }
|
|
||||||
virtual void insert(BaseComponent* component) { }
|
|
||||||
const ComponentTypeID get_data_type() const { return m_type_id; }
|
|
||||||
protected:
|
|
||||||
ComponentTypeID m_type_id = INVALID_TYPE_ID;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
class CompList : public ICompList {
|
|
||||||
|
|
||||||
public:
|
|
||||||
~CompList() = default;
|
|
||||||
CompList(): m_data({}) {
|
|
||||||
m_type_id = CompType<T>();
|
|
||||||
}
|
|
||||||
|
|
||||||
void insert(BaseComponent* component) override {
|
|
||||||
T comp = *(static_cast<T*>(component));
|
|
||||||
auto it = std::find_if(m_data.begin(), m_data.end(), [&comp](const T& c) { return c.get_id() == comp.get_id(); });
|
|
||||||
assert(it == m_data.end() && "Trying to insert duplicate of component!");
|
|
||||||
m_data.push_back(comp);
|
|
||||||
}
|
|
||||||
|
|
||||||
T& get(const EntityID entity) {
|
|
||||||
auto it = std::find_if(m_data.begin(), m_data.end(), [&](const T& comp) { return comp.get_id() == entity; });
|
|
||||||
assert(it != m_data.end() && "Trying to get non-existing component!");
|
|
||||||
return *it;
|
|
||||||
}
|
|
||||||
|
|
||||||
void erase(const EntityID entity) override {
|
|
||||||
auto it = std::find_if(m_data.begin(), m_data.end(), [&entity](const T& comp) { return comp.get_id() == entity; });
|
|
||||||
if (it != m_data.end()) {
|
|
||||||
m_data.erase(it);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::vector<T> m_data;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // POLYGUN_ECS_COMP_LIST_HPP
|
|
@ -1,49 +0,0 @@
|
|||||||
#ifndef POLYGUN_ECS_TYPES_HPP
|
|
||||||
#define POLYGUN_ECS_TYPES_HPP
|
|
||||||
|
|
||||||
#include "game/core.hpp"
|
|
||||||
|
|
||||||
namespace polygun::ecs {
|
|
||||||
|
|
||||||
class Entity;
|
|
||||||
class BaseSystem;
|
|
||||||
class BaseComponent;
|
|
||||||
class EntityManager;
|
|
||||||
|
|
||||||
// global constantes
|
|
||||||
constexpr int INVALID_TYPE_ID = 0;
|
|
||||||
constexpr int INVALID_ENTITY = -1;
|
|
||||||
constexpr int MAX_COMP_COUNT = 32;
|
|
||||||
constexpr int MAX_ENTITY_COUNT = 5000;
|
|
||||||
|
|
||||||
// typedefs
|
|
||||||
using EntityID = int;
|
|
||||||
using SystemTypeID = int;
|
|
||||||
using ComponentTypeID = int;
|
|
||||||
using Signature = std::set<ComponentTypeID>;
|
|
||||||
using FactoryType = std::shared_ptr<BaseComponent>;
|
|
||||||
using EntityList = std::vector<Entity>;
|
|
||||||
using EntityIDList = std::vector<EntityID>;
|
|
||||||
|
|
||||||
// Runtime type
|
|
||||||
inline int get_runtime_type_id() {
|
|
||||||
static int type_id = 1u;
|
|
||||||
return type_id++;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
inline SystemTypeID system_type() noexcept {
|
|
||||||
static_assert((std::is_base_of<BaseSystem, T>::value && !std::is_same<BaseSystem, T>::value), "Invalid template type");
|
|
||||||
static const SystemTypeID type_id = get_runtime_type_id();
|
|
||||||
return type_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
inline ComponentTypeID comp_type() noexcept {
|
|
||||||
static_assert((std::is_base_of<BaseComponent, T>::value && !std::is_same<BaseComponent, T>::value), "Invalid template type");
|
|
||||||
static const ComponentTypeID type_id = get_runtime_type_id();
|
|
||||||
return type_id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // POLYGUN_ECS_TYPES_HPP
|
|
22
src/game/engine/entity.hpp
Normal file
22
src/game/engine/entity.hpp
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#ifndef POLYGUN_ENGINE_ENTITY_HPP
|
||||||
|
#define POLYGUN_ENGINE_ENTITY_HPP
|
||||||
|
|
||||||
|
namespace polygun::engine {
|
||||||
|
class Entity {
|
||||||
|
public:
|
||||||
|
virtual Entity();
|
||||||
|
virtual ~Entity();
|
||||||
|
|
||||||
|
virtual void start();
|
||||||
|
virtual void update(double delta) = 0;
|
||||||
|
virtual void render();
|
||||||
|
|
||||||
|
glm::vec3 m_position, m_rotation, m_size;
|
||||||
|
renderer::Texture* m_texture;
|
||||||
|
renderer::Mesh* m_mesh;
|
||||||
|
};
|
||||||
|
|
||||||
|
class EntityManager {};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // POLYGUN_ENGINE_ENTITY_HPP
|
@ -31,6 +31,7 @@ namespace polygun::world {
|
|||||||
for (int y = 0; y < CHUNK_SIZE; y++) {
|
for (int y = 0; y < CHUNK_SIZE; y++) {
|
||||||
for (int x = 0; x < CHUNK_SIZE; x++) {
|
for (int x = 0; x < CHUNK_SIZE; x++) {
|
||||||
chunk_data[z][y][x] = 0;
|
chunk_data[z][y][x] = 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,8 +12,6 @@
|
|||||||
#include "common/logger.hpp"
|
#include "common/logger.hpp"
|
||||||
#include "common/locale/translation_manager.hpp"
|
#include "common/locale/translation_manager.hpp"
|
||||||
|
|
||||||
#include "game/ecs/base_component.hpp"
|
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
polygun::utils::Logger::create(false, true);
|
polygun::utils::Logger::create(false, true);
|
||||||
polygun::locale::TranslationManager::create("locale");
|
polygun::locale::TranslationManager::create("locale");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user