add "triggers" entity property
This commit is contained in:
@@ -314,6 +314,16 @@ void ContentLoader::loadEntity(EntityDef& def, const std::string& name, const fs
|
||||
if (auto boxarr = root->list("hitbox")) {
|
||||
def.hitbox = glm::vec3(boxarr->num(0), boxarr->num(1), boxarr->num(2));
|
||||
}
|
||||
if (auto triggersarr = root->list("triggers")) {
|
||||
for (size_t i = 0; i < triggersarr->size(); i++) {
|
||||
if (auto triggerarr = triggersarr->list(i)) {
|
||||
def.triggers.push_back({
|
||||
{triggerarr->num(0), triggerarr->num(1), triggerarr->num(2)},
|
||||
{triggerarr->num(3), triggerarr->num(4), triggerarr->num(5)}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
std::cout << "loading entity " << name << " from " << file.u8string() << std::endl;
|
||||
}
|
||||
|
||||
|
||||
@@ -36,8 +36,9 @@ entityid_t Entities::spawn(EntityDef& def, glm::vec3 pos) {
|
||||
auto id = nextID++;
|
||||
registry.emplace<EntityId>(entity, static_cast<entityid_t>(id), def);
|
||||
registry.emplace<Transform>(entity, pos, size, glm::mat3(1.0f));
|
||||
registry.emplace<Rigidbody>(entity, true, Hitbox {pos, def.hitbox}, std::vector<Trigger>{
|
||||
{true, id, AABB {glm::vec3{-0.2f, -0.2f, -0.2f}, glm::vec3{0.2f, 0.2f, 0.2f}}, {}, {}, {},
|
||||
auto& body = registry.emplace<Rigidbody>(entity, true, Hitbox {pos, def.hitbox}, std::vector<Trigger>{});
|
||||
for (auto& box : def.triggers) {
|
||||
body.triggers.emplace_back(Trigger{true, id, box, AABB{}, {}, {},
|
||||
[=](auto entityid, auto index, auto otherid) {
|
||||
if (auto entity = get(entityid)) {
|
||||
if (entity->isValid()) {
|
||||
@@ -50,8 +51,8 @@ entityid_t Entities::spawn(EntityDef& def, glm::vec3 pos) {
|
||||
scripting::on_trigger_exit(*entity, index, otherid);
|
||||
}
|
||||
}
|
||||
}}
|
||||
});
|
||||
}});
|
||||
}
|
||||
auto& scripting = registry.emplace<Scripting>(entity, entity_funcs_set {}, nullptr);
|
||||
entities[id] = entity;
|
||||
scripting.env = scripting::on_entity_spawn(def, id, scripting.funcsset);
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
#define OBJECTS_ENTITY_DEF_HPP_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "../typedefs.hpp"
|
||||
#include "../maths/aabb.hpp"
|
||||
|
||||
struct EntityDef {
|
||||
/// @brief Entity string id (with prefix included)
|
||||
@@ -12,6 +14,7 @@ struct EntityDef {
|
||||
|
||||
std::string scriptName = name.substr(name.find(':')+1);
|
||||
glm::vec3 hitbox {0.5f};
|
||||
std::vector<AABB> triggers {};
|
||||
|
||||
struct {
|
||||
entityid_t id;
|
||||
|
||||
Reference in New Issue
Block a user