2019-12-22 20:04:07 +00:00
|
|
|
#include "Runtime/CStaticInterference.hpp"
|
|
|
|
|
|
|
|
#include <zeus/Math.hpp>
|
2017-06-12 04:23:34 +00:00
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
namespace metaforce {
|
2017-06-12 04:23:34 +00:00
|
|
|
|
2020-05-27 16:40:35 +00:00
|
|
|
CStaticInterference::CStaticInterference(size_t sourceCount) { x0_sources.reserve(sourceCount); }
|
2017-06-12 04:23:34 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CStaticInterference::RemoveSource(TUniqueId id) {
|
2020-05-27 16:40:35 +00:00
|
|
|
const auto iter =
|
2020-05-27 16:45:17 +00:00
|
|
|
std::find_if(x0_sources.cbegin(), x0_sources.cend(), [id](const auto& src) { return src.x0_id == id; });
|
2020-05-20 02:17:20 +00:00
|
|
|
|
2020-05-27 16:40:35 +00:00
|
|
|
if (iter == x0_sources.cend()) {
|
2020-05-20 02:17:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-27 16:40:35 +00:00
|
|
|
x0_sources.erase(iter);
|
2017-06-12 04:23:34 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CStaticInterference::Update(CStateManager&, float dt) {
|
|
|
|
std::vector<CStaticInterferenceSource> newSources;
|
2020-05-27 16:40:35 +00:00
|
|
|
newSources.reserve(x0_sources.size());
|
|
|
|
for (CStaticInterferenceSource& src : x0_sources) {
|
|
|
|
if (src.x8_timeLeft >= 0.f) {
|
|
|
|
src.x8_timeLeft -= dt;
|
2018-12-08 05:30:43 +00:00
|
|
|
newSources.push_back(src);
|
2017-06-12 04:23:34 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2020-05-27 16:40:35 +00:00
|
|
|
x0_sources = std::move(newSources);
|
2017-06-12 04:23:34 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
float CStaticInterference::GetTotalInterference() const {
|
|
|
|
float validAccum = 0.f;
|
|
|
|
float invalidAccum = 0.f;
|
2020-05-27 16:40:35 +00:00
|
|
|
for (const CStaticInterferenceSource& src : x0_sources) {
|
|
|
|
if (src.x0_id == kInvalidUniqueId)
|
|
|
|
invalidAccum += src.x4_magnitude;
|
2018-12-08 05:30:43 +00:00
|
|
|
else
|
2020-05-27 16:40:35 +00:00
|
|
|
validAccum += src.x4_magnitude;
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
|
|
|
if (validAccum > 0.80000001f)
|
|
|
|
validAccum = 0.80000001f;
|
|
|
|
validAccum += invalidAccum;
|
|
|
|
if (validAccum > 1.f)
|
|
|
|
return 1.f;
|
|
|
|
return validAccum;
|
2017-06-12 04:23:34 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CStaticInterference::AddSource(TUniqueId id, float magnitude, float duration) {
|
|
|
|
magnitude = zeus::clamp(0.f, magnitude, 1.f);
|
2020-05-27 16:40:35 +00:00
|
|
|
const auto search = std::find_if(x0_sources.begin(), x0_sources.end(),
|
|
|
|
[id](const CStaticInterferenceSource& source) { return source.x0_id == id; });
|
|
|
|
if (search != x0_sources.cend()) {
|
|
|
|
search->x4_magnitude = magnitude;
|
|
|
|
search->x8_timeLeft = duration;
|
2018-12-08 05:30:43 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-05-20 02:17:20 +00:00
|
|
|
|
2020-05-27 16:40:35 +00:00
|
|
|
if (x0_sources.size() < x0_sources.capacity()) {
|
|
|
|
x0_sources.push_back({id, magnitude, duration});
|
2020-05-20 02:17:20 +00:00
|
|
|
}
|
2017-06-12 04:23:34 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
} // namespace metaforce
|