refactor Entities

This commit is contained in:
MihailRis
2025-08-09 21:19:01 +03:00
parent 36a522bee0
commit 3eae377024
23 changed files with 485 additions and 348 deletions
+7 -167
View File
@@ -5,108 +5,21 @@
#include <optional>
#include <vector>
#include "data/dv.hpp"
#include "physics/Hitbox.hpp"
#include "Transform.hpp"
#include "Rigidbody.hpp"
#include "ScriptComponents.hpp"
#include "typedefs.hpp"
#include "util/Clock.hpp"
#define GLM_ENABLE_EXPERIMENTAL
#include <entt/entity/registry.hpp>
#include <glm/gtx/norm.hpp>
#include <unordered_map>
struct EntityFuncsSet {
bool init;
bool on_despawn;
bool on_grounded;
bool on_fall;
bool on_sensor_enter;
bool on_sensor_exit;
bool on_save;
bool on_aim_on;
bool on_aim_off;
bool on_attacked;
bool on_used;
};
#include <entt/entity/registry.hpp>
#include <unordered_map>
struct EntityDef;
struct EntityId {
entityid_t uid;
const EntityDef& def;
bool destroyFlag = false;
int64_t player = -1;
};
struct Transform {
static inline constexpr float EPSILON = 0.0000001f;
glm::vec3 pos;
glm::vec3 size;
glm::mat3 rot;
glm::mat4 combined;
bool dirty = true;
glm::vec3 displayPos;
glm::vec3 displaySize;
void refresh();
inline void setRot(glm::mat3 m) {
rot = m;
dirty = true;
}
inline void setSize(glm::vec3 v) {
if (glm::distance2(displaySize, v) >= EPSILON) {
dirty = true;
}
size = v;
}
inline void setPos(glm::vec3 v) {
if (glm::distance2(displayPos, v) >= EPSILON) {
dirty = true;
}
pos = v;
}
};
struct Rigidbody {
bool enabled = true;
Hitbox hitbox;
std::vector<Sensor> sensors;
};
struct UserComponent {
std::string name;
EntityFuncsSet funcsset;
scriptenv env;
dv::value params;
UserComponent(
const std::string& name,
EntityFuncsSet funcsset,
scriptenv env,
dv::value params
)
: name(name),
funcsset(funcsset),
env(std::move(env)),
params(std::move(params)) {
}
};
struct ScriptComponents {
std::vector<std::unique_ptr<UserComponent>> components;
ScriptComponents() = default;
ScriptComponents(ScriptComponents&& other)
: components(std::move(other.components)) {
}
};
class Level;
class Assets;
class Entity;
class LineBatch;
class ModelBatch;
class Frustum;
@@ -118,72 +31,6 @@ namespace rigging {
class SkeletonConfig;
}
class Entity {
Entities& entities;
entityid_t id;
entt::registry& registry;
const entt::entity entity;
public:
Entity(
Entities& entities,
entityid_t id,
entt::registry& registry,
const entt::entity entity
)
: entities(entities), id(id), registry(registry), entity(entity) {
}
EntityId& getID() const {
return registry.get<EntityId>(entity);
}
bool isValid() const {
return registry.valid(entity);
}
const EntityDef& getDef() const {
return registry.get<EntityId>(entity).def;
}
Transform& getTransform() const {
return registry.get<Transform>(entity);
}
Rigidbody& getRigidbody() const {
return registry.get<Rigidbody>(entity);
}
ScriptComponents& getScripting() const {
return registry.get<ScriptComponents>(entity);
}
rigging::Skeleton& getSkeleton() const;
void setRig(const rigging::SkeletonConfig* rigConfig);
entityid_t getUID() const {
return registry.get<EntityId>(entity).uid;
}
entt::entity getHandler() const {
return entity;
}
int64_t getPlayer() const {
return registry.get<EntityId>(entity).player;
}
void setPlayer(int64_t id) {
registry.get<EntityId>(entity).player = id;
}
void setInterpolatedPosition(const glm::vec3& position);
glm::vec3 getInterpolatedPosition() const;
void destroy();
};
class Entities {
entt::registry registry;
Level& level;
@@ -229,13 +76,7 @@ public:
entityid_t uid = 0
);
std::optional<Entity> get(entityid_t id) {
const auto& found = entities.find(id);
if (found != entities.end() && registry.valid(found->second)) {
return Entity(*this, id, registry, found->second);
}
return std::nullopt;
}
std::optional<Entity> get(entityid_t id);
/// @brief Entities raycast. No blocks check included, use combined with
/// Chunks.rayCast
@@ -260,7 +101,6 @@ public:
std::vector<Entity> getAllInRadius(glm::vec3 center, float radius);
void despawn(entityid_t id);
void despawn(std::vector<Entity> entities);
dv::value serialize(const Entity& entity);
dv::value serialize(const std::vector<Entity>& entities);
void setNextID(entityid_t id) {