update RigConfig

This commit is contained in:
MihailRis
2024-07-01 05:30:27 +03:00
parent 1b41a75cf4
commit beac332c96
15 changed files with 230 additions and 62 deletions
+4 -2
View File
@@ -36,7 +36,8 @@ entityid_t Entities::spawn(EntityDef& def, glm::vec3 pos, dynamic::Value args) {
auto id = nextID++;
registry.emplace<EntityId>(entity, static_cast<entityid_t>(id), def);
registry.emplace<Transform>(entity, pos, size, glm::mat3(1.0f));
auto& body = registry.emplace<Rigidbody>(entity, true, Hitbox {pos, def.hitbox}, std::vector<Trigger>{});
auto& body = registry.emplace<Rigidbody>(
entity, true, Hitbox {pos, def.hitbox}, std::vector<Trigger>{});
for (auto& box : def.triggers) {
body.triggers.emplace_back(Trigger{true, id, box, AABB{}, {}, {},
[=](auto entityid, auto index, auto otherid) {
@@ -140,7 +141,8 @@ void Entities::updatePhysics(float delta) {
hitbox.linearDamping = hitbox.grounded * 24;
transform.setPos(hitbox.position);
if (hitbox.grounded && !grounded) {
scripting::on_entity_grounded(*get(eid.uid), glm::length(prevVel-hitbox.velocity));
scripting::on_entity_grounded(
*get(eid.uid), glm::length(prevVel-hitbox.velocity));
}
if (!hitbox.grounded && grounded) {
scripting::on_entity_fall(*get(eid.uid));
+6 -1
View File
@@ -77,7 +77,12 @@ class Entity {
entt::registry& registry;
const entt::entity entity;
public:
Entity(Entities& entities, entityid_t id, entt::registry& registry, const entt::entity entity)
Entity(
Entities& entities,
entityid_t id,
entt::registry& registry,
const entt::entity entity
)
: entities(entities), id(id), registry(registry), entity(entity) {}
EntityId& getID() const {
+6
View File
@@ -8,6 +8,10 @@
#include "../typedefs.hpp"
#include "../maths/aabb.hpp"
namespace rigging {
class RigConfig;
}
struct EntityDef {
/// @brief Entity string id (with prefix included)
std::string const name;
@@ -15,9 +19,11 @@ struct EntityDef {
std::string scriptName = name.substr(name.find(':')+1);
glm::vec3 hitbox {0.5f};
std::vector<AABB> triggers {};
std::string rigName = name.substr(name.find(":")+1);
struct {
entityid_t id;
rigging::RigConfig* rig;
} rt {};
EntityDef(const std::string& name) : name(name) {}
+81 -2
View File
@@ -1,10 +1,89 @@
#include "rigging.hpp"
#include "../assets/Assets.hpp"
#include "../graphics/core/Model.hpp"
#include "../coders/json.hpp"
using namespace rigging;
RigNode::RigNode(size_t index, std::string name, std::vector<std::unique_ptr<RigNode>> subnodes)
: index(index), name(std::move(name)), subnodes(std::move(subnodes)) {
RigNode::RigNode(
size_t index,
std::string name,
std::string model,
std::vector<std::unique_ptr<RigNode>> subnodes)
: index(index),
name(std::move(name)),
modelName(model),
subnodes(std::move(subnodes))
{}
void RigNode::setModel(const Assets* assets, const std::string& name) {
modelName = name;
model = assets->get<model::Model>(name);
}
RigConfig::RigConfig(std::unique_ptr<RigNode> root) : root(std::move(root)) {
}
size_t RigConfig::update(
size_t index,
Rig& rig,
RigNode* node,
glm::mat4 matrix)
{
rig.calculated.matrices[index] = matrix * rig.pose.matrices[index];
index++;
for (auto& subnode : node->getSubnodes()) {
index = update(index, rig, subnode.get(), rig.calculated.matrices[index]);
}
return index;
}
void RigConfig::update(Rig& rig, glm::mat4 matrix) {
update(0, rig, root.get(), matrix);
}
void RigConfig::setup(const Assets* assets, RigNode* node) {
if (node == nullptr) {
setup(assets, root.get());
} else {
node->setModel(assets, node->getModelName());
for (auto& subnode : node->getSubnodes()) {
setup(assets, subnode.get());
}
}
}
static std::tuple<size_t, std::unique_ptr<RigNode>> read_node(
dynamic::Map* root, size_t index
) {
std::string name;
std::string model;
root->str("name", name);
root->str("model", model);
std::vector<std::unique_ptr<RigNode>> subnodes;
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);
subcount += count;
subnodes.push_back(std::move(subNode));
}
}
}
return {index, std::make_unique<RigNode>(index, name, model, std::move(subnodes))};
}
std::unique_ptr<RigConfig> RigConfig::parse(
std::string_view src,
std::string_view file
) {
auto root = json::parse(file, src);
auto rootNodeMap = root->map("root");
if (rootNodeMap == nullptr) {
throw std::runtime_error("missing 'root' element");
}
auto [count, rootNode] = read_node(root.get(), 0);
return std::make_unique<RigConfig>(std::move(rootNode));
}
+34 -1
View File
@@ -9,6 +9,12 @@
#include <glm/glm.hpp>
#include <unordered_map>
class Assets;
namespace model {
struct Model;
}
namespace rigging {
struct Rig;
@@ -19,9 +25,21 @@ namespace rigging {
class RigNode {
size_t index;
std::string name;
std::string modelName;
std::vector<std::unique_ptr<RigNode>> subnodes;
model::Model* model = nullptr;
public:
RigNode(size_t index, std::string name, std::vector<std::unique_ptr<RigNode>> subnodes);
RigNode(
size_t index,
std::string name,
std::string model,
std::vector<std::unique_ptr<RigNode>> subnodes);
void setModel(const Assets* assets, const std::string& name);
const std::string& getModelName() const {
return modelName;
}
size_t getIndex() const {
return index;
@@ -36,13 +54,28 @@ namespace rigging {
std::unique_ptr<RigNode> root;
std::unordered_map<std::string, size_t> indices;
std::vector<RigNode*> nodes;
size_t update(
size_t index,
Rig& rig,
RigNode* node,
glm::mat4 matrix);
public:
RigConfig(std::unique_ptr<RigNode> root);
void update(Rig& rig, glm::mat4 matrix);
void setup(const Assets* assets, RigNode* node=nullptr);
static std::unique_ptr<RigConfig> parse(
std::string_view src,
std::string_view file
);
};
struct Rig {
RigConfig* config;
Pose pose;
Pose calculated;
std::vector<std::string> textures;
};
};