refactor Entities load + loading velocity
This commit is contained in:
parent
3cd0261f3b
commit
19c6a82ab2
@ -30,7 +30,7 @@ do -- setup visuals
|
|||||||
local bid = block.index(icon:sub(16))
|
local bid = block.index(icon:sub(16))
|
||||||
model = block.get_model(bid)
|
model = block.get_model(bid)
|
||||||
if model == "X" then
|
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}))
|
body:set_size(vec3.mul(body:get_size(), {1.0, 0.3, 1.0}))
|
||||||
rig:set_texture("$0", icon)
|
rig:set_texture("$0", icon)
|
||||||
else
|
else
|
||||||
@ -46,7 +46,7 @@ do -- setup visuals
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
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}))
|
body:set_size(vec3.mul(body:get_size(), {1.0, 0.3, 1.0}))
|
||||||
rig:set_texture("$0", icon)
|
rig:set_texture("$0", icon)
|
||||||
end
|
end
|
||||||
|
|||||||
@ -97,6 +97,10 @@ void stringifyObj(
|
|||||||
const std::string& indentstr,
|
const std::string& indentstr,
|
||||||
bool nice
|
bool nice
|
||||||
) {
|
) {
|
||||||
|
if (obj == nullptr) {
|
||||||
|
ss << "nullptr";
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (obj->values.empty()) {
|
if (obj->values.empty()) {
|
||||||
ss << "{}";
|
ss << "{}";
|
||||||
return;
|
return;
|
||||||
|
|||||||
@ -37,10 +37,7 @@ static int l_spawn(lua::State* L) {
|
|||||||
if (lua::gettop(L) > 2) {
|
if (lua::gettop(L) > 2) {
|
||||||
args = lua::tovalue(L, 3);
|
args = lua::tovalue(L, 3);
|
||||||
}
|
}
|
||||||
Transform transform {
|
level->entities->spawn(def, pos, args);
|
||||||
pos, glm::vec3(1.0f), glm::mat3(1.0f), {}, true
|
|
||||||
};
|
|
||||||
level->entities->spawn(def, transform, args);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -20,6 +20,10 @@
|
|||||||
|
|
||||||
static debug::Logger logger("entities");
|
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() {
|
void Transform::refresh() {
|
||||||
combined = glm::mat4(1.0f);
|
combined = glm::mat4(1.0f);
|
||||||
combined = glm::translate(combined, pos);
|
combined = glm::translate(combined, pos);
|
||||||
@ -61,7 +65,7 @@ static triggercallback create_trigger_callback(Entities* entities) {
|
|||||||
|
|
||||||
entityid_t Entities::spawn(
|
entityid_t Entities::spawn(
|
||||||
EntityDef& def,
|
EntityDef& def,
|
||||||
Transform transform,
|
glm::vec3 position,
|
||||||
dynamic::Value args,
|
dynamic::Value args,
|
||||||
dynamic::Map_sptr saved,
|
dynamic::Map_sptr saved,
|
||||||
entityid_t uid)
|
entityid_t uid)
|
||||||
@ -78,9 +82,9 @@ entityid_t Entities::spawn(
|
|||||||
id = uid;
|
id = uid;
|
||||||
}
|
}
|
||||||
registry.emplace<EntityId>(entity, static_cast<entityid_t>(id), def);
|
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>(
|
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());
|
body.triggers.resize(def.radialTriggers.size() + def.boxTriggers.size());
|
||||||
for (auto& [i, box] : def.boxTriggers) {
|
for (auto& [i, box] : def.boxTriggers) {
|
||||||
@ -108,8 +112,14 @@ entityid_t Entities::spawn(
|
|||||||
componentName, entity_funcs_set {}, nullptr);
|
componentName, entity_funcs_set {}, nullptr);
|
||||||
scripting.components.emplace_back(std::move(component));
|
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(
|
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;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,14 +142,20 @@ void Entities::loadEntity(const dynamic::Map_sptr& map) {
|
|||||||
throw std::runtime_error("could not read entity - invalid UID");
|
throw std::runtime_error("could not read entity - invalid UID");
|
||||||
}
|
}
|
||||||
auto& def = level->content->entities.require(defname);
|
auto& def = level->content->entities.require(defname);
|
||||||
Transform transform {
|
spawn(def, {}, dynamic::NONE, map, uid);
|
||||||
glm::vec3(), glm::vec3(1.0f), glm::mat3(1.0f), {}, true
|
}
|
||||||
};
|
|
||||||
if (auto tsfmap = map->map("transform")) {
|
void Entities::loadEntity(const dynamic::Map_sptr& map, Entity entity) {
|
||||||
dynamic::get_vec(tsfmap, "pos", transform.pos);
|
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) {
|
void Entities::loadEntities(dynamic::Map_sptr root) {
|
||||||
@ -165,7 +181,7 @@ dynamic::Value Entities::serialize(const Entity& entity) {
|
|||||||
root->put("uid", eid.uid);
|
root->put("uid", eid.uid);
|
||||||
{
|
{
|
||||||
auto& transform = entity.getTransform();
|
auto& transform = entity.getTransform();
|
||||||
auto& tsfmap = root->putMap("transform");
|
auto& tsfmap = root->putMap(COMP_TRANSFORM);
|
||||||
tsfmap.put("pos", dynamic::to_value(transform.pos));
|
tsfmap.put("pos", dynamic::to_value(transform.pos));
|
||||||
if (transform.size != glm::vec3(1.0f)) {
|
if (transform.size != glm::vec3(1.0f)) {
|
||||||
tsfmap.put("size", dynamic::to_value(transform.size));
|
tsfmap.put("size", dynamic::to_value(transform.size));
|
||||||
@ -176,7 +192,7 @@ dynamic::Value Entities::serialize(const Entity& entity) {
|
|||||||
}
|
}
|
||||||
{
|
{
|
||||||
auto& rigidbody = entity.getRigidbody();
|
auto& rigidbody = entity.getRigidbody();
|
||||||
auto& bodymap = root->putMap("rigidbody");
|
auto& bodymap = root->putMap(COMP_RIGIDBODY);
|
||||||
if (!rigidbody.enabled) {
|
if (!rigidbody.enabled) {
|
||||||
bodymap.put("enabled", rigidbody.enabled);
|
bodymap.put("enabled", rigidbody.enabled);
|
||||||
}
|
}
|
||||||
@ -188,7 +204,7 @@ dynamic::Value Entities::serialize(const Entity& entity) {
|
|||||||
root->put("rig", rig.config->getName());
|
root->put("rig", rig.config->getName());
|
||||||
}
|
}
|
||||||
if (def.save.rig.pose || def.save.rig.textures) {
|
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) {
|
if (def.save.rig.textures) {
|
||||||
auto& map = rigmap.putMap("textures");
|
auto& map = rigmap.putMap("textures");
|
||||||
for (auto& [slot, texture] : rig.textures) {
|
for (auto& [slot, texture] : rig.textures) {
|
||||||
|
|||||||
@ -65,6 +65,7 @@ struct Rigidbody {
|
|||||||
bool enabled = true;
|
bool enabled = true;
|
||||||
Hitbox hitbox;
|
Hitbox hitbox;
|
||||||
std::vector<Trigger> triggers;
|
std::vector<Trigger> triggers;
|
||||||
|
float gravityMultiplier = 1.0f;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct UserComponent {
|
struct UserComponent {
|
||||||
@ -171,7 +172,7 @@ public:
|
|||||||
|
|
||||||
entityid_t spawn(
|
entityid_t spawn(
|
||||||
EntityDef& def,
|
EntityDef& def,
|
||||||
Transform transform,
|
glm::vec3 position,
|
||||||
dynamic::Value args=dynamic::NONE,
|
dynamic::Value args=dynamic::NONE,
|
||||||
dynamic::Map_sptr saved=nullptr,
|
dynamic::Map_sptr saved=nullptr,
|
||||||
entityid_t uid=0);
|
entityid_t uid=0);
|
||||||
@ -186,6 +187,7 @@ public:
|
|||||||
|
|
||||||
void loadEntities(dynamic::Map_sptr map);
|
void loadEntities(dynamic::Map_sptr map);
|
||||||
void loadEntity(const 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);
|
void onSave(const Entity& entity);
|
||||||
std::vector<Entity> getAllInside(AABB aabb);
|
std::vector<Entity> getAllInside(AABB aabb);
|
||||||
void despawn(entityid_t id);
|
void despawn(entityid_t id);
|
||||||
|
|||||||
@ -42,9 +42,7 @@ Player::~Player() {
|
|||||||
void Player::updateEntity() {
|
void Player::updateEntity() {
|
||||||
if (eid == 0) {
|
if (eid == 0) {
|
||||||
auto& def = level->content->entities.require("base:player");
|
auto& def = level->content->entities.require("base:player");
|
||||||
eid = level->entities->spawn(def, Transform {
|
eid = level->entities->spawn(def, getPosition());
|
||||||
getPosition(), glm::vec3(1.0f), glm::mat3(1.0f), {}, true
|
|
||||||
});
|
|
||||||
} else if (auto entity = level->entities->get(eid)) {
|
} else if (auto entity = level->entities->get(eid)) {
|
||||||
position = entity->getTransform().pos;
|
position = entity->getTransform().pos;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user