Skip to content

Instantly share code, notes, and snippets.

@sknjpn
Last active July 10, 2017 13:11
Show Gist options
  • Save sknjpn/660edbd56d842cae5065c237f3e1e4e8 to your computer and use it in GitHub Desktop.
Save sknjpn/660edbd56d842cae5065c237f3e1e4e8 to your computer and use it in GitHub Desktop.
OpenSiv3D v0.1.5にて作成したサンプル、ご自由にお持ちください。
# include <Siv3D.hpp> // OpenSiv3D v0.1.5
struct Face
{
Texture tex;
Point pos;
Vec2 dPos;
};
void Main()
{
Graphics::SetBackground(Palette::Tomato);
Window::SetTitle(L"Angry face with OpenSiv3D");
Window::Resize(1280, 720);
const Array<Texture> eTex = {
Texture(Emoji(L"😁"),TextureDesc::Mipped),
Texture(Emoji(L"😎"),TextureDesc::Mipped),
Texture(Emoji(L"😘"),TextureDesc::Mipped),
Texture(Emoji(L"🙄"),TextureDesc::Mipped),
Texture(Emoji(L"😪"),TextureDesc::Mipped),
Texture(Emoji(L"😭"),TextureDesc::Mipped),
Texture(Emoji(L"😨"),TextureDesc::Mipped),
Texture(Emoji(L"😰"),TextureDesc::Mipped),
Texture(Emoji(L"😵"),TextureDesc::Mipped),
Texture(Emoji(L"😇"),TextureDesc::Mipped),
Texture(Emoji(L"😷"),TextureDesc::Mipped),
//Texture(Emoji(L"😃"),TextureDesc::Mipped),
//Texture(Emoji(L"😍"),TextureDesc::Mipped),
//Texture(Emoji(L"😀"),TextureDesc::Mipped),
//Texture(Emoji(L"😠"),TextureDesc::Mipped),
};
const Rect rect(480, 720);
Grid<Face> face(10, 15);
Array<Face*> chain;
for (const auto& p : step(face.size()))
{
face[p].tex = eTex[int(Random(eTex.size() - 1))];
face[p].pos = p;
face[p].dPos = { 0, 0 };
}
while (System::Update())
{
//Chainの捜索
int numChain = 0;
for (const auto& p : step(Size(10, 15)))
{
Array<Face*> ct;
ct.push_back(&face[p.y][p.x]);
for (int w = 0; w<int(ct.size()); w++)
{
if (ct[w]->pos.x != 0 && !ct.any([&face, &ct, &w](const Face* f) {return f == &face.at(ct[w]->pos.movedBy(-1, 0)); }) && face.at(ct[w]->pos.movedBy(-1, 0)).tex == ct[w]->tex) ct.push_back(&face.at(ct[w]->pos.movedBy(-1, 0)));
if (ct[w]->pos.y != 0 && !ct.any([&face, &ct, &w](const Face* f) {return f == &face.at(ct[w]->pos.movedBy(0, -1)); }) && face.at(ct[w]->pos.movedBy(0, -1)).tex == ct[w]->tex) ct.push_back(&face.at(ct[w]->pos.movedBy(0, -1)));
if (ct[w]->pos.x != 9 && !ct.any([&face, &ct, &w](const Face* f) {return f == &face.at(ct[w]->pos.movedBy(1, 0)); }) && face.at(ct[w]->pos.movedBy(1, 0)).tex == ct[w]->tex) ct.push_back(&face.at(ct[w]->pos.movedBy(1, 0)));
if (ct[w]->pos.y != 14 && !ct.any([&face, &ct, &w](const Face* f) {return f == &face.at(ct[w]->pos.movedBy(0, 1)); }) && face.at(ct[w]->pos.movedBy(0, 1)).tex == ct[w]->tex) ct.push_back(&face.at(ct[w]->pos.movedBy(0, 1)));
}
if (ct.size() > 2)
{
numChain++;
for (int w = 0; w<int(ct.size()); w++)
RectF(Vec2(ct[w]->pos.x * 48, 720 - ct[w]->pos.y * 48 - 48) + ct[w]->dPos, Vec2(48, 48)).draw(Palette::White);
}
}
//繋げられそうな奴が無ければリセット
if (numChain == 0)
{
chain.clear();
for (const auto& p : step(Size(10, 15)))
{
face[p.y][p.x].tex = eTex[Random(int(eTex.size() - 1))];
face[p.y][p.x].dPos = Vec2(0, -720);
}
}
//Faceの選択
if (rect.mouseOver())
{
auto& f = face[14 - Cursor::Pos().y / 48][Cursor::Pos().x / 48];
if (MouseL.down())
{
if (!f.tex.isEmpty()) chain.push_back(&f);
}
else if (MouseL.pressed() && !chain.isEmpty())
{
if ((f.pos - chain.back()->pos).length() == 1 && !chain.any([&f](const Face* t) {return t == &f; }) && f.tex == chain.front()->tex) chain.push_back(&f);
}
}
//Chainの解放
if (MouseL.up())
{
if (chain.size() > 2)
{
for (auto& c : chain) c->tex.release();
for (const auto& p : step(Size(0, 1), Size(10, 14)))
{
if (face[p.y - 1][p.x].tex.isEmpty())
{
for (int y = p.y - 1; y >= 0; y--)
{
if (y <= 0 || !face[y - 1][p.x].tex.isEmpty())
{
face[y][p.x].tex = face[p.y][p.x].tex;
face[p.y][p.x].tex.release();
face[y][p.x].dPos.y = (y - p.y) * 48;
break;
}
}
}
}
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 15; y++)
{
if (face[y][x].tex.isEmpty())
{
for (int iy = y; iy < 15; iy++)
{
face[iy][x].tex = eTex[Random(int(eTex.size() - 1))];
face[iy][x].dPos = Vec2(0, (y - 15) * 48);
}
break;
}
}
}
}
chain.clear();
}
//Chainの描画
for (int i = 0; i<int(chain.size()); i++)
{
auto& c = chain[i];
const auto& cp = Vec2(c->pos.x * 48, Window::Size().y - c->pos.y * 48 - 48);
RectF(cp, 48, 48).draw(HSV(i * 60, 0.5, 0.5));
}
//格子の描画
for (const auto& p : step(Size(10, 15)))
RectF(Vec2(p.x * 48, Window::Size().y - 48 - p.y * 48) + face[p.y][p.x].dPos, Vec2(48, 48)).drawFrame(2, 0, Palette::Blue);
//Faceの描画
for (const auto& p : step(face.size()))
{
auto& f = face[p.y][p.x];
const Vec2 d(Vec2(p.x * 48 + 24, Window::Size().y - p.y * 48 - 24) + RandomVec2(chain.any([&f](const Face* c) {return c == &f; }) ? 3 : 0));
//落とす処理
f.dPos *= 0.9;
if (!f.tex.isEmpty()) f.tex.resize(44).drawAt(d + f.dPos);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment