@ -1,136 +0,0 @@ | |||||
/* | |||||
* Copyright (C) 2023 Christopher J. Howard | |||||
* | |||||
* This file is part of Antkeeper source code. | |||||
* | |||||
* Antkeeper source code is free software: you can redistribute it and/or modify | |||||
* it under the terms of the GNU General Public License as published by | |||||
* the Free Software Foundation, either version 3 of the License, or | |||||
* (at your option) any later version. | |||||
* | |||||
* Antkeeper source code is distributed in the hope that it will be useful, | |||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
* GNU General Public License for more details. | |||||
* | |||||
* You should have received a copy of the GNU General Public License | |||||
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
#ifndef ANTKEEPER_SPRING_HPP | |||||
#define ANTKEEPER_SPRING_HPP | |||||
#include <engine/math/numbers.hpp> | |||||
/** | |||||
* Contains the variables required for numeric springing. | |||||
* | |||||
* @tparam T Value type. | |||||
* @tparam S Scalar type. | |||||
* | |||||
* @see spring() | |||||
* @see solve_numeric_spring() | |||||
*/ | |||||
template <typename T, typename S> | |||||
struct numeric_spring | |||||
{ | |||||
T x0; ///< Start value | |||||
T x1; ///< End value | |||||
T v; ///< Velocity | |||||
S z; ///< Damping ratio, which can be undamped (z = 0), underdamped (z < 1), critically damped (z = 1), or overdamped (z > 1). | |||||
S w; ///< Angular frequency of the oscillation, in radians per second (2pi = 1Hz). | |||||
}; | |||||
/** | |||||
* Solves a number spring using the implicit Euler method. | |||||
* | |||||
* @tparam T Value type. | |||||
* @tparam S Scalar type. | |||||
* | |||||
* @param[in,out] x0 Start value, which will be oscillated by this function. | |||||
* @param[in,out] v Velocity, which will be modified by this function. | |||||
* @param[in] x1 End value. | |||||
* @param[in] z Damping ratio, which can be undamped (z = 0), underdamped (z < 1), critically damped (z = 1), or overdamped (z > 1). | |||||
* @param[in] w Angular frequency of the oscillation, in radians per second (2pi = 1Hz). | |||||
* @param[in] dt Delta time, in seconds. | |||||
*/ | |||||
template <typename T, typename S> | |||||
void spring(T& x0, T& v, const T& x1, S z, S w, S dt); | |||||
/** | |||||
* Solves a number spring using the implicit Euler method. | |||||
* | |||||
* @param[in,out] ns Numeric spring to be sovled. | |||||
* @param dt Delta time, in seconds. | |||||
* | |||||
* @see spring() | |||||
*/ | |||||
template <typename T, typename S> | |||||
void solve_numeric_spring(numeric_spring<T, S>& ns, S dt); | |||||
/** | |||||
* Converts a frequency from hertz to radians per second. | |||||
* | |||||
* @param hz Frequency in hertz. | |||||
* @return Frequency in radians per second. | |||||
*/ | |||||
template <typename T> | |||||
T hz_to_rads(T hz); | |||||
/** | |||||
* Converts a frequency from radians per second to hertz. | |||||
* | |||||
* @param rads Frequency in radians per second. | |||||
* @return Frequency in hertz. | |||||
*/ | |||||
template <typename T> | |||||
T rads_to_hz(T rads); | |||||
/** | |||||
* Converts a period from seconds to radians per second. | |||||
* | |||||
* @param t Period, in seconds. | |||||
* @return Angular frequency, in radians per second. | |||||
*/ | |||||
template <typename T> | |||||
T period_to_rads(T t); | |||||
template <typename T, typename S> | |||||
void spring(T& x0, T& v, const T& x1, S z, S w, S dt) | |||||
{ | |||||
const S ww_dt = w * w * dt; | |||||
const S ww_dtdt = ww_dt * dt; | |||||
const S f = z * w * dt * S{2} + S{1}; | |||||
const T det_x = x0 * f + v * dt + x1 * ww_dtdt; | |||||
const T det_v = v + (x1 - x0) * ww_dt; | |||||
const S inv_det = S{1} / (f + ww_dtdt); | |||||
x0 = det_x * inv_det; | |||||
v = det_v * inv_det; | |||||
} | |||||
template <typename T, typename S> | |||||
void solve_numeric_spring(numeric_spring<T, S>& ns, S dt) | |||||
{ | |||||
spring(ns.x0, ns.v, ns.x1, ns.z, ns.w, dt); | |||||
} | |||||
template <typename T> | |||||
inline T hz_to_rads(T hz) | |||||
{ | |||||
return hz * math::two_pi<T>; | |||||
} | |||||
template <typename T> | |||||
inline T rads_to_hz(T rads) | |||||
{ | |||||
return rads / math::two_pi<T>; | |||||
} | |||||
template <typename T> | |||||
inline T period_to_rads(T t) | |||||
{ | |||||
return math::two_pi<T> / t; | |||||
} | |||||
#endif // ANTKEEPER_SPRING_HPP |
@ -0,0 +1,89 @@ | |||||
/* | |||||
* Copyright (C) 2023 Christopher J. Howard | |||||
* | |||||
* This file is part of Antkeeper source code. | |||||
* | |||||
* Antkeeper source code is free software: you can redistribute it and/or modify | |||||
* it under the terms of the GNU General Public License as published by | |||||
* the Free Software Foundation, either version 3 of the License, or | |||||
* (at your option) any later version. | |||||
* | |||||
* Antkeeper source code is distributed in the hope that it will be useful, | |||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
* GNU General Public License for more details. | |||||
* | |||||
* You should have received a copy of the GNU General Public License | |||||
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
#ifndef ANTKEEPER_PHYSICS_FREQUENCY_HPP | |||||
#define ANTKEEPER_PHYSICS_FREQUENCY_HPP | |||||
#include <engine/math/numbers.hpp> | |||||
namespace physics { | |||||
/** | |||||
* Converts frequency to angular frequency. | |||||
* | |||||
* @tparam T Scalar type. | |||||
* | |||||
* @param f Frequency, in hertz. | |||||
* | |||||
* @return Angular frequency, in radians per second. | |||||
*/ | |||||
template <class T> | |||||
[[nodiscard]] inline constexpr T hz_to_rads(T f) noexcept | |||||
{ | |||||
return f * math::two_pi<T>; | |||||
} | |||||
/** | |||||
* Converts angular frequency to frequency. | |||||
* | |||||
* @tparam T Scalar type. | |||||
* | |||||
* @param w Angular frequency, in radians per second. | |||||
* | |||||
* @return Frequency, in hertz. | |||||
*/ | |||||
template <class T> | |||||
[[nodiscard]] inline constexpr T rads_to_hz(T w) noexcept | |||||
{ | |||||
return w / math::two_pi<T>; | |||||
} | |||||
/** | |||||
* Converts oscillation period to angular frequency. | |||||
* | |||||
* @tparam T Scalar type. | |||||
* | |||||
* @param t Oscillation period, in seconds. | |||||
* | |||||
* @return Angular frequency, in radians per second. | |||||
*/ | |||||
template <class T> | |||||
[[nodiscard]] inline constexpr T s_to_rads(T t) noexcept | |||||
{ | |||||
return math::two_pi<T> / t; | |||||
} | |||||
/** | |||||
* Converts angular frequency to oscillation period. | |||||
* | |||||
* @tparam T Scalar type. | |||||
* | |||||
* @param w Angular frequency, in radians per second. | |||||
* | |||||
* @return Oscillation period, in seconds. | |||||
*/ | |||||
template <class T> | |||||
[[nodiscard]] inline constexpr T rads_to_s(T w) noexcept | |||||
{ | |||||
return math::two_pi<T> / w; | |||||
} | |||||
} // namespace physics | |||||
#endif // ANTKEEPER_PHYSICS_FREQUENCY_HPP |
@ -0,0 +1,201 @@ | |||||
/* | |||||
* Copyright (C) 2023 Christopher J. Howard | |||||
* | |||||
* This file is part of Antkeeper source code. | |||||
* | |||||
* Antkeeper source code is free software: you can redistribute it and/or modify | |||||
* it under the terms of the GNU General Public License as published by | |||||
* the Free Software Foundation, either version 3 of the License, or | |||||
* (at your option) any later version. | |||||
* | |||||
* Antkeeper source code is distributed in the hope that it will be useful, | |||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
* GNU General Public License for more details. | |||||
* | |||||
* You should have received a copy of the GNU General Public License | |||||
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
#ifndef ANTKEEPER_PHYSICS_SPRING_HPP | |||||
#define ANTKEEPER_PHYSICS_SPRING_HPP | |||||
#include <engine/math/numbers.hpp> | |||||
#include <engine/physics/frequency.hpp> | |||||
namespace physics { | |||||
/** | |||||
* Solves a numeric spring using the implicit Euler method. | |||||
* | |||||
* @tparam T Value type. | |||||
* @tparam S Scalar type. | |||||
* | |||||
* @param[in,out] x Current value of the spring. | |||||
* @param[in,out] v Current velocity of the spring. | |||||
* @param[in] xt Target value of the spring. | |||||
* @param[in] z Damping ratio of the spring, which can be undamped (`0`), underdamped (`< 1`), critically damped (`1`), or overdamped (`> 1`). | |||||
* @param[in] w Angular frequency of the spring oscillation, in radians per second. | |||||
* @param[in] dt Delta time, in seconds. | |||||
*/ | |||||
template <class T, class S> | |||||
constexpr void solve_spring(T& x, T& v, const T& xt, S z, S w, S dt) noexcept | |||||
{ | |||||
const auto ww_dt = w * w * dt; | |||||
const auto ww_dtdt = ww_dt * dt; | |||||
const auto f = z * w * dt * S{2} + S{1}; | |||||
const auto inv_det = S{1} / (f + ww_dtdt); | |||||
x = (x * f + v * dt + xt * ww_dtdt) * inv_det; | |||||
v = (v + (xt - x) * ww_dt) * inv_det; | |||||
} | |||||
/** | |||||
* Numeric spring. | |||||
* | |||||
* @tparam T Value type. | |||||
* @tparam S Scalar type. | |||||
*/ | |||||
template <class T, class S> | |||||
class numeric_spring | |||||
{ | |||||
public: | |||||
/// Value type. | |||||
using value_type = T; | |||||
/// Scalar type. | |||||
using scalar_type = S; | |||||
/** | |||||
* Solves the spring using the implicit Euler method. | |||||
* | |||||
* @param dt Delta time, in seconds. | |||||
*/ | |||||
inline constexpr void solve(scalar_type dt) noexcept | |||||
{ | |||||
solve_spring(m_value, m_velocity, m_target_value, m_damping_ratio, m_angular_frequency, dt); | |||||
} | |||||
/** | |||||
* Sets the current value of the spring. | |||||
* | |||||
* @param value Current value. | |||||
*/ | |||||
inline constexpr void set_value(const value_type& value) noexcept | |||||
{ | |||||
m_value = value; | |||||
} | |||||
/** | |||||
* Sets the target value of the spring. | |||||
* | |||||
* @param value Target value. | |||||
*/ | |||||
inline constexpr void set_target_value(const value_type& value) noexcept | |||||
{ | |||||
m_target_value = value; | |||||
} | |||||
/** | |||||
* Sets the velocity of the spring. | |||||
* | |||||
* @param velocity Spring velocity. | |||||
*/ | |||||
inline constexpr void set_velocity(const value_type& velocity) noexcept | |||||
{ | |||||
m_velocity = velocity; | |||||
} | |||||
/** | |||||
* Sets the damping ratio of the spring. | |||||
* | |||||
* @param ratio Damping ratio, which can be undamped (`0`), underdamped (`< 1`), critically damped (`1`), or overdamped (`> 1`). | |||||
*/ | |||||
inline constexpr void set_damping_ratio(scalar_type ratio) noexcept | |||||
{ | |||||
m_damping_ratio = ratio; | |||||
} | |||||
/** | |||||
* Sets the angular frequency of the spring oscillation. | |||||
* | |||||
* @param w Angular frequency, in radians per second. | |||||
*/ | |||||
inline constexpr void set_angular_frequency(scalar_type angular_frequency) noexcept | |||||
{ | |||||
m_angular_frequency = angular_frequency; | |||||
} | |||||
/** | |||||
* Sets the oscillation period of the spring. | |||||
* | |||||
* @param period Oscillation period, in seconds. | |||||
*/ | |||||
inline constexpr void set_period(scalar_type period) noexcept | |||||
{ | |||||
m_angular_frequency = s_to_rads(period); | |||||
} | |||||
/** | |||||
* Sets the oscillation frequency of the spring. | |||||
* | |||||
* @param frequency Oscillation frequency, in hertz. | |||||
*/ | |||||
inline constexpr void set_frequency(scalar_type frequency) noexcept | |||||
{ | |||||
m_angular_frequency = hz_to_rads(frequency); | |||||
} | |||||
/// Returns the current value of the spring. | |||||
[[nodiscard]] inline constexpr const value_type& get_value() const noexcept | |||||
{ | |||||
return m_value; | |||||
} | |||||
/// Returns the target value of the spring. | |||||
[[nodiscard]] inline constexpr const value_type& get_target_value() const noexcept | |||||
{ | |||||
return m_target_value; | |||||
} | |||||
/// Returns the velocity of the spring. | |||||
[[nodiscard]] inline constexpr const value_type& get_velocity() const noexcept | |||||
{ | |||||
return m_velocity; | |||||
} | |||||
/// Returns the damping ratio of the spring. | |||||
[[nodiscard]] inline constexpr scalar_type get_damping_ratio() const noexcept | |||||
{ | |||||
return m_damping_ratio; | |||||
} | |||||
/// Returns the angular frequency of the spring oscillation, in radians per second. | |||||
[[nodiscard]] inline constexpr scalar_type get_angular_frequency() const noexcept | |||||
{ | |||||
return m_angular_frequency; | |||||
} | |||||
/// Returns the oscillation period of the spring, in seconds. | |||||
[[nodiscard]] inline constexpr scalar_type get_period() const noexcept | |||||
{ | |||||
return rads_to_s(m_angular_frequency); | |||||
} | |||||
/// Returns the oscillation frequency of the spring, in hertz. | |||||
[[nodiscard]] inline constexpr scalar_type get_frequency() const noexcept | |||||
{ | |||||
return rads_to_hz(m_angular_frequency); | |||||
} | |||||
private: | |||||
value_type m_value{}; | |||||
value_type m_target_value{}; | |||||
value_type m_velocity{}; | |||||
scalar_type m_damping_ratio{1}; | |||||
scalar_type m_angular_frequency{math::two_pi<scalar_type>}; | |||||
}; | |||||
} // namespace physics | |||||
#endif // ANTKEEPER_PHYSICS_SPRING_HPP |
@ -1,55 +0,0 @@ | |||||
/* | |||||
* Copyright (C) 2023 Christopher J. Howard | |||||
* | |||||
* This file is part of Antkeeper source code. | |||||
* | |||||
* Antkeeper source code is free software: you can redistribute it and/or modify | |||||
* it under the terms of the GNU General Public License as published by | |||||
* the Free Software Foundation, either version 3 of the License, or | |||||
* (at your option) any later version. | |||||
* | |||||
* Antkeeper source code is distributed in the hope that it will be useful, | |||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
* GNU General Public License for more details. | |||||
* | |||||
* You should have received a copy of the GNU General Public License | |||||
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
#include "game/ant/genes/ant-cocoon-gene.hpp" | |||||
#include "game/ant/genes/ant-gene-loader.hpp" | |||||
#include <engine/resources/resource-loader.hpp> | |||||
#include <engine/resources/resource-manager.hpp> | |||||
#include <engine/render/model.hpp> | |||||
namespace { | |||||
void load_ant_cocoon_phene(ant_cocoon_phene& phene, ::resource_manager& resource_manager, deserialize_context& ctx) | |||||
{ | |||||
std::uint8_t present{0}; | |||||
ctx.read8(reinterpret_cast<std::byte*>(&present), 1); | |||||
phene.present = static_cast<bool>(present); | |||||
std::uint8_t model_filename_length{0}; | |||||
ctx.read8(reinterpret_cast<std::byte*>(&model_filename_length), 1); | |||||
std::string model_filename(model_filename_length, '\0'); | |||||
ctx.read8(reinterpret_cast<std::byte*>(model_filename.data()), model_filename_length); | |||||
if (phene.present) | |||||
{ | |||||
phene.model = resource_manager.load<render::model>(model_filename); | |||||
} | |||||
} | |||||
} // namespace | |||||
template <> | |||||
std::unique_ptr<ant_cocoon_gene> resource_loader<ant_cocoon_gene>::load(::resource_manager& resource_manager, deserialize_context& ctx) | |||||
{ | |||||
std::unique_ptr<ant_cocoon_gene> gene = std::make_unique<ant_cocoon_gene>(); | |||||
load_ant_gene(*gene, resource_manager, ctx, &load_ant_cocoon_phene); | |||||
return gene; | |||||
} |
@ -0,0 +1,58 @@ | |||||
/* | |||||
* Copyright (C) 2023 Christopher J. Howard | |||||
* | |||||
* This file is part of Antkeeper source code. | |||||
* | |||||
* Antkeeper source code is free software: you can redistribute it and/or modify | |||||
* it under the terms of the GNU General Public License as published by | |||||
* the Free Software Foundation, either version 3 of the License, or | |||||
* (at your option) any later version. | |||||
* | |||||
* Antkeeper source code is distributed in the hope that it will be useful, | |||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
* GNU General Public License for more details. | |||||
* | |||||
* You should have received a copy of the GNU General Public License | |||||
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
#include "game/ant/genes/ant-pupa-gene.hpp" | |||||
#include "game/ant/genes/ant-gene-loader.hpp" | |||||
#include <engine/resources/resource-loader.hpp> | |||||
#include <engine/resources/resource-manager.hpp> | |||||
#include <engine/render/model.hpp> | |||||
namespace { | |||||
void load_ant_pupa_phene(ant_pupa_phene& phene, ::resource_manager& resource_manager, deserialize_context& ctx) | |||||
{ | |||||
ctx.read32<std::endian::little>(reinterpret_cast<std::byte*>(&phene.development_period), 1); | |||||
ctx.read32<std::endian::little>(reinterpret_cast<std::byte*>(&phene.eclosion_period), 1); | |||||
std::uint8_t cocoon_present{0}; | |||||
ctx.read8(reinterpret_cast<std::byte*>(&cocoon_present), 1); | |||||
phene.cocoon_present = static_cast<bool>(cocoon_present); | |||||
std::uint8_t cocoon_model_filename_length{0}; | |||||
ctx.read8(reinterpret_cast<std::byte*>(&cocoon_model_filename_length), 1); | |||||
std::string cocoon_model_filename(cocoon_model_filename_length, '\0'); | |||||
ctx.read8(reinterpret_cast<std::byte*>(cocoon_model_filename.data()), cocoon_model_filename_length); | |||||
if (phene.cocoon_present) | |||||
{ | |||||
phene.cocoon_model = resource_manager.load<render::model>(cocoon_model_filename); | |||||
} | |||||
} | |||||
} // namespace | |||||
template <> | |||||
std::unique_ptr<ant_pupa_gene> resource_loader<ant_pupa_gene>::load(::resource_manager& resource_manager, deserialize_context& ctx) | |||||
{ | |||||
std::unique_ptr<ant_pupa_gene> gene = std::make_unique<ant_pupa_gene>(); | |||||
load_ant_gene(*gene, resource_manager, ctx, &load_ant_pupa_phene); | |||||
return gene; | |||||
} |
@ -0,0 +1,60 @@ | |||||
/* | |||||
* Copyright (C) 2023 Christopher J. Howard | |||||
* | |||||
* This file is part of Antkeeper source code. | |||||
* | |||||
* Antkeeper source code is free software: you can redistribute it and/or modify | |||||
* it under the terms of the GNU General Public License as published by | |||||
* the Free Software Foundation, either version 3 of the License, or | |||||
* (at your option) any later version. | |||||
* | |||||
* Antkeeper source code is distributed in the hope that it will be useful, | |||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
* GNU General Public License for more details. | |||||
* | |||||
* You should have received a copy of the GNU General Public License | |||||
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
#ifndef ANTKEEPER_GAME_AUTOFOCUS_COMPONENT_HPP | |||||
#define ANTKEEPER_GAME_AUTOFOCUS_COMPONENT_HPP | |||||
#include <engine/math/vector.hpp> | |||||
#include <engine/math/angles.hpp> | |||||
#include <engine/physics/spring.hpp> | |||||
/** | |||||
* Modulates a camera's field of view and spring arm length. | |||||
*/ | |||||
struct autofocus_component | |||||
{ | |||||
/// Height of the focal plane at maximum zoom. | |||||
double near_focal_plane_height{1.0}; | |||||
/// Height of the focal plane at minimum zoom. | |||||
double far_focal_plane_height{50.0}; | |||||
/// Horizontal FoV at maximum zoom. | |||||
double near_hfov{math::radians(90.0)}; | |||||
/// Horizontal FoV at minimum zoom. | |||||
double far_hfov{math::radians(45.0)}; | |||||
/// Zoom factor, on `[0, 1]`. | |||||
double zoom{}; | |||||
/// Dimensions of the focal plane. | |||||
math::dvec2 focal_plane_size; | |||||
/// Horizontal FoV of the camera, in radians. | |||||
double hfov{}; | |||||
/// Vertical FoV of the camera, in radians. | |||||
double vfov{}; | |||||
/// Position of the focal point, relative to the subject. | |||||
math::dvec3 focal_point_offset{}; | |||||
}; | |||||
#endif // ANTKEEPER_GAME_AUTOFOCUS_COMPONENT_HPP |
@ -0,0 +1,50 @@ | |||||
/* | |||||
* Copyright (C) 2023 Christopher J. Howard | |||||
* | |||||
* This file is part of Antkeeper source code. | |||||
* | |||||
* Antkeeper source code is free software: you can redistribute it and/or modify | |||||
* it under the terms of the GNU General Public License as published by | |||||
* the Free Software Foundation, either version 3 of the License, or | |||||
* (at your option) any later version. | |||||
* | |||||
* Antkeeper source code is distributed in the hope that it will be useful, | |||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
* GNU General Public License for more details. | |||||
* | |||||
* You should have received a copy of the GNU General Public License | |||||
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
#ifndef ANTKEEPER_GAME_LARVA_COMPONENT_HPP | |||||
#define ANTKEEPER_GAME_LARVA_COMPONENT_HPP | |||||
#include <engine/entity/id.hpp> | |||||
#include <engine/render/material-variable.hpp> | |||||
/** | |||||
* Larval development parameters. | |||||
*/ | |||||
struct larva_component | |||||
{ | |||||
/// Duration of the development period, in days. | |||||
float development_period{}; | |||||
/// Current development phase, on `[0, 1]`. | |||||
float development_phase{}; | |||||
/// Duration of the cocoon-spinning period, in days. | |||||
float spinning_period{}; | |||||
/// Current phase of the cocoon-spinning, on `[0, 1]`. | |||||
float spinning_phase{}; | |||||
/// ID of the cocoon entity. | |||||
entity::id cocoon_eid{entt::null}; | |||||
/// Material variable associated with the cocoon-spinning phase. | |||||
std::shared_ptr<render::matvar_float> spinning_phase_matvar; | |||||
}; | |||||
#endif // ANTKEEPER_GAME_LARVA_COMPONENT_HPP |
@ -0,0 +1,32 @@ | |||||
/* | |||||
* Copyright (C) 2023 Christopher J. Howard | |||||
* | |||||
* This file is part of Antkeeper source code. | |||||
* | |||||
* Antkeeper source code is free software: you can redistribute it and/or modify | |||||
* it under the terms of the GNU General Public License as published by | |||||
* the Free Software Foundation, either version 3 of the License, or | |||||
* (at your option) any later version. | |||||
* | |||||
* Antkeeper source code is distributed in the hope that it will be useful, | |||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
* GNU General Public License for more details. | |||||
* | |||||
* You should have received a copy of the GNU General Public License | |||||
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
#ifndef ANTKEEPER_GAME_METABOLISM_COMPONENT_HPP | |||||
#define ANTKEEPER_GAME_METABOLISM_COMPONENT_HPP | |||||
/** | |||||
* | |||||
*/ | |||||
struct metabolism_component | |||||
{ | |||||
/// Growth rate multiplier. | |||||
float growth_multiplier{1}; | |||||
}; | |||||
#endif // ANTKEEPER_GAME_METABOLISM_COMPONENT_HPP |
@ -0,0 +1,44 @@ | |||||
/* | |||||
* Copyright (C) 2023 Christopher J. Howard | |||||
* | |||||
* This file is part of Antkeeper source code. | |||||
* | |||||
* Antkeeper source code is free software: you can redistribute it and/or modify | |||||
* it under the terms of the GNU General Public License as published by | |||||
* the Free Software Foundation, either version 3 of the License, or | |||||
* (at your option) any later version. | |||||
* | |||||
* Antkeeper source code is distributed in the hope that it will be useful, | |||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
* GNU General Public License for more details. | |||||
* | |||||
* You should have received a copy of the GNU General Public License | |||||
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
#ifndef ANTKEEPER_GAME_PUPA_COMPONENT_HPP | |||||
#define ANTKEEPER_GAME_PUPA_COMPONENT_HPP | |||||
#include <engine/entity/id.hpp> | |||||
#include <engine/render/material-variable.hpp> | |||||
/** | |||||
* Pupal development parameters. | |||||
*/ | |||||
struct pupa_component | |||||
{ | |||||
/// Duration of the development period, in days. | |||||
float development_period{}; | |||||
/// Current development phase, on `[0, 1]`. | |||||
float development_phase{}; | |||||
/// ID of the cocoon entity. | |||||
entity::id cocoon_eid{entt::null}; | |||||
/// Material variable associated with the cocoon decay phase. | |||||
std::shared_ptr<render::matvar_float> decay_phase_matvar; | |||||
}; | |||||
#endif // ANTKEEPER_GAME_PUPA_COMPONENT_HPP |
@ -1,77 +0,0 @@ | |||||
/* | |||||
* Copyright (C) 2023 Christopher J. Howard | |||||
* | |||||
* This file is part of Antkeeper source code. | |||||
* | |||||
* Antkeeper source code is free software: you can redistribute it and/or modify | |||||
* it under the terms of the GNU General Public License as published by | |||||
* the Free Software Foundation, either version 3 of the License, or | |||||
* (at your option) any later version. | |||||
* | |||||
* Antkeeper source code is distributed in the hope that it will be useful, | |||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
* GNU General Public License for more details. | |||||
* | |||||
* You should have received a copy of the GNU General Public License | |||||
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
#ifndef ANTKEEPER_GAME_SPRING_COMPONENT_HPP | |||||
#define ANTKEEPER_GAME_SPRING_COMPONENT_HPP | |||||
#include <engine/animation/spring.hpp> | |||||
#include <engine/math/vector.hpp> | |||||
#include <functional> | |||||
/** | |||||
* Numeric spring with one component. | |||||
*/ | |||||
struct spring1_component | |||||
{ | |||||
/// Numeric spring with one component. | |||||
numeric_spring<float, float> spring; | |||||
/// Spring solved callback. | |||||
std::function<void(float)> callback; | |||||
}; | |||||
/** | |||||
* Numeric spring with two components. | |||||
*/ | |||||
struct spring2_component | |||||
{ | |||||
/// Numeric spring with two components. | |||||
numeric_spring<math::fvec2, float> spring; | |||||
/// Spring solved callback. | |||||
std::function<void(const math::fvec2&)> callback; | |||||
}; | |||||
/** | |||||
* Numeric spring with three components. | |||||
*/ | |||||
struct spring3_component | |||||
{ | |||||
/// Numeric spring with three components. | |||||
numeric_spring<math::fvec3, float> spring; | |||||
/// Spring solved callback. | |||||
std::function<void(const math::fvec3&)> callback; | |||||
}; | |||||
/** | |||||
* Numeric spring with four components. | |||||
*/ | |||||
struct spring4_component | |||||
{ | |||||
/// Numeric spring with four components. | |||||
numeric_spring<math::fvec4, float> spring; | |||||
/// Spring solved callback. | |||||
std::function<void(const math::fvec4&)> callback; | |||||
}; | |||||
#endif // ANTKEEPER_GAME_SPRING_COMPONENT_HPP |
@ -0,0 +1,49 @@ | |||||
/* | |||||
* Copyright (C) 2023 Christopher J. Howard | |||||
* | |||||
* This file is part of Antkeeper source code. | |||||
* | |||||
* Antkeeper source code is free software: you can redistribute it and/or modify | |||||
* it under the terms of the GNU General Public License as published by | |||||
* the Free Software Foundation, either version 3 of the License, or | |||||
* (at your option) any later version. | |||||
* | |||||
* Antkeeper source code is distributed in the hope that it will be useful, | |||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
* GNU General Public License for more details. | |||||
* | |||||
* You should have received a copy of the GNU General Public License | |||||
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
#include "game/systems/metabolic-system.hpp" | |||||
#include "game/components/isometric-growth-component.hpp" | |||||
#include "game/components/rigid-body-component.hpp" | |||||
#include <execution> | |||||
metabolic_system::metabolic_system(entity::registry& registry): | |||||
updatable_system(registry) | |||||
{} | |||||
void metabolic_system::update(float t, float dt) | |||||
{ | |||||
// Scale timestep | |||||
const auto scaled_timestep = dt * m_time_scale; | |||||
// Handle isometric growth | |||||
auto isometric_growth_group = registry.group<isometric_growth_component>(entt::get<rigid_body_component>); | |||||
std::for_each | |||||
( | |||||
std::execution::seq, | |||||
isometric_growth_group.begin(), | |||||
isometric_growth_group.end(), | |||||
[&](auto entity_id) | |||||
{ | |||||
auto& growth = isometric_growth_group.get<isometric_growth_component>(entity_id); | |||||
auto& rigid_body = *isometric_growth_group.get<rigid_body_component>(entity_id).body; | |||||
rigid_body.set_scale(rigid_body.get_scale() + growth.rate * scaled_timestep); | |||||
} | |||||
); | |||||
} |
@ -0,0 +1,49 @@ | |||||
/* | |||||
* Copyright (C) 2023 Christopher J. Howard | |||||
* | |||||
* This file is part of Antkeeper source code. | |||||
* | |||||
* Antkeeper source code is free software: you can redistribute it and/or modify | |||||
* it under the terms of the GNU General Public License as published by | |||||
* the Free Software Foundation, either version 3 of the License, or | |||||
* (at your option) any later version. | |||||
* | |||||
* Antkeeper source code is distributed in the hope that it will be useful, | |||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
* GNU General Public License for more details. | |||||
* | |||||
* You should have received a copy of the GNU General Public License | |||||
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
#ifndef ANTKEEPER_GAME_METABOLIC_SYSTEM_HPP | |||||
#define ANTKEEPER_GAME_METABOLIC_SYSTEM_HPP | |||||
#include "game/systems/updatable-system.hpp" | |||||
/** | |||||
* | |||||
*/ | |||||
class metabolic_system: | |||||
public updatable_system | |||||
{ | |||||
public: | |||||
explicit metabolic_system(entity::registry& registry); | |||||
virtual void update(float t, float dt); | |||||
/** | |||||
* Sets the factor by which the timestep `dt` will be scaled. | |||||
* | |||||
* @param scale Factor by which to scale the timestep. | |||||
*/ | |||||
inline constexpr void set_time_scale(float scale) noexcept | |||||
{ | |||||
m_time_scale = scale; | |||||
} | |||||
private: | |||||
float m_time_scale{1.0f}; | |||||
}; | |||||
#endif // ANTKEEPER_GAME_METABOLIC_SYSTEM_HPP |
@ -1,74 +0,0 @@ | |||||
/* | |||||
* Copyright (C) 2023 Christopher J. Howard | |||||
* | |||||
* This file is part of Antkeeper source code. | |||||
* | |||||
* Antkeeper source code is free software: you can redistribute it and/or modify | |||||
* it under the terms of the GNU General Public License as published by | |||||
* the Free Software Foundation, either version 3 of the License, or | |||||
* (at your option) any later version. | |||||
* | |||||
* Antkeeper source code is distributed in the hope that it will be useful, | |||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
* GNU General Public License for more details. | |||||
* | |||||
* You should have received a copy of the GNU General Public License | |||||
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
#include "game/systems/spring-system.hpp" | |||||
#include "game/components/spring-component.hpp" | |||||
#include <engine/entity/id.hpp> | |||||
spring_system::spring_system(entity::registry& registry): | |||||
updatable_system(registry) | |||||
{} | |||||
spring_system::~spring_system() | |||||
{} | |||||
void spring_system::update(float t, float dt) | |||||
{ | |||||
registry.view<spring1_component>().each | |||||
( | |||||
[&](entity::id spring_eid, auto& component) | |||||
{ | |||||
solve_numeric_spring<float, float>(component.spring, dt); | |||||
if (component.callback) | |||||
component.callback(component.spring.x0); | |||||
} | |||||
); | |||||
registry.view<spring2_component>().each | |||||
( | |||||
[&](entity::id spring_eid, auto& component) | |||||
{ | |||||
solve_numeric_spring<math::fvec2, float>(component.spring, dt); | |||||
if (component.callback) | |||||
component.callback(component.spring.x0); | |||||
} | |||||
); | |||||
registry.view<spring3_component>().each | |||||
( | |||||
[&](entity::id spring_eid, auto& component) | |||||
{ | |||||
solve_numeric_spring<math::fvec3, float>(component.spring, dt); | |||||
if (component.callback) | |||||
component.callback(component.spring.x0); | |||||
} | |||||
); | |||||
registry.view<spring4_component>().each | |||||
( | |||||
[&](entity::id spring_eid, auto& component) | |||||
{ | |||||
solve_numeric_spring<math::fvec4, float>(component.spring, dt); | |||||
if (component.callback) | |||||
component.callback(component.spring.x0); | |||||
} | |||||
); | |||||
} | |||||
@ -0,0 +1,101 @@ | |||||
/* | |||||
* Copyright (C) 2023 Christopher J. Howard | |||||
* | |||||
* This file is part of Antkeeper source code. | |||||
* | |||||
* Antkeeper source code is free software: you can redistribute it and/or modify | |||||
* it under the terms of the GNU General Public License as published by | |||||
* the Free Software Foundation, either version 3 of the License, or | |||||
* (at your option) any later version. | |||||
* | |||||
* Antkeeper source code is distributed in the hope that it will be useful, | |||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
* GNU General Public License for more details. | |||||
* | |||||
* You should have received a copy of the GNU General Public License | |||||
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
#include "game/textures/cocoon-silk-sdf.hpp" | |||||
#include <engine/math/noise/noise.hpp> | |||||
#include <engine/utility/image.hpp> | |||||
#include <engine/debug/log.hpp> | |||||
#include <algorithm> | |||||
#include <execution> | |||||
#include <fstream> | |||||
#include <stb/stb_image_write.h> | |||||
void generate_cocoon_silk_sdf(std::filesystem::path path) | |||||
{ | |||||
debug::log::info("Generating cocoon silk SDF image..."); | |||||
image img; | |||||
img.format(1, 4); | |||||
img.resize(2048, 2048); | |||||
auto width = img.width(); | |||||
auto height = img.height(); | |||||
unsigned char* pixels = (unsigned char*)img.data(); | |||||
const float frequency = 100.0f; | |||||
float scale_x = 1.0f / static_cast<float>(width - 1) * frequency; | |||||
float scale_y = 1.0f / static_cast<float>(height - 1) * frequency; | |||||
std::for_each | |||||
( | |||||
std::execution::par_unseq, | |||||
img.begin<math::vec4<unsigned char>>(), | |||||
img.end<math::vec4<unsigned char>>(), | |||||
[pixels, width, height, scale_x, scale_y, frequency](auto& pixel) | |||||
{ | |||||
const std::size_t i = &pixel - (math::vec4<unsigned char>*)pixels; | |||||
const std::size_t y = i / width; | |||||
const std::size_t x = i % width; | |||||
const math::fvec2 position = | |||||
{ | |||||
static_cast<float>(x) * scale_x, | |||||
static_cast<float>(y) * scale_y | |||||
}; | |||||
const auto | |||||
[ | |||||
f1_sqr_distance, | |||||
f1_displacement, | |||||
f1_id, | |||||
f1_edge_sqr_distance | |||||
] = math::noise::voronoi::f1_edge<float, 2>(position, 1.0f, {frequency, frequency}); | |||||
const float f1_edge_distance = std::sqrt(f1_edge_sqr_distance); | |||||
const float scale = 255.0f * (255.0f / 204.0f); | |||||
pixel = | |||||
{ | |||||
static_cast<unsigned char>(std::min(255.0f, f1_edge_distance * scale)), | |||||
static_cast<unsigned char>(std::min(255.0f, f1_edge_distance * scale)), | |||||
static_cast<unsigned char>(std::min(255.0f, f1_edge_distance * scale)), | |||||
255 | |||||
}; | |||||
// const float f1_distance = std::sqrt(f1_sqr_distance); | |||||
// const math::fvec2 uv = (position + f1_displacement) / frequency; | |||||
// pixel = | |||||
// { | |||||
// static_cast<unsigned char>(std::min(255.0f, f1_distance * 255.0f)), | |||||
// static_cast<unsigned char>(std::min(255.0f, uv[0] * 255.0f)), | |||||
// static_cast<unsigned char>(std::min(255.0f, uv[1] * 255.0f)), | |||||
// static_cast<unsigned char>(f1_id % 256) | |||||
// }; | |||||
} | |||||
); | |||||
debug::log::info("Generated cocoon silk SDF image"); | |||||
debug::log::info("Saving cocoon silk SDF image to \"{}\"...", path.string()); | |||||
stbi_flip_vertically_on_write(1); | |||||
stbi_write_png(path.string().c_str(), img.width(), img.height(), img.channel_count(), img.data(), img.width() * img.channel_count()); | |||||
debug::log::info("Saved cocoon silk SDF image to \"{}\"", path.string()); | |||||
} |
@ -0,0 +1,28 @@ | |||||
/* | |||||
* Copyright (C) 2023 Christopher J. Howard | |||||
* | |||||
* This file is part of Antkeeper source code. | |||||
* | |||||
* Antkeeper source code is free software: you can redistribute it and/or modify | |||||
* it under the terms of the GNU General Public License as published by | |||||
* the Free Software Foundation, either version 3 of the License, or | |||||
* (at your option) any later version. | |||||
* | |||||
* Antkeeper source code is distributed in the hope that it will be useful, | |||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
* GNU General Public License for more details. | |||||
* | |||||
* You should have received a copy of the GNU General Public License | |||||
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
#ifndef ANTKEEPER_COCOON_SILK_SDF_HPP | |||||
#define ANTKEEPER_COCOON_SILK_SDF_HPP | |||||
#include <filesystem> | |||||
/// Generates the cocoon silk signed distance field texture. | |||||
void generate_silk_sdf_image(std::filesystem::path path); | |||||
#endif // ANTKEEPER_COCOON_SILK_SDF_HPP |
@ -0,0 +1,94 @@ | |||||
/* | |||||
* Copyright (C) 2023 Christopher J. Howard | |||||
* | |||||
* This file is part of Antkeeper source code. | |||||
* | |||||
* Antkeeper source code is free software: you can redistribute it and/or modify | |||||
* it under the terms of the GNU General Public License as published by | |||||
* the Free Software Foundation, either version 3 of the License, or | |||||
* (at your option) any later version. | |||||
* | |||||
* Antkeeper source code is distributed in the hope that it will be useful, | |||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
* GNU General Public License for more details. | |||||
* | |||||
* You should have received a copy of the GNU General Public License | |||||
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
#include "game/textures/rgb-voronoi-noise.hpp" | |||||
#include <engine/math/noise/noise.hpp> | |||||
#include <engine/utility/image.hpp> | |||||
#include <engine/debug/log.hpp> | |||||
#include <algorithm> | |||||
#include <execution> | |||||
#include <fstream> | |||||
#include <stb/stb_image_write.h> | |||||
void generate_rgb_voronoi_noise(std::filesystem::path path) | |||||
{ | |||||
image img; | |||||
img.format(1, 4); | |||||
img.resize(1024, 1024); | |||||
auto width = img.width(); | |||||
auto height = img.height(); | |||||
unsigned char* pixels = (unsigned char*)img.data(); | |||||
const float frequency = 512.0f; | |||||
float scale_x = 1.0f / static_cast<float>(width - 1) * frequency; | |||||
float scale_y = 1.0f / static_cast<float>(height - 1) * frequency; | |||||
std::for_each | |||||
( | |||||
std::execution::par_unseq, | |||||
img.begin<math::vec4<unsigned char>>(), | |||||
img.end<math::vec4<unsigned char>>(), | |||||
[pixels, width, height, scale_x, scale_y, frequency](auto& pixel) | |||||
{ | |||||
const std::size_t i = &pixel - (math::vec4<unsigned char>*)pixels; | |||||
const std::size_t y = i / width; | |||||
const std::size_t x = i % width; | |||||
const math::fvec2 position = | |||||
{ | |||||
static_cast<float>(x) * scale_x, | |||||
static_cast<float>(y) * scale_y | |||||
}; | |||||
const auto | |||||
[ | |||||
f1_sqr_distance, | |||||
f1_displacement, | |||||
f1_id, | |||||
f1_edge_sqr_distance | |||||
] = math::noise::voronoi::f1_edge<float, 2>(position, 1.0f, {frequency, frequency}); | |||||
const float f1_edge_distance = std::sqrt(f1_edge_sqr_distance); | |||||
const float scale = 255.0f * (255.0f / 204.0f); | |||||
pixel = | |||||
{ | |||||
static_cast<unsigned char>(f1_id & 255), | |||||
static_cast<unsigned char>((f1_id >> 8) & 255), | |||||
static_cast<unsigned char>((f1_id >> 16) & 255), | |||||
static_cast<unsigned char>((f1_id >> 24) & 255) | |||||
}; | |||||
// const float f1_distance = std::sqrt(f1_sqr_distance); | |||||
// const math::fvec2 uv = (position + f1_displacement) / frequency; | |||||
// pixel = | |||||
// { | |||||
// static_cast<unsigned char>(std::min(255.0f, f1_distance * 255.0f)), | |||||
// static_cast<unsigned char>(std::min(255.0f, uv[0] * 255.0f)), | |||||
// static_cast<unsigned char>(std::min(255.0f, uv[1] * 255.0f)), | |||||
// static_cast<unsigned char>(f1_id % 256) | |||||
// }; | |||||
} | |||||
); | |||||
stbi_flip_vertically_on_write(1); | |||||
stbi_write_png(path.string().c_str(), img.width(), img.height(), img.channel_count(), img.data(), img.width() * img.channel_count()); | |||||
} |
@ -0,0 +1,27 @@ | |||||
/* | |||||
* Copyright (C) 2023 Christopher J. Howard | |||||
* | |||||
* This file is part of Antkeeper source code. | |||||
* | |||||
* Antkeeper source code is free software: you can redistribute it and/or modify | |||||
* it under the terms of the GNU General Public License as published by | |||||
* the Free Software Foundation, either version 3 of the License, or | |||||
* (at your option) any later version. | |||||
* | |||||
* Antkeeper source code is distributed in the hope that it will be useful, | |||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
* GNU General Public License for more details. | |||||
* | |||||
* You should have received a copy of the GNU General Public License | |||||
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
#ifndef ANTKEEPER_RGB_VORONOI_NOISE_HPP | |||||
#define ANTKEEPER_RGB_VORONOI_NOISE_HPP | |||||
#include <filesystem> | |||||
void generate_rgb_voronoi_noise(std::filesystem::path path); | |||||
#endif // ANTKEEPER_RGB_VORONOI_NOISE_HPP |