feat: loading entities (WIP)

This commit is contained in:
MihailRis
2024-07-05 22:51:03 +03:00
parent 08cc78289d
commit c8666910ce
16 changed files with 129 additions and 38 deletions
+54 -8
View File
@@ -1,8 +1,10 @@
#include "Entities.hpp"
#include "../debug/Logger.hpp"
#include "../data/dynamic_util.hpp"
#include "../assets/Assets.hpp"
#include "../world/Level.hpp"
#include "../content/Content.hpp"
#include "../physics/Hitbox.hpp"
#include "../physics/PhysicsSolver.hpp"
#include "../graphics/render/ModelBatch.hpp"
@@ -12,9 +14,12 @@
#include "../objects/EntityDef.hpp"
#include "../objects/rigging.hpp"
#include "../logic/scripting/scripting.hpp"
#include "../engine.hpp"
#include <glm/ext/matrix_transform.hpp>
static debug::Logger logger("entities");
void Transform::refresh() {
combined = glm::mat4(1.0f);
combined = glm::translate(combined, pos);
@@ -57,20 +62,26 @@ static triggercallback create_trigger_callback(Entities* entities) {
entityid_t Entities::spawn(
Assets* assets,
EntityDef& def,
glm::vec3 pos,
dynamic::Value args)
Transform transform,
dynamic::Value args,
dynamic::Map_sptr saved,
entityid_t uid)
{
auto rig = assets->get<rigging::RigConfig>(def.rigName);
if (rig == nullptr) {
throw std::runtime_error("rig "+def.rigName+" not found");
}
auto entity = registry.create();
glm::vec3 size(1);
auto id = nextID++;
entityid_t id;
if (uid == 0) {
id = nextID++;
} else {
id = uid;
}
registry.emplace<EntityId>(entity, static_cast<entityid_t>(id), def);
registry.emplace<Transform>(entity, pos, size, glm::mat3(1.0f));
registry.emplace<Transform>(entity, transform);
auto& body = registry.emplace<Rigidbody>(
entity, true, Hitbox {pos, def.hitbox}, std::vector<Trigger>{});
entity, true, Hitbox {transform.pos, def.hitbox}, std::vector<Trigger>{});
body.triggers.resize(def.radialTriggers.size() + def.boxTriggers.size());
for (auto& [i, box] : def.boxTriggers) {
@@ -98,7 +109,8 @@ entityid_t Entities::spawn(
componentName, entity_funcs_set {}, nullptr);
scripting.components.emplace_back(std::move(component));
}
scripting::on_entity_spawn(def, id, scripting.components, std::move(args));
scripting::on_entity_spawn(
def, id, scripting.components, std::move(args), std::move(saved));
return id;
}
@@ -112,6 +124,37 @@ void Entities::despawn(entityid_t id) {
}
}
void Entities::loadEntity(const dynamic::Map_sptr& map) {
entityid_t uid = 0;
std::string defname;
map->num("uid", uid);
map->str("def", defname);
if (uid == 0) {
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);
}
dynamic::Map_sptr savedMap = map->map("comps");
auto assets = scripting::engine->getAssets();
spawn(assets, def, transform, dynamic::NONE, savedMap, uid);
}
void Entities::loadEntities(dynamic::Map_sptr root) {
auto list = root->list("data");
for (size_t i = 0; i < list->size(); i++) {
try {
loadEntity(list->map(i));
} catch (const std::runtime_error& err) {
logger.error() << "could not read entity: " << err.what();
}
}
}
void Entities::onSave(const Entity& entity) {
scripting::on_entity_save(entity);
}
@@ -129,7 +172,9 @@ dynamic::Value Entities::serialize(const Entity& entity) {
if (transform.size != glm::vec3(1.0f)) {
tsfmap.put("size", dynamic::to_value(transform.size));
}
tsfmap.put("rot", dynamic::to_value(transform.rot));
if (transform.rot != glm::mat3(1.0f)) {
tsfmap.put("rot", dynamic::to_value(transform.rot));
}
}
{
auto& rigidbody = entity.getRigidbody();
@@ -166,6 +211,7 @@ dynamic::Value Entities::serialize(const Entity& entity) {
auto data = scripting::get_component_value(comp->env, "SAVED_DATA");
compsMap.put(comp->name, data);
}
std::cout << root << std::endl;
}
return root;
}
+6 -2
View File
@@ -172,8 +172,10 @@ public:
entityid_t spawn(
Assets* assets,
EntityDef& def,
glm::vec3 pos,
dynamic::Value args=dynamic::NONE);
Transform transform,
dynamic::Value args=dynamic::NONE,
dynamic::Map_sptr saved=nullptr,
entityid_t uid=0);
std::optional<Entity> get(entityid_t id) {
const auto& found = entities.find(id);
@@ -183,6 +185,8 @@ public:
return std::nullopt;
}
void loadEntities(dynamic::Map_sptr map);
void loadEntity(const dynamic::Map_sptr& map);
void onSave(const Entity& entity);
std::vector<Entity> getAllInside(AABB aabb);
void despawn(entityid_t id);
+7 -10
View File
@@ -251,10 +251,9 @@ void Player::deserialize(dynamic::Map *src) {
src->flag("flight", flight);
src->flag("noclip", noclip);
setChosenSlot(src->get("chosen-slot", getChosenSlot()));
auto invmap = src->map("inventory");
if (invmap) {
getInventory()->deserialize(invmap);
if (auto invmap = src->map("inventory")) {
getInventory()->deserialize(invmap.get());
}
}
@@ -264,16 +263,14 @@ void Player::convert(dynamic::Map* data, const ContentLUT* lut) {
if (players) {
for (uint i = 0; i < players->size(); i++) {
auto playerData = players->map(i);
auto inventory = playerData->map("inventory");
if (inventory) {
Inventory::convert(inventory, lut);
if (auto inventory = playerData->map("inventory")) {
Inventory::convert(inventory.get(), lut);
}
}
} else {
auto inventory = data->map("inventory");
if (inventory) {
Inventory::convert(inventory, lut);
if (auto inventory = data->map("inventory")) {
Inventory::convert(inventory.get(), lut);
}
}
}
+3 -3
View File
@@ -94,8 +94,8 @@ static std::tuple<size_t, std::unique_ptr<RigNode>> read_node(
size_t count = 1;
if (auto nodesList = root->list("nodes")) {
for (size_t i = 0; i < nodesList->size(); i++) {
if (auto map = nodesList->map(i)) {
auto [subcount, subNode] = read_node(map, index+count);
if (const auto& map = nodesList->map(i)) {
auto [subcount, subNode] = read_node(map.get(), index+count);
subcount += count;
subnodes.push_back(std::move(subNode));
}
@@ -114,6 +114,6 @@ std::unique_ptr<RigConfig> RigConfig::parse(
if (rootNodeMap == nullptr) {
throw std::runtime_error("missing 'root' element");
}
auto [count, rootNode] = read_node(rootNodeMap, 0);
auto [count, rootNode] = read_node(rootNodeMap.get(), 0);
return std::make_unique<RigConfig>(std::string(name), std::move(rootNode), count);
}