From 20ab48ecabd5b585989c758e04ff2ce01ee3399a Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 27 Jun 2024 03:09:16 +0300 Subject: [PATCH] add drop.json --- res/content/base/content.json | 7 +++++-- res/content/base/entities/drop.json | 3 +++ res/content/base/scripts/hud.lua | 2 +- src/content/ContentLoader.cpp | 9 ++++++--- src/logic/scripting/lua/libentity.cpp | 9 ++++++--- src/objects/Entities.cpp | 6 +++--- src/objects/Entities.hpp | 3 ++- src/objects/EntityDef.hpp | 2 ++ 8 files changed, 28 insertions(+), 13 deletions(-) create mode 100644 res/content/base/entities/drop.json diff --git a/res/content/base/content.json b/res/content/base/content.json index 0e6dc07f..dd1573a1 100644 --- a/res/content/base/content.json +++ b/res/content/base/content.json @@ -1,6 +1,6 @@ { - "items": [ - "bazalt_breaker" + "entities": [ + "drop" ], "blocks": [ "dirt", @@ -27,5 +27,8 @@ "lightbulb", "torch", "wooden_door" + ], + "items": [ + "bazalt_breaker" ] } \ No newline at end of file diff --git a/res/content/base/entities/drop.json b/res/content/base/entities/drop.json new file mode 100644 index 00000000..c0b8db42 --- /dev/null +++ b/res/content/base/entities/drop.json @@ -0,0 +1,3 @@ +{ + "hitbox": [0.2, 0.2, 0.2] +} diff --git a/res/content/base/scripts/hud.lua b/res/content/base/scripts/hud.lua index d85e3201..f5bc77ba 100644 --- a/res/content/base/scripts/hud.lua +++ b/res/content/base/scripts/hud.lua @@ -5,7 +5,7 @@ function on_hud_open() input.add_callback("player.drop", function () local pid = hud.get_player() local pvel = {player.get_vel(pid)} - local eid = entity.test() + local eid = entity.spawn("base:drop") local throw_force = vec3.mul(player.get_dir(pid), DROP_FORCE) entity.set_vel(eid, vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL))) end) diff --git a/src/content/ContentLoader.cpp b/src/content/ContentLoader.cpp index 40b0b625..f966c633 100644 --- a/src/content/ContentLoader.cpp +++ b/src/content/ContentLoader.cpp @@ -301,8 +301,7 @@ void ContentLoader::loadItem(ItemDef& def, const std::string& name, const fs::pa root->num("stack-size", def.stackSize); // item light emission [r, g, b] where r,g,b in range [0..15] - auto emissionarr = root->list("emission"); - if (emissionarr) { + if (auto emissionarr = root->list("emission")) { def.emission[0] = emissionarr->num(0); def.emission[1] = emissionarr->num(1); def.emission[2] = emissionarr->num(2); @@ -311,7 +310,11 @@ void ContentLoader::loadItem(ItemDef& def, const std::string& name, const fs::pa void ContentLoader::loadEntity(EntityDef& def, const std::string& name, const fs::path& file) { auto root = files::read_json(file); - + root->str("script-name", def.scriptName); + if (auto boxarr = root->list("hitbox")) { + def.hitbox = glm::vec3(boxarr->num(0), boxarr->num(1), boxarr->num(2)); + } + std::cout << "loading entity " << name << " from " << file.u8string() << std::endl; } void ContentLoader::loadEntity(EntityDef& def, const std::string& full, const std::string& name) { diff --git a/src/logic/scripting/lua/libentity.cpp b/src/logic/scripting/lua/libentity.cpp index 859364a8..c66cda80 100644 --- a/src/logic/scripting/lua/libentity.cpp +++ b/src/logic/scripting/lua/libentity.cpp @@ -7,6 +7,7 @@ #include "../../../physics/Hitbox.hpp" #include "../../../window/Camera.hpp" #include "../../../frontend/hud.hpp" +#include "../../../content/Content.hpp" #include @@ -21,10 +22,12 @@ static std::optional get_entity(lua::State* L, int idx) { return level->entities->get(id); } -static int l_test(lua::State* L) { +static int l_spawn(lua::State* L) { auto level = controller->getLevel(); auto player = hud->getPlayer(); - auto id = level->entities->drop(player->camera->position); + auto defname = lua::tostring(L, 1); + auto& def = content->entities.require(defname); + auto id = level->entities->spawn(def, player->camera->position); return lua::pushinteger(L, id); } @@ -43,7 +46,7 @@ static int l_set_vel(lua::State* L) { } const luaL_Reg entitylib [] = { - {"test", lua::wrap}, + {"spawn", lua::wrap}, {"get_vel", lua::wrap}, {"set_vel", lua::wrap}, {NULL, NULL} diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index b4218fe9..0b83b52c 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -8,6 +8,7 @@ #include "../graphics/core/LineBatch.hpp" #include "../graphics/core/Model.hpp" #include "../maths/FrustumCulling.hpp" +#include "../objects/EntityDef.hpp" #include @@ -21,14 +22,13 @@ void Transform::refresh() { Entities::Entities(Level* level) : level(level) { } -entityid_t Entities::drop(glm::vec3 pos) { +entityid_t Entities::spawn(EntityDef& def, glm::vec3 pos) { auto entity = registry.create(); glm::vec3 size(1); auto id = nextID++; registry.emplace(entity, static_cast(id)); registry.emplace(entity, pos, size/4.0f, glm::mat3(1.0f)); - registry.emplace(entity, pos, - glm::vec3(size.x*0.2f, size.y*0.5f, size.z*0.2f)); + registry.emplace(entity, pos, def.hitbox); entities[id] = entity; return id; } diff --git a/src/objects/Entities.hpp b/src/objects/Entities.hpp index 431cf98f..3613f860 100644 --- a/src/objects/Entities.hpp +++ b/src/objects/Entities.hpp @@ -28,6 +28,7 @@ class LineBatch; class ModelBatch; class Frustum; class Rig; +struct EntityDef; class Entity { entt::registry& registry; @@ -65,7 +66,7 @@ public: void renderDebug(LineBatch& batch); void render(Assets* assets, ModelBatch& batch, Frustum& frustum); - entityid_t drop(glm::vec3 pos); + entityid_t spawn(EntityDef& def, glm::vec3 pos); std::optional get(entityid_t id) { const auto& found = entities.find(id); diff --git a/src/objects/EntityDef.hpp b/src/objects/EntityDef.hpp index f80fd5be..e001bdbe 100644 --- a/src/objects/EntityDef.hpp +++ b/src/objects/EntityDef.hpp @@ -2,6 +2,7 @@ #define OBJECTS_ENTITY_DEF_HPP_ #include +#include #include "../typedefs.hpp" @@ -10,6 +11,7 @@ struct EntityDef { std::string const name; std::string scriptName = name.substr(name.find(':')+1); + glm::vec3 hitbox {0.5f}; struct { entityid_t id;