#include "GamePopStar.h"
#include "StarSprite.h"
USING_NS_CC;
Scene* GamePopStar::createScene()
{
auto scene = Scene::create();
auto layer = GamePopStar::create();
scene->addChild(layer);
return scene;
}
bool GamePopStar::init()
{
if (!Layer::init())
return false;
addStars();
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(GamePopStar::touchBegan, this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
scheduleUpdate();
return true;
}
void GamePopStar::addStars()
{
srand(time(NULL));
for (int w = 0; w < WIDTH; ++w)
{
for (int h = 0; h < HEIGH; ++h)
{
stars[w][h] = StarSprite::createStar(rand() % 5 + 1, w, h);
addChild(stars[w][h]);
}
}
}
bool GamePopStar::touchBegan(cocos2d::Touch* touch, cocos2d::Event*)
{
auto pos = touch->getLocation();
if (pos.x < 40 * WIDTH && pos.y < 40 * HEIGH)
{
int x = pos.x / 40;
int y = pos.y / 40;
if (stars[x][y]->getType() != 0)
{
//stars[x][y]->deleStar();
delStarsFrom(stars[x][y]->getType(), x, y);
starsDrop();
if (isOver())
exit(-1);
}
}
return false;
}
void GamePopStar::delStarsFrom(int type, int w, int h)
{
if (type == 0)
return;
bool canDel = false;
if (w - 1 >= 0 && stars[w - 1][h]->getType() == type)
{
delStarsFromRight(type, w - 1, h);
canDel = true;
}
if (w + 1 < WIDTH && stars[w + 1][h]->getType() == type)
{
delStarsFromLeft(type, w + 1, h);
canDel = true;
}
if (h - 1 >= 0 && stars[w][h - 1]->getType() == type)
{
delStarsFromUp(type, w, h - 1);
canDel = true;
}
if (h + 1 < HEIGH && stars[w][h + 1]->getType() == type)
{
delStarsFromDown(type, w, h + 1);
canDel = true;
}
if (canDel)
stars[w][h]->deleStar();
}
void GamePopStar::delStarsFromUp(int type, int w, int h)
{
stars[w][h]->deleStar();
if (w - 1 >= 0 && stars[w - 1][h]->getType() == type)
delStarsFromRight(type, w - 1, h);
if (w + 1 < WIDTH && stars[w + 1][h]->getType() == type)
delStarsFromLeft(type, w + 1, h);
if (h - 1 >= 0 && stars[w][h - 1]->getType() == type)
delStarsFromUp(type, w, h - 1);
}
void GamePopStar::delStarsFromDown(int type, int w, int h)
{
stars[w][h]->deleStar();
if (w - 1 >= 0 && stars[w - 1][h]->getType() == type)
delStarsFromRight(type, w - 1, h);
if (w + 1 < WIDTH && stars[w + 1][h]->getType() == type)
delStarsFromLeft(type, w + 1, h);
if (h + 1 < HEIGH && stars[w][h + 1]->getType() == type)
delStarsFromDown(type, w, h + 1);
}
void GamePopStar::delStarsFromLeft(int type, int w, int h)
{
stars[w][h]->deleStar();
if (w + 1 < WIDTH && stars[w + 1][h]->getType() == type)
delStarsFromLeft(type, w + 1, h);
if (h - 1 >= 0 && stars[w][h - 1]->getType() == type)
delStarsFromUp(type, w, h - 1);
if (h + 1 < HEIGH && stars[w][h + 1]->getType() == type)
delStarsFromDown(type, w, h + 1);
}
void GamePopStar::delStarsFromRight(int type, int w, int h)
{
stars[w][h]->deleStar();
if (w - 1 >= 0 && stars[w - 1][h]->getType() == type)
delStarsFromRight(type, w - 1, h);
if (h - 1 >= 0 && stars[w][h - 1]->getType() == type)
delStarsFromUp(type, w, h - 1);
if (h + 1 < HEIGH && stars[w][h + 1]->getType() == type)
delStarsFromDown(type, w, h + 1);
}
void GamePopStar::starsDrop()
{
//下落
for (int w = 0; w < WIDTH; ++w)
{
for (int h = 1; h < HEIGH; ++h)
{
int num = 0;
for (int i=0; i < h; ++i)
{
/*if (stars[w][i]->getType() == 0)
{
auto temp = stars[w][i];
stars[w][i] = stars[w][h];
stars[w][h] = temp;
break;
}*/
if (stars[w][i]->getType() == 0)
++num;
}
auto temp = stars[w][h];
stars[w][h] = stars[w][h - num];
stars[w][h - num] = temp;
//stars[w][h]->move(1, w, h);
}
}
//左移
for (int w = 1; w < WIDTH; ++w)
{
for (int i = 0; i < w; ++i)
{
if (stars[i][0]->getType() == 0)
{
for (int h = 0; h < HEIGH; ++h)
{
auto temp = stars[i][h];
stars[i][h] = stars[w][h];
stars[w][h] = temp;
}
break;
}
}
}
for (int w = 0; w < WIDTH; ++w)
{
for (int h = 0; h < HEIGH; ++h)
{
stars[w][h]->move(0.5,w,h);
}
}
}
bool GamePopStar::isOver()
{
for (int w = 0; w < WIDTH; ++w)
{
for (int h = 0; h < HEIGH; ++h)
{
auto type = stars[w][h]->getType();
if (type == 0)
continue;
if (w - 1 >= 0 && stars[w - 1][h]->getType() == type)
{
return false;
}
if (w + 1 < WIDTH && stars[w + 1][h]->getType() == type)
{
return false;
}
if (h - 1 >= 0 && stars[w][h - 1]->getType() == type)
{
return false;
}
if (h + 1 < HEIGH && stars[w][h + 1]->getType() == type)
{
return false;
}
}
}
return true;
}
void GamePopStar::update(float dt)
{
static int w = 0;
static int h = 0;
if (w < WIDTH && h < HEIGH)
{
stars[w][h]->move(0.1*(w+1)/(h+1), w, h);
++w;
}
else if (h < HEIGH)
{
w = 0;
++h;
}
else
{
unscheduleUpdate();
}
}