summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVittorio Romeo <vittorio.romeo@outlook.com>2021-11-12 03:45:22 +0000
committerVittorio Romeo <vittorio.romeo@outlook.com>2021-11-12 03:45:22 +0000
commit4f23ecab39ae856068d7904409121fd6c69c159b (patch)
tree820cd615c0955e5a30ba1956d34d3dec9174a5b4
parent8ecb284245c5519fe75a01b4754cce34515b24da (diff)
Refactoring
-rw-r--r--include/SSVOpenHexagon/Utils/MoveTowards.hpp57
-rw-r--r--src/SSVOpenHexagon/Components/CPlayer.cpp55
-rw-r--r--src/SSVOpenHexagon/Core/HGUpdate.cpp23
3 files changed, 82 insertions, 53 deletions
diff --git a/include/SSVOpenHexagon/Utils/MoveTowards.hpp b/include/SSVOpenHexagon/Utils/MoveTowards.hpp
new file mode 100644
index 00000000..55c4525a
--- /dev/null
+++ b/include/SSVOpenHexagon/Utils/MoveTowards.hpp
@@ -0,0 +1,57 @@
+// Copyright (c) 2013-2020 Vittorio Romeo
+// License: Academic Free License ("AFL") v. 3.0
+// AFL License page: https://opensource.org/licenses/AFL-3.0
+
+#pragma once
+
+#include "SSVOpenHexagon/Global/Assert.hpp"
+
+#include <algorithm>
+#include <cmath>
+
+namespace hg::Utils {
+
+[[nodiscard, gnu::always_inline, gnu::const]] inline constexpr float
+getMoveTowards(float value, const float target, const float step) noexcept
+{
+ SSVOH_ASSERT(step >= 0);
+
+ if(value < target)
+ {
+ value += step;
+ if(value > target)
+ {
+ value = target;
+ }
+ }
+ else if(value > target)
+ {
+ value -= step;
+ if(value < target)
+ {
+ value = target;
+ }
+ }
+
+ return value;
+}
+
+[[nodiscard, gnu::always_inline, gnu::const]] inline constexpr float
+getMoveTowardsZero(const float value, const float step) noexcept
+{
+ return getMoveTowards(value, 0.f, step);
+}
+
+[[gnu::always_inline]] inline constexpr void moveTowards(
+ float& value, const float target, const float step) noexcept
+{
+ value = getMoveTowards(value, target, step);
+}
+
+[[gnu::always_inline]] inline constexpr void moveTowardsZero(
+ float& value, const float step) noexcept
+{
+ value = getMoveTowardsZero(value, step);
+}
+
+} // namespace hg::Utils
diff --git a/src/SSVOpenHexagon/Components/CPlayer.cpp b/src/SSVOpenHexagon/Components/CPlayer.cpp
index f1222e39..dc112760 100644
--- a/src/SSVOpenHexagon/Components/CPlayer.cpp
+++ b/src/SSVOpenHexagon/Components/CPlayer.cpp
@@ -3,22 +3,26 @@
// AFL License page: https://opensource.org/licenses/AFL-3.0
#include "SSVOpenHexagon/Components/CPlayer.hpp"
-#include "SSVOpenHexagon/Components/CWall.hpp"
+
#include "SSVOpenHexagon/Components/CCustomWall.hpp"
+#include "SSVOpenHexagon/Components/CWall.hpp"
+
#include "SSVOpenHexagon/Utils/Color.hpp"
-#include "SSVOpenHexagon/Utils/Ticker.hpp"
-#include "SSVOpenHexagon/Utils/PointInPolygon.hpp"
-#include "SSVOpenHexagon/Utils/Geometry.hpp"
#include "SSVOpenHexagon/Utils/Easing.hpp"
+#include "SSVOpenHexagon/Utils/Geometry.hpp"
+#include "SSVOpenHexagon/Utils/MoveTowards.hpp"
+#include "SSVOpenHexagon/Utils/PointInPolygon.hpp"
+#include "SSVOpenHexagon/Utils/Ticker.hpp"
#include <SSVStart/Utils/SFML.hpp>
#include <SSVUtils/Core/Common/Frametime.hpp>
#include <SSVUtils/Core/Utils/Math.hpp>
-#include <SFML/System/Vector2.hpp>
#include <SFML/Graphics/Color.hpp>
+#include <SFML/System/Vector2.hpp>
+#include <algorithm>
#include <cmath>
namespace hg {
@@ -398,19 +402,11 @@ void CPlayer::updateTriangleWidthTransition(
{
if(focused && _triangleWidthTransitionTime < 1.f)
{
- _triangleWidthTransitionTime += ft * 0.1f;
- if(_triangleWidthTransitionTime > 1.f)
- {
- _triangleWidthTransitionTime = 1.f;
- }
+ Utils::moveTowards(_triangleWidthTransitionTime, 1.f, ft * 0.1f);
}
else if(!focused && _triangleWidthTransitionTime > 0.f)
{
- _triangleWidthTransitionTime -= ft * 0.1f;
- if(_triangleWidthTransitionTime < 0.f)
- {
- _triangleWidthTransitionTime = 0.f;
- }
+ Utils::moveTowardsZero(_triangleWidthTransitionTime, ft * 0.1f);
}
_triangleWidth =
@@ -465,31 +461,10 @@ void CPlayer::updateInputMovement(const float movementDir,
const float inc = ft / 10.f;
- if(movementDir == 0)
- {
- if(_currTiltedAngle > 0)
- {
- _currTiltedAngle -= inc;
- }
- else if(_currTiltedAngle < 0)
- {
- _currTiltedAngle += inc;
- }
- }
- else if(movementDir == 1)
- {
- if(_currTiltedAngle < 1)
- {
- _currTiltedAngle += inc * 2.f;
- }
- }
- else if(movementDir == -1)
- {
- if(_currTiltedAngle > -1)
- {
- _currTiltedAngle -= inc * 2.f;
- }
- }
+ _currTiltedAngle =
+ (movementDir == 0.f)
+ ? Utils::getMoveTowardsZero(_currTiltedAngle, inc)
+ : Utils::getMoveTowards(_currTiltedAngle, movementDir, inc * 2.f);
}
void CPlayer::resetSwap(const float swapCooldown)
diff --git a/src/SSVOpenHexagon/Core/HGUpdate.cpp b/src/SSVOpenHexagon/Core/HGUpdate.cpp
index 7ef18412..bf742ffb 100644
--- a/src/SSVOpenHexagon/Core/HGUpdate.cpp
+++ b/src/SSVOpenHexagon/Core/HGUpdate.cpp
@@ -5,20 +5,24 @@
#include "SSVOpenHexagon/Core/HexagonGame.hpp"
#include "SSVOpenHexagon/Components/CWall.hpp"
+
#include "SSVOpenHexagon/Global/Assert.hpp"
#include "SSVOpenHexagon/Global/Assets.hpp"
-#include "SSVOpenHexagon/Global/Config.hpp"
#include "SSVOpenHexagon/Global/Audio.hpp"
+#include "SSVOpenHexagon/Global/Config.hpp"
+
#include "SSVOpenHexagon/Utils/Concat.hpp"
#include "SSVOpenHexagon/Utils/Easing.hpp"
#include "SSVOpenHexagon/Utils/LevelValidator.hpp"
+#include "SSVOpenHexagon/Utils/MoveTowards.hpp"
#include "SSVOpenHexagon/Utils/Split.hpp"
#include "SSVOpenHexagon/Utils/String.hpp"
-#include "SSVOpenHexagon/Core/HexagonClient.hpp"
-#include "SSVOpenHexagon/Core/Steam.hpp"
+
#include "SSVOpenHexagon/Core/Discord.hpp"
+#include "SSVOpenHexagon/Core/HexagonClient.hpp"
#include "SSVOpenHexagon/Core/Joystick.hpp"
#include "SSVOpenHexagon/Core/LuaScripting.hpp"
+#include "SSVOpenHexagon/Core/Steam.hpp"
#include <imgui.h>
#include <misc/cpp/imgui_stdlib.h>
@@ -960,18 +964,11 @@ void HexagonGame::updateTrailParticles(ssvu::FT mFT)
for(TrailParticle& p : trailParticles)
{
sf::Color color = p.sprite.getColor();
- auto alpha = static_cast<float>(color.a);
- if(alpha > 0.f)
- {
- alpha -= Config::getPlayerTrailDecay() * mFT;
- if(alpha <= 0.f)
- {
- alpha = 0;
- }
- }
+ const float newAlpha = Utils::getMoveTowardsZero(
+ static_cast<float>(color.a), Config::getPlayerTrailDecay() * mFT);
- color.a = static_cast<sf::Uint8>(alpha);
+ color.a = static_cast<sf::Uint8>(newAlpha);
p.sprite.setColor(color);
p.sprite.setScale(p.sprite.getScale() * 0.98f);