40 auto ovary_group =
registry.group<
ovary_component>(entt::get<ant_genome_component, rigid_body_component, scene_component, pose_component>);
48 auto& ovary = ovary_group.get<ovary_component>(entity_id);
51 if (ovary.egg_count < ovary.egg_capacity)
53 ovary.elapsed_egg_production_time += dt * m_time_scale;
54 if (ovary.elapsed_egg_production_time >= ovary.egg_production_duration)
56 ovary.egg_count += static_cast<std::uint16_t>(ovary.elapsed_egg_production_time / ovary.egg_production_duration);
57 ovary.egg_count = std::min(ovary.egg_count, ovary.egg_capacity);
58 ovary.elapsed_egg_production_time = math::fract(ovary.elapsed_egg_production_time);
63 if (ovary.ovipositor_egg_eid != entt::null || (ovary.ovipositing && ovary.egg_count))
66 const auto& ovipositor_rigid_body = *ovary_group.get<rigid_body_component>(entity_id).body;
67 const auto& ovipositor_pose = ovary_group.get<pose_component>(entity_id);
68 const auto ovipositor_transform = ovipositor_rigid_body.get_transform() * ovipositor_pose.current_pose.get_absolute_transform(ovary.ovipositor_bone);
71 if (ovary.ovipositing)
73 ovary.elapsed_oviposition_time += dt * m_time_scale;
77 ovary.elapsed_oviposition_time -= dt * m_time_scale;
78 ovary.elapsed_oviposition_time = std::max(0.0f, ovary.elapsed_oviposition_time);
82 const float t =
std::min(ovary.elapsed_oviposition_time / ovary.oviposition_duration, 1.0f);
83 auto egg_transform = ovipositor_transform;
84 egg_transform.translation = egg_transform *
math::lerp(ovary.oviposition_path.a, ovary.oviposition_path.b, t);
86 if (ovary.ovipositor_egg_eid == entt::null)
89 const auto& parent_genome = ovary_group.get<ant_genome_component>(entity_id);
92 const auto& ovipositor_scene = ovary_group.get<scene_component>(entity_id);
96 auto egg_rigid_body = std::make_unique<physics::rigid_body>();
97 egg_rigid_body->set_mass(0.0f);
98 egg_rigid_body->set_transform(egg_transform);
99 egg_rigid_body->set_previous_transform(egg_transform);
102 auto egg_scene_object = std::make_shared<scene::static_mesh>(parent_genome.genome->egg->phenes.front().model);
105 ovary.ovipositor_egg_eid = registry.create();
106 registry.emplace<rigid_body_component>(ovary.ovipositor_egg_eid, std::move(egg_rigid_body));
107 registry.emplace<scene_component>(ovary.ovipositor_egg_eid, std::move(egg_scene_object), ovipositor_scene.layer_mask);
108 registry.emplace<ant_genome_component>(ovary.ovipositor_egg_eid, parent_genome);
113 auto& egg_rigid_body = *registry.get<rigid_body_component>(ovary.ovipositor_egg_eid).body;
114 egg_rigid_body.set_transform(egg_transform);
117 if (ovary.elapsed_oviposition_time >= ovary.oviposition_duration)
120 auto& egg_rigid_body = *registry.get<rigid_body_component>(ovary.ovipositor_egg_eid).body;
121 const auto oviposition_ray = geom::ray<float, 3>{egg_transform.translation, egg_transform.rotation * math::fvec3{0, 0, -1}};
122 if (
auto trace = m_physics_system->
trace(oviposition_ray, ovary.ovipositor_egg_eid, ~std::uint32_t{0}))
124 egg_transform.translation = oviposition_ray.extrapolate(std::get<1>(*trace));
125 egg_transform.rotation = math::normalize(math::rotation(egg_transform.rotation * math::fvec3{0, 1, 0}, std::get<3>(*
trace)) * egg_transform.rotation);
126 egg_rigid_body.set_transform(egg_transform);
132 registry.emplace<
egg_component>(ovary.ovipositor_egg_eid, genome.egg->phenes.front().incubation_period, 0.0f);
135 ovary.ovipositing =
false;
136 ovary.elapsed_oviposition_time = 0.0f;
138 ovary.ovipositor_egg_eid = entt::null;
std::optional< std::tuple< entity::id, float, std::uint32_t, math::fvec3 > > trace(const geom::ray< float, 3 > &ray, entity::id ignore_eid=entt::null, std::uint32_t layer_mask=~std::uint32_t{0}) const
Traces a ray to the nearest point of intersection.
void update(float t, float dt) override
Perform's a system's update() function.
reproductive_system(entity::registry ®istry)
Abstract base class for updatable systems.
entity::registry & registry
Registry on which the system operate.
entt::registry registry
Component registry type.
constexpr T trace(const matrix< T, N, N > &m) noexcept
Calculates the trace of a square matrix.
constexpr T lerp(const T &x, const T &y, S a) noexcept
Linearly interpolates between x and y.
constexpr vector< T, N > min(const vector< T, N > &x, const vector< T, N > &y)
Returns a vector containing the minimum elements of two vectors.
Egg incubation parameters.