Skip to content

Instantly share code, notes, and snippets.

@m4saka
Last active September 6, 2024 07:45
Show Gist options
  • Select an option

  • Save m4saka/6736b1edd484cc52dfdc8c4574a624f1 to your computer and use it in GitHub Desktop.

Select an option

Save m4saka/6736b1edd484cc52dfdc8c4574a624f1 to your computer and use it in GitHub Desktop.
タスク一時停止で動き回る円のアニメーションを一時停止する例
#include <Siv3D.hpp>
#include <CoTaskLib.hpp> // CoTaskLib 0.3.0
class MovingCircle : public Co::SequenceBase<>
{
private:
Vec2 m_pos{ 400, 300 };
ColorF m_color = Palette::White;
Co::Task<> start() override
{
while (true)
{
// 次の位置
Vec2 nextPos = m_pos + RandomVec2(200, 200) - Vec2{ 100, 100 };
nextPos.x = Clamp(nextPos.x, 0.0, 800.0);
nextPos.y = Clamp(nextPos.y, 0.0, 600.0);
// 次の色
ColorF nextColor = RandomColorF();
// 1秒かけて推移させる
co_await Co::All(
Co::Ease(&m_pos, 1.0s).to(nextPos).setEase(EaseInOutSine).play(),
Co::Ease(&m_color, 1.0s).to(RandomColorF()).setEase(EaseInOutSine).play());
}
}
void draw() const override
{
Circle{ m_pos, 50 }.draw(m_color);
}
};
Co::Task<> MainTask()
{
// 時間をずらして5個再生
co_await Co::All(
Co::Play<MovingCircle>(),
Co::Play<MovingCircle>().delayed(0.2s),
Co::Play<MovingCircle>().delayed(0.4s),
Co::Play<MovingCircle>().delayed(0.6s),
Co::Play<MovingCircle>().delayed(0.8s));
}
void Main()
{
Co::Init();
bool isPaused = false;
auto _ = MainTask().pausedWhile([&isPaused] { return isPaused; }).runScoped();
while (System::Update())
{
SimpleGUI::CheckBox(isPaused, U"一時停止", Vec2{ 20, 20 });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment