Skip to content

Commit

Permalink
add maxSpeed
Browse files Browse the repository at this point in the history
  • Loading branch information
tyanmahou committed Jan 18, 2024
1 parent 078825a commit a136dea
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 36 deletions.
39 changes: 28 additions & 11 deletions Re-Abyss/app/components/Actor/Common/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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->
Expand All @@ -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);
Expand Down
54 changes: 29 additions & 25 deletions Re-Abyss/app/components/Actor/Common/Body.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <s3d::Optional<double>> m_minVelocity;
s3d::Vector2D <s3d::Optional<double>> 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);

Expand All @@ -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);

Expand Down Expand Up @@ -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 <s3d::Optional<double>> m_minVelocity;
s3d::Optional<double> m_minSpeed;
s3d::Vector2D <s3d::Optional<double>> m_maxVelocity;
s3d::Optional<double> 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;
};
}

Expand Down

0 comments on commit a136dea

Please sign in to comment.