Skip to content

Instantly share code, notes, and snippets.

@sknjpn
Created July 9, 2017 14:15
Show Gist options
  • Save sknjpn/890f7a4e38a3aaf5531383b3f5db4f08 to your computer and use it in GitHub Desktop.
Save sknjpn/890f7a4e38a3aaf5531383b3f5db4f08 to your computer and use it in GitHub Desktop.
# include <Siv3D.hpp>
struct Junction
{
Junction(const Vec2& _pos)
: pos(_pos), temp(false), family(-1) {}
Vec2 pos;
bool temp;
int family;
Array<Junction*> connects;
Array<Junction*> roads;
};
void Main()
{
Array<Junction> junction;
for (int i = 0; i < 48 * 24; i++) junction.push_back(Junction(Vec2(0, 0))); //メモリ確保
Window::SetTitle(L"DungeonCreater");
Window::Resize(1280, 720);
Junction* centerJunction = NULL;
Stopwatch stagingTimer;
while (System::Update())
{
if (KeySpace.down())
{
junction.clear();
//生成
for (int x = 0; x < 47; x++)
{
for (int y = 0; y < 23; y++)
{
junction.push_back(Junction(Vec2(x * 32 * 1.73 / 2.0, y * 32 + (x % 2) * 16)));
if (x == 23 && y == 11) centerJunction = &junction.back();
//描画
if (!KeyEnter.pressed())
{
for (auto& k : junction) Circle(k.pos, 4).draw();
System::Update();
}
}
}
while (System::Update() && KeyEnter.pressed());
//除去
for (auto& j : junction)
{
j.temp = !RandomBool(Min(1.5 - 1.5*Vec2(640.0 + (j.pos.x - 640.0)*0.48, j.pos.y).distanceFrom(640, 360) / 360.0, 0.9));
//描画
if (!KeyEnter.pressed())
{
for (auto& k : junction) Circle(k.pos, 4).draw(k.temp ? Palette::Red : Palette::White);
System::Update();
}
}
Erase_if(junction, [](Junction j) { return j.temp; });
while (System::Update() && KeyEnter.pressed());
//接続
for (auto& j : junction)
{
for (auto& o : junction)
{
if (j.pos.distanceFrom(o.pos) <= 32.0)
{
j.connects.push_back(&o);
}
}
//描画
if (!KeyEnter.pressed())
{
for (auto& k : junction)
{
for (auto& t : k.connects) Line(k.pos, t->pos).draw(2);
Circle(k.pos, 4).draw();
}
System::Update();
}
}
while (System::Update() && KeyEnter.pressed());
//繋がっていないか確認
centerJunction = &junction[junction.size() / 2];
for (;;)
{
Junction* j[24 * 480];
for (auto& t : j) t = NULL;
j[0] = &junction[Random(junction.size() - 1)];
int writer = 1; //書き込み位置
for (;;)
{
bool flag = false;
for (auto& v : j)
{
if (v == NULL) break;
for (auto& t : v->connects)
{
if (!t->temp)
{
flag = true;
t->temp = true;
j[writer] = t;
writer++;
//描画
if (!KeyEnter.pressed())
{
for (auto& k : junction) for (auto& d : k.connects) Line(k.pos, d->pos).draw(2, Palette::Gray);
for (auto& k : junction) Circle(k.pos, 4).draw(k.temp ? Palette::Red : Palette::Gray);
System::Update();
}
}
}
}
if (!flag) break;
}
if (writer < 20)
{
for (auto& v : j)
{
if (v == NULL) break;
v->temp = false;
}
}
else break;
}
Erase_if(junction, [](Junction j) { return !j.temp; });
//Erase_ifで崩れたポインターを整理
for (auto& j : junction) j.connects.clear();
for (auto& j : junction)
for (auto& o : junction)
if (j.pos.distanceFrom(o.pos) <= 32.0)
j.connects.push_back(&o);
while (System::Update() && KeyEnter.pressed());
//ランダムにずらす
for (auto& j : junction)
{
j.pos.moveBy(RandomVec2(Random(16.0)));
//描画
if (!KeyEnter.pressed())
{
for (auto& k : junction) for (auto& d : k.connects) Line(k.pos, d->pos).draw(2, Palette::Gray);
for (auto& k : junction) Circle(k.pos, 4).draw(Palette::White);
System::Update();
}
}
while (System::Update() && KeyEnter.pressed());
//通路の生成
{
centerJunction = &junction[Random(junction.size() - 1)];
for (int i = 0; i < 10; i++)
{
for (;;)
{
Junction& j = junction[Random(junction.size() - 1)];
if (j.family == -1)
{
j.temp = false;
j.family = i;
break;
}
}
}
for (;;)
{
{
auto& j = junction[Random(junction.size() - 1)];
if (!j.temp) continue;
Junction* v = j.connects[Random(j.connects.size() - 1)];
if (v->temp) continue;
j.roads.push_back(v);
j.temp = false;
j.family = v->family;
}
bool flag = true;
for (auto& j : junction)
{
if (j.temp)
{
flag = false;
break;
}
}
if (flag) break;
//描画
if (!KeyEnter.pressed())
{
for (auto& k : junction) for (auto& d : k.connects) Line(k.pos, d->pos).draw(2, ColorF(Palette::Darkgray, 0.2));
for (auto& k : junction) for (auto& d : k.roads) Line(k.pos, d->pos).draw(2, HSV(k.family * 36));
for (auto& k : junction) Circle(k.pos, 4).draw(k.family == -1 ? Palette::Darkgray : HSV(k.family * 36));
System::Update();
}
}
}
while (System::Update() && KeyEnter.pressed());
bool isConnect[10][10];
for (int x = 0; x < 10; x++)
for (int y = 0; y < 10; y++)
isConnect[x][y] = false;
for (int i = 1; i < 10; i++)
{
for (auto& j : junction)
{
if (j.family != i) continue;
for (auto& v : j.connects)
{
if (v->family < i && !isConnect[v->family][i])
{
isConnect[v->family][i] = true;
j.roads.push_back(v);
//描画
if (!KeyEnter.pressed())
{
for (auto& k : junction) for (auto& d : k.connects) Line(k.pos, d->pos).draw(2, ColorF(Palette::Darkgray, 0.2));
for (auto& k : junction) for (auto& d : k.roads) Line(k.pos, d->pos).draw(2, HSV(k.family * 36));
for (auto& k : junction) Circle(k.pos, 4).draw(k.family == -1 ? Palette::Darkgray : HSV(k.family * 36));
System::Update();
}
}
}
}
}
while (System::Update() && KeyEnter.pressed());
}
if (centerJunction != NULL) Circle(centerJunction->pos, 8).draw(Palette::Red);
for (auto& j : junction)
{
Circle(j.pos, 4).draw(Palette::White);
for (auto& t : j.roads)
{
Line(j.pos, t->pos).draw(2, Palette::White);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment