From a136dea1136a9f84fe5e180b6aeeba46d4e8d88c Mon Sep 17 00:00:00 2001 From: tyanmahou Date: Thu, 18 Jan 2024 20:41:46 +0900 Subject: [PATCH] add maxSpeed --- Re-Abyss/app/components/Actor/Common/Body.cpp | 39 ++++++++++---- Re-Abyss/app/components/Actor/Common/Body.hpp | 54 ++++++++++--------- 2 files changed, 57 insertions(+), 36 deletions(-) diff --git a/Re-Abyss/app/components/Actor/Common/Body.cpp b/Re-Abyss/app/components/Actor/Common/Body.cpp index e0d02a94..3b9237e6 100644 --- a/Re-Abyss/app/components/Actor/Common/Body.cpp +++ b/Re-Abyss/app/components/Actor/Common/Body.cpp @@ -35,17 +35,6 @@ namespace abyss::Actor m_velocity.x = min; } }); - - if (m_accel.x == 0.0) { - auto deltaDecel = m_decelX* dt; - if (m_velocity.x - deltaDecel > 0) { - m_velocity.x -= deltaDecel; - } else if (m_velocity.x + deltaDecel < 0) { - m_velocity.x += deltaDecel; - } else { - m_velocity.x = 0; - } - } m_maxVelocity.y.then([this](double max) { if (m_velocity.y > max) { m_velocity.y = max; @@ -56,6 +45,28 @@ namespace abyss::Actor m_velocity.y = min; } }); + m_minSpeed.then([this](double min) { + if (m_velocity.lengthSq() < min * min) { + m_velocity.setLength(min); + } + }); + m_maxSpeed.then([this](double max) { + if (m_velocity.lengthSq() > max * max) { + m_velocity.setLength(max); + } + }); + + // 減速 + if (m_accel.x == 0.0) { + auto deltaDecel = m_decelX * dt; + if (m_velocity.x - deltaDecel > 0) { + m_velocity.x -= deltaDecel; + } else if (m_velocity.x + deltaDecel < 0) { + m_velocity.x += deltaDecel; + } else { + m_velocity.x = 0; + } + } // 座標更新 m_pos += m_velocity * dt; @@ -125,6 +136,11 @@ namespace abyss::Actor m_minVelocity.y = s3d::none; return *this; } + Body& Body::setMaxSpeed(double speed) + { + m_maxSpeed = s3d::Math::Abs(speed); + return *this; + } Body& Body::setMaxSpeed(const s3d::Vec2& speed) { return this-> @@ -133,6 +149,7 @@ namespace abyss::Actor } Body& Body::setMaxSpeed(s3d::None_t) { + m_maxSpeed = s3d::none; return this-> setMaxSpeedX(s3d::none) .setMaxSpeedY(s3d::none); diff --git a/Re-Abyss/app/components/Actor/Common/Body.hpp b/Re-Abyss/app/components/Actor/Common/Body.hpp index e4d06064..875c8eb9 100644 --- a/Re-Abyss/app/components/Actor/Common/Body.hpp +++ b/Re-Abyss/app/components/Actor/Common/Body.hpp @@ -15,29 +15,10 @@ namespace abyss::Actor public IComponent, public ILocator { - private: - s3d::Vec2 m_prevPos{ 0, 0 }; - s3d::Vec2 m_pos{0, 0}; - - s3d::Vec2 m_pivot{ 0, 0 }; - s3d::Vec2 m_pivotPrev{ 0, 0 }; - s3d::Vec2 m_pivotNext{ 0, 0 }; - - s3d::Vec2 m_velocity{0, 0}; - - s3d::Vector2D > m_minVelocity; - s3d::Vector2D > m_maxVelocity; - - s3d::Vec2 m_accel{0, DefaultGravity}; - double m_decelX = 0.0; - - Forward m_forward{Forward::None()}; - - s3d::Vec2 m_size{ 0, 0 }; - s3d::Vec2 m_sizePrev{ 0, 0 }; - s3d::Vec2 m_sizeNext{ 0, 0 }; + public: + inline static constexpr double DefaultGravity = 720.0; + inline static constexpr double DefaultMaxVelocityY = 78; - ActorObj* m_pActor; public: Body(ActorObj* pActor); @@ -60,6 +41,7 @@ namespace abyss::Actor Body& setMaxSpeedY(double speed); Body& setMaxSpeedY(s3d::None_t); + Body& setMaxSpeed(double speed); Body& setMaxSpeed(const s3d::Vec2& speed); Body& setMaxSpeed(s3d::None_t); @@ -114,9 +96,31 @@ namespace abyss::Actor s3d::Vec2 getCenterPos()const override; s3d::Vec2 moveDiff() const; - public: - inline static constexpr double DefaultGravity = 720.0; - inline static constexpr double DefaultMaxVelocityY = 78; + private: + s3d::Vec2 m_prevPos{ 0, 0 }; + s3d::Vec2 m_pos{ 0, 0 }; + + s3d::Vec2 m_pivot{ 0, 0 }; + s3d::Vec2 m_pivotPrev{ 0, 0 }; + s3d::Vec2 m_pivotNext{ 0, 0 }; + + s3d::Vec2 m_velocity{ 0, 0 }; + + s3d::Vector2D > m_minVelocity; + s3d::Optional m_minSpeed; + s3d::Vector2D > m_maxVelocity; + s3d::Optional m_maxSpeed; + + s3d::Vec2 m_accel{ 0, DefaultGravity }; + double m_decelX = 0.0; + + Forward m_forward{ Forward::None() }; + + s3d::Vec2 m_size{ 0, 0 }; + s3d::Vec2 m_sizePrev{ 0, 0 }; + s3d::Vec2 m_sizeNext{ 0, 0 }; + + ActorObj* m_pActor; }; }