Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@voidproc

voidproc/ddp.cpp Secret

Created July 22, 2017 03:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voidproc/133f015c10124a9f6e85372833788215 to your computer and use it in GitHub Desktop.
Save voidproc/133f015c10124a9f6e85372833788215 to your computer and use it in GitHub Desktop.
# include <Siv3D.hpp> // OpenSiv3D v0.1.5
void Main()
{
Graphics::SetBackground(Color{ 255, 237, 216 });
const Texture textureFire(Emoji(L"🔥"), TextureDesc::Mipped);
const Texture textureCol(Emoji(L"💥"), TextureDesc::Mipped);
// 敵や障害物などのレーザーがあたるもの
Array<Circle> enemies{ Circle{50}, Circle{40}, Circle{70} };
while (System::Update())
{
// 敵を動かす/描画
const auto frame = System::FrameCount();
for (int i : step(enemies.size()))
{
enemies[i].setPos(Window::Center().x + (120 + i * 40) * sin(frame * (0.03 + i * 0.01) + i * Math::TwoPi / 3), 100);
enemies[i].draw(Color{ 255, 193, 118 });
}
// マウス位置の上方にレーザーが伸びる
// いま nearest まで伸びてる
const auto cursorPos = Cursor::PosF();
Vec2 nearest = cursorPos + Vec2{ 0, -1000 };
for (const auto& enemy : enemies)
{
if (cursorPos.intersects(enemy))
{
// レーザー発射点がめりこんでいれば、レーザーは伸びない
nearest = cursorPos;
}
else
{
// めりこんでいないので、マウス位置に一番近い衝突点までレーザーが伸びる
Line{ cursorPos, nearest }.intersectsAt(enemy).value_or(Array<Vec2>{}).each([&](const auto& pos) {
if (cursorPos.distanceFrom(pos) < cursorPos.distanceFrom(nearest)) {
nearest = pos;
};
});
}
}
Line{ cursorPos, nearest }.draw(16 + 4 * sin(frame * 10), Palette::Darkorange); //レーザー本体
textureFire.resize(70 + 8 * sin(frame * 5)).rotate(20_deg * sin(frame * 0.1)).drawAt(cursorPos); //発射点
textureCol.resize(100 + 30 * sin(frame * 0.5)).drawAt(nearest); //照射先
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment