add modeltree component

This commit is contained in:
MihailRis 2024-07-01 22:09:52 +03:00
parent 824231bfd6
commit 8e08f79b54
5 changed files with 53 additions and 13 deletions

View File

@ -23,6 +23,13 @@ function new_Rigidbody(eid)
return setmetatable({eid=eid}, Rigidbody)
end
local Modeltree = {__index={
}}
function new_Modeltree(eid)
return setmetatable({eid=eid}, Modeltree)
end
-- Entity class
local Entity = {__index={
@ -36,6 +43,7 @@ return {
local entity = setmetatable({eid=eid}, Entity)
entity.transform = new_Transform(eid)
entity.rigidbody = new_Rigidbody(eid)
entity.modeltree = new_Modeltree(eid)
entity.data = {}
entities[eid] = entity;
return entity

View File

@ -8,6 +8,7 @@
#include "../../../window/Camera.hpp"
#include "../../../frontend/hud.hpp"
#include "../../../content/Content.hpp"
#include "../../../engine.hpp"
#include <optional>
@ -35,7 +36,7 @@ static int l_spawn(lua::State* L) {
if (lua::gettop(L) > 2) {
args = lua::tovalue(L, 3);
}
level->entities->spawn(def, pos, args);
level->entities->spawn(scripting::engine->getAssets(), def, pos, args);
return 1;
}

View File

@ -9,6 +9,7 @@
#include "../graphics/core/Model.hpp"
#include "../maths/FrustumCulling.hpp"
#include "../objects/EntityDef.hpp"
#include "../objects/rigging.hpp"
#include "../logic/scripting/scripting.hpp"
#include <glm/ext/matrix_transform.hpp>
@ -30,7 +31,16 @@ void Entity::destroy() {
Entities::Entities(Level* level) : level(level) {
}
entityid_t Entities::spawn(EntityDef& def, glm::vec3 pos, dynamic::Value args) {
entityid_t Entities::spawn(
Assets* assets,
EntityDef& def,
glm::vec3 pos,
dynamic::Value args)
{
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++;
@ -54,9 +64,12 @@ entityid_t Entities::spawn(EntityDef& def, glm::vec3 pos, dynamic::Value args) {
}
}});
}
auto& scripting = registry.emplace<Scripting>(entity, entity_funcs_set {}, nullptr);
auto& scripting = registry.emplace<Scripting>(
entity, entity_funcs_set {}, nullptr);
entities[id] = entity;
scripting.env = scripting::on_entity_spawn(def, id, scripting.funcsset, std::move(args));
scripting.env = scripting::on_entity_spawn(
def, id, scripting.funcsset, std::move(args));
registry.emplace<rigging::Rig>(entity, rig->instance());
return id;
}
@ -173,7 +186,10 @@ void Entities::renderDebug(LineBatch& batch, const Frustum& frustum) {
batch.box(hitbox.position, hitbox.halfsize * 2.0f, glm::vec4(1.0f));
for (auto& trigger : rigidbody.triggers) {
batch.box(trigger.calculated.center(), trigger.calculated.size(), glm::vec4(1.0f, 1.0f, 0.0f, 1.0f));
batch.box(
trigger.calculated.center(),
trigger.calculated.size(),
glm::vec4(1.0f, 1.0f, 0.0f, 1.0f));
}
}
}

View File

@ -137,7 +137,11 @@ public:
void renderDebug(LineBatch& batch, const Frustum& frustum);
void render(Assets* assets, ModelBatch& batch, const Frustum& frustum);
entityid_t spawn(EntityDef& def, glm::vec3 pos, dynamic::Value args=dynamic::NONE);
entityid_t spawn(
Assets* assets,
EntityDef& def,
glm::vec3 pos,
dynamic::Value args=dynamic::NONE);
std::optional<Entity> get(entityid_t id) {
const auto& found = entities.find(id);

View File

@ -17,9 +17,14 @@ namespace model {
namespace rigging {
struct Rig;
class RigConfig;
struct Pose {
std::vector<glm::mat4> matrices;
Pose(size_t size) : matrices(size) {
matrices.resize(size);
}
};
class RigNode {
@ -49,6 +54,13 @@ namespace rigging {
return subnodes;
}
};
struct Rig {
RigConfig* config;
Pose pose;
Pose calculated;
std::vector<std::string> textures;
};
class RigConfig {
std::unique_ptr<RigNode> root;
@ -66,18 +78,17 @@ namespace rigging {
void update(Rig& rig, glm::mat4 matrix);
void setup(const Assets* assets, RigNode* node=nullptr);
Rig instance() {
return Rig {
this, Pose(nodes.size()), Pose(nodes.size()), {}
};
}
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;
};
};
#endif // OBJECTS_SKELETON_HPP_