add entity events: on_spawn, on_despawn

This commit is contained in:
MihailRis
2024-06-28 12:16:31 +03:00
parent e1eb652ef4
commit eb2be5e8b6
13 changed files with 116 additions and 22 deletions
+19 -2
View File
@@ -9,6 +9,7 @@
#include "../graphics/core/Model.hpp"
#include "../maths/FrustumCulling.hpp"
#include "../objects/EntityDef.hpp"
#include "../logic/scripting/scripting.hpp"
#include <glm/ext/matrix_transform.hpp>
@@ -19,6 +20,12 @@ void Transform::refresh() {
combined = combined * glm::mat4(rot);
}
void Entity::destroy() {
if (isValid()){
entities.despawn(id);
}
}
Entities::Entities(Level* level) : level(level) {
}
@@ -26,13 +33,23 @@ entityid_t Entities::spawn(EntityDef& def, glm::vec3 pos) {
auto entity = registry.create();
glm::vec3 size(1);
auto id = nextID++;
registry.emplace<EntityId>(entity, static_cast<entityid_t>(id));
registry.emplace<EntityId>(entity, static_cast<entityid_t>(id), def);
registry.emplace<Transform>(entity, pos, size/4.0f, glm::mat3(1.0f));
registry.emplace<Rigidbody>(entity, true, Hitbox {pos, def.hitbox});
entities[id] = entity;
if (def.rt.funcsset.on_spawn) {
scripting::on_entity_spawn(def, id);
}
return id;
}
void Entities::despawn(entityid_t id) {
if (auto entity = get(id)) {
scripting::on_entity_despawn(entity->getDef(), id);
registry.destroy(get(id)->getHandler());
}
}
void Entities::clean() {
for (auto it = entities.begin(); it != entities.end();) {
if (registry.valid(it->second)) {
@@ -60,7 +77,7 @@ void Entities::updatePhysics(float delta){
1.0f,
true
);
hitbox.linear_damping = hitbox.grounded * 12;
hitbox.linearDamping = hitbox.grounded * 12;
transform.pos = hitbox.position;
//transform.rot = glm::rotate(glm::mat4(transform.rot), delta, glm::vec3(0, 1, 0));
if (hitbox.grounded) {
+18 -6
View File
@@ -9,8 +9,11 @@
#include <unordered_map>
#include <entt/entity/registry.hpp>
struct EntityDef;
struct EntityId {
entityid_t uid;
const EntityDef& def;
};
struct Transform {
@@ -33,15 +36,16 @@ class LineBatch;
class ModelBatch;
class Frustum;
class Rig;
struct EntityDef;
class Entities;
class Entity {
Entities& entities;
entityid_t id;
entt::registry& registry;
const entt::entity entity;
public:
Entity(entityid_t id, entt::registry& registry, const entt::entity entity)
: id(id), registry(registry), entity(entity) {}
Entity(Entities& entities, entityid_t id, entt::registry& registry, const entt::entity entity)
: entities(entities), id(id), registry(registry), entity(entity) {}
entityid_t getID() const {
return id;
@@ -51,6 +55,10 @@ public:
return registry.valid(entity);
}
const EntityDef& getDef() const {
return registry.get<EntityId>(entity).def;
}
Transform& getTransform() const {
return registry.get<Transform>(entity);
}
@@ -63,9 +71,11 @@ public:
return registry.get<EntityId>(entity).uid;
}
void destroy() {
registry.destroy(entity);
entt::entity getHandler() const {
return entity;
}
void destroy();
};
class Entities {
@@ -86,11 +96,13 @@ public:
std::optional<Entity> get(entityid_t id) {
const auto& found = entities.find(id);
if (found != entities.end() && registry.valid(found->second)) {
return Entity(id, registry, found->second);
return Entity(*this, id, registry, found->second);
}
return std::nullopt;
}
void despawn(entityid_t id);
inline size_t size() const {
return entities.size();
}
+7
View File
@@ -6,6 +6,12 @@
#include "../typedefs.hpp"
struct entity_funcs_set {
bool init : 1;
bool on_spawn : 1;
bool on_despawn : 1;
};
struct EntityDef {
/// @brief Entity string id (with prefix included)
std::string const name;
@@ -15,6 +21,7 @@ struct EntityDef {
struct {
entityid_t id;
entity_funcs_set funcsset;
} rt;
EntityDef(const std::string& name) : name(name) {}
+3 -3
View File
@@ -110,9 +110,9 @@ void Player::updateInput(
noclip = !noclip;
}
hitbox->linear_damping = PLAYER_GROUND_DAMPING;
hitbox->linearDamping = PLAYER_GROUND_DAMPING;
if (flight){
hitbox->linear_damping = PLAYER_AIR_DAMPING;
hitbox->linearDamping = PLAYER_AIR_DAMPING;
hitbox->velocity.y *= 1.0f - delta * 9;
if (input.jump){
hitbox->velocity.y += speed * delta * 9;
@@ -122,7 +122,7 @@ void Player::updateInput(
}
}
if (!hitbox->grounded) {
hitbox->linear_damping = PLAYER_AIR_DAMPING;
hitbox->linearDamping = PLAYER_AIR_DAMPING;
}
input.noclip = false;
+1 -1
View File
@@ -11,10 +11,10 @@
#include <glm/glm.hpp>
class Camera;
class Hitbox;
class Inventory;
class ContentLUT;
class Level;
struct Hitbox;
struct EngineSettings;
struct PlayerInput {