77 lines
1.9 KiB
C++
77 lines
1.9 KiB
C++
|
|
#include "Components/CapsuleColliderComponent.h"
|
||
|
|
|
||
|
|
#include <algorithm>
|
||
|
|
#include <cmath>
|
||
|
|
#include <sstream>
|
||
|
|
|
||
|
|
namespace XCEngine {
|
||
|
|
namespace Components {
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
|
||
|
|
float SanitizePositive(float value, float fallback) {
|
||
|
|
return std::isfinite(value) && value > 0.0f ? value : fallback;
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace
|
||
|
|
|
||
|
|
void CapsuleColliderComponent::SetRadius(float value) {
|
||
|
|
m_radius = SanitizePositive(value, 0.5f);
|
||
|
|
m_height = std::max(m_height, m_radius * 2.0f);
|
||
|
|
}
|
||
|
|
|
||
|
|
void CapsuleColliderComponent::SetHeight(float value) {
|
||
|
|
m_height = std::max(SanitizePositive(value, 2.0f), m_radius * 2.0f);
|
||
|
|
}
|
||
|
|
|
||
|
|
void CapsuleColliderComponent::SetAxis(ColliderAxis value) {
|
||
|
|
switch (value) {
|
||
|
|
case ColliderAxis::X:
|
||
|
|
case ColliderAxis::Y:
|
||
|
|
case ColliderAxis::Z:
|
||
|
|
m_axis = value;
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
m_axis = ColliderAxis::Y;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void CapsuleColliderComponent::Serialize(std::ostream& os) const {
|
||
|
|
SerializeBase(os);
|
||
|
|
os << "radius=" << m_radius << ";";
|
||
|
|
os << "height=" << m_height << ";";
|
||
|
|
os << "axis=" << static_cast<int>(m_axis) << ";";
|
||
|
|
}
|
||
|
|
|
||
|
|
void CapsuleColliderComponent::Deserialize(std::istream& is) {
|
||
|
|
std::string token;
|
||
|
|
while (std::getline(is, token, ';')) {
|
||
|
|
if (token.empty()) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
const size_t eqPos = token.find('=');
|
||
|
|
if (eqPos == std::string::npos) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
const std::string key = token.substr(0, eqPos);
|
||
|
|
const std::string value = token.substr(eqPos + 1);
|
||
|
|
if (DeserializeBaseField(key, value)) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (key == "radius") {
|
||
|
|
SetRadius(std::stof(value));
|
||
|
|
} else if (key == "height") {
|
||
|
|
SetHeight(std::stof(value));
|
||
|
|
} else if (key == "axis") {
|
||
|
|
SetAxis(static_cast<ColliderAxis>(std::stoi(value)));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace Components
|
||
|
|
} // namespace XCEngine
|