From bc0d1b2f4215fee5fc7ee1ade8b9036a88729d43 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 15 Jul 2024 02:20:31 +0300 Subject: [PATCH] format Entities.cpp --- src/objects/Entities.cpp | 96 ++++++++++++++++++++++++++-------------- 1 file changed, 63 insertions(+), 33 deletions(-) diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index 75d9d975..1b8015c9 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -25,6 +25,7 @@ static debug::Logger logger("entities"); static inline std::string COMP_TRANSFORM = "transform"; static inline std::string COMP_RIGIDBODY = "rigidbody"; static inline std::string COMP_SKELETON = "skeleton"; +static inline std::string SAVED_DATA_VARNAME = "SAVED_SATA"; void Transform::refresh() { combined = glm::mat4(1.0f); @@ -47,8 +48,12 @@ rigging::Skeleton& Entity::getSkeleton() const { void Entity::setRig(const rigging::SkeletonConfig* rigConfig) { auto& skeleton = registry.get(entity); skeleton.config = rigConfig; - skeleton.pose.matrices.resize(rigConfig->getNodes().size(), glm::mat4(1.0f)); - skeleton.calculated.matrices.resize(rigConfig->getNodes().size(), glm::mat4(1.0f)); + skeleton.pose.matrices.resize( + rigConfig->getNodes().size(), glm::mat4(1.0f) + ); + skeleton.calculated.matrices.resize( + rigConfig->getNodes().size(), glm::mat4(1.0f) + ); } Entities::Entities(Level* level) : level(level) { @@ -65,6 +70,28 @@ static sensorcallback create_sensor_callback(Entities* entities) { }; } +static void initialize_body( + EntityDef& def, Rigidbody& body, entityid_t id, Entities* entities +) { + body.sensors.resize(def.radialSensors.size() + def.boxSensors.size()); + for (auto& [i, box] : def.boxSensors) { + SensorParams params {}; + params.aabb = box; + body.sensors[i] = Sensor { + true, SensorType::AABB, i, id, params, params, {}, {}, + create_sensor_callback(entities), + create_sensor_callback(entities)}; + } + for (auto& [i, radius] : def.radialSensors) { + SensorParams params {}; + params.radial = glm::vec4(radius); + body.sensors[i] = Sensor { + true, SensorType::RADIUS, i, id, params, params, {}, {}, + create_sensor_callback(entities), + create_sensor_callback(entities)}; + } +} + entityid_t Entities::spawn( EntityDef& def, glm::vec3 position, @@ -92,36 +119,33 @@ entityid_t Entities::spawn( } } auto entity = registry.create(); - registry.emplace(entity, static_cast(id), def); - const auto& tsf = registry.emplace( - entity, position, glm::vec3(1.0f), glm::mat3(1.0f), glm::mat4(1.0f), true); - auto& body = registry.emplace( - entity, true, Hitbox {def.bodyType, position, def.hitbox*0.5f}, std::vector{}); - - body.sensors.resize(def.radialSensors.size() + def.boxSensors.size()); - for (auto& [i, box] : def.boxSensors) { - SensorParams params {}; - params.aabb = box; - body.sensors[i] = Sensor { - true, SensorType::AABB, i, id, params, params, {}, {}, - create_sensor_callback(this), - create_sensor_callback(this)}; - } - for (auto& [i, radius] : def.radialSensors) { - SensorParams params {}; - params.radial = glm::vec4(radius); - body.sensors[i] = Sensor { - true, SensorType::RADIUS, i, id, params, params, {}, {}, - create_sensor_callback(this), - create_sensor_callback(this)}; - } - auto& scripting = registry.emplace(entity); entities[id] = entity; uids[entity] = id; + + registry.emplace(entity, static_cast(id), def); + const auto& tsf = registry.emplace( + entity, + position, + glm::vec3(1.0f), + glm::mat3(1.0f), + glm::mat4(1.0f), + true + ); + auto& body = registry.emplace( + entity, + true, + Hitbox {def.bodyType, position, def.hitbox * 0.5f}, + std::vector {} + ); + initialize_body(def, body, id, this); + + auto& scripting = registry.emplace(entity); registry.emplace(entity, skeleton->instance()); + for (auto& componentName : def.components) { auto component = std::make_unique( - componentName, entity_funcs_set {}, nullptr); + componentName, entity_funcs_set {}, nullptr + ); scripting.components.emplace_back(std::move(component)); } dynamic::Map_sptr componentsMap = nullptr; @@ -131,7 +155,8 @@ entityid_t Entities::spawn( } body.hitbox.position = tsf.pos; scripting::on_entity_spawn( - def, id, scripting.components, std::move(args), std::move(componentsMap)); + def, id, scripting.components, std::move(args), std::move(componentsMap) + ); return id; } @@ -189,7 +214,9 @@ void Entities::loadEntity(const dynamic::Map_sptr& map, Entity entity) { } } if (auto posearr = skeletonmap->list("pose")) { - for (size_t i = 0; i < std::min(skeleton.pose.matrices.size(), posearr->size()); i++) { + for (size_t i = 0; + i < std::min(skeleton.pose.matrices.size(), posearr->size()); + i++) { dynamic::get_mat(posearr, i, skeleton.pose.matrices[i]); } } @@ -213,8 +240,8 @@ std::optional Entities::rayCast( glm::ivec3 normal; double distance; if (ray.intersectAABB( - glm::vec3(), hitbox.getAABB(), maxDistance, normal, distance) > RayRelation::None) { - + glm::vec3(), hitbox.getAABB(), maxDistance, normal, distance + ) > RayRelation::None) { foundUID = eid.uid; foundNormal = normal; maxDistance = static_cast(distance); @@ -303,7 +330,8 @@ dynamic::Value Entities::serialize(const Entity& entity) { if (!scripts.components.empty()) { auto& compsMap = root->putMap("comps"); for (auto& comp : scripts.components) { - auto data = scripting::get_component_value(comp->env, "SAVED_DATA"); + auto data = scripting::get_component_value( + comp->env, SAVED_DATA_VARNAME); compsMap.put(comp->name, data); } } @@ -428,7 +456,9 @@ void Entities::renderDebug(LineBatch& batch, const Frustum& frustum) { } } -void Entities::render(Assets* assets, ModelBatch& batch, const Frustum& frustum, bool pause) { +void Entities::render( + Assets* assets, ModelBatch& batch, const Frustum& frustum, bool pause +) { if (!pause) { scripting::on_entities_render(); }