refactor Entities load + loading velocity

This commit is contained in:
MihailRis 2024-07-08 22:47:01 +03:00
parent 3cd0261f3b
commit 19c6a82ab2
6 changed files with 41 additions and 24 deletions

View File

@ -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

View File

@ -97,6 +97,10 @@ void stringifyObj(
const std::string& indentstr,
bool nice
) {
if (obj == nullptr) {
ss << "nullptr";
return;
}
if (obj->values.empty()) {
ss << "{}";
return;

View File

@ -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;
}

View File

@ -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<EntityId>(entity, static_cast<entityid_t>(id), def);
registry.emplace<Transform>(entity, transform);
auto& tsf = registry.emplace<Transform>(entity, position, glm::vec3(1.0f), glm::mat3(1.0f), glm::mat4(1.0f), true);
auto& body = registry.emplace<Rigidbody>(
entity, true, Hitbox {transform.pos, def.hitbox}, std::vector<Trigger>{});
entity, true, Hitbox {position, def.hitbox}, std::vector<Trigger>{});
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) {

View File

@ -65,6 +65,7 @@ struct Rigidbody {
bool enabled = true;
Hitbox hitbox;
std::vector<Trigger> 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<Entity> getAllInside(AABB aabb);
void despawn(entityid_t id);

View File

@ -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 {