99 lines
2.8 KiB
C++
99 lines
2.8 KiB
C++
|
#include "KSettings.h"
|
||
|
|
||
|
#include <algorithm>
|
||
|
#include <cmath>
|
||
|
#include "Constants.h"
|
||
|
|
||
|
namespace KapitanGame
|
||
|
{
|
||
|
KSettings::KSettings(const float maxJumpHeight, const float horizontalDistanceToMaxJumpHeight) : MaxJumpHeightValue(maxJumpHeight),
|
||
|
HorizontalDistanceToMaxJumpHeightValue(horizontalDistanceToMaxJumpHeight),
|
||
|
TimeToMaxHeightValue(NAN),
|
||
|
GravityValue(NAN),
|
||
|
JumpInitialVelocityValue(NAN),
|
||
|
ShortJumpVelocityValue(NAN),
|
||
|
CollisionEnabledValue(true),
|
||
|
Dirty(true),
|
||
|
MaxJumpHeight(this, &KSettings::GetMaxJumpHeight, &KSettings::SetMaxJumpHeight),
|
||
|
HorizontalDistanceToMaxJumpHeight(this, &KSettings::GetHorizontalDistanceToMaxJumpHeight, &KSettings::SetHorizontalDistanceToMaxJumpHeight),
|
||
|
TimeToMaxHeight(this, &KSettings::GetTimeToMaxHeight),
|
||
|
Gravity(this, &KSettings::GetGravity),
|
||
|
JumpInitialVelocity(this, &KSettings::GetJumpInitialVelocity),
|
||
|
ShortJumpVelocity(this, &KSettings::GetShortJumpVelocity),
|
||
|
CollisionEnabled(this, &KSettings::GetCollisionEnabled, &KSettings::SetCollisionEnabled)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void KSettings::SetCollisionEnabled(const bool value)
|
||
|
{
|
||
|
if (value != CollisionEnabledValue)
|
||
|
CollisionEnabledValue = value;
|
||
|
}
|
||
|
|
||
|
void KSettings::SetMaxJumpHeight(const float h)
|
||
|
{
|
||
|
if (h <= 0.f && MaxJumpHeightValue <= 0.f) return;
|
||
|
if (MaxJumpHeightValue != h) { // NOLINT(clang-diagnostic-float-equal)
|
||
|
MaxJumpHeightValue = std::max(h, 0.f);
|
||
|
Dirty = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void KSettings::SetHorizontalDistanceToMaxJumpHeight(const float xn)
|
||
|
{
|
||
|
if (xn <= 0.f && HorizontalDistanceToMaxJumpHeightValue <= 0.f) return;
|
||
|
if (HorizontalDistanceToMaxJumpHeightValue != xn) { // NOLINT(clang-diagnostic-float-equal)
|
||
|
HorizontalDistanceToMaxJumpHeightValue = std::max(xn, 0.f);
|
||
|
Dirty = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// ReSharper disable once CppMemberFunctionMayBeConst
|
||
|
bool KSettings::GetCollisionEnabled()
|
||
|
{
|
||
|
return CollisionEnabledValue;
|
||
|
}
|
||
|
|
||
|
float KSettings::GetTimeToMaxHeight()
|
||
|
{
|
||
|
if (Dirty)
|
||
|
TimeToMaxHeightValue = HorizontalDistanceToMaxJumpHeightValue / Constants::SPEED;
|
||
|
return TimeToMaxHeightValue;
|
||
|
}
|
||
|
|
||
|
float KSettings::GetGravity()
|
||
|
{
|
||
|
if (Dirty) {
|
||
|
const auto timeToMaxHeight = GetTimeToMaxHeight();
|
||
|
GravityValue = 2.0f * MaxJumpHeightValue / (timeToMaxHeight * timeToMaxHeight);
|
||
|
}
|
||
|
return GravityValue;
|
||
|
}
|
||
|
|
||
|
float KSettings::GetJumpInitialVelocity()
|
||
|
{
|
||
|
if (Dirty)
|
||
|
JumpInitialVelocityValue = 2.0f * MaxJumpHeightValue / GetTimeToMaxHeight();
|
||
|
return JumpInitialVelocityValue;
|
||
|
}
|
||
|
|
||
|
float KSettings::GetShortJumpVelocity()
|
||
|
{
|
||
|
if (Dirty)
|
||
|
ShortJumpVelocityValue = GetJumpInitialVelocity() / 2.f;
|
||
|
return ShortJumpVelocityValue;
|
||
|
}
|
||
|
|
||
|
// ReSharper disable once CppMemberFunctionMayBeConst
|
||
|
float KSettings::GetMaxJumpHeight()
|
||
|
{
|
||
|
return MaxJumpHeightValue;
|
||
|
}
|
||
|
|
||
|
// ReSharper disable once CppMemberFunctionMayBeConst
|
||
|
float KSettings::GetHorizontalDistanceToMaxJumpHeight()
|
||
|
{
|
||
|
return HorizontalDistanceToMaxJumpHeightValue;
|
||
|
}
|
||
|
}
|