夜空
https://gyazo.com/b213f42b4fdeb0cb37d102d99260f424
説明
星がいっぱいの夜空です.
流れない星
code:Star.hpp
# include <Siv3D.hpp> // OpenSiv3D v0.6.11
class Star {
//星の数
int32 starCount;
//星の大きさ
double minSize, maxSize;
//double 星の色
ColorF starCol;
//星の生成エリア
Rect starArea;
//☆
Array<Circle> stars;
public:
Star(Rect _starArea = Scene::Rect(), int32 _starCount = 30)
:starArea(_starArea), starCount(_starCount), minSize(1.0), maxSize(2.0), starCol(Palette::White)
{
//星をランダムに配置
stars.clear();
for (int i : step(starCount))stars.push_back(Circle(RandomVec2(starArea), Random(minSize, maxSize)));
}
void update() {}
void draw() const{
for (const auto& s : stars)s.draw(ColorF{ starCol,Random(0.5,0.85) });
}
Star& setStarCount(int32 _starCount) { starCount = _starCount; return *this; }
Star& setMinSize(double _minSize) { minSize = _minSize; return *this; }
Star& setMaxSize(double _maxSize) { maxSize = _maxSize; return *this; }
Star& setStarCol(ColorF _starCol) { starCol = _starCol; return *this; }
Star& setStarArea(Rect _starArea) { starArea = _starArea; return *this; }
void create() {
stars.clear();
for (int i : step(starCount))stars.push_back(Circle(RandomVec2(starArea), Random(minSize, maxSize)));
};
};
流れる星
code:ShootingStar.hpp
# include <Siv3D.hpp> // OpenSiv3D v0.6.11
class ShootingStar
{
struct ShootingStarobj
{
Vec2 pos;
double size;
Trail trail;
};
//最大の流れ星の数
int32 maxStars = 20;
//流れ星生成の間隔
double interval = 0.6;
//流れ星の速度
Vec2 speed{ -200,300 };
//流れ星が消える座標
double endPos;
//流れ星
Array<ShootingStarobj> shootingStars;
Stopwatch sw;
public:
ShootingStar(Vec2 _speed = { -200,300 }, double _interval = 0.6, int32 maxStars = 20)
:speed(_speed), interval(_interval), maxStars(maxStars), endPos{ (double)Scene::Height() * 2 / 3 }
{
sw.start();
}
void update() {
//流れ星の軌跡を更新
for (auto& s : shootingStars)s.trail.update();
//流れ星を生成
if (sw.sF() > interval and shootingStars.size() < maxStars) {
sw.restart();
double x = Random(Scene::Width() / 2, Scene::Width() + 200);
double y = Random(-500, -10);
double size = Random(1, 3);
const Trail trail{ Random(1.0, 3.0) };
shootingStars.push_back(ShootingStarobj{ {x,y},size,trail });
}
//流れ星を移動
const double t = Scene::DeltaTime();
for (int i : step(shootingStars.size())) {
shootingStarsi.pos.moveBy(speed * t); const ColorF color{ Palette::White,0.8 * EaseOutSine(Min(400.0,(endPos - shootingStarsi.pos.y)) / 400) }; if (shootingStarsi.pos.y < endPos) { shootingStarsi.trail.add(shootingStarsi.pos, color, shootingStarsi.size); }
}
//流れ星の軌跡が消えたら削除
shootingStars.remove_if([](const ShootingStarobj& s) {return s.trail.num_points() == 0; });
}
void draw() const {
// 星,流れ星
for (const auto& s : shootingStars)s.trail.draw();
}
ShootingStar& setSpeed(Vec2 _speed) { speed = _speed; return *this; }
ShootingStar& setInterval(double _interval) { interval = _interval; return *this; }
ShootingStar& setMaxStars(int32 _maxStars) { maxStars = _maxStars; return *this; }
ShootingStar& setEndPos(double _endPos) { endPos = _endPos; return *this; }
};
code:main.cpp
# include <Siv3D.hpp> // OpenSiv3D v0.6.11
void Main()
{
ShootingStar shootingStar;
shootingStar.setSpeed({ -200,300 })
.setInterval(0.6)
.setMaxStars(20)
.setEndPos(Scene::Height() * 2 / 3);
Star stars;
stars.setStarArea(Rect{ 0,0,Scene::Width(),Scene::Height() * 2 / 3 })
.setStarCol(Palette::Aqua)
.setStarCount(50);
stars.create();
while (System::Update())
{
shootingStar.update();
//背景
Scene::Rect().draw(Arg::top = ColorF(0.0, 0.0, 0.2), Arg::bottom = ColorF(0.2, 0.2, 0.4));
shootingStar.draw();
stars.draw();
}
}
作者
BetAmoto