metaforce/Runtime/CStaticInterference.cpp

65 lines
1.8 KiB
C++
Raw Normal View History

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