Commit e9d7dc0b authored by Dmitry Suzdalev's avatar Dmitry Suzdalev
Browse files

Balls are now removed from the board when there

are move then 5 of them in any direction

svn path=/trunk/KDE/kdegames/klines/; revision=615842
parent 9b67f538
......@@ -31,15 +31,16 @@ KLinesAnimator::KLinesAnimator( KLinesScene* scene )
connect(&m_timeLine, SIGNAL(finished()), SIGNAL(moveFinished()));
}
void KLinesAnimator::startMoveAnimation( const FieldPath& path )
void KLinesAnimator::animateMove( const FieldPos& from, const FieldPos& to )
{
m_path = path;
m_from = from;
m_to = to;
m_timeLine.setDuration(300);
QPointF p1 = m_scene->fieldToPix(path.first());
QPointF p2 = m_scene->fieldToPix(path.last());
QPointF p1 = m_scene->fieldToPix(m_from);
QPointF p2 = m_scene->fieldToPix(m_to);
m_movingBall = m_scene->ballAt(path.first());
m_movingBall = m_scene->ballAt(m_from);
// FIXME dimsuz: comment
if(p2.x() - p1.x() != 0)
......@@ -52,9 +53,9 @@ void KLinesAnimator::startMoveAnimation( const FieldPath& path )
void KLinesAnimator::animFrameChanged(int frame)
{
QPointF p1 = m_scene->fieldToPix(m_path.first());
QPointF p2 = m_scene->fieldToPix(m_path.last());
qreal x=0, y = 0;
QPointF p1 = m_scene->fieldToPix(m_from);
QPointF p2 = m_scene->fieldToPix(m_to);
qreal x=0, y=0;
// FIXME dimsuz: comment
if(p2.x()-p1.x() != 0)
{
......
......@@ -34,7 +34,8 @@ class KLinesAnimator : public QObject
Q_OBJECT
public:
KLinesAnimator( KLinesScene *scene );
void startMoveAnimation( const FieldPath& path );
void animateMove( const FieldPos& from, const FieldPos& to );
bool isAnimating() const { return m_timeLine.state() == QTimeLine::Running; }
signals:
void moveFinished();
private slots:
......@@ -42,7 +43,8 @@ private slots:
private:
QTimeLine m_timeLine;
KLinesScene* m_scene;
FieldPath m_path;
FieldPos m_from;
FieldPos m_to;
BallItem* m_movingBall;
};
......
......@@ -36,6 +36,4 @@ struct FieldPos
bool isValid() const { return (x != -1 && y != -1); }
};
typedef QList<FieldPos> FieldPath;
#endif
......@@ -104,6 +104,9 @@ void KLinesScene::placeRandomBall()
void KLinesScene::mousePressEvent( QGraphicsSceneMouseEvent* ev )
{
if( m_animator->isAnimating() )
return;
FieldPos fpos = pixToField(ev->scenePos());
if( m_field[fpos.x][fpos.y] ) // ball was selected
......@@ -123,11 +126,7 @@ void KLinesScene::mousePressEvent( QGraphicsSceneMouseEvent* ev )
selectedBall->stopAnimation();
// start move animation
FieldPath p;
p.append(m_selPos);
p.append(fpos);
m_animator->startMoveAnimation(p);
m_animator->animateMove(m_selPos, fpos);
}
}
}
......@@ -147,7 +146,122 @@ void KLinesScene::moveAnimFinished()
m_selPos.x = m_selPos.y = -1; // invalidate position
searchAndErase();
nextThreeBalls();
searchAndErase();
}
void KLinesScene::searchAndErase()
{
// FIXME dimsuz: put more comments about bounds in for loops
// horizontal chunks searching
for(int x=0; x<FIELD_SIZE-4; ++x)
for(int y=0;y<FIELD_SIZE; ++y)
{
if(m_field[x][y] == 0)
continue;
BallColor col = m_field[x][y]->color();
int tmpx = x+1;
while(tmpx < FIELD_SIZE && m_field[tmpx][y] && m_field[tmpx][y]->color() == col)
tmpx++;
// tmpx-x will be: how much balls of the same color we found
if(tmpx-x >= 5)
{
for(int i=x; i<tmpx;++i)
{
delete m_field[i][y];
m_field[i][y] = 0;
}
}
else
continue;
}
// vertical chunks searching
for(int y=0; y<FIELD_SIZE-4; ++y)
for(int x=0;x<FIELD_SIZE; ++x)
{
if(m_field[x][y] == 0)
continue;
BallColor col = m_field[x][y]->color();
int tmpy = y+1;
while(tmpy < FIELD_SIZE && m_field[x][tmpy] && m_field[x][tmpy]->color() == col)
tmpy++;
// tmpy-y will be: how much balls of the same color we found
if(tmpy-y >= 5)
{
for(int j=y; j<tmpy;++j)
{
delete m_field[x][j];
m_field[x][j] = 0;
}
}
else
continue;
}
// down-right diagonal
for(int x=0; x<FIELD_SIZE-4; ++x)
for(int y=0;y<FIELD_SIZE-4; ++y)
{
if(m_field[x][y] == 0)
continue;
BallColor col = m_field[x][y]->color();
int tmpx = x+1;
int tmpy = y+1;
while(tmpx < FIELD_SIZE && tmpy < FIELD_SIZE &&
m_field[tmpx][tmpy] && m_field[tmpx][tmpy]->color() == col)
{
tmpx++;
tmpy++;
}
// tmpx-x (and tmpy-y too) will be: how much balls of the same color we found
if(tmpx-x >= 5)
{
for(int i=x,j=y; i<tmpx;++i,++j)
{
delete m_field[i][j];
m_field[i][j] = 0;
}
}
else
continue;
}
// up-right diagonal
for(int x=0; x<FIELD_SIZE-4; ++x)
for(int y=5; y<FIELD_SIZE; ++y)
{
if(m_field[x][y] == 0)
continue;
BallColor col = m_field[x][y]->color();
int tmpx = x+1;
int tmpy = y-1;
while(tmpx < FIELD_SIZE && tmpy >=0 &&
m_field[tmpx][tmpy] && m_field[tmpx][tmpy]->color() == col)
{
tmpx++;
tmpy--;
}
// tmpx-x (and tmpy-y too) will be: how much balls of the same color we found
if(tmpx-x >= 5)
{
for(int i=x,j=y; i<tmpx;++i,--j)
{
delete m_field[i][j];
m_field[i][j] = 0;
}
}
else
continue;
}
}
void KLinesScene::drawBackground(QPainter *p, const QRectF&)
......
......@@ -71,6 +71,10 @@ private:
* Creates a ball of random color and places it in random free cell
*/
void placeRandomBall();
/**
* Searches for 5 or more balls in a row and deletes them from field
*/
void searchAndErase();
virtual void drawBackground( QPainter*, const QRectF& );
virtual void mousePressEvent( QGraphicsSceneMouseEvent* );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment