From 19c6a82ab2890ba51607e2f4398a60a0bc0ff660 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 8 Jul 2024 22:47:01 +0300 Subject: [PATCH] refactor Entities load + loading velocity --- res/content/base/scripts/components/drop.lua | 4 +- src/coders/json.cpp | 4 ++ src/logic/scripting/lua/libentity.cpp | 5 +-- src/objects/Entities.cpp | 44 +++++++++++++------- src/objects/Entities.hpp | 4 +- src/objects/Player.cpp | 4 +- 6 files changed, 41 insertions(+), 24 deletions(-) diff --git a/res/content/base/scripts/components/drop.lua b/res/content/base/scripts/components/drop.lua index 53de2a9e..63be68cc 100644 --- a/res/content/base/scripts/components/drop.lua +++ b/res/content/base/scripts/components/drop.lua @@ -30,7 +30,7 @@ do -- setup visuals local bid = block.index(icon:sub(16)) model = block.get_model(bid) if model == "X" then - entity:set_rig("drop-item") + entity:set_rig("base:drop-item") body:set_size(vec3.mul(body:get_size(), {1.0, 0.3, 1.0})) rig:set_texture("$0", icon) else @@ -46,7 +46,7 @@ do -- setup visuals end end else - entity:set_rig("drop-item") + entity:set_rig("base:drop-item") body:set_size(vec3.mul(body:get_size(), {1.0, 0.3, 1.0})) rig:set_texture("$0", icon) end diff --git a/src/coders/json.cpp b/src/coders/json.cpp index b3b3d1ea..c88e0c1d 100644 --- a/src/coders/json.cpp +++ b/src/coders/json.cpp @@ -97,6 +97,10 @@ void stringifyObj( const std::string& indentstr, bool nice ) { + if (obj == nullptr) { + ss << "nullptr"; + return; + } if (obj->values.empty()) { ss << "{}"; return; diff --git a/src/logic/scripting/lua/libentity.cpp b/src/logic/scripting/lua/libentity.cpp index 2ff91930..6a7ae205 100644 --- a/src/logic/scripting/lua/libentity.cpp +++ b/src/logic/scripting/lua/libentity.cpp @@ -37,10 +37,7 @@ static int l_spawn(lua::State* L) { if (lua::gettop(L) > 2) { args = lua::tovalue(L, 3); } - Transform transform { - pos, glm::vec3(1.0f), glm::mat3(1.0f), {}, true - }; - level->entities->spawn(def, transform, args); + level->entities->spawn(def, pos, args); return 1; } diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index 311a907a..7ccc926e 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -20,6 +20,10 @@ static debug::Logger logger("entities"); +static inline std::string COMP_TRANSFORM = "transform"; +static inline std::string COMP_RIGIDBODY = "rigidbody"; +static inline std::string COMP_MODELTREE = "modeltree"; + void Transform::refresh() { combined = glm::mat4(1.0f); combined = glm::translate(combined, pos); @@ -61,7 +65,7 @@ static triggercallback create_trigger_callback(Entities* entities) { entityid_t Entities::spawn( EntityDef& def, - Transform transform, + glm::vec3 position, dynamic::Value args, dynamic::Map_sptr saved, entityid_t uid) @@ -78,9 +82,9 @@ entityid_t Entities::spawn( id = uid; } registry.emplace(entity, static_cast(id), def); - registry.emplace(entity, transform); + auto& tsf = registry.emplace(entity, position, glm::vec3(1.0f), glm::mat3(1.0f), glm::mat4(1.0f), true); auto& body = registry.emplace( - entity, true, Hitbox {transform.pos, def.hitbox}, std::vector{}); + entity, true, Hitbox {position, def.hitbox}, std::vector{}); body.triggers.resize(def.radialTriggers.size() + def.boxTriggers.size()); for (auto& [i, box] : def.boxTriggers) { @@ -108,8 +112,14 @@ entityid_t Entities::spawn( componentName, entity_funcs_set {}, nullptr); scripting.components.emplace_back(std::move(component)); } + dynamic::Map_sptr componentsMap = nullptr; + if (saved) { + componentsMap = saved->map("comps"); + loadEntity(saved, get(id).value()); + } + body.hitbox.position = tsf.pos; scripting::on_entity_spawn( - def, id, scripting.components, std::move(args), std::move(saved)); + def, id, scripting.components, std::move(args), std::move(componentsMap)); return id; } @@ -132,14 +142,20 @@ void Entities::loadEntity(const dynamic::Map_sptr& map) { throw std::runtime_error("could not read entity - invalid UID"); } auto& def = level->content->entities.require(defname); - Transform transform { - glm::vec3(), glm::vec3(1.0f), glm::mat3(1.0f), {}, true - }; - if (auto tsfmap = map->map("transform")) { - dynamic::get_vec(tsfmap, "pos", transform.pos); + spawn(def, {}, dynamic::NONE, map, uid); +} + +void Entities::loadEntity(const dynamic::Map_sptr& map, Entity entity) { + auto& transform = entity.getTransform(); + auto& body = entity.getRigidbody(); + + if (auto bodymap = map->map(COMP_RIGIDBODY)) { + dynamic::get_vec(bodymap, "vel", body.hitbox.velocity); + } + if (auto tsfmap = map->map(COMP_TRANSFORM)) { + dynamic::get_vec(tsfmap, "pos", transform.pos); + dynamic::get_vec(tsfmap, "size", transform.size); } - dynamic::Map_sptr savedMap = map->map("comps"); - spawn(def, transform, dynamic::NONE, savedMap, uid); } void Entities::loadEntities(dynamic::Map_sptr root) { @@ -165,7 +181,7 @@ dynamic::Value Entities::serialize(const Entity& entity) { root->put("uid", eid.uid); { auto& transform = entity.getTransform(); - auto& tsfmap = root->putMap("transform"); + auto& tsfmap = root->putMap(COMP_TRANSFORM); tsfmap.put("pos", dynamic::to_value(transform.pos)); if (transform.size != glm::vec3(1.0f)) { tsfmap.put("size", dynamic::to_value(transform.size)); @@ -176,7 +192,7 @@ dynamic::Value Entities::serialize(const Entity& entity) { } { auto& rigidbody = entity.getRigidbody(); - auto& bodymap = root->putMap("rigidbody"); + auto& bodymap = root->putMap(COMP_RIGIDBODY); if (!rigidbody.enabled) { bodymap.put("enabled", rigidbody.enabled); } @@ -188,7 +204,7 @@ dynamic::Value Entities::serialize(const Entity& entity) { root->put("rig", rig.config->getName()); } if (def.save.rig.pose || def.save.rig.textures) { - auto& rigmap = root->putMap("rig"); + auto& rigmap = root->putMap(COMP_MODELTREE); if (def.save.rig.textures) { auto& map = rigmap.putMap("textures"); for (auto& [slot, texture] : rig.textures) { diff --git a/src/objects/Entities.hpp b/src/objects/Entities.hpp index aa66327a..d16b2b65 100644 --- a/src/objects/Entities.hpp +++ b/src/objects/Entities.hpp @@ -65,6 +65,7 @@ struct Rigidbody { bool enabled = true; Hitbox hitbox; std::vector triggers; + float gravityMultiplier = 1.0f; }; struct UserComponent { @@ -171,7 +172,7 @@ public: entityid_t spawn( EntityDef& def, - Transform transform, + glm::vec3 position, dynamic::Value args=dynamic::NONE, dynamic::Map_sptr saved=nullptr, entityid_t uid=0); @@ -186,6 +187,7 @@ public: void loadEntities(dynamic::Map_sptr map); void loadEntity(const dynamic::Map_sptr& map); + void loadEntity(const dynamic::Map_sptr& map, Entity entity); void onSave(const Entity& entity); std::vector getAllInside(AABB aabb); void despawn(entityid_t id); diff --git a/src/objects/Player.cpp b/src/objects/Player.cpp index 0c08e30d..7579f789 100644 --- a/src/objects/Player.cpp +++ b/src/objects/Player.cpp @@ -42,9 +42,7 @@ Player::~Player() { void Player::updateEntity() { if (eid == 0) { auto& def = level->content->entities.require("base:player"); - eid = level->entities->spawn(def, Transform { - getPosition(), glm::vec3(1.0f), glm::mat3(1.0f), {}, true - }); + eid = level->entities->spawn(def, getPosition()); } else if (auto entity = level->entities->get(eid)) { position = entity->getTransform().pos; } else {