migrate from dynamic::Value to dv::value & total erase namespace 'dynamic'
This commit is contained in:
+61
-61
@@ -5,7 +5,7 @@
|
||||
|
||||
#include "assets/Assets.hpp"
|
||||
#include "content/Content.hpp"
|
||||
#include "data/dynamic_util.hpp"
|
||||
#include "data/dv_util.hpp"
|
||||
#include "debug/Logger.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "graphics/core/DrawContext.hpp"
|
||||
@@ -113,8 +113,8 @@ static void initialize_body(
|
||||
entityid_t Entities::spawn(
|
||||
const EntityDef& def,
|
||||
glm::vec3 position,
|
||||
dynamic::Map_sptr args,
|
||||
dynamic::Map_sptr saved,
|
||||
dv::value args,
|
||||
dv::value saved,
|
||||
entityid_t uid
|
||||
) {
|
||||
auto skeleton = level->content->getSkeleton(def.skeletonName);
|
||||
@@ -166,9 +166,9 @@ entityid_t Entities::spawn(
|
||||
);
|
||||
scripting.components.emplace_back(std::move(component));
|
||||
}
|
||||
dynamic::Map_sptr componentsMap = nullptr;
|
||||
if (saved) {
|
||||
componentsMap = saved->map("comps");
|
||||
dv::value componentsMap = nullptr;
|
||||
if (saved != nullptr) {
|
||||
componentsMap = saved["comps"];
|
||||
loadEntity(saved, get(id).value());
|
||||
}
|
||||
body.hitbox.position = tsf.pos;
|
||||
@@ -188,54 +188,54 @@ 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");
|
||||
}
|
||||
void Entities::loadEntity(const dv::value& map) {
|
||||
entityid_t uid = map["uid"].asInteger();
|
||||
std::string defname = map["def"].asString();
|
||||
auto& def = level->content->entities.require(defname);
|
||||
spawn(def, {}, nullptr, map, uid);
|
||||
}
|
||||
|
||||
void Entities::loadEntity(const dynamic::Map_sptr& map, Entity entity) {
|
||||
void Entities::loadEntity(const dv::value& map, Entity entity) {
|
||||
auto& transform = entity.getTransform();
|
||||
auto& body = entity.getRigidbody();
|
||||
auto& skeleton = entity.getSkeleton();
|
||||
|
||||
if (auto bodymap = map->map(COMP_RIGIDBODY)) {
|
||||
dynamic::get_vec(bodymap, "vel", body.hitbox.velocity);
|
||||
if (map.has(COMP_RIGIDBODY)) {
|
||||
auto& bodymap = map[COMP_RIGIDBODY];
|
||||
dv::get_vec(bodymap, "vel", body.hitbox.velocity);
|
||||
std::string bodyTypeName;
|
||||
bodymap->str("type", bodyTypeName);
|
||||
map.at("type").get(bodyTypeName);
|
||||
if (auto bodyType = BodyType_from(bodyTypeName)) {
|
||||
body.hitbox.type = *bodyType;
|
||||
}
|
||||
bodymap->flag("crouch", body.hitbox.crouching);
|
||||
bodymap->num("damping", body.hitbox.linearDamping);
|
||||
bodymap["crouch"].asBoolean(body.hitbox.crouching);
|
||||
bodymap["damping"].asNumber(body.hitbox.linearDamping);
|
||||
}
|
||||
if (auto tsfmap = map->map(COMP_TRANSFORM)) {
|
||||
dynamic::get_vec(tsfmap, "pos", transform.pos);
|
||||
dynamic::get_vec(tsfmap, "size", transform.size);
|
||||
dynamic::get_mat(tsfmap, "rot", transform.rot);
|
||||
if (map.has(COMP_TRANSFORM)) {
|
||||
auto& tsfmap = map[COMP_TRANSFORM];
|
||||
dv::get_vec(tsfmap, "pos", transform.pos);
|
||||
dv::get_vec(tsfmap, "size", transform.size);
|
||||
dv::get_mat(tsfmap, "rot", transform.rot);
|
||||
}
|
||||
std::string skeletonName = skeleton.config->getName();
|
||||
map->str("skeleton", skeletonName);
|
||||
map.at("skeleton").get(skeletonName);
|
||||
if (skeletonName != skeleton.config->getName()) {
|
||||
skeleton.config = level->content->getSkeleton(skeletonName);
|
||||
}
|
||||
if (auto skeletonmap = map->map(COMP_SKELETON)) {
|
||||
if (auto texturesmap = skeletonmap->map("textures")) {
|
||||
for (auto& [slot, _] : texturesmap->values) {
|
||||
texturesmap->str(slot, skeleton.textures[slot]);
|
||||
if (auto found = map.at(COMP_SKELETON)) {
|
||||
auto& skeletonmap = *found;
|
||||
if (auto found = skeletonmap.at("textures")) {
|
||||
auto& texturesmap = *found;
|
||||
for (auto& [slot, _] : texturesmap.asObject()) {
|
||||
texturesmap.at(slot).get(skeleton.textures[slot]);
|
||||
}
|
||||
}
|
||||
if (auto posearr = skeletonmap->list("pose")) {
|
||||
if (auto found = skeletonmap.at("pose")) {
|
||||
auto& posearr = *found;
|
||||
for (size_t i = 0;
|
||||
i < std::min(skeleton.pose.matrices.size(), posearr->size());
|
||||
i < std::min(skeleton.pose.matrices.size(), posearr.size());
|
||||
i++) {
|
||||
dynamic::get_mat(posearr, i, skeleton.pose.matrices[i]);
|
||||
dv::get_mat(posearr[i], skeleton.pose.matrices[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -272,12 +272,12 @@ std::optional<Entities::RaycastResult> Entities::rayCast(
|
||||
}
|
||||
}
|
||||
|
||||
void Entities::loadEntities(dynamic::Map_sptr root) {
|
||||
void Entities::loadEntities(dv::value root) {
|
||||
clean();
|
||||
auto list = root->list("data");
|
||||
for (size_t i = 0; i < list->size(); i++) {
|
||||
auto& list = root["data"];
|
||||
for (auto& map : list) {
|
||||
try {
|
||||
loadEntity(list->map(i));
|
||||
loadEntity(map);
|
||||
} catch (const std::runtime_error& err) {
|
||||
logger.error() << "could not read entity: " << err.what();
|
||||
}
|
||||
@@ -288,82 +288,82 @@ void Entities::onSave(const Entity& entity) {
|
||||
scripting::on_entity_save(entity);
|
||||
}
|
||||
|
||||
dynamic::Value Entities::serialize(const Entity& entity) {
|
||||
auto root = dynamic::create_map();
|
||||
dv::value Entities::serialize(const Entity& entity) {
|
||||
auto root = dv::object();
|
||||
auto& eid = entity.getID();
|
||||
auto& def = eid.def;
|
||||
root->put("def", def.name);
|
||||
root->put("uid", eid.uid);
|
||||
root["def"] = def.name;
|
||||
root["uid"] = eid.uid;
|
||||
{
|
||||
auto& transform = entity.getTransform();
|
||||
auto& tsfmap = root->putMap(COMP_TRANSFORM);
|
||||
tsfmap.put("pos", dynamic::to_value(transform.pos));
|
||||
auto& tsfmap = root.object(COMP_TRANSFORM);
|
||||
tsfmap["pos"] = dv::to_value(transform.pos);
|
||||
if (transform.size != glm::vec3(1.0f)) {
|
||||
tsfmap.put("size", dynamic::to_value(transform.size));
|
||||
tsfmap["size"] = dv::to_value(transform.size);
|
||||
}
|
||||
if (transform.rot != glm::mat3(1.0f)) {
|
||||
tsfmap.put("rot", dynamic::to_value(transform.rot));
|
||||
tsfmap["rot"] = dv::to_value(transform.rot);
|
||||
}
|
||||
}
|
||||
{
|
||||
auto& rigidbody = entity.getRigidbody();
|
||||
auto& hitbox = rigidbody.hitbox;
|
||||
auto& bodymap = root->putMap(COMP_RIGIDBODY);
|
||||
auto& bodymap = root.object(COMP_RIGIDBODY);
|
||||
if (!rigidbody.enabled) {
|
||||
bodymap.put("enabled", rigidbody.enabled);
|
||||
bodymap["enabled"] = false;
|
||||
}
|
||||
if (def.save.body.velocity) {
|
||||
bodymap.put("vel", dynamic::to_value(rigidbody.hitbox.velocity));
|
||||
bodymap["vel"] = dv::to_value(rigidbody.hitbox.velocity);
|
||||
}
|
||||
if (def.save.body.settings) {
|
||||
bodymap.put("damping", rigidbody.hitbox.linearDamping);
|
||||
bodymap["damping"] = rigidbody.hitbox.linearDamping;
|
||||
if (hitbox.type != def.bodyType) {
|
||||
bodymap.put("type", to_string(hitbox.type));
|
||||
bodymap["type"] = to_string(hitbox.type);
|
||||
}
|
||||
if (hitbox.crouching) {
|
||||
bodymap.put("crouch", hitbox.crouching);
|
||||
bodymap["crouch"] = hitbox.crouching;
|
||||
}
|
||||
}
|
||||
}
|
||||
auto& skeleton = entity.getSkeleton();
|
||||
if (skeleton.config->getName() != def.skeletonName) {
|
||||
root->put("skeleton", skeleton.config->getName());
|
||||
root["skeleton"] = skeleton.config->getName();
|
||||
}
|
||||
if (def.save.skeleton.pose || def.save.skeleton.textures) {
|
||||
auto& skeletonmap = root->putMap(COMP_SKELETON);
|
||||
auto& skeletonmap = root.object(COMP_SKELETON);
|
||||
if (def.save.skeleton.textures) {
|
||||
auto& map = skeletonmap.putMap("textures");
|
||||
auto& map = skeletonmap.object("textures");
|
||||
for (auto& [slot, texture] : skeleton.textures) {
|
||||
map.put(slot, texture);
|
||||
map[slot] = texture;
|
||||
}
|
||||
}
|
||||
if (def.save.skeleton.pose) {
|
||||
auto& list = skeletonmap.putList("pose");
|
||||
auto& list = skeletonmap.list("pose");
|
||||
for (auto& mat : skeleton.pose.matrices) {
|
||||
list.put(dynamic::to_value(mat));
|
||||
list.add(dv::to_value(mat));
|
||||
}
|
||||
}
|
||||
}
|
||||
auto& scripts = entity.getScripting();
|
||||
if (!scripts.components.empty()) {
|
||||
auto& compsMap = root->putMap("comps");
|
||||
auto& compsMap = root.object("comps");
|
||||
for (auto& comp : scripts.components) {
|
||||
auto data =
|
||||
scripting::get_component_value(comp->env, SAVED_DATA_VARNAME);
|
||||
compsMap.put(comp->name, data);
|
||||
compsMap[comp->name] = data;
|
||||
}
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
dynamic::List_sptr Entities::serialize(const std::vector<Entity>& entities) {
|
||||
auto list = dynamic::create_list();
|
||||
dv::value Entities::serialize(const std::vector<Entity>& entities) {
|
||||
auto list = dv::list();
|
||||
for (auto& entity : entities) {
|
||||
if (!entity.getDef().save.enabled) {
|
||||
continue;
|
||||
}
|
||||
level->entities->onSave(entity);
|
||||
list->put(level->entities->serialize(entity));
|
||||
list.add(level->entities->serialize(entity));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
#include "data/dynamic.hpp"
|
||||
#include "data/dv.hpp"
|
||||
#include "physics/Hitbox.hpp"
|
||||
#include "typedefs.hpp"
|
||||
#include "util/Clock.hpp"
|
||||
@@ -204,8 +204,8 @@ public:
|
||||
entityid_t spawn(
|
||||
const EntityDef& def,
|
||||
glm::vec3 position,
|
||||
dynamic::Map_sptr args = nullptr,
|
||||
dynamic::Map_sptr saved = nullptr,
|
||||
dv::value args = nullptr,
|
||||
dv::value saved = nullptr,
|
||||
entityid_t uid = 0
|
||||
);
|
||||
|
||||
@@ -231,17 +231,17 @@ public:
|
||||
entityid_t ignore = -1
|
||||
);
|
||||
|
||||
void loadEntities(dynamic::Map_sptr map);
|
||||
void loadEntity(const dynamic::Map_sptr& map);
|
||||
void loadEntity(const dynamic::Map_sptr& map, Entity entity);
|
||||
void loadEntities(dv::value map);
|
||||
void loadEntity(const dv::value& map);
|
||||
void loadEntity(const dv::value& map, Entity entity);
|
||||
void onSave(const Entity& entity);
|
||||
bool hasBlockingInside(AABB aabb);
|
||||
std::vector<Entity> getAllInside(AABB aabb);
|
||||
std::vector<Entity> getAllInRadius(glm::vec3 center, float radius);
|
||||
void despawn(entityid_t id);
|
||||
void despawn(std::vector<Entity> entities);
|
||||
dynamic::Value serialize(const Entity& entity);
|
||||
dynamic::List_sptr serialize(const std::vector<Entity>& entities);
|
||||
dv::value serialize(const Entity& entity);
|
||||
dv::value serialize(const std::vector<Entity>& entities);
|
||||
|
||||
void setNextID(entityid_t id) {
|
||||
nextID = id;
|
||||
|
||||
+44
-65
@@ -256,92 +256,71 @@ glm::vec3 Player::getSpawnPoint() const {
|
||||
return spawnpoint;
|
||||
}
|
||||
|
||||
std::unique_ptr<dynamic::Map> Player::serialize() const {
|
||||
auto root = std::make_unique<dynamic::Map>();
|
||||
auto& posarr = root->putList("position");
|
||||
posarr.put(position.x);
|
||||
posarr.put(position.y);
|
||||
posarr.put(position.z);
|
||||
dv::value Player::serialize() const {
|
||||
auto root = dv::object();
|
||||
|
||||
auto& rotarr = root->putList("rotation");
|
||||
rotarr.put(cam.x);
|
||||
rotarr.put(cam.y);
|
||||
rotarr.put(cam.z);
|
||||
root["position"] = dv::list({position.x, position.y, position.z});
|
||||
root["rotation"] = dv::list({cam.x, cam.y, cam.z});
|
||||
root["spawnpoint"] = dv::list({spawnpoint.x, spawnpoint.y, spawnpoint.z});
|
||||
|
||||
auto& sparr = root->putList("spawnpoint");
|
||||
sparr.put(spawnpoint.x);
|
||||
sparr.put(spawnpoint.y);
|
||||
sparr.put(spawnpoint.z);
|
||||
|
||||
root->put("flight", flight);
|
||||
root->put("noclip", noclip);
|
||||
root->put("chosen-slot", chosenSlot);
|
||||
root->put("entity", eid);
|
||||
root->put("inventory", inventory->serialize());
|
||||
root["flight"] = flight;
|
||||
root["noclip"] = noclip;
|
||||
root["chosen-slot"] = chosenSlot;
|
||||
root["entity"] = eid;
|
||||
root["inventory"] = inventory->serialize();
|
||||
auto found =
|
||||
std::find(level->cameras.begin(), level->cameras.end(), currentCamera);
|
||||
if (found != level->cameras.end()) {
|
||||
root->put(
|
||||
"camera",
|
||||
level->content->getIndices(ResourceType::CAMERA)
|
||||
.getName(found - level->cameras.begin())
|
||||
);
|
||||
root["camera"] = level->content->getIndices(ResourceType::CAMERA)
|
||||
.getName(found - level->cameras.begin());
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
void Player::deserialize(dynamic::Map* src) {
|
||||
auto posarr = src->list("position");
|
||||
position.x = posarr->num(0);
|
||||
position.y = posarr->num(1);
|
||||
position.z = posarr->num(2);
|
||||
void Player::deserialize(const dv::value& src) {
|
||||
const auto& posarr = src["position"];
|
||||
position.x = posarr[0].asNumber();
|
||||
position.y = posarr[1].asNumber();
|
||||
position.z = posarr[2].asNumber();
|
||||
camera->position = position;
|
||||
|
||||
auto rotarr = src->list("rotation");
|
||||
cam.x = rotarr->num(0);
|
||||
cam.y = rotarr->num(1);
|
||||
if (rotarr->size() > 2) {
|
||||
cam.z = rotarr->num(2);
|
||||
const auto& rotarr = src["rotation"];
|
||||
cam.x = rotarr[0].asNumber();
|
||||
cam.y = rotarr[1].asNumber();
|
||||
cam.z = rotarr[2].asNumber();
|
||||
|
||||
const auto& sparr = src["spawnpoint"];
|
||||
setSpawnPoint(glm::vec3(
|
||||
sparr[0].asNumber(), sparr[1].asNumber(), sparr[2].asNumber()));
|
||||
|
||||
flight = src["flight"].asBoolean();
|
||||
noclip = src["noclip"].asBoolean();
|
||||
setChosenSlot(src["chosen-slot"].asInteger());
|
||||
eid = src["entity"].asNumber();
|
||||
|
||||
if (src.has("inventory")) {
|
||||
getInventory()->deserialize(src["inventory"]);
|
||||
}
|
||||
|
||||
if (src->has("spawnpoint")) {
|
||||
auto sparr = src->list("spawnpoint");
|
||||
setSpawnPoint(glm::vec3(sparr->num(0), sparr->num(1), sparr->num(2)));
|
||||
} else {
|
||||
setSpawnPoint(position);
|
||||
}
|
||||
|
||||
src->flag("flight", flight);
|
||||
src->flag("noclip", noclip);
|
||||
setChosenSlot(src->get("chosen-slot", getChosenSlot()));
|
||||
src->num("entity", eid);
|
||||
|
||||
if (auto invmap = src->map("inventory")) {
|
||||
getInventory()->deserialize(invmap.get());
|
||||
}
|
||||
|
||||
if (src->has("camera")) {
|
||||
std::string name;
|
||||
src->str("camera", name);
|
||||
if (src.has("camera")) {
|
||||
std::string name = src["camera"].asString();
|
||||
if (auto camera = level->getCamera(name)) {
|
||||
currentCamera = camera;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Player::convert(dynamic::Map* data, const ContentLUT* lut) {
|
||||
auto players = data->list("players");
|
||||
if (players) {
|
||||
for (uint i = 0; i < players->size(); i++) {
|
||||
auto playerData = players->map(i);
|
||||
if (auto inventory = playerData->map("inventory")) {
|
||||
Inventory::convert(inventory.get(), lut);
|
||||
void Player::convert(dv::value& data, const ContentLUT* lut) {
|
||||
if (data.has("players")) {
|
||||
auto& players = data["players"];
|
||||
for (uint i = 0; i < players.size(); i++) {
|
||||
auto& playerData = players[i];
|
||||
if (playerData.has("inventory")) {
|
||||
Inventory::convert(playerData["inventory"], lut);
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
if (auto inventory = data->map("inventory")) {
|
||||
Inventory::convert(inventory.get(), lut);
|
||||
}
|
||||
} else if (data.has("inventory")){
|
||||
Inventory::convert(data["inventory"], lut);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
#include "data/dynamic.hpp"
|
||||
#include "interfaces/Object.hpp"
|
||||
#include "interfaces/Serializable.hpp"
|
||||
#include "settings.hpp"
|
||||
@@ -103,10 +102,10 @@ public:
|
||||
void setSpawnPoint(glm::vec3 point);
|
||||
glm::vec3 getSpawnPoint() const;
|
||||
|
||||
std::unique_ptr<dynamic::Map> serialize() const override;
|
||||
void deserialize(dynamic::Map* src) override;
|
||||
dv::value serialize() const override;
|
||||
void deserialize(const dv::value& src) override;
|
||||
|
||||
static void convert(dynamic::Map* data, const ContentLUT* lut);
|
||||
static void convert(dv::value& data, const ContentLUT* lut);
|
||||
|
||||
inline int getId() const {
|
||||
return objectUID;
|
||||
|
||||
+12
-17
@@ -2,7 +2,7 @@
|
||||
|
||||
#include "assets/Assets.hpp"
|
||||
#include "coders/json.hpp"
|
||||
#include "data/dynamic_util.hpp"
|
||||
#include "data/dv_util.hpp"
|
||||
#include "graphics/core/Model.hpp"
|
||||
#include "graphics/render/ModelBatch.hpp"
|
||||
|
||||
@@ -139,25 +139,23 @@ Bone* SkeletonConfig::find(std::string_view str) const {
|
||||
}
|
||||
|
||||
static std::tuple<size_t, std::unique_ptr<Bone>> read_node(
|
||||
const dynamic::Map_sptr& root, size_t index
|
||||
const dv::value& root, size_t index
|
||||
) {
|
||||
std::string name;
|
||||
std::string model;
|
||||
root->str("name", name);
|
||||
root->str("model", model);
|
||||
|
||||
root.at("name").get(name);
|
||||
root.at("model").get(model);
|
||||
glm::vec3 offset(0.0f);
|
||||
dynamic::get_vec(root, "offset", offset);
|
||||
dv::get_vec(root, "offset", offset);
|
||||
|
||||
std::vector<std::unique_ptr<Bone>> bones;
|
||||
size_t count = 1;
|
||||
if (auto nodesList = root->list("nodes")) {
|
||||
for (size_t i = 0; i < nodesList->size(); i++) {
|
||||
if (const auto& map = nodesList->map(i)) {
|
||||
auto [subcount, subNode] = read_node(map, index + count);
|
||||
count += subcount;
|
||||
bones.push_back(std::move(subNode));
|
||||
}
|
||||
if (auto found = root.at("nodes")) {
|
||||
const auto& nodesList = *found;
|
||||
for (const auto& map : nodesList) {
|
||||
auto [subcount, subNode] = read_node(map, index + count);
|
||||
count += subcount;
|
||||
bones.push_back(std::move(subNode));
|
||||
}
|
||||
}
|
||||
return {
|
||||
@@ -169,10 +167,7 @@ std::unique_ptr<SkeletonConfig> SkeletonConfig::parse(
|
||||
std::string_view src, std::string_view file, std::string_view name
|
||||
) {
|
||||
auto root = json::parse(file, src);
|
||||
auto rootNodeMap = root->map("root");
|
||||
if (rootNodeMap == nullptr) {
|
||||
throw std::runtime_error("missing 'root' element");
|
||||
}
|
||||
auto rootNodeMap = root["root"];
|
||||
auto [count, rootNode] = read_node(rootNodeMap, 0);
|
||||
return std::make_unique<SkeletonConfig>(
|
||||
std::string(name), std::move(rootNode), count
|
||||
|
||||
Reference in New Issue
Block a user