update entity script semantics

This commit is contained in:
MihailRis
2024-06-29 21:01:30 +03:00
parent c54be7b2b1
commit fc3994446a
11 changed files with 193 additions and 69 deletions
+7 -7
View File
@@ -36,16 +36,15 @@ entityid_t Entities::spawn(EntityDef& def, glm::vec3 pos) {
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});
auto& scripting = registry.emplace<Scripting>(entity, entity_funcs_set {}, nullptr);
entities[id] = entity;
if (def.rt.funcsset.on_spawn) {
scripting::on_entity_spawn(def, id);
}
scripting.env = scripting::on_entity_spawn(def, id, scripting.funcsset);
return id;
}
void Entities::despawn(entityid_t id) {
if (auto entity = get(id)) {
scripting::on_entity_despawn(entity->getDef(), id);
scripting::on_entity_despawn(entity->getDef(), *entity);
registry.destroy(get(id)->getHandler());
}
}
@@ -82,15 +81,16 @@ void Entities::updatePhysics(float delta){
transform.pos = hitbox.position;
//transform.rot = glm::rotate(glm::mat4(transform.rot), delta, glm::vec3(0, 1, 0));
if (hitbox.grounded && !grounded) {
scripting::on_entity_grounded(eid.def, eid.uid);
scripting::on_entity_grounded(eid.def, *get(eid.uid));
}
}
}
void Entities::renderDebug(LineBatch& batch) {
batch.lineWidth(1.0f);
auto view = registry.view<Transform, Hitbox>();
for (auto [entity, transform, hitbox] : view.each()) {
auto view = registry.view<Transform, Rigidbody>();
for (auto [entity, transform, rigidbody] : view.each()) {
const auto& hitbox = rigidbody.hitbox;
batch.box(hitbox.position, hitbox.halfsize * 2.0f, glm::vec4(1.0f));
}
}
+15
View File
@@ -9,6 +9,12 @@
#include <unordered_map>
#include <entt/entity/registry.hpp>
struct entity_funcs_set {
bool init : 1;
bool on_despawn : 1;
bool on_grounded : 1;
};
struct EntityDef;
struct EntityId {
@@ -30,6 +36,11 @@ struct Rigidbody {
Hitbox hitbox;
};
struct Scripting {
entity_funcs_set funcsset;
scriptenv env;
};
class Level;
class Assets;
class LineBatch;
@@ -67,6 +78,10 @@ public:
return registry.get<Rigidbody>(entity);
}
Scripting& getScripting() const {
return registry.get<Scripting>(entity);
}
entityid_t getUID() const {
return registry.get<EntityId>(entity).uid;
}
+1 -9
View File
@@ -6,13 +6,6 @@
#include "../typedefs.hpp"
struct entity_funcs_set {
bool init : 1;
bool on_spawn : 1;
bool on_despawn : 1;
bool on_grounded : 1;
};
struct EntityDef {
/// @brief Entity string id (with prefix included)
std::string const name;
@@ -22,8 +15,7 @@ struct EntityDef {
struct {
entityid_t id;
entity_funcs_set funcsset;
} rt;
} rt {};
EntityDef(const std::string& name) : name(name) {}
EntityDef(const EntityDef&) = delete;