diff --git a/res/content/base/entities/drop.json b/res/content/base/entities/drop.json index c0b8db42..174f05b2 100644 --- a/res/content/base/entities/drop.json +++ b/res/content/base/entities/drop.json @@ -1,3 +1,6 @@ { - "hitbox": [0.2, 0.2, 0.2] + "hitbox": [0.2, 0.2, 0.2], + "triggers": [ + [-0.2, -0.2, -0.2, 0.2, 0.2, 0.2] + ] } diff --git a/res/content/base/scripts/components/drop.lua b/res/content/base/scripts/components/drop.lua index 53e6c626..72acec7c 100644 --- a/res/content/base/scripts/components/drop.lua +++ b/res/content/base/scripts/components/drop.lua @@ -17,6 +17,7 @@ end function on_trigger_enter(index, oid) if ready and oid == 0 then entity:despawn() + inventory.add(player.get_inventory(oid), item.index("base:stone.item"), 1) end end diff --git a/res/modules/internal/stdcomp.lua b/res/modules/internal/stdcomp.lua index 1fd16e1b..2f97e520 100644 --- a/res/modules/internal/stdcomp.lua +++ b/res/modules/internal/stdcomp.lua @@ -36,9 +36,13 @@ return { local entity = setmetatable({eid=eid}, Entity) entity.transform = new_Transform(eid) entity.rigidbody = new_Rigidbody(eid) + entity.data = {} entities[eid] = entity; return entity end, + get_Entity = function(eid) + return entities[eid] + end, remove_Entity = function(eid) local entity = entities[eid] if entity then diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index eb0b08c4..ea49f0cb 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -304,6 +304,7 @@ function file.readlines(path) end stdcomp = require "core:internal/stdcomp" +entity.get = stdcomp.get_Entity -- Deprecated functions block_index = block.index diff --git a/src/content/ContentLoader.cpp b/src/content/ContentLoader.cpp index 23d02953..c00a0f3e 100644 --- a/src/content/ContentLoader.cpp +++ b/src/content/ContentLoader.cpp @@ -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; } diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index c730a128..b1faee13 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -36,8 +36,9 @@ entityid_t Entities::spawn(EntityDef& def, glm::vec3 pos) { auto id = nextID++; registry.emplace(entity, static_cast(id), def); registry.emplace(entity, pos, size, glm::mat3(1.0f)); - registry.emplace(entity, true, Hitbox {pos, def.hitbox}, std::vector{ - {true, id, AABB {glm::vec3{-0.2f, -0.2f, -0.2f}, glm::vec3{0.2f, 0.2f, 0.2f}}, {}, {}, {}, + auto& body = registry.emplace(entity, true, Hitbox {pos, def.hitbox}, std::vector{}); + 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(entity, entity_funcs_set {}, nullptr); entities[id] = entity; scripting.env = scripting::on_entity_spawn(def, id, scripting.funcsset); diff --git a/src/objects/EntityDef.hpp b/src/objects/EntityDef.hpp index a409de09..3db361b0 100644 --- a/src/objects/EntityDef.hpp +++ b/src/objects/EntityDef.hpp @@ -2,9 +2,11 @@ #define OBJECTS_ENTITY_DEF_HPP_ #include +#include #include #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 triggers {}; struct { entityid_t id;